|
1 | | -from typing import Any, Optional, List, TypeVar, Callable, Type, cast |
2 | | - |
3 | | - |
4 | | -T = TypeVar("T") |
5 | | - |
6 | | - |
7 | | -def from_float(x: Any) -> float: |
8 | | - assert isinstance(x, (float, int)) and not isinstance(x, bool) |
9 | | - return float(x) |
10 | | - |
11 | | - |
12 | | -def to_float(x: Any) -> float: |
13 | | - assert isinstance(x, (int, float)) |
14 | | - return x |
15 | | - |
16 | | - |
17 | | -def from_str(x: Any) -> str: |
18 | | - assert isinstance(x, str) |
19 | | - return x |
20 | | - |
21 | | - |
22 | | -def from_none(x: Any) -> Any: |
23 | | - assert x is None |
24 | | - return x |
25 | | - |
26 | | - |
27 | | -def from_union(fs, x): |
28 | | - for f in fs: |
29 | | - try: |
30 | | - return f(x) |
31 | | - except: |
32 | | - pass |
33 | | - assert False |
34 | | - |
35 | | - |
36 | | -def from_list(f: Callable[[Any], T], x: Any) -> List[T]: |
37 | | - assert isinstance(x, list) |
38 | | - return [f(y) for y in x] |
39 | | - |
40 | | - |
41 | | -def to_class(c: Type[T], x: Any) -> dict: |
42 | | - assert isinstance(x, c) |
43 | | - return cast(Any, x).to_dict() |
| 1 | +from dataclasses import dataclass |
| 2 | +from typing import List, Optional |
44 | 3 |
|
45 | 4 |
|
| 5 | +@dataclass |
46 | 6 | class Point: |
47 | 7 | x: float |
48 | 8 | y: float |
49 | 9 |
|
50 | | - def __init__(self, x: float, y: float) -> None: |
51 | | - self.x = x |
52 | | - self.y = y |
53 | | - |
54 | | - @staticmethod |
55 | | - def from_dict(obj: Any) -> "Point": |
56 | | - assert isinstance(obj, dict) |
57 | | - x = from_float(obj.get("x")) |
58 | | - y = from_float(obj.get("y")) |
59 | | - return Point(x, y) |
60 | | - |
61 | | - def to_dict(self) -> dict: |
62 | | - result: dict = {} |
63 | | - result["x"] = to_float(self.x) |
64 | | - result["y"] = to_float(self.y) |
65 | | - return result |
66 | | - |
67 | 10 |
|
| 11 | +@dataclass |
68 | 12 | class CreateAoi: |
69 | | - id: Optional[str] |
70 | 13 | name: str |
71 | 14 | """Name of the AOI""" |
72 | 15 |
|
73 | 16 | points: List[Point] |
74 | 17 | z: float |
75 | | - |
76 | | - def __init__( |
77 | | - self, id: Optional[str], name: str, points: List[Point], z: float |
78 | | - ) -> None: |
79 | | - self.id = id |
80 | | - self.name = name |
81 | | - self.points = points |
82 | | - self.z = z |
83 | | - |
84 | | - @staticmethod |
85 | | - def from_dict(obj: Any) -> "CreateAoi": |
86 | | - assert isinstance(obj, dict) |
87 | | - id = from_union([from_str, from_none], obj.get("id")) |
88 | | - name = from_str(obj.get("name")) |
89 | | - points = from_list(Point.from_dict, obj.get("points")) |
90 | | - z = from_float(obj.get("z")) |
91 | | - return CreateAoi(id, name, points, z) |
92 | | - |
93 | | - def to_dict(self) -> dict: |
94 | | - result: dict = {} |
95 | | - if self.id is not None: |
96 | | - result["id"] = from_union([from_str, from_none], self.id) |
97 | | - result["name"] = from_str(self.name) |
98 | | - result["points"] = from_list(lambda x: to_class(Point, x), self.points) |
99 | | - result["z"] = to_float(self.z) |
100 | | - return result |
101 | | - |
102 | | - |
103 | | -def create_aoi_from_dict(s: Any) -> CreateAoi: |
104 | | - return CreateAoi.from_dict(s) |
105 | | - |
106 | | - |
107 | | -def create_aoi_to_dict(x: CreateAoi) -> Any: |
108 | | - return to_class(CreateAoi, x) |
| 18 | + id: Optional[str] = None |
0 commit comments