TypeError:在编写生成器时不能腌制函数对象

时间:2016-02-24 11:49:54

标签: python pickle

我已经检查了类似的线程,我的问题似乎有所不同。

我正在尝试腌制物体。我在终端上尝试过简单的例子。我在这段代码中使用了相同的apprach,这给出了错误。这是在其他系统上工作正常的相同代码。我只是复制了我的系统。

是否是由于我试图用泡菜写的内容?

完整代码 - https://gist.github.com/anonymous/b0e84df9a05f165ea2dd

def train(self):
    global pos, neg, totals
    retrain = False

    # Load counts if they already exist.
    if not retrain and os.path.isfile(CDATA_FILE):
        pos, neg, totals = cPickle.load(open(CDATA_FILE))
        return

    limit = 12500
    for file in os.listdir("./aclImdb/train/pos")[:limit]:
        for word in set(self.negate_sequence(open("./aclImdb/train/pos/" + file).read())):
            print word

            pos[word] += 1
            neg['not_' + word] += 1
    for file in os.listdir("./aclImdb/train/neg")[:limit]:
        for word in set(self.negate_sequence(open("./aclImdb/train/neg/" + file).read())):
            neg[word] += 1
            pos['not_' + word] += 1

    self.prune_features()

    totals[0] = sum(pos.values())
    totals[1] = sum(neg.values())

    countdata = (pos, neg, totals)
    cPickle.dump(countdata, open(CDATA_FILE, 'w'))

错误:

Traceback (most recent call last):
  File "sentiment_worker.py", line 146, in <module>
    MyDict().gearman_worker.work()
  File "sentiment_worker.py", line 28, in __init__
    self.train()
  File "sentiment_worker.py", line 91, in train
    cPickle.dump(countdata, open(CDATA_FILE, 'w'))
  File "/usr/lib/python2.7/copy_reg.py", line 70, in _reduce_ex
    raise TypeError, "can't pickle %s objects" % base.__name__
TypeError: can't pickle function objects

0 个答案:

没有答案