-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathissue-1.py
50 lines (38 loc) · 1.28 KB
/
issue-1.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
import json
class Object:
def __init__(self, mapping):
for k, v in mapping.items():
if isinstance(v, dict):
self.__dict__[k] = Object(v)
else:
self.__dict__[k] = v
class ColorizeMixin:
ENDC = '\033[0m'
def __str__(self):
return f'\033[1;{self.repr_color_code};48m' + self.__repr__() + self.ENDC
class Advert(ColorizeMixin):
repr_color_code = 32 # green
def __init__(self, mapping):
self.price = 0
self.__dict__.update(Object(mapping).__dict__)
if 'title' not in self.__dict__.keys():
raise ValueError("Title is required parameter")
if self.price < 0:
raise ValueError("Price must be >= 0")
def __repr__(self):
return f'{self.title} | {self.price} ₽'
if __name__ == "__main__":
lesson_str = """{
"title": "python",
"price": 0,
"location": {
"address": "город Москва, Лесная, 7",
"metro_stations": ["Белорусская"]
}
}"""
lesson = json.loads(lesson_str)
lesson_ad = Advert(lesson)
print(lesson_ad.location.address)
print(lesson_ad.price)
iphone_ad = Advert({'title': 'iPhone X', 'price': 100})
print(iphone_ad)