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