相关疑难解决方法(0)

使用Pickle/cPickle命中最大递归深度

背景:我正在使用最小构造算法构建一个代表字典的trie.输入列表是4.3M utf-8字符串,按字典顺序排序.生成的图形是非循环的,最大深度为638个节点.我的脚本的第一行将递归限制设置为1100 sys.setrecursionlimit().

问题:我希望能够将我的trie序列化到磁盘,因此我可以将其加载到内存中而无需从头开始重建(大约22分钟).我曾经尝试都pickle.dump()cPickle.dump(),用文本和二进制协议两种.每次,我得到一个如下所示的堆栈跟踪:

  File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/pickle.py", line 649, in save_dict
    self._batch_setitems(obj.iteritems())
  File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/pickle.py", line 663, in _batch_setitems
    save(v)
  File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/pickle.py", line 725, in save_inst
    save(stuff)
  File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/pickle.py", line 648, in save_dict
    self.memoize(obj)
RuntimeError: maximum recursion depth exceeded
Run Code Online (Sandbox Code Playgroud)

我的数据结构相对简单: trie包含对开始状态的引用,并定义了一些方法. dfa_state包含布尔字段,字符串字段和从标签到状态的字典映射.

我对内部工作原理并不十分熟悉pickle- …

python tree recursion pickle depth

52
推荐指数
4
解决办法
3万
查看次数

在 Python 中使用 __getattr__ 方法对对象进行 Pickle 返回 `TypeError, object not callable`

我想定义一个None__getattr__方法返回未知属性的类。

这样做之后,我试图将该类的对象转储到 Pickle。

但是,我得到了错误

Traceback (most recent call last):
  File "c:\SVN\Scripts\Rally\examples\t_pickle_None.py", line 14, in <module>
    pickle.dump(toto, f, pickle.HIGHEST_PROTOCOL)
TypeError: 'NoneType' object is not callable
Run Code Online (Sandbox Code Playgroud)

没有定义__getattr__,它工作正常,但我想保留这个功能。

这是我的代码:如何使其工作__getattr__

谢谢

import pickle
from typing import Any

class Toto:
    def __init__(self, name:str) -> None:
        self.name = name

    def __getattr__(self, _: str) -> Any:
        """Return None for all unknown attributes"""
        return None

toto = Toto("Toto")
with open('toto.pkl', 'wb') as f:
    pickle.dump(toto, f, pickle.HIGHEST_PROTOCOL)
Run Code Online (Sandbox Code Playgroud)

python pickle python-3.x

1
推荐指数
1
解决办法
1298
查看次数

标签 统计

pickle ×2

python ×2

depth ×1

python-3.x ×1

recursion ×1

tree ×1