Skip to main content

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:

  1. A string surrounded by "<" and ">" is called a block, and blocks can be nested, e.g., "<...>".
  2. The string inside a block is treated as a Python f-string for interpolation. If interpolation fails, the block returns an empty string "".
  3. 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}".
  4. Named blocks can be defined without output, allowing later references. The syntax is "<?{{name}}...>".
  5. If you want to use "<" and ">", but not as block indicators, write them as "<" and ">".
  6. Supports the binary operator "||". The syntax is "part1||part2". If part1 doesn't raise an error, its value will be used; otherwise, part2 is 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)

Source distribution for partial_fstring 0.0.5.3
File Size Uploaded
partial_fstring-0.0.5.3.tar.gz 5.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for partial_fstring 0.0.5.3
File Interpreter ABI Platform
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

Release history Release notifications | RSS feed

This release

0.0.5.3 This release

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page