如何持久化和加载数据类的所有属性
How to persist and load all attributes of a dataclass
我想保留作为数据类实例的对象的所有属性。然后我想从我保留的文件中加载回那个对象。
这是一个完成任务的例子:
from dataclasses import dataclass
import pickle
@dataclass
class Circle:
radius: float
centre: tuple
def save(self, path: str):
name = ".".join(("radius", "pkl"))
with open("/".join((path, name)), "wb") as f:
pickle.dump(self.radius, f)
name = ".".join(("centre", "pkl"))
with open("/".join((path, name)), "wb") as f:
pickle.dump(self.centre, f)
@classmethod
def load(cls, path):
my_model = {}
name = "radius"
file_name = ".".join((name, "pkl"))
with open("\".join((path, file_name)), "rb") as f:
my_model[name] = pickle.load(f)
name = "centre"
file_name = ".".join((name, "pkl"))
with open("\".join((path, file_name)), "rb") as f:
my_model[name] = pickle.load(f)
return cls(**my_model)
>>> c = Circle(2, (0, 0))
>>> c.save(r".\Circle")
>>> c_loaded = Circle.load(r".\Circle")
>>> c_loaded == c
True
如您所见,我需要为每个属性重复相同的代码,更好的方法是什么?
在保存方法中使用self.__dict__
。它包含作为字典的所有属性名称和值。 Load 是一个类方法,所以在那个阶段没有 __dict__
。但是,cls.__annotations__
包含属性名称和类型,仍然存储在字典中。
这是最终结果:
from dataclasses import dataclass
import pickle
@dataclass
class Circle:
radius: float
centre: tuple
def save(self, path):
for name, attribute in self.__dict__.items():
name = ".".join((name, "pkl"))
with open("/".join((path, name)), "wb") as f:
pickle.dump(attribute, f)
@classmethod
def load(cls, path):
my_model = {}
for name in cls.__annotations__:
file_name = ".".join((name, "pkl"))
with open("/".join((path, file_name)), "rb") as f:
my_model[name] = pickle.load(f)
return cls(**my_model)
我想保留作为数据类实例的对象的所有属性。然后我想从我保留的文件中加载回那个对象。
这是一个完成任务的例子:
from dataclasses import dataclass
import pickle
@dataclass
class Circle:
radius: float
centre: tuple
def save(self, path: str):
name = ".".join(("radius", "pkl"))
with open("/".join((path, name)), "wb") as f:
pickle.dump(self.radius, f)
name = ".".join(("centre", "pkl"))
with open("/".join((path, name)), "wb") as f:
pickle.dump(self.centre, f)
@classmethod
def load(cls, path):
my_model = {}
name = "radius"
file_name = ".".join((name, "pkl"))
with open("\".join((path, file_name)), "rb") as f:
my_model[name] = pickle.load(f)
name = "centre"
file_name = ".".join((name, "pkl"))
with open("\".join((path, file_name)), "rb") as f:
my_model[name] = pickle.load(f)
return cls(**my_model)
>>> c = Circle(2, (0, 0))
>>> c.save(r".\Circle")
>>> c_loaded = Circle.load(r".\Circle")
>>> c_loaded == c
True
如您所见,我需要为每个属性重复相同的代码,更好的方法是什么?
在保存方法中使用self.__dict__
。它包含作为字典的所有属性名称和值。 Load 是一个类方法,所以在那个阶段没有 __dict__
。但是,cls.__annotations__
包含属性名称和类型,仍然存储在字典中。
这是最终结果:
from dataclasses import dataclass
import pickle
@dataclass
class Circle:
radius: float
centre: tuple
def save(self, path):
for name, attribute in self.__dict__.items():
name = ".".join((name, "pkl"))
with open("/".join((path, name)), "wb") as f:
pickle.dump(attribute, f)
@classmethod
def load(cls, path):
my_model = {}
for name in cls.__annotations__:
file_name = ".".join((name, "pkl"))
with open("/".join((path, file_name)), "rb") as f:
my_model[name] = pickle.load(f)
return cls(**my_model)