Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- import time
- import orjson
- from pydantic import BaseModel, conlist
- class InnerModel(BaseModel):
- id: int
- name: str
- class FooModel(BaseModel):
- x: int
- y: dict[str, str] = {}
- inner_model: conlist(InnerModel, max_items=10, min_items=8)
- class FooModelList(BaseModel):
- data: list[FooModel]
- def load_bytes():
- with open('file.json', 'rb') as file:
- return file.read()
- def load_data_pydantic():
- with open('file.json') as file:
- raw_data = json.load(file)
- return FooModelList(**raw_data)
- def test_encode_pydantic():
- foo_list = load_data_pydantic()
- start_time = time.monotonic()
- foo_list.json(encoder=orjson.dumps)
- print('Pydantic + orjson, Encode: ', time.monotonic() - start_time)
- def test_decode_pydantic():
- raw_data = load_bytes()
- start_time = time.monotonic()
- FooModelList(**orjson.loads(raw_data))
- print('Pydantic + orjson, Decode: ', time.monotonic() - start_time)
- test_encode_pydantic()
- test_decode_pydantic()
Advertisement
Add Comment
Please, Sign In to add comment