Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zigai/strto
Type-guided parsing library for converting strings to Python objects
https://github.com/zigai/strto
string-parser type-hints
Last synced: 2 months ago
JSON representation
Type-guided parsing library for converting strings to Python objects
- Host: GitHub
- URL: https://github.com/zigai/strto
- Owner: zigai
- License: mit
- Created: 2023-04-09T12:20:57.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-10-17T17:45:48.000Z (2 months ago)
- Last Synced: 2024-10-17T20:13:06.343Z (2 months ago)
- Topics: string-parser, type-hints
- Language: Python
- Homepage:
- Size: 46.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# strto
[![Tests](https://github.com/zigai/strto/actions/workflows/tests.yml/badge.svg)](https://github.com/zigai/strto/actions/workflows/tests.yml)
[![PyPI version](https://badge.fury.io/py/strto.svg)](https://badge.fury.io/py/strto)
![Supported versions](https://img.shields.io/badge/python-3.10+-blue.svg)
[![Downloads](https://static.pepy.tech/badge/strto)](https://pepy.tech/project/strto)
[![license](https://img.shields.io/github/license/zigai/strto.svg)](https://github.com/zigai/strto/blob/master/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
```python
>>> 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
```python
from dataclasses import dataclass
from strto import ParserBase, get_parser@dataclass
class NetworkAddress:
host: str
port: intclass 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](https://github.com/zigai/strto/blob/master/LICENSE)