Type-guided parsing library for converting strings to Python objects
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file strto-0.2.6.tar.gz.
File metadata
- Download URL: strto-0.2.6.tar.gz
- Upload date:
- Size: 73.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7d2b8614266a0ce43b69d85c8b23c9b36b00ee397bc3a2c8151dd7043dd99b75
|
|
| MD5 |
bc8002dc1bcd4a4465dee603cc81141a
|
|
| BLAKE2b-256 |
e6199cd3f9a2c3b772ec4a14ffa5c846faeced0d6595bc27018c71010f8bdd37
|
File details
Details for the file strto-0.2.6-py3-none-any.whl.
File metadata
- Download URL: strto-0.2.6-py3-none-any.whl
- Upload date:
- Size: 14.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c878e634c34cdbf7f43d6e573ed40910b304ec32c026ecfafc9c766cf2e035ea
|
|
| MD5 |
92d7638e14531f426429725753565d43
|
|
| BLAKE2b-256 |
dce292b68da3641dd86ba6a74df1b8700d49647ec0874a15dc65b84f7affeea6
|