Skip to main content

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:

  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}'

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

partial_fstring-0.0.5.3.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

partial_fstring-0.0.5.3-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

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

Hashes for partial_fstring-0.0.5.3.tar.gz
Algorithm Hash digest
SHA256 57f8a86f79ce8638222351714fae5f99dcea09677308f7293c48551797f74920
MD5 92bee31ade0b19be53e695fcb181a5ed
BLAKE2b-256 9992d54b83cba5dd609293bf0bd7fec273783c57147962e1a325759d6682ed52

See more details on using hashes here.

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

Hashes for partial_fstring-0.0.5.3-py3-none-any.whl
Algorithm Hash digest
SHA256 b6809e74501b61dc1292b0cb121f1e86ecd88496e5dbfb7e82a5b0e600688977
MD5 adb91b1383eb473164faed0ec94e460c
BLAKE2b-256 387cfae6596634e71552512c201f352d3df502d630d23b5423cb143756720e60

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