TelenkovDmitry 8 luni în urmă
părinte
comite
3968d2acf9

+ 117 - 1
courses/python_oop/dunder_method/poly.py

@@ -2,6 +2,8 @@
 
 # Проблема 
 
+from functools import total_ordering
+
 class Rectangle:
     def __init__(self, a, b):
         self.a = a
@@ -29,9 +31,123 @@ class Circle:
         return 3.14*self.r**2
         
 
+class UnitedKingdom:
+
+    def __init__(self) -> None:
+        pass
+
+    @staticmethod
+    def get_capital():
+        print("London is the capital of Great Britain.")
+
+    @staticmethod
+    def get_language():
+        print("English is the primary language of Great Britain.")
+
+
+class Spain:
+
+    def __init__(self):
+        pass
 
+    @staticmethod
+    def get_capital():
+        print("Madrid is the capital of Spain.")
+
+    @staticmethod
+    def get_language():
+        print("Spanish is the primary language of Spain.")
+
+
+class DateUSA:
+
+    def __init__(self, day, month, year) -> None:
+        self.day = day
+        self.month = month
+        self.year = year
+
+    def format(self):
+        return f"{self.month:02}/{self.day:02}/{self.year:04}"
+    
+    def isoformat(self):
+        return f"{self.year:04}-{self.month:02}-{self.day:02}"
+
+class DateEurope:
+    
+    def __init__(self, day, month, year):
+        self.day = day
+        self.month = month
+        self.year = year
+
+    def format(self):
+        return f"{self.day:02}/{self.month:02}/{self.year:02}"
+    
+    def isoformat(self):
+        return f"{self.year:04}-{self.month:02}-{self.day:02}"
+
+
+@total_ordering
+class BankAccount:
+    def __init__(self, name, balance):
+        self.name = name
+        self.balance = balance
+
+    def __str__(self):
+        return self.name
+    
+    def __len__(self):
+        return len(str(self))
+
+    def __lt__(self, other):
+        if isinstance(other, BankAccount):
+            return self.balance < other.balance
+        elif isinstance(other, int):
+            return self.balance < other
+        else:
+            raise ValueError
+
+    def __add__(self, other):
+        if isinstance(other, BankAccount):
+            return self.balance + other.balance
+        elif isinstance(other, Numbers):
+            return self.balance + sum(other._values)
+        elif isinstance(other, (int, float)):
+            return self.balance + other
+        else:
+            raise ValueError
+
+    def __radd__(self, other):
+        return self.__add__(other)
+
+
+class Numbers:
+
+    def __init__(self, values: list):
+        self._values = values
+
+    def __add__(self, other):
+        if isinstance(other, Numbers):
+            return sum(other._values) + sum(other._values)
+        elif isinstance(other, BankAccount):
+            return sum(self._values) + other.balance
+        elif isinstance(other, (int, float)):
+            return sum(self._values) + other
+        else:
+            raise ValueError
+        
+    def __radd__(self, other):
+        return self.__add__(other)
+    
 
 def main():
+
+    lst = [4, BankAccount('Petr', 100), 5]
+    print(sum(lst))
+
+    # usa = DateUSA(1, 3, 2016)
+    # print(usa.format())
+
+    '''
     rect1 = Rectangle(3, 4)
     rect2 = Rectangle(12, 5)
 
@@ -41,7 +157,7 @@ def main():
     figures = [rect1, rect2, sq1, sq2]
     for figure in figures:
         print(figure.get_area())
-
+    '''
 
 
 if __name__ == '__main__':

+ 84 - 0
courses/python_oop/inheritance/inh_1.py

@@ -0,0 +1,84 @@
+# Наследование
+
+# issubclass(sub, par)
+
+class Vehicle:
+    
+    def __init__(self, name, max_speed, mileage):
+        self.name = name
+        self.max_speed = max_speed
+        self.mileage = mileage
+
+    def display_info(self):
+        print(f"Vehicle Name: {self.name}, Speed: {self.max_speed}, Mileage: {self.mileage}")
+
+
+class Bus(Vehicle):
+    pass
+
+
+class Person:
+    
+    def __init__(self, name):
+        self.name = name
+
+    def get_name(self):
+        return self.name
+    
+    def is_employee(self):
+        return False
+    
+
+class Employee(Person):
+
+    def is_employee(self):
+        return True
+    
+
+class Shape:
+    pass
+
+class Ellipse(Shape):
+    pass
+
+class Circle(Ellipse):
+    pass
+
+class Polygon(Shape):
+    pass
+
+class Triangle(Polygon):
+    pass
+
+class Rectangle(Polygon):
+    pass
+
+class Square(Rectangle):
+    pass
+
+
+def main():
+    shapes = [
+    Polygon(), Triangle(), Ellipse(), Polygon(), Triangle(), Ellipse(), Polygon(), Square(), Polygon(), Circle(),
+    Shape(), Polygon(), Triangle(), Circle(), Ellipse(), Shape(), Circle(), Rectangle(), Circle(), Circle(),
+    Square(), Square(), Circle(), Rectangle(), Rectangle(), Polygon(), Polygon(), Polygon(), Square(), Square(),
+    Rectangle(), Square(), Rectangle(), Polygon(), Circle(), Triangle(), Rectangle(), Shape(), Rectangle(),
+    Polygon(), Polygon(), Ellipse(), Square(), Circle(), Shape(), Polygon(), Ellipse(), Triangle(), Square(),
+    Polygon(), Triangle(), Circle(), Rectangle(), Rectangle(), Ellipse(), Triangle(), Rectangle(), Polygon(),
+    Shape(), Circle(), Rectangle(), Polygon(), Triangle(), Circle(), Polygon(), Rectangle(), Polygon(), Square(),
+    Triangle(), Circle(), Ellipse(), Circle(), Shape(), Circle(), Triangle(), Ellipse(), Square(), Circle(),
+    Triangle(), Polygon(), Square(), Polygon(), Circle(), Ellipse(), Polygon(), Shape(), Triangle(), Rectangle(),
+    Circle(), Square(), Triangle(), Triangle(), Ellipse(), Square(), Circle(), Rectangle(), Ellipse(), Shape(),
+    Triangle(), Ellipse(), Circle(), Shape(), Polygon(), Polygon(), Ellipse(), Rectangle(), Square(), Shape(),
+    Circle(), Triangle(), Circle(), Circle(), Circle(), Triangle(), Ellipse(), Polygon(), Circle(), Ellipse(),
+    Rectangle(), Circle(), Shape(), Polygon(), Polygon(), Triangle(), Rectangle(), Polygon(), Shape(), Circle(),
+    Shape(), Circle(), Triangle(), Ellipse(), Square(), Circle(), Triangle(), Ellipse(), Square(), Circle(),
+]
+
+    ret = [i for i in shapes if isinstance(i, (Polygon))]
+    print(len(ret))
+# 29 33 79
+
+if __name__ == "__main__":
+    main()
+