File: complex.py

package info (click to toggle)
dataclass-wizard 0.37.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,924 kB
  • sloc: python: 17,189; makefile: 126; javascript: 23
file content (328 lines) | stat: -rw-r--r-- 10,334 bytes parent folder | download
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import logging
from collections import defaultdict
from dataclasses import dataclass, field, asdict
from datetime import datetime
from timeit import timeit
from typing import Optional, TypeVar, Dict, Any, List, Union, NamedTuple, Tuple, Type

import dacite
import dataclass_factory
import marshmallow
import pytest
from dataclasses_json import DataClassJsonMixin, config
from jsons import JsonSerializable
from dacite import from_dict as dacite_from_dict
from pydantic import BaseModel
import attr
import mashumaro

from dataclass_wizard import JSONWizard, LoadMeta
from dataclass_wizard.class_helper import create_new_class
from dataclass_wizard.utils.string_conv import to_snake_case
from dataclass_wizard.utils.type_conv import as_datetime


log = logging.getLogger(__name__)

@dataclass
class MyClass:
    my_ledger: Dict[str, Any]
    the_answer_to_life: Optional[int]
    people: List['Person']
    is_enabled: bool = True


@dataclass
class MyClassDJ(DataClassJsonMixin):
    my_ledger: Dict[str, Any]
    the_answer_to_life: Optional[int]
    people: List['PersonDJ']
    is_enabled: bool = True


@dataclass
class MyClassDacite:
    my_ledger: Dict[str, Any]
    the_answer_to_life: Optional[int]
    people: List['PersonDJ']
    is_enabled: bool = True


# New Pydantic Models
class MyClassPydantic(BaseModel):
    my_ledger: Dict[str, Any]
    the_answer_to_life: Optional[int]
    people: List['PersonPydantic']
    is_enabled: bool = True


# New Pydantic Models
class PersonPydantic(BaseModel):
    name: 'NamePydantic'
    age: int
    birthdate: datetime
    gender: str
    occupation: Union[str, List[str]]
    hobbies: Dict[str, List[str]] = defaultdict(list)


class NamePydantic(BaseModel):
    first: str
    last: str
    salutation: Optional[str] = 'Mr.'


@dataclass
class Person:
    name: 'Name'
    age: int
    birthdate: datetime
    gender: str
    occupation: Union[str, List[str]]
    hobbies: Dict[str, List[str]] = field(
        default_factory=lambda: defaultdict(list))


class Name(NamedTuple):
    first: str
    last: str
    salutation: Optional[str] = 'Mr.'


@dataclass
class NameDataclass:
    first: str
    last: str
    salutation: Optional[str] = 'Mr.'


@dataclass
class PersonDJ:
    name: NameDataclass
    age: int
    birthdate: datetime = field(metadata=config(
        encoder=datetime.isoformat,
        decoder=as_datetime,
        mm_field=marshmallow.fields.DateTime(format='iso')
    ))
    gender: str
    occupation: Union[str, List[str]]
    hobbies: Dict[str, List[str]] = field(
        default_factory=lambda: defaultdict(list))


# Model for `dataclass-wizard`
WizType = TypeVar('WizType', MyClass, JSONWizard)
# Model for `jsons`
JsonsType = TypeVar('JsonsType', MyClass, JsonSerializable)
# Model for `dataclasses-json`
DJType = TypeVar('DJType', MyClass, DataClassJsonMixin)
# Model for `mashumaro`
MashumaroType = TypeVar('MashumaroType', MyClass, mashumaro.DataClassDictMixin)
# Factory for `dataclass-factory`
factory = dataclass_factory.Factory()

MyClassWizard: WizType = create_new_class(
    MyClass, (MyClass, JSONWizard), 'Wizard',
    attr_dict=vars(MyClass).copy())
# MyClassDJ: DJType = create_new_class(
#     MyClass, (MyClass, DataClassJsonMixin), 'DJ',
#     attr_dict=vars(MyClass).copy())
MyClassJsons: JsonsType = create_new_class(
    MyClass, (MyClass, JsonSerializable), 'Jsons',
    attr_dict=vars(MyClass).copy())
MyClassMashumaro: MashumaroType = create_new_class(
    MyClass, (MyClass, mashumaro.DataClassDictMixin), 'Mashumaro',
    attr_dict=vars(MyClass).copy())


# Enable experimental `v1` mode for optimized de/serialization
LoadMeta(v1=True).bind_to(MyClassWizard)


@pytest.fixture(scope='session')
def data():
    return {
        'my_ledger': {
            'Day 1': 'some details',
            'Day 17': ['a', 'sample', 'list']
        },
        'the_answer_to_life': '42',
        'people': [
            {
                'name': ('Roberto', 'Fuirron'),
                'age': 21,
                'birthdate': '1950-02-28T17:35:20Z',
                'gender': 'M',
                'occupation': ['sailor', 'fisher'],
                'hobbies': {'M-F': ('chess', '123', 'reading'), 'Sat-Sun': ['parasailing']}
            },
            {
                'name': ('Janice', 'Darr', 'Dr.'),
                'age': 45,
                'birthdate': '1971-11-05T05:10:59Z',
                'gender': 'F',
                'occupation': 'Dentist'
            }
        ]
    }


@pytest.fixture(scope='session')
def data_2(data):
    """data for `dataclasses-factory`, which has issue with tuple -> NamedTuple"""

    d = data.copy()
    d['people'] = [p.copy() for p in data['people']]

    # I want to make this into a Tuple - ('Roberto', 'Fuirron') -
    # but `dataclass-factory` doesn't seem to like that.

    d['people'][0]['name'] = {'first': 'Roberto', 'last': 'Fuirron'}
    d['people'][1]['name'] = {'first': 'Janice', 'last': 'Darr', 'salutation': 'Dr.'}

    return d

@pytest.fixture(scope='session')
def data_dacite(data_2):
    """data for `dacite`, which has a *TON* of issues."""

    # It's official, I hate this library ;-(
    d = data_2.copy()
    d['the_answer_to_life'] = int(d['the_answer_to_life'])
    d['people'][0]['hobbies'] = data_2['people'][0]['hobbies'].copy()
    d['people'][0]['hobbies']['M-F'] = list(d['people'][0]['hobbies']['M-F'])

    return d


def parse_iso_format(data):
    return as_datetime(data)

iso_format_schema = dataclass_factory.Schema(
    parser=parse_iso_format,
    serializer=datetime.isoformat
)

factory.schemas = {
    datetime: iso_format_schema
}

def parse_datetime(value: str) -> datetime:
    return datetime.fromisoformat(value.rstrip('Z'))  # Remove 'Z' if it's present

dacite_cfg = dacite.Config(
    type_hooks={datetime: parse_datetime})


def test_load(request, data, data_2, data_dacite, n):
    """
    [ RESULTS ON MAC OS X ]

    benchmarks.complex.complex - [INFO] dataclass-wizard     0.317641
    benchmarks.complex.complex - [INFO] dataclass-factory    0.751124
    benchmarks.complex.complex - [INFO] dacite               6.350958
    benchmarks.complex.complex - [INFO] mashumaro            0.343612
    benchmarks.complex.complex - [INFO] pydantic             0.538801
    benchmarks.complex.complex - [INFO] dataclasses-json     28.214992
    benchmarks.complex.complex - [INFO] jsons                31.735730
    benchmarks.complex.complex - [INFO] jsons (strict)       34.855084
    """
    g = globals().copy()
    g.update(locals())

    log.info('dataclass-wizard     %f',
             timeit('MyClassWizard.from_dict(data)', globals=g, number=n))

    log.info('dataclass-factory    %f',
             timeit('factory.load(data_2, MyClass)', globals=g, number=n))

    log.info('dacite               %f',
             timeit('dacite_from_dict(MyClassDacite, data_dacite, config=dacite_cfg)',
                    globals=g, number=n))

    log.info('mashumaro            %f',
             timeit('MyClassMashumaro.from_dict(data)', globals=g, number=n))

    log.info('pydantic             %f',
             timeit('MyClassPydantic(**data_2)', globals=g, number=n))

    # Assert the dataclass instances have the same values for all fields.
    c1 = MyClassWizard.from_dict(data)
    c2 = factory.load(data_2, MyClass)
    c3 = MyClassDJ.from_dict(data_2)
    c4 = MyClassJsons.load(data)
    c5 = MyClassMashumaro.from_dict(data)
    c6 = dacite_from_dict(MyClassDacite, data_dacite, config=dacite_cfg)
    c7 = MyClassPydantic(**data_2)

    # Since these models might differ slightly, we can skip exact equality checks
    # assert c1.__dict__ == c2.__dict__ == c3.__dict__ == c4.__dict__ == c5.__dict__

    if not request.config.getoption("--all"):
        pytest.skip("Skipping benchmarks for the rest by default, unless --all is specified.")

    log.info('dataclasses-json     %f',
             timeit('MyClassDJ.from_dict(data_2)', globals=g, number=n))

    log.info('jsons                %f',
             timeit('MyClassJsons.load(data)', globals=g, number=n))

    log.info('jsons (strict)       %f',
             timeit('MyClassJsons.load(data, strict=True)', globals=g, number=n))


def test_dump(request, data, data_2, data_dacite, n):
    """
    [ RESULTS ON MAC OS X ]

    benchmarks.complex.complex - [INFO] dataclass-wizard     0.405688
    benchmarks.complex.complex - [INFO] asdict (dataclasses) 1.727631
    benchmarks.complex.complex - [INFO] dataclass-factory    0.831178
    benchmarks.complex.complex - [INFO] dataclasses-json     11.072727
    benchmarks.complex.complex - [INFO] mashumaro            0.248298
    benchmarks.complex.complex - [INFO] pydantic             0.316203
    benchmarks.complex.complex - [INFO] jsons                37.361450
    benchmarks.complex.complex - [INFO] jsons (strict)       31.578708
    """
    c1 = MyClassWizard.from_dict(data)
    c2 = factory.load(data_2, MyClass)
    c3 = MyClassDJ.from_dict(data_2)
    c4 = MyClassJsons.load(data)
    c5 = MyClassMashumaro.from_dict(data)
    c6 = MyClassPydantic(**data_2)

    g = globals().copy()
    g.update(locals())

    log.info('dataclass-wizard     %f',
             timeit('c1.to_dict()', globals=g, number=n))

    log.info('asdict (dataclasses) %f',
             timeit('asdict(c1)', globals=g, number=n))

    log.info('dataclass-factory    %f',
             timeit('factory.dump(c2, MyClass)', globals=g, number=n))

    log.info('dataclasses-json     %f',
             timeit('c3.to_dict()', globals=g, number=n))

    log.info('mashumaro            %f',
             timeit('c5.to_dict()', globals=g, number=n))

    log.info('pydantic             %f',
             timeit('c6.model_dump()', globals=g, number=n))

    if not request.config.getoption("--all"):
        pytest.skip("Skipping benchmarks for the rest by default, unless --all is specified.")

    log.info('jsons                %f',
             timeit('c4.dump()', globals=g, number=n))

    log.info('jsons (strict)       %f',
             timeit('c4.dump(strict=True)', globals=g, number=n))

    # Assert the dict objects which are the result of `to_dict` are all equal.
    c1_dict = {to_snake_case(f): fval for f, fval in c1.to_dict().items()}

   #  assert c1_dict == factory.dump(c2, MyClass) == c3.to_dict() == c4.dump() == c5.to_dict()