Skip to main content

f-strings(Python 3.6) style literal string interpolation.

Project description

https://img.shields.io/travis/damnever/fmt.svg?style=flat-square https://img.shields.io/pypi/v/fmt.svg?style=flat-square

Using f-strings(PEP 498) style literal string interpolation without Python 3.6.

Usages

  • Accessing the globals and locals.

    import os
    import fmt as f
    
    g_foo = 'global-foo'
    g_bar = 'global-bar'
    g_num = 23
    g_ls = [1, 2, 3]
    
    def scope():
        l_foo = 'local-foo'
        l_bar = 'local-bar'
        print( f('{l_foo}, {l_bar}') )    # 'local-foo, local-bar'
        print( f('{g_foo}, {g_bar!r}') )  # "global-foo, 'global-bar'"
    
    scope()
    print( f('{{ }}') )                   # '{ }'
    print( f('hex: {g_num:#x}') )         # '0x17'
    print( f('{os.EX_OK}') )              # '0'
    print( f('{g_ls[0]}, {g_ls[1]}, {g_ls[2]}') )  # '1, 2, 3'
  • NOTE: Closure will be a little tricky, must pass the outside scope variables as arguments to f, which added a reference to inside the closure in order this can work.

    import fmt as f
    
    def outer(x='xx'):
        y = 'yy'
        def inner():
            print( f('{x}, {y}', x, y) )  # "xx, 'yy'"
        return inner
    
    outer()()
  • Expression evaluation.

    from datetime import datetime
    import fmt as f
    
    class S(object):
        def __str__(self):
            return 'hello'
        def __repr__(self):
            return 'hi'
        def __format__(self, fmt):
            return 'abcdefg'[int(fmt)]
    
    print( f('{1234567890:,}') )             # '1,234,567,890'
    print( f('{1 + 2}') )                    # '3'
    print( f('{str(1 + 2)!r}') )             # "'3'"
    print( f('{[i for i in range(5)]}') )    # '[0, 1, 2, 3, 4]'
    ls = range(5)
    print( f('{{i for i in ls}}') )          # 'set([0, 1, 2, 3, 4])' or '{0, 1, 2, 3, 4}'
    print( f('{{k:v for k,v in zip(range(3), range(3, 6))}}') )  # '{0: 3, 1: 4, 2: 5}'
    print( f('{datetime(1994, 11, 6):%Y-%m-%d}') )               # '1994-11-06'
    print( f('{list(map(lambda x: x+1, range(3)))}') )           # '[1, 2, 3]'
    print( f('{S()!r} {S()!s} {S():1}') )                        # 'hello hi b'
  • Also, you can register some namespaces for convenience. NOTE: local namespace may cover the namespace that you registered.

    import fmt as f
    
    f.mregister({'x': 1, 'y': 2})  # register multiple
    f.register('z', 3)             # register only one
    
    def func(x, y):
        return x + y
    
    print( f('{func(x, y)}') )  # '3'
    print( f('{func(x, z)}') )  # '4'
    print( f('{func(y, z)}') )  # '5'

Installation

Install by pip:

[sudo] pip install fmt -U

LICENSE

The BSD 3-Clause License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fmt-0.1.2.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

fmt-0.1.2-py2.py3-none-any.whl (7.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file fmt-0.1.2.tar.gz.

File metadata

  • Download URL: fmt-0.1.2.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for fmt-0.1.2.tar.gz
Algorithm Hash digest
SHA256 34a36eced07e028547886ed97dee0d55c893b2a48e07c5a77388a8ab37f23d71
MD5 5bf1b04a5aea4a15baa8442bdf8288d3
BLAKE2b-256 d04682e2e672d8efb0b208e72328593313a49071e9f18be702a2699928596269

See more details on using hashes here.

File details

Details for the file fmt-0.1.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for fmt-0.1.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b44a34651b94ce9536f001fa32e18cd654ce43da854da16edee23128b44babec
MD5 3738ed357a5207a2a50d8807306884f0
BLAKE2b-256 bff410dfc6816fbf7982a6a475894a288efefbfeab4b2c0ff2b9ba04e41bda07

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page