File: nested.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 (323 lines) | stat: -rw-r--r-- 8,641 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
import logging
from dataclasses import dataclass, field, asdict
from datetime import date, datetime
from timeit import timeit
from typing import TypeVar, List, Union

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 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, as_date


log = logging.getLogger(__name__)

# Dataclass Definitions (Same as before, no changes needed)
@dataclass
class Data1:
    instance: 'Instance'
    result: 'Result'


@dataclass
class Instance:
    name: str
    data: 'Data2'


@dataclass
class Data2:
    date: date
    owner: str


@dataclass
class Result:
    status: str
    iteration_results: 'IterationResults'


@dataclass
class IterationResults:
    iterations: List['Iteration']


@dataclass
class Iteration:
    name: str
    data: 'Data3'


@dataclass
class Data3:
    question1: str
    question2: str


# New Model Class Definitions for Libraries

class MyClassPydantic(BaseModel):
    instance: 'InstancePydantic'
    result: 'ResultPydantic'


class InstancePydantic(BaseModel):
    name: str
    data: 'Data2Pydantic'


class Data2Pydantic(BaseModel):
    date: date
    owner: str


class ResultPydantic(BaseModel):
    status: str
    iteration_results: 'IterationResultsPydantic'


@dataclass
class IterationResultsPydantic:
    iterations: List['IterationPydantic']


class IterationPydantic(BaseModel):
    name: str
    data: 'Data3Pydantic'


class Data3Pydantic(BaseModel):
    question1: str
    question2: str


@dataclass
class MyClassMashumaro(mashumaro.DataClassDictMixin):
    instance: 'InstanceMashumaro'
    result: 'Result'


@dataclass
class InstanceMashumaro:
    name: str
    data: 'Data2Mashumaro'


@dataclass
class Data2Mashumaro:
    date: date
    owner: str


# Corrected Definition for `MyClassDJ`
@dataclass
class MyClassDJ(DataClassJsonMixin):
    instance: 'InstanceDJ'
    result: 'Result'


class InstanceDJ:
    name: str
    data: 'Data2DJ'


class Data2DJ:
    date: date
    owner: str


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

MyClassWizard: WizType = create_new_class(
    Data1, (Data1, JSONWizard), 'Wizard',
    attr_dict=vars(Data1).copy())
MyClassJsons: JsonsType = create_new_class(
    Data1, (Data1, JsonSerializable), 'Jsons',
    attr_dict=vars(Data1).copy())
MyClassMashumaroModel: MashumaroType = create_new_class(
    Data1, (Data1, mashumaro.DataClassDictMixin), 'Mashumaro',
    attr_dict=vars(Data1).copy())

# Pydantic Model for Benchmarking
MyClassPydanticModel = MyClassPydantic

# Mashumaro Model for Benchmarking
# MyClassMashumaroModel = MyClassMashumaro


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


@pytest.fixture(scope='session')
def data():
    return {
        "instance": {
            "name": "example1",
            "data": {
                "date": "2021-01-01",
                "owner": "Maciek"
            }
        },
        "result": {
            "status": "complete",
            "iteration_results": {
                "iterations": [
                    {
                        "name": "first",
                        "data": {
                            "question1": "yes",
                            "question2": "no"
                        }
                    }
                ]
            }
        }
    }


dt_iso_format_schema = dataclass_factory.Schema(
    parser=as_datetime,
    serializer=datetime.isoformat
)

date_iso_format_schema = dataclass_factory.Schema(
    parser=as_date,
    serializer=date.isoformat
)

factory.schemas = {
    datetime: dt_iso_format_schema,
    date: date_iso_format_schema
}


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

    benchmarks.nested.nested - [INFO] dataclass-wizard     0.130734
    benchmarks.nested.nested - [INFO] dataclass-factory    0.404371
    benchmarks.nested.nested - [INFO] dataclasses-json     11.315233
    benchmarks.nested.nested - [INFO] mashumaro            0.158986
    benchmarks.nested.nested - [INFO] pydantic             0.330295
    benchmarks.nested.nested - [INFO] jsons                25.084872
    benchmarks.nested.nested - [INFO] jsons (strict)       28.306646

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

    MyClassWizard.from_dict(data)

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

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

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

    # JUST SKKIPING IN INTERESTS OF TIME
    # log.info('dacite               %f',
    #          timeit('dacite_from_dict(MyClass, data)', globals=g, number=n))

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

    log.info('pydantic             %f',
             timeit('MyClassPydantic(**data)', 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('MyClassJsons.load(data)', globals=g, number=n))

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

    c1 = MyClassWizard.from_dict(data)
    c2 = factory.load(data, Data1)
    c3 = MyClassDJ.from_dict(data)
    c4 = MyClassJsons.load(data)
    c5 = MyClassMashumaro.from_dict(data)
    # c6 = dacite_from_dict(MyClass, data)
    c7 = MyClassPydantic(**data)

    assert c1.__dict__ == c2.__dict__ == c3.__dict__ == c4.__dict__ == c5.__dict__ == c7.__dict__ # == c6.__dict__


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

    INFO     benchmarks.nested:nested.py:258 dataclass-wizard     0.460812
    INFO     benchmarks.nested:nested.py:261 asdict (dataclasses) 0.674034
    INFO     benchmarks.nested:nested.py:264 dataclass-factory    0.233023
    INFO     benchmarks.nested:nested.py:267 dataclasses-json     5.717344
    INFO     benchmarks.nested:nested.py:270 mashumaro            0.086356
    INFO     benchmarks.nested:nested.py:273 pydantic             0.209953
    INFO     benchmarks.nested:nested.py:279 jsons                49.321013
    INFO     benchmarks.nested:nested.py:282 jsons (strict)       44.051063
    """
    c1 = MyClassWizard.from_dict(data)
    c2 = factory.load(data, Data1)
    c3 = MyClassDJ.from_dict(data)
    c4 = MyClassJsons.load(data)
    c5 = MyClassMashumaro.from_dict(data)
    c6 = MyClassPydantic(**data)

    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, Data1)', 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, Data1) == c3.to_dict() == c4.dump() == c5.to_dict()