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

Uploaded Python 3

File details

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

File metadata

  • Download URL: fstringinterpolation-0.1.0.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for fstringinterpolation-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4d71273df92a1d22a2764075e23308013817c65aa45840c9a3909a1e01a5c069
MD5 1466c6beeba45e81a35d0078b3c4aaac
BLAKE2b-256 4c7370665610cf39ee0d999a68db1540f5d7cbcae6f94c7f9dca4cd549d28341

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fstringinterpolation-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2d51b4d728ad4ace1993b2c8bbe422c00c0a003281163b5927403288908ee8e6
MD5 6d537a9eb875d726b91f27661edfb7c2
BLAKE2b-256 195b4914fb897a960d1a5cd2b6f88d8f94c606f3180d0f1d039e412d78db79c8

See more details on using hashes here.

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