valtypes 6.0.1

Creator: bradpython12

Last updated:

Add to Cart

Description:

valtypes 6.0.1

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

License

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Customer Reviews

There are no reviews.