Dill模块中BadItem的好例子 [英] Good example of BadItem in Dill Module

查看:138
本文介绍了Dill模块中BadItem的好例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Dill的detect方法,并在寻找一个很好的简单示例,说明一个不良物品-Dill无法拾取的物品.

I'm exploring the detect method of Dill and am looking for a good simple example of a bad item - one that's unpicklable by Dill.

我首先想到一个过程并尝试:

I first thought of a process and tried:

>>> proc = os.popen('ls -l')
>>> proc
<open file 'ls -l', mode 'r' at 0x10071d780>
>>> dill.detect.baditems(proc)
[]
>>> dill.dumps(proc)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/mikekilmer/Envs/env1/lib/python2.7/site-packages/dill/dill.py", line 143, in dumps
    dump(obj, file, protocol, byref)
  File "/Users/mikekilmer/Envs/env1/lib/python2.7/site-packages/dill/dill.py", line 136, in dump
    pik.dump(obj)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 224, in dump
    self.save(obj)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "/Users/mikekilmer/Envs/env1/lib/python2.7/site-packages/dill/dill.py", line 557, in save_file
    position = obj.tell()
IOError: [Errno 29] Illegal seek

我想这是可以预料的,因为Dill使用了detectbaditems的搜索,因为您不能在PIPE上搜索.

Which I guess would be expected if Dill uses seek to detectbaditems as you cannot seek on a PIPE.

然后我想,globals()当然可以提供一些东西.它再次提供相同的IOerror,直到proc被删除,然后产生:

Then I thought, surely globals() has something to offer. It again offered the same IOerror until proc was removed, and then yielded:

>>> dill.detect.baditems(globals)
[<module 'pickle' from '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.pyc'>, <module 'os' from '/Users/mikekilmer/Envs/env1/lib/python2.7/os.pyc'>, <__main__.Child object at 0x100776090>]

dill.detect将作为不良项目返回的项目的简单示例是什么?

What would be a good simple example of an item that dill.detect would return as a bad item?

推荐答案

dill.detect.baditems应该检查对象的内部中的不良品"(即,检查对象内部是否存在不腌制).也许在顶层,它应该检查对象本身是否泡菜……它当前不是泡菜,这可能会误导人.

dill.detect.baditems is supposed to check for "bad items" inside of an object (i.e. check what is inside of an object that doesn't pickle). Maybe, at the top level it should check if the object itself pickles… it doesn't currently, and that could be misleading.

在这里,我将演示一个不可刺的物品,baditems说里面没有任何不可刺的东西,这是事实.然后,我将说明baditems如何在globals内找到不可腌制的物品,并正确地识别哪些不能腌制.

Here I'll demonstrate an unpicklable item that baditems says there's nothing unpicklable on the inside, which is true. Then I'll show how baditems finds unpicklable items inside of globals, and correctly identifies what cannot be pickled.

>>> x = iter([1,2,3,4,5])
>>> x
<listiterator object at 0x10d743510>
>>> import dill
>>> # everything inside a listiterator is serializable
>>> dill.detect.baditems(x)
[]
>>> # however, not everything in globals is serializable
>>> dill.detect.baditems(globals())
[<module '__builtin__' (built-in)>, <listiterator object at 0x10d743510>]

希望这似乎不太符合直觉.

Hopefully, this doesn't seem too counter-intuitive.

这篇关于Dill模块中BadItem的好例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆