File: README.md

package info (click to toggle)
python-lazy-model 0.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 152 kB
  • sloc: python: 280; makefile: 7
file content (66 lines) | stat: -rw-r--r-- 1,116 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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
```shell
poetry add lazy-model
```

pip
```shell
pip install lazy-model
```

## Usage

```python
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
```