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.2.0.tar.gz
(8.2 kB
view details)
Built Distribution
File details
Details for the file lazy-model-0.2.0.tar.gz
.
File metadata
- Download URL: lazy-model-0.2.0.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.0 CPython/3.10.5 Linux/5.15.0-1041-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 57c0e91e171530c4fca7aebc3ac05a163a85cddd941bf7527cc46c0ddafca47c |
|
MD5 | 9065f3f5b1cd92cbe0efc0e8d50881e0 |
|
BLAKE2b-256 | 479ec60681be72f03845c209a86d5ce0404540c8d1818fc29bc64fc95220de5c |
File details
Details for the file lazy_model-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: lazy_model-0.2.0-py3-none-any.whl
- Upload date:
- Size: 13.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.0 CPython/3.10.5 Linux/5.15.0-1041-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5a3241775c253e36d9069d236be8378288a93d4fc53805211fd152e04cc9c342 |
|
MD5 | 47c9d5d26e59858fb36dd858de9eaca3 |
|
BLAKE2b-256 | 0a13e37962a20f7051b2d6d286c3feb85754f9ea8c4cac302927971e910cc9f6 |