Skip to main content

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

Documentation

Source Code - GitHub

PyPI - lenient-string-formatter

Usage

This package provides a function called lformat that you can use to format strings in a lenient way. You can use it in the same way as you would use Python's built-in str.format method.

This package also provides the LenientFormatter class, a subclass of Python's built-in string.Formatter. It is used internally by the lformat function, but it can be used directly or subclassed if you need more control over the formatting process.

The examples below will use the lformat function. LenientFormatter().format can be used in the same way.

Basic example

from lenient_string_formatter import lformat

template = "{} {} {a} {b}"
formatted = lformat(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 = lformat(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 = lformat(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 = lformat(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 = lformat(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

lenient_string_formatter-1.1.1.tar.gz (15.9 kB view details)

Uploaded Source

Built Distribution

lenient_string_formatter-1.1.1-py3-none-any.whl (17.6 kB view details)

Uploaded Python 3

File details

Details for the file lenient_string_formatter-1.1.1.tar.gz.

File metadata

  • Download URL: lenient_string_formatter-1.1.1.tar.gz
  • Upload date:
  • Size: 15.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.11.9 Linux/6.5.0-1021-azure

File hashes

Hashes for lenient_string_formatter-1.1.1.tar.gz
Algorithm Hash digest
SHA256 8df33d9eee7bf150a5cf5f8b30ae6f171ad3603301007b07156380bc8686695a
MD5 72f3d2bf691faa953d72f745fdb67db2
BLAKE2b-256 9753eb8c8df48977de8286c0901adf8d61173ad8b987f35cab913913d7efca5d

See more details on using hashes here.

File details

Details for the file lenient_string_formatter-1.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for lenient_string_formatter-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7c0e8efeee6d4e71d034fb33f6a02beaf2ff2b85388f3edc20a6aab7436e7f09
MD5 79c522d1cb1f2632c0aa6049e2f8181a
BLAKE2b-256 231f1a452216bb2981774101a03987bd891185aef36a15cbc176de24c1a646cd

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