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.
Metadata
Release files for varformat 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| varformat-1.0.0.tar.gz | 14.4 kB | Without provenance | Details for varformat-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| varformat-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for varformat-1.0.0-py3-none-any.whl |
Total release size: 27.9 kB
Release files / varformat-1.0.0.tar.gz
| Download URL | varformat-1.0.0.tar.gz |
|---|---|
| Size | 14.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f83f7d67af0d41fc0ad41f1771eec4dd642b573c43f3f5eff2dc865253ceafa2
|
|
BLAKE2b-256 checksum How to use checksums |
9f1a84a29903bf1256315f0c902c11739ffa6d064c94af18f40f84832afe7640
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.0.0 CPython/3.12.2
|
Release files / varformat-1.0.0-py3-none-any.whl
| Download URL | varformat-1.0.0-py3-none-any.whl |
|---|---|
| Size | 13.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3b68b25959489f9187e594a8e75ace9974c12f220108f81e9b994344ddda3a35
|
|
BLAKE2b-256 checksum How to use checksums |
07132d15f426a5c5040f2b94819c3949c30ec96d9e10731ef0112309203558a5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.0.0 CPython/3.12.2
|
Security for varformat 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.