挑选一个类定义

Gio*_*gio 8 python pickle

有没有办法挑选一个类定义?

我想做的是挑选定义(可以动态创建),然后通过TCP连接发送它,以便可以在另一端创建实例.

我知道可能存在依赖关系,例如类所依赖的模块和全局变量.我也希望在酸洗过程中捆绑这些,但我并不担心自动检测依赖关系,因为如果用户有责任指定它们,那就没关系.

Ale*_*lli 6

唉,不是直接。您可以发送语句的字符串形式class或字节码形式,并exec在接收端使用“重新水化”它。


Mik*_*rns 5

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

>>> 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)

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

>>> 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 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
>>> 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
>>> dill.load_session('foo.pkl')
>>> b.x
11
>>> b.bar(2)
>>> b.x
15
>>> foo(3)
9
Run Code Online (Sandbox Code Playgroud)

最后,如果您希望透明地为您“完成”传输,您可以使用pathos.ppor 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...但如果你使用的远程计算机的名称和端口,而不是(或为好),你会断火到远程计算机- “毫不费力”。

获取dillpathosppft这里: https://github.com/uqfoundation