Skip to main content

temparse is template + parse

Project description

temparse

temparse = template + parse, parse strings using t-string templates.

Inspired by parse, but built on Python 3.14's t-strings, with better type hints.

Installation

uv

uv add temparse

pip

# Requires Python >= 3.14.
pip install temparse

Quick Start

from temparse import parse

city, year = parse[str, int](
    t"I live in {str}, since {int}", "I live in Tokyo, since 2010"
)
assert city == "Tokyo"
assert year == 2010

The return type is tuple[str, int].

Supported Types

Interpolation Example Output
{str} "hello" "hello"
{int} "42" 42
{int} "0xff" 255
{int:16} "ff" 255
{float} "3.14" 3.14
{complex} "-1.23+4.5j" -1.23+4.5j
{list} '[1,2,3]' [1, 2, 3]
{dict} '{"a":1}' {"a": 1}
{json} '{"a":1}' {"a": 1}
{datetime.datetime:%d/%m/%y %H:%M:%S} "31/01/22 23:59:59" datetime
{datetime.date:%Y-%m-%d} "2024-03-15" date
{datetime.time:%H:%M:%S} "13:23:27" time

[!IMPORTANT] list and dict are just aliases for json.

Custom Conversions

Decorate a function with @Conversion to use it directly in a template:

from temparse import Conversion, parse


@Conversion
def percent(s: str) -> float:
    return float(s.rstrip("%")) / 100


(result,) = parse[float](t"x = {percent}", "x = 30%")
assert result == 0.3

When your converter needs the format spec as well, use FormatConversion:

from temparse import FormatConversion, parse


@FormatConversion
def between(s: str, spec: str) -> str:
    lo, hi = spec.split(",")
    return s[int(lo) : int(hi)]


(result,) = parse[str](t"{between:2,5}", "abcdefg")
assert result == "cde"

Parser (Compiled Templates)

Build a parser once and reuse it:

from temparse import Parser

parser = Parser[str, int, float](t"{str} + {int} = {float}")

assert parser.parse("foo + 3 = 3.14") == ("foo", 3, 3.14)
assert parser.parse("bar + 7 = 2.72") == ("bar", 7, 2.72)

License

MIT

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

temparse-0.0.2.tar.gz (3.0 kB view details)

Uploaded Source

Built Distribution

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

temparse-0.0.2-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file temparse-0.0.2.tar.gz.

File metadata

  • Download URL: temparse-0.0.2.tar.gz
  • Upload date:
  • Size: 3.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.17 {"installer":{"name":"uv","version":"0.11.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for temparse-0.0.2.tar.gz
Algorithm Hash digest
SHA256 7a8c53aae2cfd643ef9fdd578a76f635d1aba77857225fac2aa4b4099a873476
MD5 b82a32316539ad795bfdb0bc23c7a6fb
BLAKE2b-256 4127e6d35eee714ebda85e3b1ffa09805f5ebdafd3056c7314f1e3574558b3cf

See more details on using hashes here.

File details

Details for the file temparse-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: temparse-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.17 {"installer":{"name":"uv","version":"0.11.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for temparse-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a8d018d89a2d3e552460c88b2d56f42001c5d08a27d837042b3724ad427469dc
MD5 f75217c8ae8dbdd770d304d4f3f31ca0
BLAKE2b-256 474aa83c30c928c3b3d66d6903f04a18418fa6b5f8666b8cf077a39693216d0e

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