繁体   English   中英

Pickle class 实例加定义?

[英]Pickle class instance plus definition?

这是一个我怀疑很常见的问题,但我还没有找到解决方案。 我想要的很简单,而且在技术上似乎可行:我有一个简单的 python class,我想将它存储在磁盘上,实例和定义,在一个文件中。 Pickle 将存储数据,但不存储 class 定义。 有人可能会争辩说 class 定义已经存储在 my.py 文件中,但我不想要单独的.py 文件; 我的目标是拥有一个独立的单个文件,我可以用一行代码将它弹回我的命名空间。

所以是的,我知道这可能使用两个文件和两行代码,但我希望它在一个文件和一行代码中。 原因是因为我经常发现自己处于这种情况; 我正在处理一些大数据集,在 python 中对其进行操作,然后必须将切片、切块和转换后的数据写回到一些预先存在的目录结构中。 我不想在这些数据目录中乱扔乱七八糟的 python class 存根以保持我的代码和数据相关联,而我更想要的是跟踪和组织所有这些定义的小临时类的麻烦在脚本中独立运行。

因此,便利性不在于代码的可读性,而在于代码和数据之间毫不费力且不可篡改的关联。 这对我来说似乎是一个有价值的目标,尽管我知道这在大多数情况下是不合适的。

所以问题是:有没有做这种事情的 package 或代码片段,因为我似乎找不到任何东西。

如果您使用dill ,它可以让您将__main__视为 python 模块(在大多数情况下)。 因此,您可以序列化交互式定义的类等。 dill也(默认情况下)可以传输 class 定义作为泡菜的一部分。

>>> class MyTest(object):
...   def foo(self, x):
...     return self.x * x
...   x = 4
... 
>>> f = MyTest() 
>>> import dill
>>>
>>> with open('test.pkl', 'wb') as s:
...   dill.dump(f, s)
... 
>>> 

然后关闭解释器,并通过 TCP 发送文件test.pkl 在您的远程机器上,现在您可以获得 class 实例。

Python 2.7.9 (default, Dec 11 2014, 01:21:43) 
[GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import dill
>>> with open('test.pkl', 'rb') as s:
...   f = dill.load(s)
... 
>>> f
<__main__.MyTest object at 0x1069348d0>
>>> f.x
4
>>> f.foo(2)
8
>>>             

但是如何获得 class 定义? 所以这不是你想要的。 然而,以下是。

>>> class MyTest2(object):
...   def bar(self, x):
...     return x*x + self.x
...   x = 1
... 
>>> import dill
>>> with open('test2.pkl', 'wb') as s:
...   dill.dump(MyTest2, s)
... 
>>>

然后发送文件后……你可以得到 class 定义。

Python 2.7.9 (default, Dec 11 2014, 01:21:43) 
[GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import dill
>>> with open('test2.pkl', 'rb') as s:
...   MyTest2 = dill.load(s)
... 
>>> print dill.source.getsource(MyTest2)
class MyTest2(object):
  def bar(self, x):
    return x*x + self.x
  x = 1

>>> f = MyTest2()
>>> f.x
1
>>> f.bar(4)
17

既然你在寻找一个班轮,我可以做得更好。 我没有显示您可以同时发送 class 和实例,也许这就是您想要的。

>>> import dill
>>> class Foo(object): 
...   def bar(self, x):
...     return x+self.x
...   x = 1
... 
>>> b = Foo()
>>> b.x = 5
>>> 
>>> with open('blah.pkl', 'wb') as s:
...   dill.dump((Foo, b), s)
... 
>>> 

它仍然不是一条线,但是,它可以工作。

Python 2.7.9 (default, Dec 11 2014, 01:21:43) 
[GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import dill
>>> with open('blah.pkl', 'rb') as s:
...   Foo, b = dill.load(s)
... 
>>> b.x  
5
>>> Foo.bar(b, 2)
7

因此,在dill中,有dill.source ,它具有可以检测函数和类的依赖关系的方法,并将它们与 pickle 一起使用(大部分情况下)。

>>> def foo(x):
...   return x*x
... 
>>> class Bar(object):
...   def zap(self, x):
...     return foo(x) * self.x
...   x = 3
... 
>>> print dill.source.importable(Bar.zap, source=True)
def foo(x):
  return x*x
def zap(self, x):
  return foo(x) * self.x

所以这不是“完美的”(或者可能不是预期的)......但它确实序列化了动态构建方法的代码及其依赖项。 您只是没有得到 class 的 rest - 但在这种情况下不需要 ZA2F2ED4F8DCECC2CBB4DZ21A 的 rest。 不过,这似乎不是你想要的。

如果你想得到所有东西,你可以腌制整个 session。 并在一行中(两个计算import )。

>>> import dill
>>> def foo(x):
...   return x*x
... 
>>> class Blah(object):
...   def bar(self, x):
...     self.x = (lambda x:foo(x)+self.x)(x)
...   x = 2
... 
>>> b = Blah()
>>> b.x
2
>>> b.bar(3)
>>> b.x
11
>>> # the one line
>>> dill.dump_session('foo.pkl')
>>> 

然后在远程机器上...

Python 2.7.9 (default, Dec 11 2014, 01:21:43) 
[GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import dill
>>> # the one line
>>> dill.load_session('foo.pkl')
>>> b.x
11
>>> b.bar(2)
>>> b.x
15
>>> foo(3)
9

最后,如果您希望透明地为您“完成”传输(而不是使用文件),您可以使用pathos.ppppft ,它们提供将对象发送到第二个 python 服务器(在远程计算机上)的能力或 python 进程。 他们在引擎盖下使用dill ,只需通过电线传递代码。

>>> class More(object):
...   def squared(self, x):
...     return x*x
... 
>>> import pathos
>>> 
>>> p = pathos.pp.ParallelPythonPool(servers=('localhost,1234',))
>>> 
>>> m = More()
>>> p.map(m.squared, range(5))
[0, 1, 4, 9, 16]

servers参数是可选的,这里只是连接到端口1234上的本地机器……但是如果您使用远程机器名称和端口代替(或同样),您将启动到远程机器——“毫不费力” .

在此处获取dillpathosppfthttps://github.com/uqfoundation

泡菜不能泡菜 python 代码,所以我认为泡菜根本不可能。

>>> from pickle import *
>>> def A(object):
...     def __init__(self):
...             self.potato = "Hello"
...             print "Starting"
...                                                                                                                                                                  
>>> A.__code__                                                                                                                                                       
<code object A at 0xb76bc0b0, file "<stdin>", line 1>                                                                                                                
>>> dumps(A.__code__)                                                                                                                                                
Traceback (most recent call last):                                                                                                                                   
  File "<stdin>", line 1, in <module>                                                                                                                                
  File "/usr/lib/python2.6/pickle.py", line 1366, in dumps
    Pickler(file, protocol).dump(obj)
  File "/usr/lib/python2.6/pickle.py", line 224, in dump
    self.save(obj)
  File "/usr/lib/python2.6/pickle.py", line 306, in save
    rv = reduce(self.proto)
  File "/usr/lib/python2.6/copy_reg.py", line 70, in _reduce_ex
    raise TypeError, "can't pickle %s objects" % base.__name__
TypeError: can't pickle code objects

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM