腌菜cython类 [英] pickle cython class

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

问题描述

我必须保存并加载一个cython类实例。
我的cython类是这个加上几个方法:

I have to save and load a cython class instance. My cython class is this plus several methods:

import numpy as np
cimport numpy as np
cimport cython    
cdef class Perceptron_avg_my:
    cdef int wlen,freePos
    cdef np.ndarray w,wtot,wac,wtotc #np.ndarray[np.int32_t]
    cdef np.ndarray wmean  #np.ndarray[np.float32_t]    
    cdef public dict fpos    

    def __cinit__(self,np.int64_t wlen=4*10**7):
        self.fpos= dict()
        self.freePos=1
        self.wlen=wlen
        self.w=np.zeros(wlen,np.int32)
        self.wtot=np.zeros(wlen,np.int32)
        self.wac=np.zeros(wlen,np.int32)
        self.wtotc=np.zeros(wlen,np.int32)
        self.wmean=np.zeros(wlen,np.float32)

    cpdef evaluate_noavg(self,list f):
        cdef np.ndarray[np.int32_t] w = self.w
        cdef dict fpos = self.fpos        
        cdef bytes ff
        cdef int i
        cdef long int score=0

        for ff in f:
            i=fpos.get(ff,0)  
            if i != 0: 
                score += w[i]
        return score

我在想使用cPickle模块。
我理解我必须实现__reduce __(self)方法,但我有一些问题,以找到一个例子,并理解文档

I was thinking to use the cPickle module. I understand that I have to implement a __reduce__(self) method but I have some problem to find an example and to understand well the documentation

我试图添加类似这样的Perceptron_avg_my但不工作:

I tried to add something like this to Perceptron_avg_my but not works:

    def rebuild(self,l):
        self.fpos=l[0]
        self.freePos=l[1]

    def __reduce__(self):
        #print 'reduce call'
        return (Perceptron_avg_my.rebuild,(self.fpos,self.freePos))

有任何建议吗?
非常感谢!

any suggestions? Thanks a lot!!!

推荐答案

我不知道你是否找到它,有有关选择扩展类型的部分

I don't know if you found it, but the official Python documentation has a section on pickling extension types.

我想你在这里有三个问题。首先, __ reduce __ 返回的函数应该从头创建一个新对象并返回,而你的 rebuild 函数只是设置一些属性。其次, __ reduce __ 返回的元组本身必须是可拾取的,作为一个方法, Perceptron_avg_my.rebuild 我认为这是预期在python 3.3或3.4中修复)。相反,您可以将其转换为模块级函数。最后,参数(self.fpos,self.freePos)单独传递给 rebuild

I think you have three problems here. Firstly, the function returned by __reduce__ is supposed to create a new object from scratch and return it, whereas your rebuild function just sets some attributes. Secondly, the tuple returned by __reduce__ must itself be picklable, and as a method, Perceptron_avg_my.rebuild is not picklable (I think this is expected to be fixed in python 3.3 or 3.4). Instead, you could turn it into a module-level function. Finally, the arguments (self.fpos,self.freePos) are passed to rebuild individually - you don't have to unpack the tuple yourself.

以下似乎适用于我(虽然你可能想存储其他属性的值,否则它们只会有初始值 __ init __ 设置的值:

The following seems to work for me (though you probably want to store the values of the other attributes too, otherwise they will just have the initial values set by __init__):

#inside the class definition
def __reduce__(self):
    return (rebuild, (self.wlen, self.fpos, self.freePos))

#standalone function
def rebuild(wlen, fpos, freePos):
    p = Perceptron_avg_my(wlen)
    p.fpos = fpos
    p.freePos = freePos
    return p

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

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