调试泡菜 [英] debugging pickle

查看:98
本文介绍了调试泡菜的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试腌制相当复杂的对象层次结构并获取异常:

I'm trying to pickle quite an involved object hierarchy and getting the exception:

pickle.PicklingError: Can't pickle <class 'function'>: attribute lookup builtins.function failed

有没有一种合理的方法可以用来测试对象层次结构的可标记性?我的目的是找到有问题的功能的位置

Are there any reasonable methods one can use to test the pickleablility of an object hierarchy? My aim would be to find the location of the offending function

推荐答案

为此,我将使用莳萝,可以在python中序列化几乎所有内容. Dill还具有一些好工具,可帮助您了解什么是在代码失败时导致酸洗失败.

To do this, I'd use dill, which can serialize almost anything in python. Dill also has some good tools for helping you understand what is causing your pickling to fail when your code fails.

>>> import dill
>>> dill.loads(dill.dumps(your_bad_object))
>>> ...
>>> # if you get a pickling error, use dill's tools to figure out a workaround
>>> dill.detect.badobjects(your_bad_object, depth=0)
>>> dill.detect.badobjects(your_bad_object, depth=1)
>>> ...

如果您绝对愿意,可以使用莳萝的badobjects(或其他检测功能之一)以递归方式深入到对象的参考链中,弹出无法钻探的对象,而不是在每个深度都进行调用,如上所述.

If you absolutely wanted to, you could use dill's badobjects (or one of the other detection functions) to dive recursively into your object's reference chain, and pop out the unpickleable objects, instead of calling it at at every depth, as above.

此外, objgraph 对测试套件也很方便.

Also, objgraph is a pretty handy compliment to the test suite too.

>>> # visualize the references in your bad objects
>>> objgraph.show_refs(your_bad_object, filename='your_bad_object.png')

这篇关于调试泡菜的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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