diff --git a/hometask8/task1.py b/hometask8/task1.py new file mode 100644 index 0000000..6c85380 --- /dev/null +++ b/hometask8/task1.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python +# nevl 2021 + +class Date: + + def __init__(self, date): + self.date = date + + @classmethod + def get_date_int(cls, date): + if not Date.is_date(date): + return None + + s = date.split('-') + if len(s) != 3: + return None + + try: + return int(f'{s[0]}{s[1]}{s[2]}') + except: + return None + + @staticmethod + def is_date(date_str): + date_data = ([str(c) for c in range(10)]) + date_data.append('-') + + # check symbols + for c in date_str: + if c not in date_data: + return False + + # check format + s = date_str.split('-') + if len(s) != 3: + return False + + for i, it in enumerate([2, 2, 4]): + if len(s[i]) != it: + return False + + # default + return True + + +if __name__ == '__main__': + date1 = '01-01-1970' + date2 = '2020-02-02' + + for date in [date1, date2]: + print(f'Valid date {date}: {Date.get_date_int(date)}' if Date.is_date(date) else f'Invalid date {date}') diff --git a/hometask8/task2.py b/hometask8/task2.py new file mode 100644 index 0000000..87c615c --- /dev/null +++ b/hometask8/task2.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python +# nevl 2021 + +class NewException(Exception): + def __init__(self, txt): + self.txt = txt + + +if __name__ == '__main__': + while True: + n1 = input('Enter first number or press ENTER to complete input: ') + n2 = input('Enter second number or press ENTER to complete input: ') + if not n1 or not n2: + break + try: + n1 = float(n1) + n2 = float(n2) + except Exception as e: + print(e) + continue + + try: + if n2 == 0: + raise NewException('Division by zero') + print(f'Division result {n1 / n2}') + except NewException as e: + print(e) + except Exception as e: + print(e) diff --git a/hometask8/task3.py b/hometask8/task3.py new file mode 100644 index 0000000..ec68bfc --- /dev/null +++ b/hometask8/task3.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python +# nevl 2021 + +class NotNumberException(Exception): + def __init__(self, txt): + self.txt = txt + + +def is_number(value): + data = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '.', '-'] + for s in value: + if not s in data: + raise NotNumberException('Entering value isn\'t number') + if value.find('-') > 0 or value.count('.') > 1: + raise NotNumberException('Entering value isn\'t number') + + return value + + +if __name__ == '__main__': + l = list() + + while True: + t = input('Enter number/string or press ENTER to complete input: ') + if not t: + break + try: + l.append(is_number(t)) + except NotNumberException as e: + print(e) + + print(l) diff --git a/hometask8/task4-6.py b/hometask8/task4-6.py new file mode 100644 index 0000000..74bf815 --- /dev/null +++ b/hometask8/task4-6.py @@ -0,0 +1,127 @@ +#!/usr/bin/env python +# nevl 2021 + +class Warehouse: + + def __init__(self, warehouse_data: dict): + # warehouse_data: type: count + self.data = warehouse_data + self.in_department = dict() + + def put(self, type_of_equipment, count, department): + try: + count = int(count) + except: + return False + + if f'{type_of_equipment}-{department}' not in self.in_department: + self.in_department[f'{type_of_equipment}-{department}'] = 0 + elif count > self.in_department[f'{type_of_equipment}-{department}']: + return False + else: + self.in_department[f'{type_of_equipment}-{department}'] -= count + + if type_of_equipment not in self.data: + self.data[type_of_equipment] = count + else: + self.data[type_of_equipment] += count + + return True + + def get(self, type_of_equipment, count, department): + try: + count = int(count) + except: + return False + + if type_of_equipment not in self.data: + return False + if count > self.data[type_of_equipment]: + return False + self.data[type_of_equipment] -= count + if f'{type_of_equipment}-{department}' not in self.in_department: + self.in_department[f'{type_of_equipment}-{department}'] = count + else: + self.in_department[f'{type_of_equipment}-{department}'] += count + + return True + + def get_count(self, type_of_equipment, department=''): + """ + Return count of equipment in department or in warehouse if department not set + :param type_of_equipment: string + :param department: string + :return: number + """ + if not department: + return self.data.get(type_of_equipment, 0) + else: + return self.in_department.get(f'{type_of_equipment}-{department}', 0) + + +class OfficeEquipment: + + def __init__(self, cost, size: tuple): + self.cost = cost + self.size = size + + +class Printer(OfficeEquipment): + + def __init__(self, cost, size: tuple, printer='hp'): + super().__init__(cost, size) + self.printer = printer + + +class Scanner(OfficeEquipment): + + def __init__(self, cost, size: tuple, scanner='hp'): + super().__init__(cost, size) + self.scanner = scanner + + +class Copier(OfficeEquipment): + + def __init__(self, cost, size: tuple, copier='xerox'): + super().__init__(cost, size) + self.copier = copier + + +if __name__ == '__main__': + p = Printer(1000, (20, 30, 50), 'Samsung') + s = Scanner(250, (10, 50, 40), 'Canon') + c = Copier(2500, (100, 60, 70), 'HP') + + warehouse = Warehouse({p.__hash__(): 20, + s.__hash__(): 20, + c.__hash__(): 30}) + + for dep in ['', 'dep1', 'dep2']: + print('There are {} prints in the {}'.format(warehouse.get_count(p.__hash__(), dep), + 'warehouse' if not dep else dep)) + + if not warehouse.get(p.__hash__(), 10, 'dep1'): + print('1. Something went wrong while get printer from warehouse in dep1') + if not warehouse.get(p.__hash__(), 'ten', 'dep2'): + print('2. Something went wrong while get printer from warehouse in dep2') + if not warehouse.get(p.__hash__(), 11, 'dep2'): + print('3. Something went wrong while get printer from warehouse in dep2') + if not warehouse.get(p.__hash__(), 9, 'dep2'): + print('4. Something went wrong while get printer from warehouse in dep2') + + for dep in ['', 'dep1', 'dep2']: + print('There are {} prints in the {}'.format(warehouse.get_count(p.__hash__(), dep), + 'warehouse' if not dep else dep)) + + if not warehouse.put(p.__hash__(), 10, 'dep2'): + print('5. Something went wrong while put printer to warehouse from dep2') + if not warehouse.put(p.__hash__(), 9, 'dep2'): + print('6. Something went wrong while put printer to warehouse from dep2') + if not warehouse.put(p.__hash__(), 9, 'dep1'): + print('7. Something went wrong while put printer to warehouse from dep2') + if not warehouse.put(p.__hash__(), 9, 'dep1'): + print('8. Something went wrong while put printer to warehouse from dep2') + + for dep in ['', 'dep1', 'dep2']: + print('There are {} prints in the {}'.format(warehouse.get_count(p.__hash__(), dep), + 'warehouse' if not dep else dep)) diff --git a/hometask8/task7.py b/hometask8/task7.py new file mode 100644 index 0000000..8a2a667 --- /dev/null +++ b/hometask8/task7.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python +# nevl 2021 + +class MyComplex: + + def __init__(self, a, b): + self.a = a + self.b = b + + def __add__(self, other): + return MyComplex(self.a + other.a, self.b + other.b) + + def __mul__(self, other): + return MyComplex(self.a * other.a - self.b * other.b, self.a * other.b + other.a * self.b) + + def __str__(self): + return '({}{}{}j)'.format(self.a, '+' if self.b >= 0 else '', self.b) + + +if __name__ == '__main__': + c1 = MyComplex(2, 3) + c2 = MyComplex(-1, 0) + c3 = c1 + c2 + c4 = c1 * c2 + + for it in [c1, c2, c3, c4]: + print(it)