如何腌制包含模块 & class 的字典?
How do I pickle a dictionary containing a module & class?
我需要分配一个模块 & class 给字典键。然后将该字典腌制到文件中。然后,加载 pkl 文件,然后根据该字典键值导入并实例化 class。
我试过这个:
import module_example
from module_example import ClassExample
dictionary = {'module': module_example, 'class': ClassExample)
但它不会在 pkl 文件中存储对 module_exmaple.py 的引用。
我试过使用字符串代替模块和 class 名称的解决方法。但是,如果模块名称被重构或位置被更改,这将导致混乱。
有没有直接做的?以某种方式在字典中存储对模块的引用 & class,然后根据该引用导入和实例化?
如果您希望未腌制的 class 与经过腌制的对象完全相同,您必须存储 class 的代码(使用 [=10= 这样的外部库],例如)。
否则,标准 pickle 无法存储对 class 的引用,这将 'survive' 重构。
这适用于单身 class。如果您想在多个模块和 classes 中执行此操作,您可以扩展以下代码。
module_class_writer.py
import module_example
from module_example import ClassExample
included_module = ["module_example"]
d = {}
for name, val in globals().items():
if name in included_module:
if "__module__" in dir(val):
d["module"] = val.__module__
d["class"] = name
#d = {'module': module_example, 'class': ClassExample}
import pickle
filehandler = open("imports.pkl","wb")
pickle.dump(d, filehandler)
filehandler.close()
module_class_reader.py
import pickle
filehandler = open("imports.pkl",'rb')
d = pickle.load(filehandler)
filehandler.close()
def reload_class(module_name, class_name):
mod = __import__(module_name, fromlist=[class_name])
reload(mod)
return getattr(mod, class_name)
if "class" in d and "module" in d:
reload(__import__(d["module"]))
ClassExample = reload_class(d["module"], d["class"])
我需要分配一个模块 & class 给字典键。然后将该字典腌制到文件中。然后,加载 pkl 文件,然后根据该字典键值导入并实例化 class。
我试过这个:
import module_example
from module_example import ClassExample
dictionary = {'module': module_example, 'class': ClassExample)
但它不会在 pkl 文件中存储对 module_exmaple.py 的引用。
我试过使用字符串代替模块和 class 名称的解决方法。但是,如果模块名称被重构或位置被更改,这将导致混乱。
有没有直接做的?以某种方式在字典中存储对模块的引用 & class,然后根据该引用导入和实例化?
如果您希望未腌制的 class 与经过腌制的对象完全相同,您必须存储 class 的代码(使用 [=10= 这样的外部库],例如)。
否则,标准 pickle 无法存储对 class 的引用,这将 'survive' 重构。
这适用于单身 class。如果您想在多个模块和 classes 中执行此操作,您可以扩展以下代码。
module_class_writer.py
import module_example
from module_example import ClassExample
included_module = ["module_example"]
d = {}
for name, val in globals().items():
if name in included_module:
if "__module__" in dir(val):
d["module"] = val.__module__
d["class"] = name
#d = {'module': module_example, 'class': ClassExample}
import pickle
filehandler = open("imports.pkl","wb")
pickle.dump(d, filehandler)
filehandler.close()
module_class_reader.py
import pickle
filehandler = open("imports.pkl",'rb')
d = pickle.load(filehandler)
filehandler.close()
def reload_class(module_name, class_name):
mod = __import__(module_name, fromlist=[class_name])
reload(mod)
return getattr(mod, class_name)
if "class" in d and "module" in d:
reload(__import__(d["module"]))
ClassExample = reload_class(d["module"], d["class"])