Skip to main content

interpolation for python's `configparser` that does f-string substitution

Project description

FStringInterpolation - interpolation configparser that does f-string substitution

Extended Interpolation for the normal Python configparser

  • Apply f-string interpolation on values.

Usage

>>> import configparser
>>> from fstringinterpolation import FStringInterpolation
>>> config = configparser.ConfigParser(interpolation=FStringInterpolation())
>>> config.read_string("""
[DEFAULT]
_pi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286
[full]
pi = {_pi}
[short]
pi = {float(_pi):.2f}
[stringy]
pi = {_pi:.2s}
""")
>>> config["full"]["pi"]
'3.141592653589793238462643383279502884197169399375105820974944592307816406286'
>>> config["short"]["pi"]
'3.14'
>>> config["stringy"]["pi"]
'3.'

More examples

This will automatically create the proper title, depending on the values of a and b

Input

[DEFAULT]
a = 7
title = {a} is {"more" if float(a) > float(b) else "less"} than {b}
[A]
b = 12
[B]
b = 3
[C]
a = 3.14
b = 2.71

Output

section title
A "7 is less than 12"
B "7 is more than 3"
C "3.14 is more than 2.71"

Installing

TODO

Building

python3 -m build

Caveats

  • no recursion! e.g. the following is invalid:

    [DEFAULT]
    pi = 3.141592654
    [int]
    pi = {float(pi)}
    
  • the type of each value is always . if you need something else, you must explicitely convert the values:

    [DEFAULT]
    pi = 3.141592654
    [bad]
    halfpi = {pi/2}
    [good]
    halfpi = {float(pi)/2}
    
  • care must be taken to not override python built-ins (that you want to use) with variables:

    [DEFAULT]
    a = 10
    b = 12
    [good]
    realsum = {sum(float(_) for _ in [a,b])}
    [bad]
    realsum = {sum(float(_) for _ in [a,b])}
    sum = 22
    [bad recursion]
    sum = {sum(float(_) for _ in [a,b])}
    
  • currently, recursion is somewhat broken (unless the interpolated values are re-used as strings):

    [test]
    a = 3
    # b becomes '4'
    b = {int(a)+1}
    # GOOD: c becomes '34' ('3'+'4')
    c = {a+b}
    # BAD: d should become 7, but really fails with "int('{int(a)+1)}')"
    d = {int(a)+int(b)}
    

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

fstringinterpolation-0.2.0.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fstringinterpolation-0.2.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file fstringinterpolation-0.2.0.tar.gz.

File metadata

  • Download URL: fstringinterpolation-0.2.0.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for fstringinterpolation-0.2.0.tar.gz
Algorithm Hash digest
SHA256 96551d065792d534a1feeb7b979010b44c65ada24d093b2d44f0a2728784314f
MD5 4daca7557f43dced6d931d461f2e94a0
BLAKE2b-256 b970007ccff3cc04de97f407691ef71aa23817c30d77271d1c3098d2fec422e1

See more details on using hashes here.

Provenance

The following attestation bundles were made for fstringinterpolation-0.2.0.tar.gz:

Publisher: pypi.yml on umlaeute/FStringInterpolation

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fstringinterpolation-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fstringinterpolation-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ec793ebe46b99c3fe01eaca0d75237aea8266ba596da519dc352d082636f6a9e
MD5 c87d0cb7a9a6b14d7ffbb53f7615c484
BLAKE2b-256 0694e07e9d952bc4bb6738b111317b652fad0e8056f8935d2360ff4d49e98fa0

See more details on using hashes here.

Provenance

The following attestation bundles were made for fstringinterpolation-0.2.0-py3-none-any.whl:

Publisher: pypi.yml on umlaeute/FStringInterpolation

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page