dtelenkov 5 miesięcy temu
rodzic
commit
7ad2e64484

+ 60 - 0
courses/python_oop/monostate/mono_1.py

@@ -0,0 +1,60 @@
+'''
+Моносостояние - паттерн, который позволяет получить экземпляры
+клсса с одним общим набором атрибутов
+'''
+
+class SimpleCat:
+    def __init__(self, breed, color):
+        self.breed = breed
+        self.color = color
+
+def test_1():
+    cat1 = SimpleCat('pers', 'black')
+    cat2 = SimpleCat('siam', 'gray')
+    print(cat1.__dict__)  # {'breed': 'pers', 'color': 'black'}
+    print(cat2.__dict__)  # {'breed': 'siam', 'color': 'gray'}
+    print(id(cat1.__dict__), id(cat2.__dict__)) # разные ссылки на атрибут __dict__
+
+
+class Cat:
+    __shared_attr = {  # Формирование единого словаря атрибутов класса
+        'breed': 'pers',  # При изменении значений - эти значения обновятся
+        'color': 'black'  # во всех экземплярах класса
+    }
+
+    def __init__(self):
+        self.__dict__ = Cat.__shared_attr  # Подменяем в инициализации __dict__
+                                           # ссылкой на созданный моно-словарь
+
+def test_2():
+    cat1 = Cat()
+    cat2 = Cat()
+    print(cat1.__dict__)  # {'breed': 'pers', 'color': 'black'}
+    print(cat2.__dict__)  # {'breed': 'pers', 'color': 'black'}
+    print(id(cat1.__dict__), id(cat2.__dict__))  # одинаковые ссылки
+
+    cat1.breed, cat1.color = 'pers', 'white'  # Меняем атрибуты у cat1
+    print('cat1:', cat1.__dict__)  # {'breed': 'pers', 'color': 'white'}
+    print('cat2:', cat2.__dict__)  # {'breed': 'pers', 'color': 'white'}
+
+    cat2.breed, cat2.color = 'siam', 'gray'  # Меняем атрибуты у cat2
+    print('cat1:', cat1.__dict__)  # {'breed': 'siam', 'color': 'gray'}
+    print('cat2:', cat2.__dict__)  # {'breed': 'siam', 'color': 'gray'}
+
+'''
+Атрибут weight появится во всех экземплярах класса Cat
+'''
+def test_3():
+    cat1 = Cat()
+    cat2 = Cat()
+    cat1.weight = 5  # Добавляем параметр в ЭК
+    print('cat1:', cat1.__dict__)  # {'breed': 'pers', 'color': 'black', 'weight': 5}
+    print('cat2:', cat2.__dict__)  # {'breed': 'pers', 'color': 'black', 'weight': 5}
+
+
+def main():
+    # test_2()
+    test_3()
+
+if __name__ == '__main__':
+    main()

+ 54 - 0
courses/python_oop/namespace/namespace_1.py

@@ -0,0 +1,54 @@
+'''
+Пространство имен класса
+
+- встроенное пространство имен builtins
+- глобальное пространство имен global
+- объемлющие пространство имен enclosing
+- локальное пространство имен local
+
+'''
+
+class DepartmentIT:
+    PYTHON_DEV = 4
+    GO_DEV = 3
+    REACT_DEV = 2
+    language = 'python'
+
+    def __init__(self, name=None):
+        if name is not None:
+            self.language = name
+    
+    def get_info(self):
+        print(self.__class__.language)
+
+    @staticmethod
+    def make_backend():
+        print('Python and go')
+
+    @staticmethod
+    def make_frontend():
+        print('Python and go')
+
+def test_1():
+    it1 = DepartmentIT()
+    print(DepartmentIT)
+    print(it1)
+    print(dir(DepartmentIT))
+
+    # print(it1.__dict__)
+    # print(it1.__dir__())
+
+def test_2():
+    it1 = DepartmentIT('Go')
+    it2 = DepartmentIT('React')
+    it3 = DepartmentIT('Rust')
+    it1.get_info()
+    it2.get_info()
+    it3.get_info()
+
+def main():
+    # test_1()
+    test_2()
+
+if __name__ == '__main__':
+    main()

+ 111 - 0
courses/python_oop/property/property_1.py

@@ -0,0 +1,111 @@
+'''
+Вычисляемые свойства
+Если вам нужен атрибут, который будет вычислять 
+свое значение каждый раз динамически при доступе к нему, то property — это то, что вам нужно. Такие атрибуты обычно называются вычисляемыми атрибутами.
+'''
+
+import time
+
+class Square:
+    def __init__(self, s):
+        self.__side = s
+        self.__area = None
+
+    @property
+    def side(self):
+        return self.__side
+    
+    @side.setter
+    def side(self, value):
+        self.side = value
+        self.__area = None
+
+    @property
+    def area(self):
+        if self.__area is None:
+            print('Calculate area...')
+            time.sleep(0.5)
+            self.__area = self.side ** 2
+        return self.__area
+
+def test_1():
+    sq = Square(4)
+    print(sq.area)
+    print(sq.area)
+
+#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+class Colour:
+    def __init__(self, colour):
+        self.colour = colour
+
+    @property
+    def red(self):
+        return int(self.colour[1:3], base=16)
+
+    @property
+    def green(self):
+        return int(self.colour[3:5], base=16)
+
+    @property
+    def blue(self):
+        return int(self.colour[5:7], base=16)
+
+
+def test_2():
+    colour = Colour("#aacce4")
+    print(colour.red)
+    print(colour.green)
+    print(colour.blue)
+
+#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+
+class Ingredient:
+    def __init__(self, name, weight, price):
+        self.name = name
+        self.weight = weight
+        self.price = price  # стоимость за 100гр
+
+    @property
+    def cost(self):
+        return self.weight * self.price/100
+
+class Pizza:
+    def __init__(self, name, ingredients=None):
+        self.name = name
+        if ingredients is None:
+            self.ingredients = []
+        else:
+            self.ingredients = ingredients
+
+    @property
+    def cost(self):
+        s = 0
+        for x in self.ingredients:
+            s += x.cost
+        return s + 100
+
+def test_3():
+    chicken = Ingredient('chicken', 200, 80)
+    mozzarella = Ingredient('mozzarella', 300, 110)
+    sauce_bbq = Ingredient('sauce bbq', 150, 70)
+    red_onion = Ingredient('red onion', 150, 50)
+
+    print('Цена курицы', chicken.cost)
+    print('Цена моцарелы', mozzarella.cost)
+    print('Цена соуса', sauce_bbq.cost)
+    print('Цена красного лука', red_onion.cost)
+
+    barbecue = Pizza('BBQ', [chicken, mozzarella, sauce_bbq, red_onion])
+    print('Стоимость пиццы BBQ', barbecue.cost)
+
+
+def main():
+    # test_1()
+    # test_2()
+    test_3()
+
+
+if __name__ == '__main__':
+    main()

+ 90 - 0
courses/python_oop/static_class_method/method_1.py

@@ -0,0 +1,90 @@
+'''
+@classmethod
+@staticmethod
+
+classmethod - привязывает функцию к клсассу. Вызывается от класса.
+
+staticmethod - Вызывается от класса или экземпляра.
+'''
+
+class Example1:
+
+    @classmethod
+    def class_hello(cls):
+        print(f'class_hello {cls}')
+        
+
+class Example2:
+
+    @staticmethod
+    def static_hello():
+        print('static hello')
+
+
+class Date:
+    def __init__(self, day, month, year):
+        self.day = day
+        self.month = month
+        self.year = year
+    
+    @staticmethod
+    def from_str(data):
+        data = data.split('-')
+        return Date(int(data[0], base=10), int(data[1], base=10), 
+                    int(data[2], base=10))
+
+def test_1():
+    day_1 = Date(20, 9, 1997)
+    print(day_1.day)
+    print(day_1.month)
+    print(day_1.year)
+
+    day_2 = Date(1, 2, 2003)
+    print(day_2.day, day_2.month, day_2.year)
+
+
+def test_2():
+    day_1 = Date.from_str('12-4-2024')
+    day_2 = Date.from_str('06-09-2022')
+    print(day_1.day, day_1.month, day_1.year)
+    print(day_2.day, day_2.month, day_2.year)
+
+#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+class Pizza:
+    def __init__(self, ingredients=None):
+        if ingredients is None:
+            ingredients = []
+        self.ingredients = ingredients
+
+    @staticmethod
+    def margherita():
+        return Pizza(['mozzarella', 'tomatoes'])
+    
+    @staticmethod
+    def peperoni():
+        return Pizza(['mozzarella', 'peperoni', 'tomatoes'])
+    
+    @staticmethod
+    def barbecue():
+        return Pizza(['mozzarella', 'red onion', 'sauce bbq', 'chicken'])
+
+
+def test_3():
+    bbq = Pizza.barbecue()
+    peperoni = Pizza.peperoni()
+    margherita = Pizza.margherita()
+    print(sorted(bbq.ingredients))
+    print(sorted(peperoni.ingredients))
+    print(sorted(margherita.ingredients))    
+
+
+
+def main():
+    # test_1()
+    # test_2()
+    test_3()
+
+
+if __name__ == '__main__':
+    main()