0

我在一个类中命名了元组。使用 dill 进行酸洗时,它会抱怨无法在顶部模块中找到 namedtuple 对象的经典问题。

import dill as pickle    
class NNRTMParse(object):

    def __init__(self,logfile)):
        .
        .
        .
        .

        self.TM = namedtuple('TM',tmeas_fields)
        #print self.TM
        CFH = namedtuple('CFH',cfhdr_fields)
        PM = namedtuple('PM',pmeas_fields2)
        print PM

这是 self.TM 和其他人的默认分配,我在解析日志时动态分配 namedtuple。

但是酸洗:

 if __name__ == "__main__":

    filename = 'dbggen_rx_loc_2-llh_rtm_lla_out_20160929_130711_day2_4381_JN2_SN64_rtmproc_2M5M.txt'
    N = NNRTMParse(filename)
    N.parse()
    N.get_rx_loc('oak484_bora-llh')

    filehandler = open("NNRTMParse_JB2-SN052.obj","wb")
    pickle.dump(N,filehandler)
    filehandler.close()






Traceback (most recent call last):
  File "C:/NN_Hardware/spos_proc/NNRTMParse.py", line 937, in <module>
    pickle.dump(N,filehandler)
  File "C:\Python27\lib\site-packages\dill\dill.py", line 236, in dump
    pik.dump(obj)
  File "C:\Python27\lib\pickle.py", line 224, in dump
    self.save(obj)
  File "C:\Python27\lib\pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)
  File "C:\Python27\lib\pickle.py", line 419, in save_reduce
    save(state)
  File "C:\Python27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:\Python27\lib\site-packages\dill\dill.py", line 835, in save_module_dict
    StockPickler.save_dict(pickler, obj)
  File "C:\Python27\lib\pickle.py", line 649, in save_dict
    self._batch_setitems(obj.iteritems())
  File "C:\Python27\lib\pickle.py", line 681, in _batch_setitems
    save(v)
  File "C:\Python27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "C:\Python27\lib\site-packages\dill\dill.py", line 1189, in save_type
    StockPickler.save_global(pickler, obj)
  File "C:\Python27\lib\pickle.py", line 748, in save_global
    (obj, module, name))
pickle.PicklingError: Can't pickle <class '__main__.TM'>: it's not found as __main__.TM

我不确定是否有任何出路。否则将不得不通过路径来挑选要腌制的数据帧,我讨厌这样做。

非常感谢任何帮助。

4

1 回答 1

0

问题是self.TM namedtuple. 如果你不使用namedtuple作为类属性,那么你的类应该腌制。

# file: xxx.py
from collections import namedtuple

class NNRTMParse(object):

  def __init__(self):
    TM = namedtuple("TM", 'a')
    CFH = namedtuple("CFH", 'b')
    print CFH

它应该像这样工作:

>>> from xxx import *
>>> a = NNRTMParse()
<class 'xxx.CFH'>
>>> import dill
>>> dill.copy(a)
<xxx.NNRTMParse object at 0x107da3350>

但是,一旦您尝试将namedtuple用作属性,它就会失败并显示您看到的错误。

我建议添加一个__reduce__方法(https://docs.python.org/3/library/pickle.html#object.reduce)来告诉如何pickle序列化类实例的状态。

于 2016-10-09T09:27:35.697 回答