Skip to main content

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

Metadata

Release files for lazy-model 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lazy-model 0.4.0
File Size Uploaded
lazy-model-0.4.0.tar.gz 8.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for lazy-model 0.4.0
File Interpreter ABI Platform
lazy_model-0.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 22.0 kB

Release files / lazy-model-0.4.0.tar.gz

Download URL lazy-model-0.4.0.tar.gz
Size 8.3 kB
Tags Source
SHA-256 checksum
How to use checksums
a851d85d0b518b0b9c8e626bbee0feb0494c0e0cb5636550637f032dbbf9c55f
BLAKE2b-256 checksum
How to use checksums
7285e25dc36dee49cf0726c03a1558b5c311a17095bc9361bcbf47226cb3075a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.0 CPython/3.12.11 Linux/6.11.0-1018-azure

Release files / lazy_model-0.4.0-py3-none-any.whl

Download URL lazy_model-0.4.0-py3-none-any.whl
Size 13.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
95ea59551c1ac557a2c299f75803c56cc973923ef78c67ea4839a238142f7927
BLAKE2b-256 checksum
How to use checksums
5c54653ea0d7c578741e9867ccf0cbf47b7eac09ff22e4238f311ac20671a911
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.0 CPython/3.12.11 Linux/6.11.0-1018-azure

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page