Show More
@@ -37,21 +37,21 Usage | |||
|
37 | 37 | import ast |
|
38 | 38 | import re |
|
39 | 39 | |
|
40 | from IPython.core.magic import Magics, register_magics, line_magic | |
|
40 | 41 | from IPython.core.plugin import Plugin |
|
41 | from IPython.utils.traitlets import Bool, Any, Instance | |
|
42 | 42 | from IPython.testing.skipdoctest import skip_doctest |
|
43 | from IPython.utils.traitlets import Bool, Any, Instance | |
|
43 | 44 | |
|
44 | 45 | #----------------------------------------------------------------------------- |
|
45 | 46 | # Definitions of magic functions for use with IPython |
|
46 | 47 | #----------------------------------------------------------------------------- |
|
47 | 48 | |
|
48 | ||
|
49 | 49 | NO_ACTIVE_VIEW = """ |
|
50 | 50 | Use activate() on a DirectView object to activate it for magics. |
|
51 | 51 | """ |
|
52 | 52 | |
|
53 | 53 | |
|
54 |
class Paralle |
|
|
54 | class ParallelTricks(Plugin): | |
|
55 | 55 | """A component to manage the %result, %px and %autopx magics.""" |
|
56 | 56 | |
|
57 | 57 | active_view = Instance('IPython.parallel.client.view.DirectView') |
@@ -59,19 +59,22 class ParalleMagic(Plugin): | |||
|
59 | 59 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC') |
|
60 | 60 | |
|
61 | 61 | def __init__(self, shell=None, config=None): |
|
62 |
super(Paralle |
|
|
63 |
self. |
|
|
62 | super(ParallelTricks, self).__init__(shell=shell, config=config) | |
|
63 | self.shell.register_magics(ParallelMagics) | |
|
64 | ||
|
65 | ||
|
66 | @register_magics | |
|
67 | class ParallelMagics(Magics): | |
|
68 | """A set of magics useful when controlling a parallel IPython cluster. | |
|
69 | """ | |
|
70 | def __init__(self, shell): | |
|
71 | super(ParallelMagics, self).__init__(shell) | |
|
64 | 72 | # A flag showing if autopx is activated or not |
|
65 | 73 | self.autopx = False |
|
66 | 74 | |
|
67 | def _define_magics(self): | |
|
68 | """Define the magic functions.""" | |
|
69 | self.shell.define_magic('result', self.magic_result) | |
|
70 | self.shell.define_magic('px', self.magic_px) | |
|
71 | self.shell.define_magic('autopx', self.magic_autopx) | |
|
72 | ||
|
73 | 75 | @skip_doctest |
|
74 | def magic_result(self, ipself, parameter_s=''): | |
|
76 | @line_magic | |
|
77 | def result(self, parameter_s=''): | |
|
75 | 78 | """Print the result of command i on all engines.. |
|
76 | 79 | |
|
77 | 80 | To use this a :class:`DirectView` instance must be created |
@@ -103,7 +106,8 class ParalleMagic(Plugin): | |||
|
103 | 106 | return result |
|
104 | 107 | |
|
105 | 108 | @skip_doctest |
|
106 | def magic_px(self, ipself, parameter_s=''): | |
|
109 | @line_magic | |
|
110 | def px(self, parameter_s=''): | |
|
107 | 111 | """Executes the given python command in parallel. |
|
108 | 112 | |
|
109 | 113 | To use this a :class:`DirectView` instance must be created |
@@ -129,7 +133,8 class ParalleMagic(Plugin): | |||
|
129 | 133 | self._maybe_display_output(result) |
|
130 | 134 | |
|
131 | 135 | @skip_doctest |
|
132 | def magic_autopx(self, ipself, parameter_s=''): | |
|
136 | @line_magic | |
|
137 | def autopx(self, parameter_s=''): | |
|
133 | 138 | """Toggles auto parallel mode. |
|
134 | 139 | |
|
135 | 140 | To use this a :class:`DirectView` instance must be created |
@@ -282,8 +287,9 class ParalleMagic(Plugin): | |||
|
282 | 287 | """ |
|
283 | 288 | ipself = self.shell |
|
284 | 289 | # check code object for the autopx magic |
|
285 |
if 'get_ipython' in code_obj.co_names and 'magic' in code_obj.co_names |
|
|
286 |
|
|
|
290 | if 'get_ipython' in code_obj.co_names and 'magic' in code_obj.co_names \ | |
|
291 | and any( [ isinstance(c, basestring) and 'autopx' in c | |
|
292 | for c in code_obj.co_consts ]): | |
|
287 | 293 | self._disable_autopx() |
|
288 | 294 | return False |
|
289 | 295 | else: |
@@ -305,11 +311,11 class ParalleMagic(Plugin): | |||
|
305 | 311 | |
|
306 | 312 | |
|
307 | 313 | __doc__ = __doc__.replace('@AUTOPX_DOC@', |
|
308 |
" " + Paralle |
|
|
314 | " " + ParallelTricks.magic_autopx.__doc__) | |
|
309 | 315 | __doc__ = __doc__.replace('@PX_DOC@', |
|
310 |
" " + Paralle |
|
|
316 | " " + ParallelTricks.magic_px.__doc__) | |
|
311 | 317 | __doc__ = __doc__.replace('@RESULT_DOC@', |
|
312 |
" " + Paralle |
|
|
318 | " " + ParallelTricks.magic_result.__doc__) | |
|
313 | 319 | |
|
314 | 320 | |
|
315 | 321 | _loaded = False |
@@ -319,7 +325,6 def load_ipython_extension(ip): | |||
|
319 | 325 | """Load the extension in IPython.""" |
|
320 | 326 | global _loaded |
|
321 | 327 | if not _loaded: |
|
322 |
plugin = Paralle |
|
|
328 | plugin = ParallelTricks(shell=ip, config=ip.config) | |
|
323 | 329 | ip.plugin_manager.register_plugin('parallelmagic', plugin) |
|
324 | 330 | _loaded = True |
|
325 |
General Comments 0
You need to be logged in to leave comments.
Login now