|
| 1 | +import time |
| 2 | +from typing import Union |
| 3 | +from exacting import Exact |
| 4 | + |
| 5 | +start = time.perf_counter() |
| 6 | + |
| 7 | + |
| 8 | +class AnotherDataclass(Exact): |
| 9 | + id: int |
| 10 | + payload: str |
| 11 | + flag: bool |
| 12 | + checksum: bytes |
| 13 | + |
| 14 | + |
| 15 | +class Config(Exact): |
| 16 | + retries: int |
| 17 | + timeout: float |
| 18 | + use_ssl: bool |
| 19 | + endpoint: str |
| 20 | + |
| 21 | + |
| 22 | +class Metadata(Exact): |
| 23 | + tags: list[str] |
| 24 | + created_at: str |
| 25 | + version: Union[str, int] |
| 26 | + notes: Union[None, str, bytes] |
| 27 | + |
| 28 | + |
| 29 | +class NestedLevelThree(Exact): |
| 30 | + data: list[AnotherDataclass] |
| 31 | + metadata: Metadata |
| 32 | + value: Union[int, float, str, bool] |
| 33 | + binary: bytes |
| 34 | + |
| 35 | + |
| 36 | +class NestedLevelTwo(Exact): |
| 37 | + name: str |
| 38 | + configs: list[Config] |
| 39 | + children: list[NestedLevelThree] |
| 40 | + stats: dict[str, Union[int, float]] |
| 41 | + optional_info: Union[str, None, AnotherDataclass] |
| 42 | + |
| 43 | + |
| 44 | +class NestedLevelOne(Exact): |
| 45 | + key: str |
| 46 | + nested: NestedLevelTwo |
| 47 | + mixed_list: list[Union[str, int, AnotherDataclass]] |
| 48 | + fallback: Union[AnotherDataclass, bool] |
| 49 | + retry_limit: int |
| 50 | + |
| 51 | + |
| 52 | +class UltimateRoot(Exact): |
| 53 | + identifier: str |
| 54 | + deep: list[NestedLevelOne] |
| 55 | + extra: dict[str, list[AnotherDataclass]] |
| 56 | + settings: Config |
| 57 | + blob: Union[bytes, str, list[Union[str, bytes]]] |
| 58 | + flags: list[bool] |
| 59 | + |
| 60 | + |
| 61 | +test_data = UltimateRoot( |
| 62 | + identifier="root-001", |
| 63 | + deep=[ |
| 64 | + NestedLevelOne( |
| 65 | + key="level-one-a", |
| 66 | + nested=NestedLevelTwo( |
| 67 | + name="level-two-a", |
| 68 | + configs=[ |
| 69 | + Config( |
| 70 | + retries=3, |
| 71 | + timeout=1.5, |
| 72 | + use_ssl=True, |
| 73 | + endpoint="https://api.service", |
| 74 | + ), |
| 75 | + Config( |
| 76 | + retries=5, |
| 77 | + timeout=2.0, |
| 78 | + use_ssl=False, |
| 79 | + endpoint="http://backup.service", |
| 80 | + ), |
| 81 | + ], |
| 82 | + children=[ |
| 83 | + NestedLevelThree( |
| 84 | + data=[ |
| 85 | + AnotherDataclass( |
| 86 | + id=1, payload="data-1", flag=True, checksum=b"\x01\x02" |
| 87 | + ), |
| 88 | + AnotherDataclass( |
| 89 | + id=2, payload="data-2", flag=False, checksum=b"\x03\x04" |
| 90 | + ), |
| 91 | + ], |
| 92 | + metadata=Metadata( |
| 93 | + tags=["alpha", "beta"], |
| 94 | + created_at="2023-04-01T12:00:00Z", |
| 95 | + version="v1.0", |
| 96 | + notes=b"Some binary notes", |
| 97 | + ), |
| 98 | + value=True, |
| 99 | + binary=b"\x99\x88", |
| 100 | + ) |
| 101 | + ], |
| 102 | + stats={"requests": 1234, "load": 0.85}, |
| 103 | + optional_info=AnotherDataclass( |
| 104 | + id=99, payload="optional", flag=True, checksum=b"\xff" |
| 105 | + ), |
| 106 | + ), |
| 107 | + mixed_list=[ |
| 108 | + "string", |
| 109 | + 42, |
| 110 | + AnotherDataclass(id=77, payload="mixed", flag=False, checksum=b"\xab"), |
| 111 | + ], |
| 112 | + fallback=True, |
| 113 | + retry_limit=10, |
| 114 | + ) |
| 115 | + ], |
| 116 | + extra={ |
| 117 | + "groupA": [ |
| 118 | + AnotherDataclass(id=3, payload="x", flag=True, checksum=b"\xde"), |
| 119 | + AnotherDataclass(id=4, payload="y", flag=False, checksum=b"\xad"), |
| 120 | + ] |
| 121 | + }, |
| 122 | + settings=Config( |
| 123 | + retries=1, timeout=0.5, use_ssl=True, endpoint="https://fast.service" |
| 124 | + ), |
| 125 | + blob=[b"chunk1", b"chunk2", "fallback text"], |
| 126 | + flags=[True, False, True], |
| 127 | +) |
| 128 | + |
| 129 | +print((time.perf_counter() - start) * 1000, "ms") |
0 commit comments