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.1.1.tar.gz (5.7 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.1.1-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for fstringinterpolation-0.1.1.tar.gz
Algorithm Hash digest
SHA256 d7cee7907904999e918c8ea9b5de0535538b58ed836549d083e541207a73e69c
MD5 38ee018972dc55c90000ceedc143d2b6
BLAKE2b-256 7039cb294e35d9152c608b817069fbe90fc696d030c27bb9df0c5324e9f15064

See more details on using hashes here.

Provenance

The following attestation bundles were made for fstringinterpolation-0.1.1.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.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for fstringinterpolation-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 58e1c8569592e7c451c4b196c339e12515cc15272fcd6076920c7d838d3b55a9
MD5 2c63ad7854702763ffa1a198347e5ff1
BLAKE2b-256 0da696cfba4e1348a6a530598cf91df5136308f3910eb0b285ed89cdbc0fe886

See more details on using hashes here.

Provenance

The following attestation bundles were made for fstringinterpolation-0.1.1-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