Skip to content
Browse files

add Reference object

  • Loading branch information...
1 parent a0bfb1f commit 7c24f2e09e75c8b81475b857f1aa130aff41b59a @minrk minrk committed Mar 7, 2011
Showing with 24 additions and 8 deletions.
  1. +23 −8 IPython/utils/pickleutil.py
  2. +1 −0 IPython/zmq/parallel/client.py
View
31 IPython/utils/pickleutil.py
@@ -42,7 +42,24 @@ def getObject(self, g=None):
setattr(self.obj, key, uncan(getattr(self.obj, key), g))
return self.obj
-
+class Reference(CannedObject):
+ """object for wrapping a remote reference by name."""
+ def __init__(self, name):
+ if not isinstance(name, basestring):
+ raise TypeError("illegal name: %r"%name)
+ self.name = name
+
+ def __repr__(self):
+ return "<Reference: %r>"%self.name
+
+ def getObject(self, g=None):
+ if g is None:
+ g = globals()
+ try:
+ return g[self.name]
+ except KeyError:
+ raise NameError("name %r is not defined"%self.name)
+
class CannedFunction(CannedObject):
@@ -54,7 +71,7 @@ def __init__(self, f):
def _checkType(self, obj):
assert isinstance(obj, FunctionType), "Not a function type"
- def getFunction(self, g=None):
+ def getObject(self, g=None):
if g is None:
g = globals()
newFunc = FunctionType(self.code, g)
@@ -66,11 +83,11 @@ def getFunction(self, g=None):
def can(obj):
- if isinstance(obj, FunctionType):
- return CannedFunction(obj)
- elif isinstance(obj, dependent):
+ if isinstance(obj, dependent):
keys = ('f','df')
return CannedObject(obj, keys=keys)
+ elif isinstance(obj, FunctionType):
+ return CannedFunction(obj)
elif isinstance(obj,dict):
return canDict(obj)
elif isinstance(obj, (list,tuple)):
@@ -95,9 +112,7 @@ def canSequence(obj):
return obj
def uncan(obj, g=None):
- if isinstance(obj, CannedFunction):
- return obj.getFunction(g)
- elif isinstance(obj, CannedObject):
+ if isinstance(obj, CannedObject):
return obj.getObject(g)
elif isinstance(obj,dict):
return uncanDict(obj, g)
View
1 IPython/zmq/parallel/client.py
@@ -24,6 +24,7 @@
# from zmq.eventloop import ioloop, zmqstream
from IPython.utils.path import get_ipython_dir
+from IPython.utils.pickleutil import Reference
from IPython.utils.traitlets import (HasTraits, Int, Instance, CUnicode,
Dict, List, Bool, Str, Set)
from IPython.external.decorator import decorator

0 comments on commit 7c24f2e

Please sign in to comment.
Something went wrong with that request. Please try again.