A lenient string formatter that leaves unmatched fields untouched in the output string instead of raising a KeyError.
Project description
lenient-string-formatter
A lenient string formatter that leaves unmatched fields untouched in the output string instead of raising exceptions.
The following exceptions that are normally raised by the built-in string formatter are caught and handled as follows:
- KeyError and IndexError will not be raised if a field in the template is not matched by the arguments. Instead, the field will be left untouched in the output string.
- ValueError in case numbered and auto-numbered fields are mixed in the template (e.g. "{1} {}") will not be raised. Explicitly numbered fields will be matched according to their index (remaining untouched if the index is out of bounds), while auto-numbered fields will be matched according to their order in the arguments (again, remaining untouched if the index is out of bounds) independent of the explicit numbering.
- KeyError is not raised on unnumbered field with key/attribute access. (https://bugs.python.org/issue27307)
Installation
You can install this package with pip.
$ pip install lenient-string-formatter
Links
Usage
This package provides the LenientFormatter
class, a subclass of Python's built-in string.Formatter
. You can use it in the same way as you would use the built-in formatter.
Basic example
from lenient_string_formatter import LenientFormatter
formatter = LenientFormatter()
template = "{} {} {a} {b}"
formatted = formatter.format(template, 1, 2, a=3, b=4)
assert formatted == "1 2 3 4"
Unmatched fields
Unmatched fields are left untouched instead of raising exceptions.
template = "{} {} {a} {b}"
formatted = formatter.format(template, 1, a=3)
assert formatted == "1 {} 3 {b}"
Mixing numbered and auto-numbered fields
Explicitly numbered fields are matched according to their index, while auto-numbered fields are matched according to their order in the arguments. They are matched independently of each other.
template = "{1} {}"
formatted = formatter.format(template, 1, 2)
assert formatted == "2 1"
Unnumbered field with key/attribute access
The built-in formatter raises a KeyError when an unnumbered field is used with key/attribute access. This is a bug in the built-in formatter (https://bugs.python.org/issue27307). This implementation doesn't have this bug.
from types import SimpleNamespace
template = "{.attr} {[0]}"
formatted = formatter.format(template, SimpleNamespace(attr=1), [2])
assert formatted == "1 2"
Format specifiers and conversion flags for unmatched fields
Unmatched fields are left untouched. This includes any format specifiers and conversion flags that are applied to the field. Furthermore, if the field is matched but the format specifier has a field which is unmatched, or vice versa, the field is still left untouched.
For example, below {a:3}
and {b!r}
have no matching values, and neither c=3
nor f=4
provide enough values to completely fill the fields {c:{d}}
and {e:{f}}
, so all fields are left untouched.
template = "{a:3} {b!r} {c:{d}} {e:{f}}"
formatted = formatter.format(template, c=3, f=4)
assert formatted == template
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
Built Distribution
File details
Details for the file lenient_string_formatter-1.0.1.tar.gz
.
File metadata
- Download URL: lenient_string_formatter-1.0.1.tar.gz
- Upload date:
- Size: 15.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.11.9 Linux/6.5.0-1017-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b7be50d01159b94cfdc5681151fc0c43c3c60e565a37ba5c16500a29cd908d5d |
|
MD5 | b2e83cbd02be2fe19033b398982b2e00 |
|
BLAKE2b-256 | e7a35a5c0ae307ab2a7c04ab6a58dc78a2d26c15a5317dd3736744e2f6bd3496 |
File details
Details for the file lenient_string_formatter-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: lenient_string_formatter-1.0.1-py3-none-any.whl
- Upload date:
- Size: 16.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.11.9 Linux/6.5.0-1017-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d10bf1e0ed723f665580c6578ee2b80e1f6cbf79bd245c60c047dc539bbc207d |
|
MD5 | d3a3cad7c41f00efe911cc8af28568af |
|
BLAKE2b-256 | 9c28d8a5f000910817ea9547a04453d79973d9bcb3eca73be2cd8ed4b890adbd |