这是一个我怀疑很常见的问题,但我还没有找到解决方案.我想要的是非常简单,看似技术上可行:我有一个简单的python类,我想将它存储在光盘,实例和定义中,在一个文件中.Pickle将存储数据,但它不存储类定义.有人可能会说类定义已存储在我的.py文件中,但我不想要一个单独的.py文件; 我的目标是拥有一个自包含的单个文件,我可以用一行代码弹回我的命名空间.
所以,是的,我知道这可能使用两个文件和两行代码,但我希望它在一个文件和一行代码中.原因是因为我经常发现自己处于这种状况; 我正在研究一些大数据集,在python中操作它,然后将我的切片,切块和转换后的数据写回一些预先存在的目录结构.我不想要的是将这些数据目录与错误命名的python类存根一起丢弃以保持我的代码和数据相关联,而我想要的更少是跟踪和组织定义的所有这些小的ad hoc类的麻烦在脚本中独立运行.
因此,代码可读性方面的便利性不是很大,而是代码和数据之间的轻松和不可理解的关联.这对我来说似乎是一个有价值的目标,尽管我知道它在大多数情况下都不合适.
所以问题是:是否有一个包或代码片段做了这样的事情,因为我似乎找不到任何东西.
如果你使用dill
它,它可以让你__main__
像对待python模块一样(大多数情况下).因此,您可以序列化交互式定义的类等. dill
(默认情况下)也可以将类定义作为pickle的一部分传输.
>>> 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)
...
>>>
Run Code Online (Sandbox Code Playgroud)
然后关闭解释器,并test.pkl
通过TCP 发送文件.在远程计算机上,现在可以获取类实例.
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
>>>
Run Code Online (Sandbox Code Playgroud)
但是如何获得类定义?所以这不是你想要的.但是,以下是.
>>> 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)
...
>>>
Run Code Online (Sandbox Code Playgroud)
然后在发送文件后...您可以获得类定义.
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
Run Code Online (Sandbox Code Playgroud)
既然你正在寻找一个班轮,我可以做得更好.我没有表明你可以同时发送课程和实例,也许这就是你想要的.
>>> 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)
...
>>>
Run Code Online (Sandbox Code Playgroud)
它仍然不是一条线,但它有效.
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
Run Code Online (Sandbox Code Playgroud)
因此,在内部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
Run Code Online (Sandbox Code Playgroud)
所以这不是"完美的"(或者可能不是预期的那样)......但是它确实为动态构建的方法及其依赖性序列化了代码.你只是没有得到课程的其余部分 - 但在这种情况下不需要课程的其余部分.不过,它似乎并不像你想要的那样.
如果你想得到所有东西,你可以腌制整个会话.并在一行(两个计数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')
>>>
Run Code Online (Sandbox Code Playgroud)
然后在远程机器上......
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
Run Code Online (Sandbox Code Playgroud)
最后,如果您希望透明地(而不是使用文件)为您"完成"传输,您可以使用pathos.pp
或ppft
,它提供将对象发送到第二个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]
Run Code Online (Sandbox Code Playgroud)
该servers
参数是可选的,这里只是连接到本地计算机上的端口1234
...但如果你使用的远程计算机的名称和端口,而不是(或为好),你会断火到远程计算机- "毫不费力".
获取dill
,pathos
和ppft
这里:https: //github.com/uqfoundation
归档时间: |
|
查看次数: |
2951 次 |
最近记录: |