From 2d78aa8df2f5019d13f51bce302f8a8736def56a Mon Sep 17 00:00:00 2001 From: "Serebryakov.AO" Date: Fri, 30 Apr 2021 17:59:26 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A3=D1=80=D0=BE=D0=BA=208?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- orgsklad.py | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++ task1.py | 32 +++++++++++++++++++++++ task2.py | 16 ++++++++++++ task3.py | 26 +++++++++++++++++++ task7.py | 26 +++++++++++++++++++ 5 files changed, 173 insertions(+) create mode 100644 orgsklad.py create mode 100644 task1.py create mode 100644 task2.py create mode 100644 task3.py create mode 100644 task7.py diff --git a/orgsklad.py b/orgsklad.py new file mode 100644 index 0000000..76ca627 --- /dev/null +++ b/orgsklad.py @@ -0,0 +1,73 @@ +# Склад оргтехники +class Sklad: + contain = {} + last_id = 0 + data = [] + + @staticmethod + def dep_validate(depart): + try: + return bool(str(depart)) + except ValueError: + return False + + @staticmethod + def org_validate(org_technic): + try: + return isinstance(org_technic, Org) + except ValueError: + return False + + @classmethod + def add_tehnic(cls, org_technic, depart): + if cls.org_validate(org_technic) and cls.dep_validate(depart): + print(org_technic.type) + now_count = cls.contain.get(org_technic.type) + if not now_count: + now_count = 0 + cls.contain[org_technic.type] = now_count + 1 + new_row = org_technic.__dict__ + new_row['id'] = cls.last_id = cls.last_id + 1 + new_row['department'] = depart + + cls.data.append(new_row) + + +class Org: + def __init__(self, power, color, model): + self.type = self.__class__.__name__ + self.power = power + self.color = color + self.model = model + + def __str__(self): + dictinary = self.__dict__ + dictinary['class'] = self.__class__.__name__ + return str(self.__dict__) + # printable_list[param] = self[param] + + +class Printer(Org): + def __init__(self, *args, speed): + Org.__init__(self, *args) + self.print_speed = speed + + +class Scanner(Org): + def __init__(self, *args, speed): + Org.__init__(self, *args) + self.scan_speed = speed + + +class Xerox(Org): + def __init__(self, *args, speed): + Org.__init__(self, *args) + self.copy_speed = speed + + +org1 = Printer(100, 'green', 'hp-100', speed=100) +print(org1) +Sklad.add_tehnic(org1, 'otdel1') +Sklad.add_tehnic(org1, 'otdel2') +print(Sklad.contain) +print(Sklad.data) diff --git a/task1.py b/task1.py new file mode 100644 index 0000000..18b6879 --- /dev/null +++ b/task1.py @@ -0,0 +1,32 @@ +from datetime import datetime + +class Date: + + def __init__(self, date): + if not Date.validate(date): + print("Неверный формат даты, должен быть 'день-месяц-год'") + exit(0) + self.date = date + print('объект создан') + + @classmethod + def to_int(cls, date): + if cls.validate(date): + return list(map(int, date.split('-'))) + else: + print("Неверный формат даты, должен быть 'день-месяц-год'") + exit(0) + + @staticmethod + def validate(date): + try: + return bool(datetime.strptime(date, '%d-%m-%Y')) + except ValueError: + return False + + +# new_day = Date('31-05-2021') +day, month, year = Date.to_int('32-05-2021') +print(day, type(day)) +print(month, type(month)) +print(year, type(year)) diff --git a/task2.py b/task2.py new file mode 100644 index 0000000..ca0da5f --- /dev/null +++ b/task2.py @@ -0,0 +1,16 @@ +class OwnZeroDivisionError(ZeroDivisionError): + def __init__(self, txt): + self.txt = txt + +try: + num1 = int(input('Делимое: ')) + num2 = int(input('Делитель: ')) + if num2 == 0: + raise OwnZeroDivisionError("Беконечно большое число!") + res = num1 / num2 +except OwnZeroDivisionError as err: + print(err) +else: + print(f"Все хорошо. Результат - {res}") +finally: + print("Программа завершена") diff --git a/task3.py b/task3.py new file mode 100644 index 0000000..d6e01bf --- /dev/null +++ b/task3.py @@ -0,0 +1,26 @@ +import re + +class NotIntError(ValueError): + def __init__(self, txt): + self.txt = txt + + +my_list = [] + +while True: + num = input('Введите число ("stop" - для выхода): ') + if num == 'stop': + break + if not num: + print('0') + num = '0' + try: + num_match = re.match(r'(\d*)', num).group() + if num_match != num: + raise NotIntError(f"'{num}' - не число.'") + continue + my_list.append(int(num)) + except NotIntError as err: + print(err) + +print(my_list) diff --git a/task7.py b/task7.py new file mode 100644 index 0000000..3a8b3f8 --- /dev/null +++ b/task7.py @@ -0,0 +1,26 @@ +class ComplexNum: + + def __init__(self, a, b, *args): + self.a = a + self.b = b + self.c = 'a + b * i' + + def __add__(self, other): + print(f'c1 + c2 = ', end='') + return f' {self.a + other.a} + {self.b + other.b} * i' + + def __mul__(self, other): + print(f'c1 * c2 = ', end='') + return f' {self.a * other.a - (self.b * other.b)} + {self.b * other.a} * i' + + def __str__(self): + return f'c = {self.a} + {self.b} * i' + + +c_1 = ComplexNum(1, -2) +c_2 = ComplexNum(3, 4) + +print(c_1) +print(c_2) +print(c_1 + c_2) +print(c_1 * c_2)