TelenkovDmitry 1 سال پیش
والد
کامیت
f60ef867c1
4فایلهای تغییر یافته به همراه86 افزوده شده و 3 حذف شده
  1. BIN
      books/python/__pycache__/misc_class.cpython-310.pyc
  2. BIN
      books/python/cellphones.dat
  3. 54 2
      books/python/main.py
  4. 32 1
      books/python/misc_class.py

BIN
books/python/__pycache__/misc_class.cpython-310.pyc


BIN
books/python/cellphones.dat


+ 54 - 2
books/python/main.py

@@ -1,4 +1,5 @@
 import misc_class
+import pickle
 
 
 def coin_test():
@@ -26,12 +27,63 @@ def bankaccount_test():
     # Здесь тоже будет вызван метод __str__, но через str()
     print(str(savings))    
 
-    
+
+# Создает 4 объектов CellPhone и сохраняет из в списке
+def cellphone_test():
+    phone_list = []
+
+    for _ in range(5):
+        phone = misc_class.CellPhone()
+        phone_list.append(phone)
+
+    for item in phone_list:
+        print(item.get_manufact())        
+        print(item.get_model())
+        print(item.get_retail_price())
+
+# Сериализация/десериализация объектов
+def cellphone_serial():
+    FILE_NAME = 'cellphones.dat'    
+    file = open(FILE_NAME, 'wb')
+
+    for _ in range(5):
+        phone = misc_class.CellPhone()
+        pickle.dump(phone, file)
+
+    file.close()
+    print(f'Данные записаны в {FILE_NAME}')
+
+    file = open(FILE_NAME, 'rb')
+    end_of_file = False
+    while not end_of_file:
+        try:
+            phone = pickle.load(file)
+            print(f'Производитель: {phone.get_manufact()}')
+            print(f'Номер моделы: {phone.get_model()}')
+            print(f'Розничная цена: {phone.get_retail_price()}')
+            print()
+        except Exception as e:
+            end_of_file = True
+
+    file.close()
+
 
 
 def main():
+    #
     # coin_test()    
-    bankaccount_test()
+
+    # Методы __str__
+    # bankaccount_test()
+
+    # Создание списка объектов CellPhone
+    # cellphone_test()
+
+    # Сериализация объектов 
+    # cellphone_serial()
+
+
+
 
 if __name__ == '__main__':
     main()

+ 32 - 1
books/python/misc_class.py

@@ -54,4 +54,35 @@ class BankAccaunt:
             print('Ошибка: недостаточно средств')
 
     def get_balance(self):
-        return self.__balance
+        return self.__balance
+    
+
+# Класс CellPhone содержит данные о сотовом телефоне
+    
+class CellPhone:
+
+    def __init__(self, manufact='Simens', model='c55', price=120) -> None:
+        self.__manufact = manufact
+        self.__model = model
+        self.__retail_price = price
+
+    def set_manufact(self, manufact):
+        self.__manufact = manufact
+
+    def set_model(self, model):
+        self.__model = model
+
+    def set_retail_price(self, price):
+        self.__retail_price = price
+
+    def get_manufact(self):
+        return self.__manufact
+    
+    def get_model(self):
+        return self.__model
+    
+    def get_retail_price(self):
+        return self.__retail_price
+    
+
+