Skip to content

Ideomatic and lightweight Python dataclasses inspired by Pydantic

License

Notifications You must be signed in to change notification settings

codefather-labs/datamodels

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

datamodels

Ideomatic and lightweight Python dataclasses inspired by Pydantic

  • Installation pip install git+https://github.com/codefather-labs/datamodels.git
from datamodels import DataModel
from datamodels import exceptions


class Request(DataModel):
    data: dict
    producer: int
    consumer: int
    

request = Request(
    data={},
    producer=1,
    consumer=2,
)
>>> "<Request data: dict={}, producer: int=1, consumer: int=2>"
# statictypes by default is True
request = Request(
    data={}, 
    producer=1, 
    consumer="2",
)
>>> "datamodels.exceptions.InvalidType: consumer expected <class 'int'> got <class 'str'>"

request = Request(
    data={}, 
    producer=1, 
    consumer=2,
)

request.update({
    "producer": "3"
})
>>> "datamodels.exceptions.InvalidType: Can't assign undeclared type of attribute 'producer'"
# readonly by default is False
request = Request(
    data={},
    producer=1,
    consumer=2,
)

request.update({
    "producer": 3
})
>>> "<Request data: dict={}, producer: int=3, consumer: int=2>"

request = Request(
    data={},
    producer=1,
    consumer=2,
    readonly=True
)

request.update({
    "producer": 3
})
>>> "datamodels.exceptions.ReadOnlyAccessError: <Request data: dict={}, producer: int=1, consumer: int=2 readonly>"

More examples at test.py

About

Ideomatic and lightweight Python dataclasses inspired by Pydantic

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages