Python fstring-based template interpolation.
Installation
You can install from pypi
pip install -U partial_fstring
Usage
from partial_fstring import parse, render
block = parse("<{a}>{b}")
block.render({"b": 1}) # get '1'
# OR
render("<{a}>{b}", {"b": 1})
Syntax Overview:
- A string surrounded by "<" and ">" is called a block, and blocks can be nested, e.g., "<...>".
- The string inside a block is treated as a Python f-string for interpolation. If interpolation fails, the block returns an empty string "".
- Blocks can have names (similar to capture groups in regular expressions), placed inside "{{" and "}}". The syntax is "<{{name}}...>", and the value of this block can be referenced repeatedly using "{name}".
- Named blocks can be defined without output, allowing later references. The syntax is "<?{{name}}...>".
- If you want to use "<" and ">", but not as block indicators, write them as "<" and ">".
- Supports the binary operator "||". The syntax is "part1||part2".
If
part1doesn't raise an error, its value will be used; otherwise,part2is executed, and its value will be used or raises an error. This operator can be chained indefinitely.
Some more detailed tests show below:
>>> from partial_fstring import render
>>> class mdict(dict):
... @staticmethod
... def __missing__(key):
... return "{%s}" % key
>>> s = "{title}< ({year})>< [tmdbid={tmdbid}]>/Season {season}/{title} - {season_episode}<-{part}>< - 第 {episode} 集>< - {videoFormat}><.{edition}><.{videoCodec}><.{audioCodec}><-{releaseGroup}>{fileExt}"
>>> render(s, mdict())
'{title} ({year}) [tmdbid={tmdbid}]/Season {season}/{title} - {season_episode}-{part} - 第 {episode} 集 - {videoFormat}.{edition}.{videoCodec}.{audioCodec}-{releaseGroup}{fileExt}'
>>> s = "{title}< ({year})>||< [tmdbid={tmdbid}]>/Season {season}/{title} - {season_episode}<-{part}>< - 第 {episode} 集>< - {videoFormat}><.{edition}><.{videoCodec}><.{audioCodec}><-{releaseGroup}>{fileExt}"
>>> render(s, mdict())
'{title} ({year})'
>>> s = """{1} {f"{1} {2}"} { {3} }"""
>>> render(s, mdict())
'1 1 2 {3}'
>>> f"""{1} {f"{1} {2}"} { {3} }"""
'1 1 2 {3}'
Metadata
Release files for partial_fstring 0.0.5.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| partial_fstring-0.0.5.3.tar.gz | 5.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| partial_fstring-0.0.5.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.3 kB
Release files / partial_fstring-0.0.5.3.tar.gz
| Download URL | partial_fstring-0.0.5.3.tar.gz |
|---|---|
| Size | 5.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
57f8a86f79ce8638222351714fae5f99dcea09677308f7293c48551797f74920
|
|
BLAKE2b-256 checksum How to use checksums |
9992d54b83cba5dd609293bf0bd7fec273783c57147962e1a325759d6682ed52
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.4 CPython/3.12.4 Darwin/23.5.0
|
Release files / partial_fstring-0.0.5.3-py3-none-any.whl
| Download URL | partial_fstring-0.0.5.3-py3-none-any.whl |
|---|---|
| Size | 7.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b6809e74501b61dc1292b0cb121f1e86ecd88496e5dbfb7e82a5b0e600688977
|
|
BLAKE2b-256 checksum How to use checksums |
387cfae6596634e71552512c201f352d3df502d630d23b5423cb143756720e60
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.4 CPython/3.12.4 Darwin/23.5.0
|