Skip to content

Commit

Permalink
PyPI available
Browse files Browse the repository at this point in the history
  • Loading branch information
damnever committed Sep 28, 2016
1 parent 283b49d commit d1a0549
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 28 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
__pycache__/
*-info/
*.egg-info/
insert.py
test.py
build/
dist/
.cache/
123 changes: 102 additions & 21 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,33 +1,114 @@
f-strings(Python 3.6) style literal string interpolation.
==========================================================

Ref: https://www.python.org/dev/peps/pep-0498/
.. image:: https://img.shields.io/travis/damnever/fmt.svg?style=flat-square
:target: https://travis-ci.org/damnever/fmt

:cry:
.. image:: https://img.shields.io/pypi/v/fmt.svg?style=flat-square
:target: https://pypi.python.org/pypi/fmt

Example
-------

.. code:: python
Using `f-strings(PEP 498) <https://www.python.org/dev/peps/pep-0498/>`_ style literal string interpolation without Python 3.6.


Usages
------

- Accessing the globals and locals.

.. code:: python
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 parameters to f,
which added a reference to inside the closure in order this can work.

.. code:: python
import fmt as f
def outer(x='xx'):
y = 'yy'
def inner():
print( f('{x}, {y}', x, y) ) # "xx, 'yy'"
return inner
outer()()
import fmt as f
a = 0
b = 'bb'
- Expression evaluation.

def dd():
return 'dd'
.. code:: python
def func():
c = 'c@'
print(f("a = {a}, b = {b!r}, c = {c}, dd() = {{{ dd() }}}, set == {{{{k for k in range(5)}}}}"))
from datetime import datetime
import fmt as f
# A little ticky...
def outer(foo):
bar = 'bar'
def inner():
print(f("a = {a}, b = {b}, foo = {foo}, bar = {bar}", foo, bar))
return inner
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.

.. code:: python
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, z)}') ) # '4'
print( f('{func(y, z)}') ) # '5'
print( f('{func(x, y)}') ) # '6'
Installation
------------

Install by pip: ::

[sudo] pip install fmt -U


LICENSE
-------

func() # a = 0, b = 'bb', c = c@, dd() = {dd}, set == {set([0, 1, 2, 3, 4])}
outer('foo')() # a = 0, b = bb, foo = foo, bar = bar
`The BSD 3-Clause License <https://github.com/damnever/fmt/blob/master/LICENSE>`_
4 changes: 3 additions & 1 deletion fmt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

"""
f-strings(Python 3.6) style literal string interpolation.
Ref: https://www.python.org/dev/peps/pep-0498/
"""

from __future__ import absolute_import
Expand All @@ -14,6 +13,9 @@

version = __version__ = '0.1.0'
version_info = [int(num) for num in version.split('.')]
__author__ = 'damnever (X.C Dong)'
__email__ = 'dxc.wolf@gmail.com'
__license__ = 'The BSD 3-Clause License'


# Install the Fmt() object in sys.modules,
Expand Down
9 changes: 4 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
setup(
name='fmt',
version=version,
description=('f-strings(Python 3.6) style literal string interpolation.'),
description='f-strings(Python 3.6) style literal string interpolation.',
long_description=long_description,
url='https://github.com/damnever/fmt',
author='damnever',
author_email='dxc.wolf@gmail.com',
license='The BSD 3-Clause License',
classifiers=[
'Development Status :: 5 - Production/Stable',
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
'Topic :: Utilities',
'License :: OSI Approved :: BSD License',
Expand All @@ -36,7 +36,6 @@
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
],
keywords='requirements tool',
packages=find_packages(),
include_package_data=True,
keywords='f-strings, format, literal string interpolation',
packages=find_packages()
)

0 comments on commit d1a0549

Please sign in to comment.