ex_1.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. class ColourComponent:
  2. def __init__(self, start, end):
  3. self.start = start
  4. self.end = end
  5. def __get__(self, instance, owner_class):
  6. # print(f'__get__ called')
  7. return int(instance.hex[self.start:self.end], 16)
  8. def __set__(self, instance, value):
  9. # print('__set__ called')
  10. pass
  11. class Colour:
  12. r = ColourComponent(1, 3)
  13. g = ColourComponent(3, 5)
  14. b = ColourComponent(5, 7)
  15. def __init__(self, hex):
  16. self.hex = hex
  17. def test_colour():
  18. colour = Colour('#ff8823')
  19. print(colour.r)
  20. print(colour.g)
  21. print(colour.b)
  22. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  23. class MaxLengthAttribute:
  24. def __get__(self, instance, owner_class):
  25. if instance.__dict__ == {}:
  26. return None
  27. return sorted(instance.__dict__.keys(), key= lambda x: (len(x), x), reverse=True)[0]
  28. class MyClass:
  29. max_length_attribute = MaxLengthAttribute()
  30. class JustClass:
  31. max_atr = MaxLengthAttribute()
  32. def test_len():
  33. # obj = MyClass()
  34. # obj.name = "Vasiliy"
  35. # obj.city = "Saint Peterburg"
  36. # obj.country = "Rus"
  37. # print(obj.max_length_attribute)
  38. obj = JustClass()
  39. obj.mock = 15
  40. obj.city = "Saint Peterburg"
  41. obj.name = "Vasiliy"
  42. obj.door = 'wood'
  43. print(obj.max_atr)
  44. if __name__ == '__main__':
  45. # test_colour()
  46. test_len()