Skip to main content

Format a string using names in the current scope.

Project description

ScopeFormatter allows Python’s string formatting to be used with names drawn from the current scope, similar to the variable interpolation found in languages such as Ruby and Perl.

Examples

>>> from scopeformatter import F
>>> greeting = 'Hello'
>>> def greet(name):
...     return F('{greeting}, {name}!')
>>> greet('world')
'Hello, world!'

Positional and keyword arguments are accepted:

>>> F('{greeting} {0} times, {name}!', len(greeting), name='world')
'Hello 5 times, world!'

Requirements

The stack inspection requires a Python VM that provides sys._getframe(), such as CPython.

Limitations

Non-global names from enclosing scopes will not be found unless they are referenced in the local scope.

>>> def outer():
...     non_local = 'non-local'
...     def inner():
...         return F('{non_local} is not referenced locally')
...     return inner()
>>> outer()
Traceback (most recent call last):
    ...
KeyError: 'non_local'
>>> def outer():
...     non_local = 'non-local'
...     def inner():
...         non_local
...         return F('{non_local} is referenced locally')
...     return inner()
>>> outer()
'non-local is referenced locally'

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

ScopeFormatter-1.0.2.tar.gz (1.9 kB view details)

Uploaded Source

File details

Details for the file ScopeFormatter-1.0.2.tar.gz.

File metadata

File hashes

Hashes for ScopeFormatter-1.0.2.tar.gz
Algorithm Hash digest
SHA256 cf2a855fa03443c8b3659171de3ea8108e5073c0c701fb5ddea752edca600209
MD5 359ac2ad3e024d801988744b56a7c824
BLAKE2b-256 8b03bca2ed1aec8ac480a0c5ac1e3574666bdbdcaa4e4df1c4dfbb4a463e6a28

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