/
elder247
/
aaa-python-advanced
Обзор
Документация
Войти
/
elder247
/
aaa-python-advanced
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
hw_1/task_1.py
59 строк
2 KB
Anton Trofimuk
add task_1.py and task_1_test.py
18 ноя 2024, 20:28
18 ноя 2024, 20:28
f24855a
Код
Авторство
О чём код?
from keyword import iskeyword class ColorizeMixin: @staticmethod def get_color_code(repr_color_code): return f"\033[1;{repr_color_code};40m" class DotDictMixin: def __init__(self, json_obj: dict): for key, value in json_obj.items(): if iskeyword(key): # rename keyword key = key + '_' if isinstance(value, dict): # recursion transform dict to object with dot notation value = DotDictMixin(value) setattr(self, key, value) class Advert(ColorizeMixin, DotDictMixin): repr_color_code = 33 # yellow def __init__(self, json_obj: dict): self.price = 0 super().__init__(json_obj) if not hasattr(self, 'title'): raise ValueError('json_obj mush have a "title" attribute') @property def price(self): return self._price @price.setter def price(self, value): if not (isinstance(value, int) or isinstance(value, float)): raise TypeError('price must be int or float') elif value < 0: raise ValueError('price must be >= 0') self._price = value def __repr__(self): color_code = super().get_color_code(self.repr_color_code) return f'{color_code} {self.title} | {self.price} ₽' if __name__ == "__main__": dog_advert = Advert({ 'title': 'Вельш-корги', 'price': 1000, 'class': 'dogs', 'location': { 'address': '"город Москва, Лесная, 7' } }) print(dog_advert)