Skip to main content

Type-guided parsing library for converting strings to Python objects

Project description

strto

Tests PyPI version Supported versions Downloads license

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

Add custom parser

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

MIT License

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

strto-0.1.5.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

strto-0.1.5-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file strto-0.1.5.tar.gz.

File metadata

  • Download URL: strto-0.1.5.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for strto-0.1.5.tar.gz
Algorithm Hash digest
SHA256 983ba4ee4974f0d4b0d3c40ff09b978994b5c114a636e44f47408483e611fa45
MD5 ae5af608da9cc59c537c08e5b4636e67
BLAKE2b-256 e1d8b3e3d93b2a5cfc09c1421e0cc87dab3604fe74cb0bd63d7f40ac69a28d15

See more details on using hashes here.

File details

Details for the file strto-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: strto-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for strto-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 f7187f3d08589966219e60f408f34383e9f71e25e6ffbfd8c15a7f09c8e2b47a
MD5 bc69471535c8d25d8308612e14b9d350
BLAKE2b-256 c8c5f53a2da38ddd4f06c119ff81a46606cfe1903f01064df09dd5c7cafb484f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page