我问的第一个问题是how to load a pickle object and resolve certain references。我面临的另一个问题是我无法将dumps
或loads
个对象称为二进制对象。
以下是ContextAwarePickler
和ContextAwareUnpickler
的实现。如何使用这些将对象转换为二进制表示形式?据我所知,这仅适用于文件。
import pickle
class ContextAwarePickler(pickle.Pickler):
def persistent_id(self, obj):
# if this is a context, return the key
if isinstance(obj, Context):
return ("Context", context.key)
# pickle as usual
return None
class ContextAwareUnpickler(pickle.Unpickler):
def recover_context(self, key_id):
...
def persistent_load(self, pid):
type_tag, key_id = pid
if type_tag == "Context":
return self.recover_context(key_id)
else:
raise pickle.UnpicklingError("unsupported persistent object")
答案 0 :(得分:1)
您的解决方案类似于AutofacServiceLocator
(我是作者)中的解决方案 - 但不是那么强大。
https://github.com/uqfoundation/dill/blob/cccbea9b715e16b742288e1e5a21a687a4d4081b/dill/temp.py#L169(代码剪辑转载如下)
dill
请注意,您可能需要谨慎对待def loadIO(buffer, **kwds):
"""load an object that was stored with dill.temp.dumpIO
buffer: buffer object
>>> dumpfile = dill.temp.dumpIO([1, 2, 3, 4, 5])
>>> dill.temp.loadIO(dumpfile)
[1, 2, 3, 4, 5]
"""
import dill as pickle
if PY3:
from io import BytesIO as StringIO
else:
from StringIO import StringIO
value = getattr(buffer, 'getvalue', buffer) # value or buffer.getvalue
if value != buffer: value = value() # buffer.getvalue()
return pickle.load(StringIO(value))
def dumpIO(object, **kwds):
"""dill.dump of object to a buffer.
Loads with "dill.temp.loadIO". Returns the buffer object.
>>> dumpfile = dill.temp.dumpIO([1, 2, 3, 4, 5])
>>> dill.temp.loadIO(dumpfile)
[1, 2, 3, 4, 5]
"""
import dill as pickle
if PY3:
from io import BytesIO as StringIO
else:
from StringIO import StringIO
file = StringIO()
pickle.dump(object, file)
file.flush()
return file
上的flush
缓冲区,dump
。{/ p>
答案 1 :(得分:0)
我想我找到了答案:
class ContextawarePickling :
@staticmethod
def load_aware (input_file,context=None) :
return ContextawareUnpickler(input_file).load()
@staticmethod
def dump_aware (object_to_save,output_file):
ContextawarePickler(output_file).dump(object_to_save)
@staticmethod
def loads_aware (stream,context=None) :
file = io.BytesIO(stream)
return ContextawarePickling.load_aware(file)
@staticmethod
def dumps_aware (object_to_save):
f = io.BytesIO()
ContextawarePickling.dump_aware(object_to_save,f)
return f.getvalue()
基本上,您首先要创建两个实用工具方法:load_aware
和dump_aware
。下一个可以实现loads_aware
和dumps_aware
,其中一个包装io.BytesIO
,作为处理程序,可以从中加载/存储数据。