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
Metadata
Release files for temparse 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| temparse-1.0.0.tar.gz | 3.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| temparse-1.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 7.1 kB
Release files / temparse-1.0.0.tar.gz
| Download URL | temparse-1.0.0.tar.gz |
|---|---|
| Size | 3.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9215629129db3ef9f9cef33a576185fd26c36026d57ab765e9a191a49ab34982
|
|
BLAKE2b-256 checksum How to use checksums |
36e4457c9a981b734d0bc7a70a5a0f84b0e5abdb4583d421d47e8e019cd6c8df
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","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}
|
Release files / temparse-1.0.0-py3-none-any.whl
| Download URL | temparse-1.0.0-py3-none-any.whl |
|---|---|
| Size | 4.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
44b34b79bd1a68caf8c29cee0e7ff1756311b3cf98570db1b6b5efae6505e9ae
|
|
BLAKE2b-256 checksum How to use checksums |
f50e3674411a71618a9fb0344da76a04aadd797b27c94589aa70f733f427c261
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","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}
|