method.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import math
  2. class Cat:
  3. breed = 'pers'
  4. def hello():
  5. print("hello")
  6. def show_breed(instance):
  7. print(f'my breed is {instance.breed}')
  8. print(instance)
  9. def show_name(instance):
  10. if hasattr(instance, 'name'):
  11. print(f'my name is {instance.name}')
  12. else:
  13. print('nothing')
  14. def set_value(koshka, value, age=0):
  15. koshka.name = value
  16. koshka.age = age
  17. def new_method(self):
  18. pass
  19. class Point:
  20. list_points = []
  21. def __init__(self, x, y) -> None:
  22. self.move_to(x, y)
  23. Point.list_points.append(self)
  24. def move_to(self, x, y):
  25. self.x = x
  26. self.y = y
  27. def go_home(self):
  28. self.move_to(0, 0)
  29. def print_point(self):
  30. print(f"Точка с координатами ({self.x}, {self.y})")
  31. def calc_distance(self, another_point):
  32. if not isinstance(another_point, Point):
  33. raise ValueError("Аргумент должен принадлежать классу Точка")
  34. return math.sqrt((self.x - another_point.x)**2 + (self.y -another_point.y)**2)
  35. class Numbers:
  36. def __init__(self, *args) -> None:
  37. self.values = []
  38. self.values.extend(args)
  39. def add_number(self, val):
  40. self.values.append(val)
  41. def get_positive(self):
  42. return [x for x in self.values if x > 0]
  43. def get_negative(self):
  44. return [x for x in self.values if x < 0]
  45. def get_zeroes(self):
  46. return [x for x in self.values if x == 0]
  47. class Stack:
  48. def __init__(self):
  49. self.values = []
  50. def push(self, value):
  51. self.values.append(value)
  52. def pop(self):
  53. if not self.values:
  54. print("Empty Stack")
  55. else:
  56. return self.values.pop()
  57. def peek(self):
  58. if not self.values:
  59. print("Empty Stack")
  60. return None
  61. return self.values[-1]
  62. def is_empty(self):
  63. return not bool(self.values)
  64. def size(self):
  65. return len(self.values)
  66. class Worker:
  67. def __init__(self, name, salary, gender, passport):
  68. self.name = name
  69. self.salary = salary
  70. self.gender = gender
  71. self.passport = passport
  72. def get_info(self):
  73. print(f"Worker {self.name}; passport-{self.passport}")
  74. class CustomLabel:
  75. def __init__(self, text, **kw):
  76. self.text = text
  77. for name, value in kw.items():
  78. setattr(self, name, value)
  79. def config(self, **kw):
  80. for name, value in kw.items():
  81. setattr(self, name, value)
  82. def main():
  83. """CustomLabel"""
  84. label1 = CustomLabel(text="Hello Python", fg="#eee", bg="#333")
  85. label2 = CustomLabel(text="Username")
  86. label3 = CustomLabel(text="Password", font=("Comic Sans MS", 24, "bold"), bd=20, bg='#ffaaaa')
  87. label4 = CustomLabel(text="Hello", bd=20, bg='#ffaaaa')
  88. label5 = CustomLabel(text="qwwerty", a=20, b='#ffaaaa', r=[3, 4, 5, 6], p=32)
  89. assert label1.__dict__ == {'text': 'Hello Python', 'fg': '#eee', 'bg': '#333'}
  90. assert label2.__dict__ == {'text': 'Username'}
  91. assert label3.__dict__ == {'text': 'Password', 'font': ('Comic Sans MS', 24, 'bold'), 'bd': 20, 'bg': '#ffaaaa'}
  92. assert label4.__dict__ == {'text': 'Hello', 'bd': 20, 'bg': '#ffaaaa'}
  93. assert label5.__dict__ == {'text': 'qwwerty', 'a': 20, 'b': '#ffaaaa', 'r': [3, 4, 5, 6], 'p': 32}
  94. print(label1.__dict__)
  95. print(label2.__dict__)
  96. print(label3.__dict__)
  97. print(label4.__dict__)
  98. print(label5.__dict__)
  99. label4.config(color='red', bd=100)
  100. label5.config(color='red', bd=100, a=32, b=432, p=100, z=432)
  101. assert label4.__dict__ == {'text': 'Hello', 'bd': 100, 'bg': '#ffaaaa', 'color': 'red'}
  102. assert label5.__dict__ == {'text': 'qwwerty', 'a': 32, 'b': 432, 'r': [3, 4, 5, 6], 'p': 100,
  103. 'color': 'red', 'bd': 100, 'z': 432}
  104. """Class Numbers"""
  105. '''
  106. num = Numbers(1, 2, -3, -4, 5)
  107. num.get_values()
  108. print(num.get_positive())
  109. '''
  110. """Class Stack"""
  111. # stack = Stack()
  112. # stack.peek()
  113. # print(stack.is_empty())
  114. """Worker"""
  115. """
  116. persons= [
  117. ('Allison Hill', 334053, 'M', '1635644202'),
  118. ('Megan Mcclain', 191161, 'F', '2101101595'),
  119. ('Brandon Hall', 731262, 'M', '6054749119'),
  120. ('Michelle Miles', 539898, 'M', '1355368461'),
  121. ('Donald Booth', 895667, 'M', '7736670978'),
  122. ('Gina Moore', 900581, 'F', '7018476624'),
  123. ('James Howard', 460663, 'F', '5461900982'),
  124. ('Monica Herrera', 496922, 'M', '2955495768'),
  125. ('Sandra Montgomery', 479201, 'M', '5111859731'),
  126. ('Amber Perez', 403445, 'M', '0602870126')
  127. ]
  128. # workers_objects = [Worker('Bob Moore', 330000, 'M', '1635777202')]
  129. workers_objects = [Worker(*x) for x in persons]
  130. for worker in workers_objects:
  131. worker.get_info()
  132. """
  133. # print(persons[0][1])
  134. # for person in persons:
  135. # workers_objects.append('')
  136. # cat = Cat()
  137. # cat.show_breed()
  138. # tom = Cat()
  139. # print(isinstance(tom, Cat))
  140. # tom.show_name()
  141. # tom.set_value('Tom')
  142. # tom.show_name()
  143. # leo.score(700)
  144. # assert leo.goals == 700
  145. # leo.make_assist(500)
  146. # assert leo.assists == 500
  147. # leo.statistics()
  148. if __name__ == '__main__':
  149. main()