酸洗和解酸用户定义的类 [英] pickling and unpickling user-defined class

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

问题描述

我有一个用户定义的类'myclass',我使用pickle模块将其存储在文件中,但是我在解开它方面遇到了问题.我有大约20个相同结构的不同实例,我将它们保存在不同的文件中.当我读取每个文件时,出现错误时,代码将对某些文件起作用,而对其他文件则无效:

I have a user-defined class 'myclass' that I store on file with the pickle module, but I am having problem unpickling it. I have about 20 distinct instances of the same structure, that I save in distinct files. When I read each file, the code works on some files and not on others, when I get the error:

'module' object has no attribute 'myclass'

我今天生成了一些文件,昨天生成了其他文件,并且我的代码仅适用于今天生成的文件(在昨天和今天之间我没有更改类定义).

I have generated some files today and some other yesterday, and my code only works on the files generated today (I have NOT changed class definition between yesterday and today).

我想知道我的方法是否不健壮,是否没有按应做的方式做事,例如我可能无法腌制用户定义的类,以及是否在过程中引入了一些随机性.

I was wondering if maybe my method is not robust, if I am not doing things as I should do, for example maybe I cannot pickled user-defined class, and if this is introducing some randomness in the process.

另一个问题可能是,我昨天生成的文件是在另一台机器上生成的---因为我在学术集群上工作,所以我有一些登录节点和一些计算节点,它们在体系结构上有所不同.因此,我在计算节点上生成了昨天的文件,在登录节点上生成了今天的文件,并且正在读取登录节点上的所有内容.

Another issue could be that the files that I generated yesterday were generated on a different machine --- because I work on an academic cluster, I have some login nodes and some computing nodes, that differs by architecture. So I generated yesterday files on the computing nodes, and today files on the login nodes, and I am reading everything on the login nodes.

如某些注释中所建议,我已经安装了dill并将其加载到import dill as pickle中.现在,我可以将文件从计算节点读取到同一群集的登录节点.但是,如果我尝试读取在一个群集的计算节点上生成的文件,则无法在另一个群集的登录节点上读取文件.我在dill.py的_load_type(name)中得到KeyError: 'ClassType'

As suggested in some of the comments, I have installed dill and loaded it with import dill as pickle. Now I can read the files from computing nodes to login nodes of the same cluster. But if I try to read the files generated on the computing node of one cluster, on the login node of another cluster I cannot. I get KeyError: 'ClassType' in _load_type(name) in dill.py

难道是因为python版本不同吗?我已经使用python2.7生成了文件,并使用python3.3读取了它们.

Can it be because the python version is different? I have generated the files with python2.7 and I read them with python3.3.

如果我在所有地方使用python 2.7,我都可以读取腌制的文件.不幸的是,我用python 3编写的部分代码无法自动与python 2.7兼容:(

I can read the pickled files, if I use everywhere python 2.7. Sadly, part of my code, written in python 3, is not automatically compatible with python 2.7 :(

推荐答案

可以from mymodule import myclass吗?腌制不会使该类腌制,而只是对它的引用.要加载一个腌制的对象,python必须能够找到用于创建该对象的类.

Can you from mymodule import myclass? Pickling does not pickle the class, just a reference to it. To load a pickled object python must be able to find the class that was to be used to create the object.

例如

import pickle

class A(object):
    pass

obj = A()
pickled = pickle.dumps(obj)

_A = A; del A # hide class

try:
    pickle.loads(pickled)
except AttributeError as e:
    print(e)

A = _A # unhide class
print(pickle.loads(pickled))

这篇关于酸洗和解酸用户定义的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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