Format an parse strings with ${variables} of any syntax.
Project description
Varformat Library
Varformat can format and un-format (parse) strings containing various styles of variables.
>>> import varformat as vf
>>> vf.format('Hi ${name}!', name='mom')
'Hi mom!'
>>> vf.parse('archive-${date}.tar.gz', 'archive-1970-01-01.tar.gz')
{'date': '1970-01-01'}
>>> from varformat.formats import python
>>> python.format('Classic {style}', style='python braces')
'Classic python braces'
>>> from varformat.formats import posix_shell as sh
>>> sh.format('POSIX compliant $style', style='dollar variables')
'POSIX compliant dollar variables'
Getting Started
Varformat is available to install via pip:
pip install varformat
When installed, the modules varformat and varformat.formats will be available. Global functions format, vformat, and parse represent the default formmatter with a ${} style:
>>> import varformat as vf
>>> vf.format('my name ${name}', name='jeff')
'my name jeff'
If it is necessary to specify keys which are not valid python identifiers, such as numbers or string with spaces, you can use vformat instead:
>>> import varformat as vf
>>> vf.vformat('My three favorite foods: ${1}, ${2}, and ${1} again',
... {'1': 'pizza', '2': 'chocolate'})
'My three favorite foods: pizza, chocolate, and pizza again'
vformat also supports keyword arguments to customize formatting behavior. partial_ok (default False) and extra_ok (default: True) control whether it is allowed to provide less (or more) arguments than the format string requires. ambiguity_check (default: False) will raise an error if your resulting string will be ambiguous:
>>> import varformat as vf
>>> vf.vformat('package-${os}-${arch}', {'os': 'ubuntu-22.04', 'arch': 'amd64'}, ambiguity_check=True)
Traceback (most recent call last):
...
varformat.AmbiguityError: refusing to format because parsing would be ambiguous:
could be: {'os': 'ubuntu-22.04', 'arch': 'amd64'}
or: {'os': 'ubuntu', 'arch': '22.04-amd64'}
The parse function, which performs the inverse of vformat, also supports ambiguity_check (default: True):
>>> import varformat as vf
>>> vf.parse('package-${os}-${arch}', 'package-ubuntu-22.04-amd64')
Traceback (most recent call last):
...
varformat.AmbiguityError: parsing is ambiguous:
could be: {'os': 'ubuntu-22.04', 'arch': 'amd64'}
or: {'os': 'ubuntu', 'arch': '22.04-amd64'}
You can of course set ambiguity_check to False, and parse will parse using the regular expression rules (greedily).
Other formatters
Module varformat.formats contains formatters with other syntaxes:
varformat.formats.posix_shellfollows POSIX shell variable rules: it disallows numeric identifiers, identifiers with spaces, but allows referencing variables like$varin addition to${var};varformat.formats.pythonfollows classic python format string rules (e.g.{var}).
You can define your own formatter with your own custom syntax by subclassing either varformat.RegexFormatter and defining a regular expression that detects placeholders, or varformat.AbstractFormatter and defining a parsing function. See class docstrings for more information.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file varformat-1.0.0.tar.gz.
File metadata
- Download URL: varformat-1.0.0.tar.gz
- Upload date:
- Size: 14.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.0.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f83f7d67af0d41fc0ad41f1771eec4dd642b573c43f3f5eff2dc865253ceafa2
|
|
| MD5 |
e12c769298cca2100ad0150743c965a8
|
|
| BLAKE2b-256 |
9f1a84a29903bf1256315f0c902c11739ffa6d064c94af18f40f84832afe7640
|
File details
Details for the file varformat-1.0.0-py3-none-any.whl.
File metadata
- Download URL: varformat-1.0.0-py3-none-any.whl
- Upload date:
- Size: 13.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.0.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3b68b25959489f9187e594a8e75ace9974c12f220108f81e9b994344ddda3a35
|
|
| MD5 |
68aa061a0c98f83e0795dfe7d349f14b
|
|
| BLAKE2b-256 |
07132d15f426a5c5040f2b94819c3949c30ec96d9e10731ef0112309203558a5
|