我有一個基類:
#!/usr/bin/python3
"""Base class for all other classes in this module"""
import json
class Base:
"""Base Class
Args:
__nb_objects (int): number of instances of class
"""
__nb_objects = 0
def __init__(self, id=None):
"""class constructor
Args:
id (int, optional): argument value to initialize. Defaults to None.
"""
if id is not None:
self.id = id
else:
type(self).__nb_objects = 1
self.id = type(self).__nb_objects
@staticmethod
def to_json_string(list_dictionaries):
"""returns the JSON string representation of list_dictionaries
Args:
list_dictionaries ([{}]): a list of dictionaries.
"""
if list_dictionaries is None or len(list_dictionaries) == 0:
return "[]"
return json.dumps(list_dictionaries)
@classmethod
def save_to_file(cls, list_objs):
"""writes the JSON string representation of list_objs to a file
Args:
list_objs (list): list of instances who inherits of Base
"""
filename = type(list_objs[0]).__name__ ".json"
json_file = open(filename, "w")
if list_objs is None:
json.dump([], json_file)
if type(list_objs[0]).__name__ == "Rectangle":
new_dict = [item.to_dictionary() for item in list_objs]
json_string = cls.to_json_string(new_dict)
json.dump(new_dict, json_file)
json_file.close()
和一個子類:
#!/usr/bin/python3
"""Definition of Rectangle"""
from . base import Base
class Rectangle(Base):
"""define a rectangle object
Args:
Base (class): Base Class
"""
def __init__(self, width, height, x=0, y=0, id=None):
"""rectangle constructor
Args:
width (int): width of rectangle
height (int): height of rectangle
x (int, optional): x offset of rectangle. Defaults to 0.
y (int, optional): y offset of rectangle. Defaults to 0.
id (int, optional): identifier. Defaults to None.
"""
super().__init__(id)
if type(width) is not int:
raise TypeError('width must be an integer')
if width < 1:
raise ValueError('width must be > 0')
self.width = width
if type(height) is not int:
raise TypeError('height must be an integer')
if height < 1:
raise ValueError('height must be > 0')
self.height = height
if type(x) is not int:
raise TypeError('x must be an integer')
if x < 0:
raise ValueError('x must be >= 0')
self.x = x
if type(y) is not int:
raise TypeError('y must be an integer')
if y < 0:
raise ValueError('y must be >= 0')
self.y = y
@property
def width(self):
"""Getter and Setter methods for width"""
return self.width
@width.setter
def width(self, value):
if type(value) is not int:
raise TypeError('width must be an integer')
if value < 1:
raise ValueError('width must be > 0')
self.width = value
@property
def height(self):
"""Getter and Setter methods for height"""
return self.height
@height.setter
def height(self, value):
if type(value) is not int:
raise TypeError('height must be an integer')
if value < 1:
raise ValueError('height must be > 0')
self.height = value
@property
def x(self):
"""Getter and Setter methods for x"""
return self.x
@x.setter
def x(self, value):
if type(value) is not int:
raise TypeError('x must be an integer')
if value < 0:
raise ValueError('x must be >= 0')
self.x = value
@property
def y(self):
"""Getter and Setter methods for y"""
return self.y
@y.setter
def y(self, value):
if type(value) is not int:
raise TypeError('y must be an integer')
if value < 0:
raise ValueError('y must be >= 0')
self.y = value
def area(self):
"""returns the area value of the Rectangle instance"""
return self.width * self.height
def display(self):
"""prints in stdout the Rectangle instance with the character #"""
print(('\n' * self.y) '\n'
.join(' ' * self.x '#' * self.width
for _ in range(self.height)))
def __str__(self):
"""Returns an informal string representation a Rectangle object"""
return '[Rectangle] ({}) {}/{} - {}/{}'\
.format(self.id, self.x, self.y,
self.width, self.height)
def update(self, *args, **kwargs):
"""update the instance attributes"""
if len(kwargs):
if 'height' in kwargs:
self.height = kwargs['height']
if 'width' in kwargs:
self.width = kwargs['width']
if 'x' in kwargs:
self.x = kwargs['x']
if 'y' in kwargs:
self.y = kwargs['y']
if 'id' in kwargs:
self.id = kwargs['id']
else:
try:
self.id = args[0]
self.width = args[1]
self.height = args[2]
self.x = args[3]
self.y = args[4]
except IndexError:
pass
def to_dictionary(self):
"""returns the dictionary representation of a Rectangle"""
return self.__dict__
這是主檔案:
#!/usr/bin/python3
""" 15-main """
from models.rectangle import Rectangle
if __name__ == "__main__":
r1 = Rectangle(10, 7, 2, 8)
r2 = Rectangle(2, 4)
Rectangle.save_to_file([r1, r2])
with open("Rectangle.json", "r") as file:
print(file.read())
任務是使用 Base 中的“ to_json_string ”方法(回傳實體屬性字典串列的 json 字串)撰寫函式“ save_to_file ”,該函式保存從 base 繼承的實體屬性字典串列
to_json_string接受字典串列,save_to_file接受子類 Recangle(list_objs) 的實體串列,因此為了將 list_objs 傳遞給to_json_string,我必須創建實體屬性的字典串列,這就是這一行的作用:
new_dict = [item.to_dictionary() for item in list_objs]
但是當我將此串列傳遞給to_json_string并嘗試將其轉儲到檔案 Rectangle.json 中時
new_dict = [item.to_dictionary() for item in list_objs]
json_string = cls.to_json_string(new_dict)
json.dump(json_string, json_file)
我得到如下輸出:
"[{\"id\": 1, \"width\": 10, \"height\": 7, \"x\": 2, \"y\": 8}, {\"id\": 2, \"width\": 2, \"height\": 4, \"x\": 0, \"y\": 0}]"
但是當將串列轉儲到檔案時,我會得到檔案中的字典串列,例如:
[{"id": 1, "width": 10, "height": 7, "x": 2, "y": 8}, {"id": 2, "width": 2, "height": 4, "x": 0, "y": 0}]
那么為什么我用雙引號得到輸出,我怎樣才能使用to_json_string來得到正確的輸出呢?
uj5u.com熱心網友回復:
問題是您嘗試 json 轉儲兩次,一次一次,to_json_string然后再次使用結果,所以您得到一個字串,然后將該字串寫為整個內容(結果字串是有效的 json,但值得懷疑)
>>> d = {"foo": "bar"}
>>> print(json.dumps(json.dumps(d)))
"{\"foo\": \"bar\"}"
>>> print(json.dumps(d))
{"foo": "bar"}
您可以洗掉一個.dump實體,使轉儲字串化可選,設定一些標志,在重新執行之前檢查字串.dump,或者您喜歡的任何其他邏輯!
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/472796.html
上一篇:從類體中參考一個超類
下一篇:繪制具有相同變數的多個類
