TelenkovDmitry преди 8 месеца
родител
ревизия
37d527ed20
променени са 2 файла, в които са добавени 250 реда и са изтрити 0 реда
  1. 52 0
      courses/python_oop/method_practic.py
  2. 198 0
      courses/python_oop/namespace.py

+ 52 - 0
courses/python_oop/method_practic.py

@@ -0,0 +1,52 @@
+
+# Практика по property
+
+from string import digits
+
+class User:
+
+    def __init__(self, login, password) -> None:
+        self.login = login
+        self.password = password
+        self.__secret = 'abracadabra'
+        
+    @property
+    def secret(self):
+        s = input("Введите ваш пароль: ")
+        if s == self.password:
+            return self.__secret
+        else:
+            raise ValueError("Value error")
+
+    @property
+    def password(self):
+        print('getter called')
+        return self.__password
+
+    @staticmethod
+    def is_include_number(password):
+        for digit in digits:
+            if digit in password:
+                return True
+        return False
+
+    @password.setter
+    def password(self, value):
+        print('setter called')
+        if not isinstance(value, str):
+            raise TypeError("Пароль должен быть строкой")
+        if len(value) < 4:
+            raise ValueError("Длина пароля должна быть не менее 4-ех символов")
+        if len(value) > 12:
+            raise ValueError("Длина пароля должна быть не более 12-и символов")
+        if not User.is_include_number(value):
+            raise ValueError("Пароль должен содержать хотя бы одну цифру")
+        self.__password = value
+
+
+def main():
+    user1 = User('Ivan', '123df')
+
+
+if __name__ == '__main__':
+    main()

+ 198 - 0
courses/python_oop/namespace.py

@@ -0,0 +1,198 @@
+
+class DepartmentIT:
+    PYTHON_DEV = 3
+    GO_DEV = 3
+    REACT_DEV = 2
+
+    def info(self):
+        print(self.PYTHON_DEV, self.GO_DEV, self.REACT_DEV)
+
+    @property
+    def info_prop(self):
+        print(self.PYTHON_DEV, self.GO_DEV, self.REACT_DEV)
+
+
+class MyClass:
+    class_attribute = 'I am a class attribute'
+
+    def __init__(self) -> None:
+        self.instance_attribute = 'I am an instatnce attribute'
+
+    @classmethod
+    def create_attr(cls, attr_name, attr_value):
+        setattr(cls, attr_name, attr_value)
+
+
+class Container:
+    items = []
+
+    def add_item(self, value):
+        # self.items.append(value)
+        self.items = [value] + self.items
+
+
+class Robot:
+
+    population = 0
+    my_list = []
+
+    def __init__(self, name) -> None:
+        # Robot.population += 1
+        self.population += 1
+        self.name = name
+        self.my_list.append(name)
+        print(f'Робот {self.name} был создан')
+
+    def destroy(self):
+        print(f'Робот {self.name} был уничтожен')
+        # Robot.population -= 1
+        self.population -= 1
+
+    def say_hello(self):
+        print(f'Робот {self.name} приветствует тебя, особь человеческого рода')
+
+    def how_many_new(self):
+        print(f'{self.population}, вот сколько нас еще осталось')
+
+    @classmethod
+    def how_many(cls):
+        # print(f'{cls.population}, вот сколько нас еще осталось')
+        print(f'{cls.population}, вот сколько нас еще осталось')
+        
+
+class User:
+
+    def __init__(self, name, role) -> None:
+        self.name = name
+        self.role = role
+
+
+class Access:
+
+    __access_list = ['admin', 'developer']
+
+    def __init__(self) -> None:
+        pass
+
+    @staticmethod
+    def __check_access(role):
+        return True if role in Access.__access_list else False
+
+    @staticmethod
+    def get_access(user: User):
+        if not isinstance(user, User):
+            print('AccessTypeError')
+            return
+        if Access.__check_access(user.role):
+            print(f'User {user.name}: success')
+        else:
+            print(f'AccessDenied')
+
+
+class BankAccount:
+
+    bank_name = 'Tinkoff Bank'
+    address = 'Москва, ул. 2-я Хуторская, д. 38А'
+
+    def __init__(self, name, balance) -> None:
+        self.name = name
+        self.balance = balance
+
+    @classmethod
+    def create_account(cls, name, balance):
+        return cls(name, balance)
+    
+    @classmethod
+    def bank_info(cls):
+        return f"{cls.bank_name} is located in {cls.address}"
+
+
+def main():
+
+    oleg = BankAccount.create_account("Олег Тинкофф", 1000)
+    assert isinstance(oleg, BankAccount)
+    assert oleg.name == 'Олег Тинкофф'
+    assert oleg.balance == 1000
+    assert BankAccount.bank_info() == 'Tinkoff Bank is located in Москва, ул. 2-я Хуторская, д. 38А'
+
+    ivan = BankAccount.create_account("Ivan Reon", 50)
+    assert isinstance(ivan, BankAccount)
+    assert ivan.name == 'Ivan Reon'
+    assert ivan.balance == 50
+    print('Good')
+
+    '''
+    user1 = User('batya99', 'admin')
+    Access.get_access(user1) # печатает "User batya99: success"
+
+    zaya = User('milaya_zaya999', 'user')
+    Access.get_access(zaya) # печатает AccessDenied
+
+    Access.get_access(5) # печатает AccessTypeError
+    '''
+
+    '''
+    box1 = Container()
+    box1.add_item(2)
+    box1.add_item(4)
+
+
+    box2 = Container()
+    box2.add_item(5)
+    box2.add_item(7)
+
+    print(Container.items)
+    '''
+
+    '''
+    droid1 = Robot("R2-D2")
+    print(droid1.name)
+    print(Robot.population)
+    assert droid1.name == 'R2-D2'
+    # assert Robot.population == 1
+    droid1.say_hello()
+    Robot.how_many()
+    droid1.how_many_new()
+    droid2 = Robot("C-3PO")
+    assert droid2.name == 'C-3PO'
+    # assert Robot.population == 2
+    droid2.say_hello()
+    droid2.how_many_new()
+    Robot.how_many()
+    droid1.how_many_new()
+    droid1.destroy()
+    # assert Robot.population == 1
+    droid2.destroy()
+    # assert Robot.population == 0
+    Robot.how_many()
+    '''
+    
+
+    '''
+    example_1 = MyClass()
+    example_2 = MyClass()
+    example_3 = MyClass()
+
+    example_1.create_attr('new_attr', 'Hello world')
+
+    print(example_1.new_attr)
+    print(example_2.new_attr)
+    print(example_3.new_attr)
+    '''
+    
+    '''
+    dep1 = DepartmentIT()
+    dep2 = DepartmentIT()
+
+    print(dep1.REACT_DEV)
+    dep1.REACT_DEV = 5
+    print(dep1.REACT_DEV)
+    print(dep2.REACT_DEV)
+
+    dep3 = DepartmentIT()
+    print(dep3.REACT_DEV)
+    '''
+
+
+if __name__ == '__main__':
+    main()