Parsing in Python has never been easier
Project description
Nothing (almost) should ever be any str or any int
What is valtypes
Valtypes is a flexible data parsing library which will help you make illegal states unrepresentable and enable you to practice "Parse, don’t validate" in Python. It has many features that might interest you, so let's dive into some examples.
Examples
Create constrained types:
from valtypes.type.str import NonEmpty, MaximumLength
class Name(NonEmpty, MaximumLength):
__maximum_length__ = 20
def initials(name: Name) -> str:
# name is guaranteed to be a non-empty string of maximum length 20
return f"{name[0]}."
initials(Name("Fred")) # passes
initials(Name("")) # parsing error
initials("") # fails at static type checking
Parse complex data structures:
from dataclasses import dataclass
from valtypes import parse
from valtypes.type import int, list, str
@dataclass
class User:
id: int.Positive
name: Name
hobbies: list.NonEmpty[str.NonEmpty]
raw = {"id": 1, "name": "Fred", "hobbies": ["origami", "curling", "programming"]}
print(parse(User, raw))
User(id=1, name='Fred', hobbies=['origami', 'curling', 'programming'])
Installation
Install from PyPI:
pip install valtypes
Build the latest version from source:
pip install git+https://github.com/LeeeeT/valtypes
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 valtypes-6.0.1.tar.gz.
File metadata
- Download URL: valtypes-6.0.1.tar.gz
- Upload date:
- Size: 17.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4faf8b1391d6358bde2cef06a9e2ad53f61e659a20f11a1b41c868954f483756
|
|
| MD5 |
21fc6980aabe725fc4d597b206708bcc
|
|
| BLAKE2b-256 |
5ad4000586f3c23120deae3b35633097859623a9fbf33bdf01d9092cc5f1c6d6
|
File details
Details for the file valtypes-6.0.1-py3-none-any.whl.
File metadata
- Download URL: valtypes-6.0.1-py3-none-any.whl
- Upload date:
- Size: 33.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a8d2ee8c304a5a2701c74a979cc208b16ae807d6e85144bf4d6e46073baab11f
|
|
| MD5 |
c03a1cad3c0068741dae91e1a2c5d8b7
|
|
| BLAKE2b-256 |
48c41756daff3aacf4222cd1ff911923db863c90ec59f08da0a56b1681d62623
|