123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- 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()
|