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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d7cee7907904999e918c8ea9b5de0535538b58ed836549d083e541207a73e69c
|
|
| MD5 |
38ee018972dc55c90000ceedc143d2b6
|
|
| BLAKE2b-256 |
7039cb294e35d9152c608b817069fbe90fc696d030c27bb9df0c5324e9f15064
|
Provenance
The following attestation bundles were made for fstringinterpolation-0.1.1.tar.gz:
Publisher:
pypi.yml on umlaeute/FStringInterpolation
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fstringinterpolation-0.1.1.tar.gz -
Subject digest:
d7cee7907904999e918c8ea9b5de0535538b58ed836549d083e541207a73e69c - Sigstore transparency entry: 319576590
- Sigstore integration time:
-
Permalink:
umlaeute/FStringInterpolation@d7a0449c2566e8e9d5a10c73960af0fe1f62ad26 -
Branch / Tag:
refs/tags/0.1.1 - Owner: https://github.com/umlaeute
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi.yml@d7a0449c2566e8e9d5a10c73960af0fe1f62ad26 -
Trigger Event:
push
-
Statement type:
File details
Details for the file fstringinterpolation-0.1.1-py3-none-any.whl.
File metadata
- Download URL: fstringinterpolation-0.1.1-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
58e1c8569592e7c451c4b196c339e12515cc15272fcd6076920c7d838d3b55a9
|
|
| MD5 |
2c63ad7854702763ffa1a198347e5ff1
|
|
| BLAKE2b-256 |
0da696cfba4e1348a6a530598cf91df5136308f3910eb0b285ed89cdbc0fe886
|
Provenance
The following attestation bundles were made for fstringinterpolation-0.1.1-py3-none-any.whl:
Publisher:
pypi.yml on umlaeute/FStringInterpolation
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fstringinterpolation-0.1.1-py3-none-any.whl -
Subject digest:
58e1c8569592e7c451c4b196c339e12515cc15272fcd6076920c7d838d3b55a9 - Sigstore transparency entry: 319576605
- Sigstore integration time:
-
Permalink:
umlaeute/FStringInterpolation@d7a0449c2566e8e9d5a10c73960af0fe1f62ad26 -
Branch / Tag:
refs/tags/0.1.1 - Owner: https://github.com/umlaeute
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi.yml@d7a0449c2566e8e9d5a10c73960af0fe1f62ad26 -
Trigger Event:
push
-
Statement type: