forked from yukinarit/pyserde
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathskip.py
More file actions
52 lines (38 loc) · 1.18 KB
/
skip.py
File metadata and controls
52 lines (38 loc) · 1.18 KB
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
"""
skip.py
Example usage of skip and skip_if attributes.
Usage:
$ poetry install
$ poetry run python skip.py
"""
from dataclasses import dataclass
from typing import Dict, List
from serde import field, serde
from serde.json import to_json
@serde
@dataclass
class Resource:
name: str
hash: str
metadata: Dict[str, str] = field(default_factory=dict, skip=True)
@serde
@dataclass
class World:
player: str
enemies: List[str] = field(default_factory=list, skip_if_false=True)
buddy: str = field(default='', skip_if=lambda v: v == 'Pikachu')
town: str = field(default='Masara Town', skip_if_default=True)
def main():
resources = [
Resource("Stack Overflow", "b6469c3f31653d281bbbfa6f94d60fea130abe38"),
Resource("GitHub", "5cb7a0c47e53854cd00e1a968de5abce1c124601", metadata={"headquarters": "San Francisco"}),
]
print(to_json(resources))
# "buddy" and "town" field will be omitted
world = World('satoshi', ['Rattata', 'Pidgey'], 'Pikachu')
print(to_json(world))
# "enemies" field will be omitted
world = World('green', [], 'Charmander', 'Black City')
print(to_json(world))
if __name__ == '__main__':
main()