Python fstring-based template interpolation.
Project description
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}'
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 partial_fstring-0.0.5.3.tar.gz.
File metadata
- Download URL: partial_fstring-0.0.5.3.tar.gz
- Upload date:
- Size: 5.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.12.4 Darwin/23.5.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
57f8a86f79ce8638222351714fae5f99dcea09677308f7293c48551797f74920
|
|
| MD5 |
92bee31ade0b19be53e695fcb181a5ed
|
|
| BLAKE2b-256 |
9992d54b83cba5dd609293bf0bd7fec273783c57147962e1a325759d6682ed52
|
File details
Details for the file partial_fstring-0.0.5.3-py3-none-any.whl.
File metadata
- Download URL: partial_fstring-0.0.5.3-py3-none-any.whl
- Upload date:
- Size: 7.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.12.4 Darwin/23.5.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b6809e74501b61dc1292b0cb121f1e86ecd88496e5dbfb7e82a5b0e600688977
|
|
| MD5 |
adb91b1383eb473164faed0ec94e460c
|
|
| BLAKE2b-256 |
387cfae6596634e71552512c201f352d3df502d630d23b5423cb143756720e60
|