No project description provided
Project description
Lazy parsing for Pydantic models
This library provides a lazy interface for parsing objects from dictionaries. During the parsing, it saves the raw data inside the object and parses each field on demand.
Install
poetry
poetry add lazy-model
pip
pip install lazy-model
Usage
from lazy_model import LazyModel
from pydantic import validator
class Sample(LazyModel):
i: int
s: str
@validator("s")
def s_upper(cls, v):
return v.upper()
obj = Sample.lazy_parse({"i": "10", "s": "test"})
# at this point the data is stored in a raw format inside the object
print(obj.__dict__)
# >>> {'i': NAO, 's': NAO}
# NAO - Not An Object. It shows that the field was not parsed yet.
print(obj.s)
# >>> TEST
# Custom validator works during lazy parsing
print(obj.__dict__)
# >>> {'i': NAO, 's': 'TEST'}
# The `s` field was already parsed by this step
print(obj.i, type(obj.i))
# >>> 10 <class 'int'>
# It converted `10` from string to int based on the annotations
print(obj.__dict__)
# >>> {'i': 10, 's': 'TEST'}
# Everything was parsed
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
lazy-model-0.0.5.tar.gz
(7.1 kB
view hashes)
Built Distribution
lazy_model-0.0.5-py3-none-any.whl
(11.3 kB
view hashes)
Close
Hashes for lazy_model-0.0.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8b4fc5eac99029f84b11b21e81a6894911a475f25e53227b7e44833e62e26553 |
|
MD5 | 69b397c12778814304c07e1b01de3357 |
|
BLAKE2b-256 | 16197d72b219dd73dd3e5c2212fcc54d58b255bd62d9469da39c830f8a41cc70 |