strto
strto is a Python library for parsing strings into Python objects based on types and type annotations.
Installation
From PyPi
pip install strto
From source
pip install git+https://github.com/zigai/strto.git
Examples
>>> from strto import get_parser
>>> parser = get_parser()
>>> parser.parse("5", int)
5
>>> parser.parse("1.5", int | float)
1.5
>>> parser.parse("1,2,3,4,5", list[int])
[1, 2, 3, 4, 5]
>>> parser.parse('{"a":1,"b":2,"c":3}', dict[str, int])
{'a': 1, 'b': 2, 'c': 3}
import datetime
>>> parser.parse("2022.07.19", datetime.date)
datetime.date(2022, 7, 19)
>>> parser.parse("0:5:1", range)
range(0, 5, 1)
>>> import enum
>>> class Color(enum.Enum):
... RED = 1
... GREEN = 2
... BLUE = 3
>>> parser.parse("RED", Color)
Color.RED
Automatic model parsing (dataclasses and pydantic v2)
strto can parse dataclasses and pydantic v2 models from JSON, files, or key/value strings:
from dataclasses import dataclass
from strto import get_parser
@dataclass
class NetworkAddress:
host: str
port: int
parser = get_parser()
parser.parse('{"host":"localhost","port":5432}', NetworkAddress)
parser.parse("host=localhost port=5432", NetworkAddress)
parser.parse("@addr.yaml", NetworkAddress)
Nested models via dotted keys (or nested JSON):
from dataclasses import dataclass
@dataclass
class NetworkAddress:
host: str
port: int
@dataclass
class ApplicationConfig:
debug: bool = False
network: NetworkAddress | None = None
parser.parse(
"debug=true network.host=db network.port=5433",
ApplicationConfig,
)
Notes:
- Key/value parsing supports dotted keys for nested objects.
- Use JSON arrays/objects for complex values (e.g., lists of objects).
- Pydantic support requires pydantic v2 to be installed.
Optional: enable parsing for any class by inspecting __init__ with objinspect:
from strto import get_parser
parser = get_parser(allow_class_init=True)
Custom parser for alternative string formats
If you want a non-standard string format (e.g., host:port), register a custom parser
to override the default model parsing for that type.
from dataclasses import dataclass
from strto import ParserBase, get_parser
@dataclass
class NetworkAddress:
host: str
port: int
class NetworkAddressParser(ParserBase):
def parse(self, value: str) -> NetworkAddress:
host, port = value.rsplit(":")
return NetworkAddress(host=host, port=int(port))
parser = get_parser()
parser.add(NetworkAddress, NetworkAddressParser())
result = parser.parse("example.com:8080", NetworkAddress)
print(result) # NetworkAddress(host='example.com', port=8080)
# You can also use a function
def parse_network_address(value: str) -> NetworkAddress:
host, port = value.rsplit(":")
return NetworkAddress(host=host, port=int(port))
parser = get_parser()
parser.add(NetworkAddress, parse_network_address)
result = parser.parse("example.com:8080", NetworkAddress)
print(result) # NetworkAddress(host='example.com', port=8080)
License
Metadata
Release files for strto 0.2.9
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| strto-0.2.9.tar.gz | 84.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| strto-0.2.9-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 99.8 kB
Release files / strto-0.2.9.tar.gz
| Download URL | strto-0.2.9.tar.gz |
|---|---|
| Size | 84.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5ee1da6028ceaa401cfd3804ae079c32272fa558e3e80f1a878c4bae3e600124
|
|
BLAKE2b-256 checksum How to use checksums |
426d7e615938fdb28fc3b5f9fa8c72b0b5c7e0d498401c3fb2618dfbaa4edcbb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Release files / strto-0.2.9-py3-none-any.whl
| Download URL | strto-0.2.9-py3-none-any.whl |
|---|---|
| Size | 15.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c53bee5e04dcbb844f0c7dbb290d34fa3f551af973062907c62aa681703a188c
|
|
BLAKE2b-256 checksum How to use checksums |
55bfb082c4e92f4e772fae50dbe2505b47669f453539d820a61150fba6fade38
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|