-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopo.py
159 lines (127 loc) · 4.66 KB
/
popo.py
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
# This code parses date/times, so please
#
# pip install python-dateutil
#
# To use this code, make sure you
#
# import json
#
# and then, to convert JSON from a string, do
#
# result = welcome_from_dict(json.loads(json_string))
from typing import Any, List, TypeVar, Callable, Type, cast
from datetime import datetime
import dateutil.parser
T = TypeVar("T")
def from_str(x: Any) -> str:
# assert isinstance(x, str)
return x
def from_datetime(x: Any) -> datetime:
return dateutil.parser.parse(x)
def from_int(x: Any) -> int:
return x
def from_list(f: Callable[[Any], T], x: Any) -> List[T]:
# assert isinstance(x, list)
return [f(y) for y in x]
def to_class(c: Type[T], x: Any) -> dict:
# assert isinstance(x, c)
return cast(Any, x).to_dict()
class QA:
id: str
question: str
answer: str
def __init__(self, id: str, question: str, answer: str) -> None:
self.id = id
self.question = question
self.answer = answer
@staticmethod
def from_dict(obj: Any) -> 'QA':
# assert isinstance(obj, dict)
id = from_str(obj.get("_id"))
question = from_str(obj.get("question"))
answer = from_str(obj.get("answer"))
return QA(id, question, answer)
def to_dict(self) -> dict:
result: dict = {}
result["_id"] = from_str(self.id)
result["question"] = from_str(self.question)
result["answer"] = from_str(self.answer)
return result
class Review:
id: str
date_of_review: datetime
score: int
def __init__(self, id: str, date_of_review: datetime, score: int) -> None:
self.id = id
self.date_of_review = date_of_review
self.score = score
@staticmethod
def from_dict(obj: Any) -> 'Review':
# assert isinstance(obj, dict)
id = from_str(obj.get("_id"))
date_of_review = from_datetime(obj.get("dateOfReview"))
score = from_int(obj.get("score"))
return Review(id, date_of_review, score)
def to_dict(self) -> dict:
result: dict = {}
result["_id"] = from_str(self.id)
result["dateOfReview"] = self.date_of_review.isoformat()
result["score"] = from_int(self.score)
return result
class WelcomeElement:
threshold: int
id: str
qa: List[QA]
name: str
retainability: int
reviews: List[Review]
strength: int
priority: int
created_at: datetime
updated_at: datetime
v: int
def __init__(self, threshold: int, id: str, qa: List[QA], name: str, retainability: int, reviews: List[Review], strength: int, priority: int, created_at: datetime, updated_at: datetime, v: int) -> None:
self.threshold = threshold
self.id = id
self.qa = qa
self.name = name
self.retainability = retainability
self.reviews = reviews
self.strength = strength
self.priority = priority
self.created_at = created_at
self.updated_at = updated_at
self.v = v
@staticmethod
def from_dict(obj: Any) -> 'WelcomeElement':
# assert isinstance(obj, dict)
threshold = from_int(obj.get("threshold"))
id = from_str(obj.get("_id"))
qa = from_list(QA.from_dict, obj.get("qa"))
name = from_str(obj.get("name"))
retainability = from_int(obj.get("retainability"))
reviews = from_list(Review.from_dict, obj.get("reviews"))
strength = from_int(obj.get("strength"))
priority = int(from_str(obj.get("priority")))
created_at = from_datetime(obj.get("createdAt"))
updated_at = from_datetime(obj.get("updatedAt"))
v = from_int(obj.get("__v"))
return WelcomeElement(threshold, id, qa, name, retainability, reviews, strength, priority, created_at, updated_at, v)
def to_dict(self) -> dict:
result: dict = {}
result["threshold"] = from_int(self.threshold)
result["_id"] = from_str(self.id)
result["qa"] = from_list(lambda x: to_class(QA, x), self.qa)
result["name"] = from_str(self.name)
result["retainability"] = from_int(self.retainability)
result["reviews"] = from_list(lambda x: to_class(Review, x), self.reviews)
result["strength"] = from_int(self.strength)
result["priority"] = from_str(str(self.priority))
result["createdAt"] = self.created_at.isoformat()
result["updatedAt"] = self.updated_at.isoformat()
result["__v"] = from_int(self.v)
return result
def welcome_from_dict(s: Any) -> List[WelcomeElement]:
return from_list(WelcomeElement.from_dict, s)
def welcome_to_dict(x: List[WelcomeElement]) -> Any:
return from_list(lambda x: to_class(WelcomeElement, x), x)