diff --git a/lesson_7.py b/lesson_7.py new file mode 100644 index 0000000..2cf35a4 --- /dev/null +++ b/lesson_7.py @@ -0,0 +1,238 @@ +''' +class MyClass: + def __init__(self, param): + self.param = param + + def __add__(self, other): + return MyClass(self.param + other.param) + + def __str__(self): + return f"object with param ({self.param})" + + def __del__(self): + print(f"deleting object {self.param} of MyClass") + + +mc_1 = MyClass("concatenate ") +mc_2 = MyClass("text") +print(mc_1 + mc_2) + + + + +class MyClass: + def __setattr__(self, attr, value): + self.__dict__[attr] = value + + +mc = MyClass() +mc.width = 40 +mc.defence = 35 +print(mc.width) +print(mc.defence) + + + + +class Class1: + def __init__(self, param): + self.param = param + + def __call__(self, newparam): + self.param = newparam + + def __str__(self): + return str(self.param) + + +class Class2: + def __init__(self, *args): + self.my_list = [] + for el in args: + self.my_list.append(Class1(el)) + + def __getitem__(self, index): + return self.my_list[index] + + +my_obj = Class2(10, True, "text") +print(my_obj.my_list[1]) +print(my_obj[1]) + + + +class MyClass: + def __init__(self): + self.x = 40 + + def __eq__(self, y): + return self.x == y + + +mc = MyClass() +print(mc == 40) + + + +class Parent: + def __init__(self): + print("rawr parent") + + def my_method(self): + print("parent method") + + +class Child(Parent): + def __init__(self): + print("child") + super().__init__() + + def my_method(self): + print("child method") + super().my_method() + + +c = Child() +c.my_method() + + +class Iterator: + def __init__(self, start=0): + self.i = start - 1 + + def __iter__(self): + return self + + def __next__(self): + self.i += 1 + if self.i <= 5: + return self.i + else: + raise StopIteration + + +obj = Iterator(2) +for el in obj: + print(el) + + + +class Auto: + def __init__(self, year): + self.year = year + + @property + def year(self): + return self.__year + + @year.setter + def year(self, year): + if year < 2000: + self.__year = 2000 + elif year > 2019: + self.__year = 2019 + else: + self.__year = year + + def get_auto_year(self): + return f"auto in {str(self.year)}" + + +a = Auto(2090) +print(a.get_auto_year()) +''' + +#1 + + +class Matrix: + def __init__(self, user_matrix): + self.user_matrix = user_matrix + + def __str__(self): + return '\n'.join([' '.join([str(elem) for elem in line]) for line in self.user_matrix]) + + def __add__(self, other): + total = '' + if len(self.user_matrix) == len(other.user_matrix): + for line1, line2 in zip(self.user_matrix, other.user_matrix): + if len(line1) != len(line2): + return "please type correct size of matrix" + + matrix_line = [x + y for x, y in zip(line1, line2)] + total += ' '.join([str(i) for i in matrix_line]) + '\n' + else: + return "please type correct size of matrix" + return total + + +p_1 = Matrix([[1, 2], [3, 4], [5, 6]]) +p_2 = Matrix([[3, 4], [2, 1], [9, 2]]) +print(p_1 + p_2) + +#2 + +from abc import ABC, abstractmethod + + +class Clothes(ABC): + def __init__(self, parameter): + self.parameter = parameter + + @abstractmethod + def calculate(self): + pass + + +class Coat(Clothes): + + @property + def calculate(self): + return round((self.parameter / 6.5) + 0.5) + + +class Costume(Clothes): + + @property + def calculate(self): + return round((2 * self.parameter) + 0.3) + + +coat = Coat(30) +costume = Costume(120) +print(coat.calculate) +print(costume.calculate) + +#3 + + +class Cage: + def __init__(self, nums): + self.nums = nums + + def orders(self, rows): + return '\n'.join(['*' * rows for _ in range(self.nums // rows)]) + '\n' + '*' * (self.nums % rows) + + def __str__(self): + return str(self.nums) + + def __add__(self, other): + return str(self.nums + other.nums) + + def __sub__(self, other): + return self.nums - other.nums if self.nums - other.nums > 0 \ + else "too much dots on second cage or they're equal" + + def __mul__(self, other): + return str(self.nums * other.nums) + + def __truediv__(self, other): + return self.nums / other.nums if other.nums != 0 \ + else "you cannot divide on 0" + + +cage_1 = Cage(20) +cage_2 = Cage(40) +print(cage_1) +print(cage_1 + cage_2) +print(cage_1.orders(5)) diff --git a/lesson_8.py b/lesson_8.py new file mode 100644 index 0000000..7a3ccaa --- /dev/null +++ b/lesson_8.py @@ -0,0 +1,179 @@ +''' +p = "3-12-25" +a = p.split("-") +hours = int(a[0]) +minutes = int(a[1]) +seconds = int(a[2]) + +print(hours, minutes, seconds) +''' + +#1 + + +class Data: + def __init__(self, date_string): + self.date_string = str(date_string) + + @classmethod + def str_to_int(cls, date_string): + date = [] + for i in date_string.split(): + if i != '-': #создаем список, в который помещаем полученные после + date.append(i) #split значения и возвращаем их в виде int + return int(date[0]), int(date[1]), int(date[2]) + + @staticmethod + def validator(day, month, year): + if 1 <= day <= 31: + if 1 <= month <= 12: + if 9999 >= year >= 0: + return f"validation is succesfull" + else: + return "please type correct year" #через цикл if проверяем значения + else: + return "please type correct month" + else: + return "please type correct day of month" + + def __dir__(self): + return f"date is: {Data.str_to_int(self.date_string)}" + + +date_1 = Data("21 - 05 - 2019") +print(date_1.str_to_int("21 - 05 - 2019")) +print(Data.validator(20, 10, 2015)) + + +#2 + + +class ZeroDivError(Exception): + def __init__(self, arg_1, arg_2): + self.arg_1 = arg_1 + self.arg_2 = arg_2 + + @staticmethod + def division_on_zero(arg_1, arg_2): + try: + return arg_1 / arg_2 + except ZeroDivisionError: + return f"you can't divide by zero" + + +print(ZeroDivError.division_on_zero(10, 0)) + + +#3 + + +class Int_validator(Exception): + def __init__(self, text, *args): + self.txt = text + self.user_list = [] + + def list_validation(self): + while True: + try: + asker = int(input('Type digits and Enter then:')) + self.user_list.append(asker) + print(f'list now is {self.user_list}') + except: + print("only int enabled") + answer = input("try again? Yes or No") + if answer.title() == "Yes": + print(p.list_validation()) + else: + print(f'ok then. Your list is {self.user_list}') + break + + + +p = Int_validator(2) +p.list_validation() +p.user_list + + + +# 4,5,6 + + +class Warehouse: + + def __init__(self, name, quantity, working_speed, price): + self.name = name + self.quantity = quantity + self.working_speed = working_speed + self.price = price + self.stack = [] + self.full_stack = [] + self.stack_unit = {'name': self.name, 'quantity': self.quantity, 'speed': self.working_speed, 'price': self.price} + + def __str__(self): + return f'{self.name} price is {self.price}, quantity is {self.quantity}' + + def warehouse_stacker(self): + try: + unit = input('type name of tech: ') + unit_price = float(input('type price of tech: ')) + unit_quantity = int(input('type quantity: ')) + unit_speed = float(input('type speed of tech: ')) + unit_stack = {'name': unit, 'quantity': unit_quantity, 'speed': unit_speed, 'price': unit_price} + self.stack_unit.update(unit_stack) + self.stack.append(self.stack_unit) + print(f'now store is: \n {self.stack}') + except: + return 'data error!' + q = input('for exit type q, enter to continue') + if q.title() == "Q": + self.full_stack.append(self.stack) + print(f'this is warehouse: {self.full_stack}') + return 'exit' + else: + return Warehouse.warehouse_stacker(self) + + +class Printer(Warehouse): + def printing(self): + return "printing!" + + +class Scanner(Warehouse): + def scanning(self): + return "scanning!" + + +class Xerox(Warehouse): + def copying(self): + return "i'm actually copier. Ok. copying!" + + +r = Printer('hp', 2000, 5, 10) +p = Scanner('rawr', 2000, 3, 5) +g = Xerox('arthas', 300, 5, 10) +print(r.warehouse_stacker()) + + +#7 + +class Complex: + def __init__(self, a, b): + self.a = a + self.b = b + self.c = 'a + b * i' + + def __add__(self, other): + return f'sum of first number and second is: {self.a + other.a} + {self.b + other.b} * i' + + def __mul__(self, other): + return f'multiply is: {self.a * other.a - (self.b * other.b)} + {self.b * other.a} * i' + + def __str__(self): + return f'z = {self.a} + {self.b} * i' + + +first = Complex(1, -10) +second = Complex(3, 5) +print(first) +print(first + second) +print(first * second)