Skip to main content

No project description provided

Project description

f-strings...sorta
=================

Home-page: https://github.com/danbradham/fstrings
Author: Dan Bradham
Author-email: danielbradham@gmail.com
License: MIT
Description: =================
f-strings...sorta
=================
.. image:: https://travis-ci.org/danbradham/fstrings.svg?branch=master
:target: https://travis-ci.org/danbradham/fstrings

Python 3.6 f-strings are pretty awesome. It's too bad you can't use them in 2.7 or 3.5. With **fstrings** you can...sorta. The **fstrings** module provides a function *f* that acts similar to f-strings. Pass a string with str.format tokens and *f* will return a string formatted using the available globals and locals. Like this::

>>> from fstrings import f
>>> x = 'Hello, World...'
>>> f('{x}')
'Hello, World...'

You can't evaluate arbitrary python code within the format tokens like you can in Python 3.6, but, some of that lost functionality is gained by allowing you to pass *args* and *kwargs* to *f*. For example, you can still use positional arguments with *f*::

>>> x = 'World...'
>>> f('{} {x}', 'Hello,')
'Hello, World...'

Or you could override globals and locals by passing keyword arguments::

>>> x = 'Hello'
>>> y = 'World...'
>>> f('{x}, {y}', x='Goodbye')
'Goodbye, World...'

Or do both, it's your life. In addition to *f*, fstrings also provides some other nifty stuff.


fdocstring Decorator
====================
::

>>> from fstrings import fdocstring
>>> x = 'Hello from ya docs'
>>> @fdocstring()
... def func():
... '''{x}'''
...
>>> func.__doc__
'Hello from ya docs'

Right now you might be thinking, "Cool, *fdocstring* provides the same funcality as *f*, but, for doc strings." You would be correct. You can even use *fdocstring* to format class doc strings:

::

>>> x = 'BOOM!'
>>> @fdocstring()
... class Obj(object):
... '''{x}'''
... def method(self):
... '''{x}'''
...
>>> Obj.__doc__
'BOOM!'
>>> Obj.method.__doc__
'BOOM!'

"Boom boom" is right. Methods are auto formatted too.


printf?
=======
After implementing *f*, *printf* was too obvious not to implement.

::

>>> from fstrings import printf
>>> x = 'PRINTFED'
>>> printf('{x}')
PRINTFED

*printf* and *fdocstring* accept *args* and *kwargs* for overriding globals and locals just like *f*.

Features and Differences
========================

- Uses str.format instead of evaluating python code in {}
- Allows overriding globals and locals by passing in \*args and \*\*kwargs
- Supports python 2.7 to python 3.6

Tests
=====
**fstrings** comes with a robust set of tests. *pip install nose* and run them if you like.

::

> nosetests -v --with-coverage --with-doctest --doctest-extension rst

Similar Projects
================
If you're looking for an implementation truer to Python 3.6 f-strings check out `fmt <https://github.com/damnever/fmt>`_.

Platform: UNKNOWN
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 2.7
Classifier: Programming Language :: Python :: 3.2
Classifier: Programming Language :: Python :: 3.3
Classifier: Programming Language :: Python :: 3.4
Classifier: Programming Language :: Python :: 3.5
Classifier: Programming Language :: Python :: 3.6
Classifier: Topic :: Software Development :: Libraries :: Python Modules

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

fstrings-0.1.0.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

fstrings-0.1.0-py2.py3-none-any.whl (5.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file fstrings-0.1.0.tar.gz.

File metadata

  • Download URL: fstrings-0.1.0.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for fstrings-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ee7f7d552ec6f83c74a0d6d93459d906cb470f2f57539bf4186c7c6534789198
MD5 7f88a027d162893e4673ea135e36e270
BLAKE2b-256 c9a669aa2dc8ca7c379145bbb0abe1d5e1fe1fc06ea23d33c29946524bf9fcfd

See more details on using hashes here.

File details

Details for the file fstrings-0.1.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for fstrings-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 053c27bc2fbfa586582f0866b5cbc037bd81305f0fe10eeceac342391efe7faa
MD5 75993ce83fb6ca137d7b49ffec0253c3
BLAKE2b-256 567001cbc51a2541cb97917037b41e5102e21c9c4d792a77bcd87f0304c11946

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