Skip to main content

Keep track of components in strings, especially formatted values in f-strings.

Project description

fvalues

Build Status Coverage Status

This is a Python library for keeping track of the combination of components in a string. In particular it lets you separate out the formatted values in an f-string. Here's an example:

from fvalues import F, FValue

x = 1.2345
f = F(f"twice x is {x * 2:.2f}")
assert f == "twice x is 2.47"
assert f.parts == ("twice x is ", FValue(source="x * 2", value=2.469, formatted="2.47"))

Key facts:

  • F is a subclass of str so it can generally be used like any other string.
  • Calls to the constructor F() are magically detected using the executing library, and f-strings within are parsed to extract their components.
  • These are saved in the attribute F.parts. Each part is either a str representing a constant section or an FValue representing a dynamic expression.
  • FValue.source contains the source code between the braces ({}) but before the colon (:) and format spec (.2f). In some cases it may not be the exact original source code, but equivalent code produced by ast.unparse.
  • FValue.value and FValue.formatted are calculated using eval(), so be careful of expressions that you wouldn't want to evaluate twice due to performance or side effects.

Concatenation

The F class also has special support for concatenation with the + operator:

f += "!"
assert f == "twice x is 2.47!"
assert f.parts == (
    FValue(
        source="f",
        value="twice x is 2.47",
        formatted="twice x is 2.47",
    ),
    "!",
)

Similar to deconstructing f-strings, you can see how the parts distinguish between the dynamic expression f on the left of +=, representing it as an FValue, and the static "!" on the right.

Flattening

In the assertion above above, FValue.value is shown as a plain string, but remember that it's actually also an F object itself. The assertion works because F is a subclass of str so they can be used interchangeably. But it still has the same parts that we saw earlier. Sometimes keeping the tree of parts in its original form can be useful, other times you may want to bring everything to the surface to make things easier. You can produce an equivalent F object with a flat list of parts using F.flatten:

assert f.flatten().parts == (
    "twice x is ",
    FValue(
        source="x * 2",
        value=2.469,
        formatted="2.47",
    ),
    "!",
)

Other string methods

Another method that's specially implemented for F is .strip() and its cousins lstrip and rstrip. It does the same thing as the usual str.strip as far as the whole string is concerned, but also strips the internal parts in the way you'd probably expect. See the docstring for more details.

All other methods are directly inherited from str, which means that methods such as .lower() will return a plain str rather than another F object. So be careful with those methods if you don't want to lose information about the parts! More specialised implementations may be added in the future.

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

fvalues-0.0.1.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

fvalues-0.0.1-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file fvalues-0.0.1.tar.gz.

File metadata

  • Download URL: fvalues-0.0.1.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.0

File hashes

Hashes for fvalues-0.0.1.tar.gz
Algorithm Hash digest
SHA256 f7b985cb4f50dba4c0cf7a4d2c86106ea9b404099b88ce322852799c917fe5a8
MD5 435396486c7ccaadb6c48a866a3168ff
BLAKE2b-256 0781dff839c48a109a7bc84531aa29355ce903c8c8eebcc920dfb26ddc55fc05

See more details on using hashes here.

File details

Details for the file fvalues-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: fvalues-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.0

File hashes

Hashes for fvalues-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9e603ea2a54aba641b64c7bd7a3ffea5644bf94318a88324e2f56bda94553386
MD5 adb891795b014778f2a74db0c6caa10c
BLAKE2b-256 46a8ea20742633b892401fcafe8808152aa8c7ad1bd1983d5fa8377c6a8c973b

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