dec.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. class BankAccount:
  2. def __init__(self, name, balance):
  3. self.name = name
  4. self.__balance = balance
  5. @property
  6. def my_balance(self):
  7. return self.__balance
  8. @my_balance.setter
  9. def my_balance(self, value):
  10. if not isinstance(value, (int, float)):
  11. raise ValueError('Баланс должен быть числом')
  12. @my_balance.deleter
  13. def my_balance(self):
  14. del self.__balance
  15. class MagicBox:
  16. def __init__(self, contents=None):
  17. self._contents = contents
  18. @property
  19. def contents(self):
  20. if self._contents == "rabbit":
  21. return "A magical rabbit!"
  22. else:
  23. return self._contents
  24. @contents.setter
  25. def entity(self, new_contents):
  26. if new_contents == "wishes":
  27. print("Your wishes are granted!")
  28. self._contents = new_contents
  29. else:
  30. print("the magic didn't work this time.")
  31. self._contents = new_contents
  32. class Celsius:
  33. def __init__(self, temp) -> None:
  34. self.__temperature = temp
  35. def to_fahrenheit(self):
  36. return self.__temperature*9/5 + 32
  37. @property
  38. def temperature(self):
  39. return self.__temperature
  40. @temperature.setter
  41. def temperature(self, value):
  42. if value < -273.15:
  43. raise ValueError("Температура должна быть больше 273.15")
  44. else:
  45. self.__temperature = value
  46. class File:
  47. def __init__(self, size_in_bytes):
  48. self._size_in_bytes = size_in_bytes
  49. @property
  50. def size(self):
  51. if self._size_in_bytes < 1024:
  52. return f"{self._size_in_bytes} B"
  53. elif self._size_in_bytes < 1048576:
  54. return "%.2f KB" % (self._size_in_bytes/1024.0)
  55. elif self._size_in_bytes < 1073741824:
  56. return "%.2f MB" % (self._size_in_bytes/1048576.0)
  57. else:
  58. return "%.2f GB" % (self._size_in_bytes/1073741824.0)
  59. @size.setter
  60. def size(self, value):
  61. self._size_in_bytes = value
  62. class Notebook:
  63. def __init__(self, notes: list):
  64. self._notes = notes
  65. @property
  66. def notes_list(self):
  67. for i, j in enumerate(self._notes):
  68. print(f"{i+1}.{j}")
  69. def main():
  70. # acc = BankAccount('Ivan', 200)
  71. # print(acc.my_balance)
  72. note = Notebook(['Buy Potato', 'Buy Carrot', 'Wash car'])
  73. note.notes_list
  74. '''
  75. box = MagicBox("rubies")
  76. print(box.contents)
  77. print(box._contents)
  78. box.entity = "wishes"
  79. print(box._contents)
  80. print(box.entity)
  81. '''
  82. '''
  83. cel = Celsius(12.0)
  84. cel.temperature = 15
  85. cel.temperature = -300
  86. print(cel.temperature)
  87. '''
  88. if __name__ == '__main__':
  89. main()