namespace.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. class DepartmentIT:
  2. PYTHON_DEV = 3
  3. GO_DEV = 3
  4. REACT_DEV = 2
  5. def info(self):
  6. print(self.PYTHON_DEV, self.GO_DEV, self.REACT_DEV)
  7. @property
  8. def info_prop(self):
  9. print(self.PYTHON_DEV, self.GO_DEV, self.REACT_DEV)
  10. class MyClass:
  11. class_attribute = 'I am a class attribute'
  12. def __init__(self) -> None:
  13. self.instance_attribute = 'I am an instatnce attribute'
  14. @classmethod
  15. def create_attr(cls, attr_name, attr_value):
  16. setattr(cls, attr_name, attr_value)
  17. class Container:
  18. items = []
  19. def add_item(self, value):
  20. # self.items.append(value)
  21. self.items = [value] + self.items
  22. class Robot:
  23. population = 0
  24. my_list = []
  25. def __init__(self, name) -> None:
  26. # Robot.population += 1
  27. self.population += 1
  28. self.name = name
  29. self.my_list.append(name)
  30. print(f'Робот {self.name} был создан')
  31. def destroy(self):
  32. print(f'Робот {self.name} был уничтожен')
  33. # Robot.population -= 1
  34. self.population -= 1
  35. def say_hello(self):
  36. print(f'Робот {self.name} приветствует тебя, особь человеческого рода')
  37. def how_many_new(self):
  38. print(f'{self.population}, вот сколько нас еще осталось')
  39. @classmethod
  40. def how_many(cls):
  41. # print(f'{cls.population}, вот сколько нас еще осталось')
  42. print(f'{cls.population}, вот сколько нас еще осталось')
  43. class User:
  44. def __init__(self, name, role) -> None:
  45. self.name = name
  46. self.role = role
  47. class Access:
  48. __access_list = ['admin', 'developer']
  49. def __init__(self) -> None:
  50. pass
  51. @staticmethod
  52. def __check_access(role):
  53. return True if role in Access.__access_list else False
  54. @staticmethod
  55. def get_access(user: User):
  56. if not isinstance(user, User):
  57. print('AccessTypeError')
  58. return
  59. if Access.__check_access(user.role):
  60. print(f'User {user.name}: success')
  61. else:
  62. print(f'AccessDenied')
  63. class BankAccount:
  64. bank_name = 'Tinkoff Bank'
  65. address = 'Москва, ул. 2-я Хуторская, д. 38А'
  66. def __init__(self, name, balance) -> None:
  67. self.name = name
  68. self.balance = balance
  69. @classmethod
  70. def create_account(cls, name, balance):
  71. return cls(name, balance)
  72. @classmethod
  73. def bank_info(cls):
  74. return f"{cls.bank_name} is located in {cls.address}"
  75. def main():
  76. oleg = BankAccount.create_account("Олег Тинкофф", 1000)
  77. assert isinstance(oleg, BankAccount)
  78. assert oleg.name == 'Олег Тинкофф'
  79. assert oleg.balance == 1000
  80. assert BankAccount.bank_info() == 'Tinkoff Bank is located in Москва, ул. 2-я Хуторская, д. 38А'
  81. ivan = BankAccount.create_account("Ivan Reon", 50)
  82. assert isinstance(ivan, BankAccount)
  83. assert ivan.name == 'Ivan Reon'
  84. assert ivan.balance == 50
  85. print('Good')
  86. '''
  87. user1 = User('batya99', 'admin')
  88. Access.get_access(user1) # печатает "User batya99: success"
  89. zaya = User('milaya_zaya999', 'user')
  90. Access.get_access(zaya) # печатает AccessDenied
  91. Access.get_access(5) # печатает AccessTypeError
  92. '''
  93. '''
  94. box1 = Container()
  95. box1.add_item(2)
  96. box1.add_item(4)
  97. box2 = Container()
  98. box2.add_item(5)
  99. box2.add_item(7)
  100. print(Container.items)
  101. '''
  102. '''
  103. droid1 = Robot("R2-D2")
  104. print(droid1.name)
  105. print(Robot.population)
  106. assert droid1.name == 'R2-D2'
  107. # assert Robot.population == 1
  108. droid1.say_hello()
  109. Robot.how_many()
  110. droid1.how_many_new()
  111. droid2 = Robot("C-3PO")
  112. assert droid2.name == 'C-3PO'
  113. # assert Robot.population == 2
  114. droid2.say_hello()
  115. droid2.how_many_new()
  116. Robot.how_many()
  117. droid1.how_many_new()
  118. droid1.destroy()
  119. # assert Robot.population == 1
  120. droid2.destroy()
  121. # assert Robot.population == 0
  122. Robot.how_many()
  123. '''
  124. '''
  125. example_1 = MyClass()
  126. example_2 = MyClass()
  127. example_3 = MyClass()
  128. example_1.create_attr('new_attr', 'Hello world')
  129. print(example_1.new_attr)
  130. print(example_2.new_attr)
  131. print(example_3.new_attr)
  132. '''
  133. '''
  134. dep1 = DepartmentIT()
  135. dep2 = DepartmentIT()
  136. print(dep1.REACT_DEV)
  137. dep1.REACT_DEV = 5
  138. print(dep1.REACT_DEV)
  139. print(dep2.REACT_DEV)
  140. dep3 = DepartmentIT()
  141. print(dep3.REACT_DEV)
  142. '''
  143. if __name__ == '__main__':
  144. main()