forked from yukinarit/pyserde
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenum34.py
More file actions
49 lines (35 loc) · 904 Bytes
/
enum34.py
File metadata and controls
49 lines (35 loc) · 904 Bytes
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
import enum
from dataclasses import dataclass
import imported
from serde import serde
from serde.json import from_json, to_json
class Nested(enum.Enum):
S = 'foo'
class E(enum.Enum):
S = 'foo'
F = 10.0
B = True
N = Nested.S
class IE(enum.IntEnum):
V0 = enum.auto()
V1 = enum.auto()
V2 = enum.auto()
@serde
@dataclass
class Foo:
v0: IE
v1: IE = IE.V1 # Default enum value.
v2: E = E.S
v3: imported.ImportedEnum = imported.ImportedEnum.V3 # Use enum imported from other module.
v4: E = E.N # Use nested enum.
if __name__ == "__main__":
f = Foo(IE.V0)
s = to_json(f)
print(s)
f = from_json(Foo, s)
print(f)
s = to_json(f)
# You can also pass an enum-compabitle value (in this case True for E.B).
# Caveat: Foo takes any value IE accepts. e.g., Foo(True) is also valid.
s = to_json(Foo(3))
print(s)