File: quickstart.rst

package info (click to toggle)
dataclass-wizard 0.35.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,808 kB
  • sloc: python: 15,276; makefile: 111; javascript: 23
file content (72 lines) | stat: -rw-r--r-- 2,449 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
==========
Quickstart
==========

Here are the supported features that Dataclass Wizard currently provides:

-  *JSON (de)serialization*: marshal dataclasses to/from JSON and Python
   ``dict`` objects.
-  *Field properties*: support for using properties with default
   values in dataclass instances.

The below is an quick demo of both of these features - how to marshal dataclasses to/from JSON and Python ``dict`` objects,
and declare and use field properties with default values.


.. code:: python3

    from dataclasses import dataclass, field
    from datetime import datetime
    from typing import Optional

    from dataclass_wizard import JSONSerializable, property_wizard


    @dataclass
    class MyClass(JSONSerializable, metaclass=property_wizard):

        my_str: Optional[str]
        list_of_int: list[int] = field(default_factory=list)
        # You can also define this as `my_dt`, however only the annotation
        # will carry over in that case, since the value is re-declared by
        # the property below. See also the 'Using Field Properties' section
        # in the docs for a more elegant approach.
        _my_dt: datetime = datetime(2000, 1, 1)

        @property
        def my_dt(self):
            """
            A sample `getter` which returns the datetime with year set as 2010
            """
            if self._my_dt is not None:
                return self._my_dt.replace(year=2010)
            return self._my_dt

        @my_dt.setter
        def my_dt(self, new_dt: datetime):
            """
            A sample `setter` which sets the inverse (roughly) of the `month` and `day`
            """
            self._my_dt = new_dt.replace(
                month=13 - new_dt.month,
                day=31 - new_dt.day)


    string = '''{"myStr": 42, "listOFInt": [1, "2", 3]}'''
    # Uses the default value for `my_dt`, with year=2000, month=1, day=1
    c = MyClass.from_json(string)

    print(repr(c))
    # prints:
    #   MyClass(my_str='42', list_of_int=[1, 2, 3], my_dt=datetime.datetime(2010, 12, 30, 0, 0))

    my_dict = {'My_Str': 'string', 'myDT': '2021-01-20T15:55:30Z'}
    c = MyClass.from_dict(my_dict)

    print(repr(c))
    # prints:
    #   MyClass(my_str='string', list_of_int=[], my_dt=datetime.datetime(2010, 12, 11, 15, 55, 30, tzinfo=datetime.timezone.utc))

    print(c.to_json())
    # prints:
    #   {"myStr": "string", "listOfInt": [], "myDt": "2010-12-11T15:55:30Z"}