Skip to content

Commit

Permalink
Parallel: Support get/set of nested objects in view (e.g. dv['a.b'])
Browse files Browse the repository at this point in the history
  • Loading branch information
bfroehle committed Aug 22, 2012
1 parent 5308c36 commit e6b063b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
18 changes: 18 additions & 0 deletions IPython/parallel/tests/test_view.py
Expand Up @@ -675,4 +675,22 @@ def test_map_ref(self):
drank = amr.get(5)
self.assertEqual(drank, [ r*2 for r in ranks ])

def test_nested_getitem_setitem(self):
"""get and set with view['a.b']"""
view = self.client[-1]
view.execute('\n'.join([
'class A(object): pass',
'a = A()',
'a.b = 128',
]), block=True)
ra = pmod.Reference('a')

r = view.apply_sync(lambda x: x.b, ra)
self.assertEqual(r, 128)
self.assertEqual(view['a.b'], 128)

view['a.b'] = 0

r = view.apply_sync(lambda x: x.b, ra)
self.assertEqual(r, 0)
self.assertEqual(view['a.b'], 0)
21 changes: 12 additions & 9 deletions IPython/parallel/util.py
Expand Up @@ -229,21 +229,24 @@ def interactive(f):
@interactive
def _push(**ns):
"""helper method for implementing `client.push` via `client.apply`"""
globals().update(ns)
user_ns = globals()
tmp = '_IP_PUSH_TMP_'
while tmp in user_ns:
tmp = tmp + '_'
try:
for name, value in ns.iteritems():
user_ns[tmp] = value
exec "%s = %s" % (name, tmp) in user_ns
finally:
user_ns.pop(tmp, None)

@interactive
def _pull(keys):
"""helper method for implementing `client.pull` via `client.apply`"""
user_ns = globals()
if isinstance(keys, (list,tuple, set)):
for key in keys:
if key not in user_ns:
raise NameError("name '%s' is not defined"%key)
return map(user_ns.get, keys)
return map(lambda key: eval(key, globals()), keys)
else:
if keys not in user_ns:
raise NameError("name '%s' is not defined"%keys)
return user_ns.get(keys)
return eval(keys, globals())

@interactive
def _execute(code):
Expand Down

0 comments on commit e6b063b

Please sign in to comment.