Show More
@@ -0,0 +1,40 b'' | |||||
|
1 | """Implementation of all the magic functions built into IPython. | |||
|
2 | """ | |||
|
3 | #----------------------------------------------------------------------------- | |||
|
4 | # Copyright (c) 2012 The IPython Development Team. | |||
|
5 | # | |||
|
6 | # Distributed under the terms of the Modified BSD License. | |||
|
7 | # | |||
|
8 | # The full license is in the file COPYING.txt, distributed with this software. | |||
|
9 | #----------------------------------------------------------------------------- | |||
|
10 | ||||
|
11 | #----------------------------------------------------------------------------- | |||
|
12 | # Imports | |||
|
13 | #----------------------------------------------------------------------------- | |||
|
14 | ||||
|
15 | from ..magic import Magics, magics_class | |||
|
16 | from .auto import AutoMagics | |||
|
17 | from .basic import BasicMagics | |||
|
18 | from .code import CodeMagics, MacroToEdit | |||
|
19 | from .config import ConfigMagics | |||
|
20 | from .deprecated import DeprecatedMagics | |||
|
21 | from .execution import ExecutionMagics | |||
|
22 | from .extension import ExtensionMagics | |||
|
23 | from .history import HistoryMagics | |||
|
24 | from .logging import LoggingMagics | |||
|
25 | from .namespace import NamespaceMagics | |||
|
26 | from .osm import OSMagics | |||
|
27 | from .pylab import PylabMagics | |||
|
28 | ||||
|
29 | #----------------------------------------------------------------------------- | |||
|
30 | # Magic implementation classes | |||
|
31 | #----------------------------------------------------------------------------- | |||
|
32 | ||||
|
33 | @magics_class | |||
|
34 | class UserMagics(Magics): | |||
|
35 | """Placeholder for user-defined magics to be added at runtime. | |||
|
36 | ||||
|
37 | All magics are eventually merged into a single namespace at runtime, but we | |||
|
38 | use this class to isolate the magics defined dynamically by the user into | |||
|
39 | their own class. | |||
|
40 | """ |
@@ -0,0 +1,128 b'' | |||||
|
1 | """Implementation of magic functions that control various automatic behaviors. | |||
|
2 | """ | |||
|
3 | #----------------------------------------------------------------------------- | |||
|
4 | # Copyright (c) 2012 The IPython Development Team. | |||
|
5 | # | |||
|
6 | # Distributed under the terms of the Modified BSD License. | |||
|
7 | # | |||
|
8 | # The full license is in the file COPYING.txt, distributed with this software. | |||
|
9 | #----------------------------------------------------------------------------- | |||
|
10 | ||||
|
11 | #----------------------------------------------------------------------------- | |||
|
12 | # Imports | |||
|
13 | #----------------------------------------------------------------------------- | |||
|
14 | ||||
|
15 | # Our own packages | |||
|
16 | from IPython.core.magic import Bunch, Magics, magics_class, line_magic | |||
|
17 | from IPython.testing.skipdoctest import skip_doctest | |||
|
18 | from IPython.utils.warn import error | |||
|
19 | ||||
|
20 | #----------------------------------------------------------------------------- | |||
|
21 | # Magic implementation classes | |||
|
22 | #----------------------------------------------------------------------------- | |||
|
23 | ||||
|
24 | @magics_class | |||
|
25 | class AutoMagics(Magics): | |||
|
26 | """Magics that control various autoX behaviors.""" | |||
|
27 | ||||
|
28 | def __init__(self, shell): | |||
|
29 | super(AutoMagics, self).__init__(shell) | |||
|
30 | # namespace for holding state we may need | |||
|
31 | self._magic_state = Bunch() | |||
|
32 | ||||
|
33 | @line_magic | |||
|
34 | def automagic(self, parameter_s=''): | |||
|
35 | """Make magic functions callable without having to type the initial %. | |||
|
36 | ||||
|
37 | Without argumentsl toggles on/off (when off, you must call it as | |||
|
38 | %automagic, of course). With arguments it sets the value, and you can | |||
|
39 | use any of (case insensitive): | |||
|
40 | ||||
|
41 | - on, 1, True: to activate | |||
|
42 | ||||
|
43 | - off, 0, False: to deactivate. | |||
|
44 | ||||
|
45 | Note that magic functions have lowest priority, so if there's a | |||
|
46 | variable whose name collides with that of a magic fn, automagic won't | |||
|
47 | work for that function (you get the variable instead). However, if you | |||
|
48 | delete the variable (del var), the previously shadowed magic function | |||
|
49 | becomes visible to automagic again.""" | |||
|
50 | ||||
|
51 | arg = parameter_s.lower() | |||
|
52 | mman = self.shell.magics_manager | |||
|
53 | if arg in ('on', '1', 'true'): | |||
|
54 | val = True | |||
|
55 | elif arg in ('off', '0', 'false'): | |||
|
56 | val = False | |||
|
57 | else: | |||
|
58 | val = not mman.auto_magic | |||
|
59 | mman.auto_magic = val | |||
|
60 | print '\n' + self.shell.magics_manager.auto_status() | |||
|
61 | ||||
|
62 | @skip_doctest | |||
|
63 | @line_magic | |||
|
64 | def autocall(self, parameter_s=''): | |||
|
65 | """Make functions callable without having to type parentheses. | |||
|
66 | ||||
|
67 | Usage: | |||
|
68 | ||||
|
69 | %autocall [mode] | |||
|
70 | ||||
|
71 | The mode can be one of: 0->Off, 1->Smart, 2->Full. If not given, the | |||
|
72 | value is toggled on and off (remembering the previous state). | |||
|
73 | ||||
|
74 | In more detail, these values mean: | |||
|
75 | ||||
|
76 | 0 -> fully disabled | |||
|
77 | ||||
|
78 | 1 -> active, but do not apply if there are no arguments on the line. | |||
|
79 | ||||
|
80 | In this mode, you get:: | |||
|
81 | ||||
|
82 | In [1]: callable | |||
|
83 | Out[1]: <built-in function callable> | |||
|
84 | ||||
|
85 | In [2]: callable 'hello' | |||
|
86 | ------> callable('hello') | |||
|
87 | Out[2]: False | |||
|
88 | ||||
|
89 | 2 -> Active always. Even if no arguments are present, the callable | |||
|
90 | object is called:: | |||
|
91 | ||||
|
92 | In [2]: float | |||
|
93 | ------> float() | |||
|
94 | Out[2]: 0.0 | |||
|
95 | ||||
|
96 | Note that even with autocall off, you can still use '/' at the start of | |||
|
97 | a line to treat the first argument on the command line as a function | |||
|
98 | and add parentheses to it:: | |||
|
99 | ||||
|
100 | In [8]: /str 43 | |||
|
101 | ------> str(43) | |||
|
102 | Out[8]: '43' | |||
|
103 | ||||
|
104 | # all-random (note for auto-testing) | |||
|
105 | """ | |||
|
106 | ||||
|
107 | if parameter_s: | |||
|
108 | arg = int(parameter_s) | |||
|
109 | else: | |||
|
110 | arg = 'toggle' | |||
|
111 | ||||
|
112 | if not arg in (0, 1, 2, 'toggle'): | |||
|
113 | error('Valid modes: (0->Off, 1->Smart, 2->Full') | |||
|
114 | return | |||
|
115 | ||||
|
116 | if arg in (0, 1, 2): | |||
|
117 | self.shell.autocall = arg | |||
|
118 | else: # toggle | |||
|
119 | if self.shell.autocall: | |||
|
120 | self._magic_state.autocall_save = self.shell.autocall | |||
|
121 | self.shell.autocall = 0 | |||
|
122 | else: | |||
|
123 | try: | |||
|
124 | self.shell.autocall = self._magic_state.autocall_save | |||
|
125 | except AttributeError: | |||
|
126 | self.shell.autocall = self._magic_state.autocall_save = 1 | |||
|
127 | ||||
|
128 | print "Automatic calling is:",['OFF','Smart','Full'][self.shell.autocall] |
This diff has been collapsed as it changes many lines, (538 lines changed) Show them Hide them | |||||
@@ -0,0 +1,538 b'' | |||||
|
1 | """Implementation of basic magic functions. | |||
|
2 | """ | |||
|
3 | #----------------------------------------------------------------------------- | |||
|
4 | # Copyright (c) 2012 The IPython Development Team. | |||
|
5 | # | |||
|
6 | # Distributed under the terms of the Modified BSD License. | |||
|
7 | # | |||
|
8 | # The full license is in the file COPYING.txt, distributed with this software. | |||
|
9 | #----------------------------------------------------------------------------- | |||
|
10 | ||||
|
11 | #----------------------------------------------------------------------------- | |||
|
12 | # Imports | |||
|
13 | #----------------------------------------------------------------------------- | |||
|
14 | from __future__ import print_function | |||
|
15 | ||||
|
16 | # Stdlib | |||
|
17 | import io | |||
|
18 | import sys | |||
|
19 | from pprint import pformat | |||
|
20 | ||||
|
21 | # Our own packages | |||
|
22 | from IPython.core.error import UsageError | |||
|
23 | from IPython.core.magic import Magics, magics_class, line_magic | |||
|
24 | from IPython.core.prefilter import ESC_MAGIC | |||
|
25 | from IPython.utils.text import format_screen | |||
|
26 | from IPython.core import magic_arguments, page | |||
|
27 | from IPython.testing.skipdoctest import skip_doctest | |||
|
28 | from IPython.utils.ipstruct import Struct | |||
|
29 | from IPython.utils.path import unquote_filename | |||
|
30 | from IPython.utils.warn import warn, error | |||
|
31 | ||||
|
32 | #----------------------------------------------------------------------------- | |||
|
33 | # Magics class implementation | |||
|
34 | #----------------------------------------------------------------------------- | |||
|
35 | ||||
|
36 | @magics_class | |||
|
37 | class BasicMagics(Magics): | |||
|
38 | """Magics that provide central IPython functionality. | |||
|
39 | ||||
|
40 | These are various magics that don't fit into specific categories but that | |||
|
41 | are all part of the base 'IPython experience'.""" | |||
|
42 | ||||
|
43 | def _lsmagic(self): | |||
|
44 | mesc = ESC_MAGIC | |||
|
45 | cesc = mesc*2 | |||
|
46 | mman = self.shell.magics_manager | |||
|
47 | magics = mman.lsmagic() | |||
|
48 | out = ['Available line magics:', | |||
|
49 | mesc + (' '+mesc).join(magics['line']), | |||
|
50 | '', | |||
|
51 | 'Available cell magics:', | |||
|
52 | cesc + (' '+cesc).join(magics['cell']), | |||
|
53 | '', | |||
|
54 | mman.auto_status()] | |||
|
55 | return '\n'.join(out) | |||
|
56 | ||||
|
57 | @line_magic | |||
|
58 | def lsmagic(self, parameter_s=''): | |||
|
59 | """List currently available magic functions.""" | |||
|
60 | print(self._lsmagic()) | |||
|
61 | ||||
|
62 | @line_magic | |||
|
63 | def magic(self, parameter_s=''): | |||
|
64 | """Print information about the magic function system. | |||
|
65 | ||||
|
66 | Supported formats: -latex, -brief, -rest | |||
|
67 | """ | |||
|
68 | ||||
|
69 | mode = '' | |||
|
70 | try: | |||
|
71 | mode = parameter_s.split()[0][1:] | |||
|
72 | if mode == 'rest': | |||
|
73 | rest_docs = [] | |||
|
74 | except IndexError: | |||
|
75 | pass | |||
|
76 | ||||
|
77 | magic_docs = [] | |||
|
78 | escapes = dict(line=ESC_MAGIC, cell=ESC_MAGIC*2) | |||
|
79 | magics = self.shell.magics_manager.magics | |||
|
80 | ||||
|
81 | for mtype in ('line', 'cell'): | |||
|
82 | escape = escapes[mtype] | |||
|
83 | for fname, fn in magics[mtype].iteritems(): | |||
|
84 | ||||
|
85 | if mode == 'brief': | |||
|
86 | # only first line | |||
|
87 | if fn.__doc__: | |||
|
88 | fndoc = fn.__doc__.split('\n',1)[0] | |||
|
89 | else: | |||
|
90 | fndoc = 'No documentation' | |||
|
91 | else: | |||
|
92 | if fn.__doc__: | |||
|
93 | fndoc = fn.__doc__.rstrip() | |||
|
94 | else: | |||
|
95 | fndoc = 'No documentation' | |||
|
96 | ||||
|
97 | if mode == 'rest': | |||
|
98 | rest_docs.append('**%s%s**::\n\n\t%s\n\n' % | |||
|
99 | (escape, fname, fndoc)) | |||
|
100 | else: | |||
|
101 | magic_docs.append('%s%s:\n\t%s\n' % | |||
|
102 | (escape, fname, fndoc)) | |||
|
103 | ||||
|
104 | magic_docs = ''.join(magic_docs) | |||
|
105 | ||||
|
106 | if mode == 'rest': | |||
|
107 | return "".join(rest_docs) | |||
|
108 | ||||
|
109 | if mode == 'latex': | |||
|
110 | print(self.format_latex(magic_docs)) | |||
|
111 | return | |||
|
112 | else: | |||
|
113 | magic_docs = format_screen(magic_docs) | |||
|
114 | if mode == 'brief': | |||
|
115 | return magic_docs | |||
|
116 | ||||
|
117 | out = [""" | |||
|
118 | IPython's 'magic' functions | |||
|
119 | =========================== | |||
|
120 | ||||
|
121 | The magic function system provides a series of functions which allow you to | |||
|
122 | control the behavior of IPython itself, plus a lot of system-type | |||
|
123 | features. There are two kinds of magics, line-oriented and cell-oriented. | |||
|
124 | ||||
|
125 | Line magics are prefixed with the % character and work much like OS | |||
|
126 | command-line calls: they get as an argument the rest of the line, where | |||
|
127 | arguments are passed without parentheses or quotes. For example, this will | |||
|
128 | time the given statement:: | |||
|
129 | ||||
|
130 | %timeit range(1000) | |||
|
131 | ||||
|
132 | Cell magics are prefixed with a double %%, and they are functions that get as | |||
|
133 | an argument not only the rest of the line, but also the lines below it in a | |||
|
134 | separate argument. These magics are called with two arguments: the rest of the | |||
|
135 | call line and the body of the cell, consisting of the lines below the first. | |||
|
136 | For example:: | |||
|
137 | ||||
|
138 | %%timeit x = numpy.random.randn((100, 100)) | |||
|
139 | numpy.linalg.svd(x) | |||
|
140 | ||||
|
141 | will time the execution of the numpy svd routine, running the assignment of x | |||
|
142 | as part of the setup phase, which is not timed. | |||
|
143 | ||||
|
144 | In a line-oriented client (the terminal or Qt console IPython), starting a new | |||
|
145 | input with %% will automatically enter cell mode, and IPython will continue | |||
|
146 | reading input until a blank line is given. In the notebook, simply type the | |||
|
147 | whole cell as one entity, but keep in mind that the %% escape can only be at | |||
|
148 | the very start of the cell. | |||
|
149 | ||||
|
150 | NOTE: If you have 'automagic' enabled (via the command line option or with the | |||
|
151 | %automagic function), you don't need to type in the % explicitly for line | |||
|
152 | magics; cell magics always require an explicit '%%' escape. By default, | |||
|
153 | IPython ships with automagic on, so you should only rarely need the % escape. | |||
|
154 | ||||
|
155 | Example: typing '%cd mydir' (without the quotes) changes you working directory | |||
|
156 | to 'mydir', if it exists. | |||
|
157 | ||||
|
158 | For a list of the available magic functions, use %lsmagic. For a description | |||
|
159 | of any of them, type %magic_name?, e.g. '%cd?'. | |||
|
160 | ||||
|
161 | Currently the magic system has the following functions:""", | |||
|
162 | magic_docs, | |||
|
163 | "Summary of magic functions (from %slsmagic):", | |||
|
164 | self._lsmagic(), | |||
|
165 | ] | |||
|
166 | page.page('\n'.join(out)) | |||
|
167 | ||||
|
168 | ||||
|
169 | @line_magic | |||
|
170 | def page(self, parameter_s=''): | |||
|
171 | """Pretty print the object and display it through a pager. | |||
|
172 | ||||
|
173 | %page [options] OBJECT | |||
|
174 | ||||
|
175 | If no object is given, use _ (last output). | |||
|
176 | ||||
|
177 | Options: | |||
|
178 | ||||
|
179 | -r: page str(object), don't pretty-print it.""" | |||
|
180 | ||||
|
181 | # After a function contributed by Olivier Aubert, slightly modified. | |||
|
182 | ||||
|
183 | # Process options/args | |||
|
184 | opts, args = self.parse_options(parameter_s, 'r') | |||
|
185 | raw = 'r' in opts | |||
|
186 | ||||
|
187 | oname = args and args or '_' | |||
|
188 | info = self._ofind(oname) | |||
|
189 | if info['found']: | |||
|
190 | txt = (raw and str or pformat)( info['obj'] ) | |||
|
191 | page.page(txt) | |||
|
192 | else: | |||
|
193 | print('Object `%s` not found' % oname) | |||
|
194 | ||||
|
195 | @line_magic | |||
|
196 | def profile(self, parameter_s=''): | |||
|
197 | """Print your currently active IPython profile.""" | |||
|
198 | from IPython.core.application import BaseIPythonApplication | |||
|
199 | if BaseIPythonApplication.initialized(): | |||
|
200 | print(BaseIPythonApplication.instance().profile) | |||
|
201 | else: | |||
|
202 | error("profile is an application-level value, but you don't appear to be in an IPython application") | |||
|
203 | ||||
|
204 | @line_magic | |||
|
205 | def pprint(self, parameter_s=''): | |||
|
206 | """Toggle pretty printing on/off.""" | |||
|
207 | ptformatter = self.shell.display_formatter.formatters['text/plain'] | |||
|
208 | ptformatter.pprint = bool(1 - ptformatter.pprint) | |||
|
209 | print('Pretty printing has been turned', | |||
|
210 | ['OFF','ON'][ptformatter.pprint]) | |||
|
211 | ||||
|
212 | @line_magic | |||
|
213 | def colors(self, parameter_s=''): | |||
|
214 | """Switch color scheme for prompts, info system and exception handlers. | |||
|
215 | ||||
|
216 | Currently implemented schemes: NoColor, Linux, LightBG. | |||
|
217 | ||||
|
218 | Color scheme names are not case-sensitive. | |||
|
219 | ||||
|
220 | Examples | |||
|
221 | -------- | |||
|
222 | To get a plain black and white terminal:: | |||
|
223 | ||||
|
224 | %colors nocolor | |||
|
225 | """ | |||
|
226 | def color_switch_err(name): | |||
|
227 | warn('Error changing %s color schemes.\n%s' % | |||
|
228 | (name, sys.exc_info()[1])) | |||
|
229 | ||||
|
230 | ||||
|
231 | new_scheme = parameter_s.strip() | |||
|
232 | if not new_scheme: | |||
|
233 | raise UsageError( | |||
|
234 | "%colors: you must specify a color scheme. See '%colors?'") | |||
|
235 | return | |||
|
236 | # local shortcut | |||
|
237 | shell = self.shell | |||
|
238 | ||||
|
239 | import IPython.utils.rlineimpl as readline | |||
|
240 | ||||
|
241 | if not shell.colors_force and \ | |||
|
242 | not readline.have_readline and sys.platform == "win32": | |||
|
243 | msg = """\ | |||
|
244 | Proper color support under MS Windows requires the pyreadline library. | |||
|
245 | You can find it at: | |||
|
246 | http://ipython.org/pyreadline.html | |||
|
247 | Gary's readline needs the ctypes module, from: | |||
|
248 | http://starship.python.net/crew/theller/ctypes | |||
|
249 | (Note that ctypes is already part of Python versions 2.5 and newer). | |||
|
250 | ||||
|
251 | Defaulting color scheme to 'NoColor'""" | |||
|
252 | new_scheme = 'NoColor' | |||
|
253 | warn(msg) | |||
|
254 | ||||
|
255 | # readline option is 0 | |||
|
256 | if not shell.colors_force and not shell.has_readline: | |||
|
257 | new_scheme = 'NoColor' | |||
|
258 | ||||
|
259 | # Set prompt colors | |||
|
260 | try: | |||
|
261 | shell.prompt_manager.color_scheme = new_scheme | |||
|
262 | except: | |||
|
263 | color_switch_err('prompt') | |||
|
264 | else: | |||
|
265 | shell.colors = \ | |||
|
266 | shell.prompt_manager.color_scheme_table.active_scheme_name | |||
|
267 | # Set exception colors | |||
|
268 | try: | |||
|
269 | shell.InteractiveTB.set_colors(scheme = new_scheme) | |||
|
270 | shell.SyntaxTB.set_colors(scheme = new_scheme) | |||
|
271 | except: | |||
|
272 | color_switch_err('exception') | |||
|
273 | ||||
|
274 | # Set info (for 'object?') colors | |||
|
275 | if shell.color_info: | |||
|
276 | try: | |||
|
277 | shell.inspector.set_active_scheme(new_scheme) | |||
|
278 | except: | |||
|
279 | color_switch_err('object inspector') | |||
|
280 | else: | |||
|
281 | shell.inspector.set_active_scheme('NoColor') | |||
|
282 | ||||
|
283 | @line_magic | |||
|
284 | def xmode(self, parameter_s=''): | |||
|
285 | """Switch modes for the exception handlers. | |||
|
286 | ||||
|
287 | Valid modes: Plain, Context and Verbose. | |||
|
288 | ||||
|
289 | If called without arguments, acts as a toggle.""" | |||
|
290 | ||||
|
291 | def xmode_switch_err(name): | |||
|
292 | warn('Error changing %s exception modes.\n%s' % | |||
|
293 | (name,sys.exc_info()[1])) | |||
|
294 | ||||
|
295 | shell = self.shell | |||
|
296 | new_mode = parameter_s.strip().capitalize() | |||
|
297 | try: | |||
|
298 | shell.InteractiveTB.set_mode(mode=new_mode) | |||
|
299 | print('Exception reporting mode:',shell.InteractiveTB.mode) | |||
|
300 | except: | |||
|
301 | xmode_switch_err('user') | |||
|
302 | ||||
|
303 | @line_magic | |||
|
304 | def quickref(self,arg): | |||
|
305 | """ Show a quick reference sheet """ | |||
|
306 | from IPython.core.usage import quick_reference | |||
|
307 | qr = quick_reference + self.magic('-brief') | |||
|
308 | page.page(qr) | |||
|
309 | ||||
|
310 | @line_magic | |||
|
311 | def doctest_mode(self, parameter_s=''): | |||
|
312 | """Toggle doctest mode on and off. | |||
|
313 | ||||
|
314 | This mode is intended to make IPython behave as much as possible like a | |||
|
315 | plain Python shell, from the perspective of how its prompts, exceptions | |||
|
316 | and output look. This makes it easy to copy and paste parts of a | |||
|
317 | session into doctests. It does so by: | |||
|
318 | ||||
|
319 | - Changing the prompts to the classic ``>>>`` ones. | |||
|
320 | - Changing the exception reporting mode to 'Plain'. | |||
|
321 | - Disabling pretty-printing of output. | |||
|
322 | ||||
|
323 | Note that IPython also supports the pasting of code snippets that have | |||
|
324 | leading '>>>' and '...' prompts in them. This means that you can paste | |||
|
325 | doctests from files or docstrings (even if they have leading | |||
|
326 | whitespace), and the code will execute correctly. You can then use | |||
|
327 | '%history -t' to see the translated history; this will give you the | |||
|
328 | input after removal of all the leading prompts and whitespace, which | |||
|
329 | can be pasted back into an editor. | |||
|
330 | ||||
|
331 | With these features, you can switch into this mode easily whenever you | |||
|
332 | need to do testing and changes to doctests, without having to leave | |||
|
333 | your existing IPython session. | |||
|
334 | """ | |||
|
335 | ||||
|
336 | # Shorthands | |||
|
337 | shell = self.shell | |||
|
338 | pm = shell.prompt_manager | |||
|
339 | meta = shell.meta | |||
|
340 | disp_formatter = self.shell.display_formatter | |||
|
341 | ptformatter = disp_formatter.formatters['text/plain'] | |||
|
342 | # dstore is a data store kept in the instance metadata bag to track any | |||
|
343 | # changes we make, so we can undo them later. | |||
|
344 | dstore = meta.setdefault('doctest_mode',Struct()) | |||
|
345 | save_dstore = dstore.setdefault | |||
|
346 | ||||
|
347 | # save a few values we'll need to recover later | |||
|
348 | mode = save_dstore('mode',False) | |||
|
349 | save_dstore('rc_pprint',ptformatter.pprint) | |||
|
350 | save_dstore('xmode',shell.InteractiveTB.mode) | |||
|
351 | save_dstore('rc_separate_out',shell.separate_out) | |||
|
352 | save_dstore('rc_separate_out2',shell.separate_out2) | |||
|
353 | save_dstore('rc_prompts_pad_left',pm.justify) | |||
|
354 | save_dstore('rc_separate_in',shell.separate_in) | |||
|
355 | save_dstore('rc_plain_text_only',disp_formatter.plain_text_only) | |||
|
356 | save_dstore('prompt_templates',(pm.in_template, pm.in2_template, pm.out_template)) | |||
|
357 | ||||
|
358 | if mode == False: | |||
|
359 | # turn on | |||
|
360 | pm.in_template = '>>> ' | |||
|
361 | pm.in2_template = '... ' | |||
|
362 | pm.out_template = '' | |||
|
363 | ||||
|
364 | # Prompt separators like plain python | |||
|
365 | shell.separate_in = '' | |||
|
366 | shell.separate_out = '' | |||
|
367 | shell.separate_out2 = '' | |||
|
368 | ||||
|
369 | pm.justify = False | |||
|
370 | ||||
|
371 | ptformatter.pprint = False | |||
|
372 | disp_formatter.plain_text_only = True | |||
|
373 | ||||
|
374 | shell.magic('xmode Plain') | |||
|
375 | else: | |||
|
376 | # turn off | |||
|
377 | pm.in_template, pm.in2_template, pm.out_template = dstore.prompt_templates | |||
|
378 | ||||
|
379 | shell.separate_in = dstore.rc_separate_in | |||
|
380 | ||||
|
381 | shell.separate_out = dstore.rc_separate_out | |||
|
382 | shell.separate_out2 = dstore.rc_separate_out2 | |||
|
383 | ||||
|
384 | pm.justify = dstore.rc_prompts_pad_left | |||
|
385 | ||||
|
386 | ptformatter.pprint = dstore.rc_pprint | |||
|
387 | disp_formatter.plain_text_only = dstore.rc_plain_text_only | |||
|
388 | ||||
|
389 | shell.magic('xmode ' + dstore.xmode) | |||
|
390 | ||||
|
391 | # Store new mode and inform | |||
|
392 | dstore.mode = bool(1-int(mode)) | |||
|
393 | mode_label = ['OFF','ON'][dstore.mode] | |||
|
394 | print('Doctest mode is:', mode_label) | |||
|
395 | ||||
|
396 | @line_magic | |||
|
397 | def gui(self, parameter_s=''): | |||
|
398 | """Enable or disable IPython GUI event loop integration. | |||
|
399 | ||||
|
400 | %gui [GUINAME] | |||
|
401 | ||||
|
402 | This magic replaces IPython's threaded shells that were activated | |||
|
403 | using the (pylab/wthread/etc.) command line flags. GUI toolkits | |||
|
404 | can now be enabled at runtime and keyboard | |||
|
405 | interrupts should work without any problems. The following toolkits | |||
|
406 | are supported: wxPython, PyQt4, PyGTK, Tk and Cocoa (OSX):: | |||
|
407 | ||||
|
408 | %gui wx # enable wxPython event loop integration | |||
|
409 | %gui qt4|qt # enable PyQt4 event loop integration | |||
|
410 | %gui gtk # enable PyGTK event loop integration | |||
|
411 | %gui gtk3 # enable Gtk3 event loop integration | |||
|
412 | %gui tk # enable Tk event loop integration | |||
|
413 | %gui OSX # enable Cocoa event loop integration | |||
|
414 | # (requires %matplotlib 1.1) | |||
|
415 | %gui # disable all event loop integration | |||
|
416 | ||||
|
417 | WARNING: after any of these has been called you can simply create | |||
|
418 | an application object, but DO NOT start the event loop yourself, as | |||
|
419 | we have already handled that. | |||
|
420 | """ | |||
|
421 | opts, arg = self.parse_options(parameter_s, '') | |||
|
422 | if arg=='': arg = None | |||
|
423 | try: | |||
|
424 | return self.enable_gui(arg) | |||
|
425 | except Exception as e: | |||
|
426 | # print simple error message, rather than traceback if we can't | |||
|
427 | # hook up the GUI | |||
|
428 | error(str(e)) | |||
|
429 | ||||
|
430 | @skip_doctest | |||
|
431 | @line_magic | |||
|
432 | def precision(self, s=''): | |||
|
433 | """Set floating point precision for pretty printing. | |||
|
434 | ||||
|
435 | Can set either integer precision or a format string. | |||
|
436 | ||||
|
437 | If numpy has been imported and precision is an int, | |||
|
438 | numpy display precision will also be set, via ``numpy.set_printoptions``. | |||
|
439 | ||||
|
440 | If no argument is given, defaults will be restored. | |||
|
441 | ||||
|
442 | Examples | |||
|
443 | -------- | |||
|
444 | :: | |||
|
445 | ||||
|
446 | In [1]: from math import pi | |||
|
447 | ||||
|
448 | In [2]: %precision 3 | |||
|
449 | Out[2]: u'%.3f' | |||
|
450 | ||||
|
451 | In [3]: pi | |||
|
452 | Out[3]: 3.142 | |||
|
453 | ||||
|
454 | In [4]: %precision %i | |||
|
455 | Out[4]: u'%i' | |||
|
456 | ||||
|
457 | In [5]: pi | |||
|
458 | Out[5]: 3 | |||
|
459 | ||||
|
460 | In [6]: %precision %e | |||
|
461 | Out[6]: u'%e' | |||
|
462 | ||||
|
463 | In [7]: pi**10 | |||
|
464 | Out[7]: 9.364805e+04 | |||
|
465 | ||||
|
466 | In [8]: %precision | |||
|
467 | Out[8]: u'%r' | |||
|
468 | ||||
|
469 | In [9]: pi**10 | |||
|
470 | Out[9]: 93648.047476082982 | |||
|
471 | """ | |||
|
472 | ptformatter = self.shell.display_formatter.formatters['text/plain'] | |||
|
473 | ptformatter.float_precision = s | |||
|
474 | return ptformatter.float_format | |||
|
475 | ||||
|
476 | @magic_arguments.magic_arguments() | |||
|
477 | @magic_arguments.argument( | |||
|
478 | '-e', '--export', action='store_true', default=False, | |||
|
479 | help='Export IPython history as a notebook. The filename argument ' | |||
|
480 | 'is used to specify the notebook name and format. For example ' | |||
|
481 | 'a filename of notebook.ipynb will result in a notebook name ' | |||
|
482 | 'of "notebook" and a format of "xml". Likewise using a ".json" ' | |||
|
483 | 'or ".py" file extension will write the notebook in the json ' | |||
|
484 | 'or py formats.' | |||
|
485 | ) | |||
|
486 | @magic_arguments.argument( | |||
|
487 | '-f', '--format', | |||
|
488 | help='Convert an existing IPython notebook to a new format. This option ' | |||
|
489 | 'specifies the new format and can have the values: xml, json, py. ' | |||
|
490 | 'The target filename is chosen automatically based on the new ' | |||
|
491 | 'format. The filename argument gives the name of the source file.' | |||
|
492 | ) | |||
|
493 | @magic_arguments.argument( | |||
|
494 | 'filename', type=unicode, | |||
|
495 | help='Notebook name or filename' | |||
|
496 | ) | |||
|
497 | @line_magic | |||
|
498 | def notebook(self, s): | |||
|
499 | """Export and convert IPython notebooks. | |||
|
500 | ||||
|
501 | This function can export the current IPython history to a notebook file | |||
|
502 | or can convert an existing notebook file into a different format. For | |||
|
503 | example, to export the history to "foo.ipynb" do "%notebook -e foo.ipynb". | |||
|
504 | To export the history to "foo.py" do "%notebook -e foo.py". To convert | |||
|
505 | "foo.ipynb" to "foo.json" do "%notebook -f json foo.ipynb". Possible | |||
|
506 | formats include (json/ipynb, py). | |||
|
507 | """ | |||
|
508 | args = magic_arguments.parse_argstring(self.notebook, s) | |||
|
509 | ||||
|
510 | from IPython.nbformat import current | |||
|
511 | args.filename = unquote_filename(args.filename) | |||
|
512 | if args.export: | |||
|
513 | fname, name, format = current.parse_filename(args.filename) | |||
|
514 | cells = [] | |||
|
515 | hist = list(self.shell.history_manager.get_range()) | |||
|
516 | for session, prompt_number, input in hist[:-1]: | |||
|
517 | cells.append(current.new_code_cell(prompt_number=prompt_number, | |||
|
518 | input=input)) | |||
|
519 | worksheet = current.new_worksheet(cells=cells) | |||
|
520 | nb = current.new_notebook(name=name,worksheets=[worksheet]) | |||
|
521 | with io.open(fname, 'w', encoding='utf-8') as f: | |||
|
522 | current.write(nb, f, format); | |||
|
523 | elif args.format is not None: | |||
|
524 | old_fname, old_name, old_format = current.parse_filename(args.filename) | |||
|
525 | new_format = args.format | |||
|
526 | if new_format == u'xml': | |||
|
527 | raise ValueError('Notebooks cannot be written as xml.') | |||
|
528 | elif new_format == u'ipynb' or new_format == u'json': | |||
|
529 | new_fname = old_name + u'.ipynb' | |||
|
530 | new_format = u'json' | |||
|
531 | elif new_format == u'py': | |||
|
532 | new_fname = old_name + u'.py' | |||
|
533 | else: | |||
|
534 | raise ValueError('Invalid notebook format: %s' % new_format) | |||
|
535 | with io.open(old_fname, 'r', encoding='utf-8') as f: | |||
|
536 | nb = current.read(f, old_format) | |||
|
537 | with io.open(new_fname, 'w', encoding='utf-8') as f: | |||
|
538 | current.write(nb, f, new_format) |
@@ -0,0 +1,478 b'' | |||||
|
1 | """Implementation of code management magic functions. | |||
|
2 | """ | |||
|
3 | #----------------------------------------------------------------------------- | |||
|
4 | # Copyright (c) 2012 The IPython Development Team. | |||
|
5 | # | |||
|
6 | # Distributed under the terms of the Modified BSD License. | |||
|
7 | # | |||
|
8 | # The full license is in the file COPYING.txt, distributed with this software. | |||
|
9 | #----------------------------------------------------------------------------- | |||
|
10 | ||||
|
11 | #----------------------------------------------------------------------------- | |||
|
12 | # Imports | |||
|
13 | #----------------------------------------------------------------------------- | |||
|
14 | ||||
|
15 | # Stdlib | |||
|
16 | import inspect | |||
|
17 | import io | |||
|
18 | import json | |||
|
19 | import os | |||
|
20 | import sys | |||
|
21 | from urllib2 import urlopen | |||
|
22 | ||||
|
23 | # Our own packages | |||
|
24 | from IPython.core.error import TryNext | |||
|
25 | from IPython.core.macro import Macro | |||
|
26 | from IPython.core.magic import Magics, magics_class, line_magic | |||
|
27 | from IPython.testing.skipdoctest import skip_doctest | |||
|
28 | from IPython.utils import openpy | |||
|
29 | from IPython.utils import py3compat | |||
|
30 | from IPython.utils.io import file_read | |||
|
31 | from IPython.utils.path import get_py_filename, unquote_filename | |||
|
32 | from IPython.utils.warn import warn | |||
|
33 | ||||
|
34 | #----------------------------------------------------------------------------- | |||
|
35 | # Magic implementation classes | |||
|
36 | #----------------------------------------------------------------------------- | |||
|
37 | ||||
|
38 | # Used for exception handling in magic_edit | |||
|
39 | class MacroToEdit(ValueError): pass | |||
|
40 | ||||
|
41 | ||||
|
42 | @magics_class | |||
|
43 | class CodeMagics(Magics): | |||
|
44 | """Magics related to code management (loading, saving, editing, ...).""" | |||
|
45 | ||||
|
46 | @line_magic | |||
|
47 | def save(self, parameter_s=''): | |||
|
48 | """Save a set of lines or a macro to a given filename. | |||
|
49 | ||||
|
50 | Usage:\\ | |||
|
51 | %save [options] filename n1-n2 n3-n4 ... n5 .. n6 ... | |||
|
52 | ||||
|
53 | Options: | |||
|
54 | ||||
|
55 | -r: use 'raw' input. By default, the 'processed' history is used, | |||
|
56 | so that magics are loaded in their transformed version to valid | |||
|
57 | Python. If this option is given, the raw input as typed as the | |||
|
58 | command line is used instead. | |||
|
59 | ||||
|
60 | This function uses the same syntax as %history for input ranges, | |||
|
61 | then saves the lines to the filename you specify. | |||
|
62 | ||||
|
63 | It adds a '.py' extension to the file if you don't do so yourself, and | |||
|
64 | it asks for confirmation before overwriting existing files.""" | |||
|
65 | ||||
|
66 | opts,args = self.parse_options(parameter_s,'r',mode='list') | |||
|
67 | fname, codefrom = unquote_filename(args[0]), " ".join(args[1:]) | |||
|
68 | if not fname.endswith('.py'): | |||
|
69 | fname += '.py' | |||
|
70 | if os.path.isfile(fname): | |||
|
71 | ans = raw_input('File `%s` exists. Overwrite (y/[N])? ' % fname) | |||
|
72 | if ans.lower() not in ['y','yes']: | |||
|
73 | print 'Operation cancelled.' | |||
|
74 | return | |||
|
75 | try: | |||
|
76 | cmds = self.shell.find_user_code(codefrom, 'r' in opts) | |||
|
77 | except (TypeError, ValueError) as e: | |||
|
78 | print e.args[0] | |||
|
79 | return | |||
|
80 | with io.open(fname,'w', encoding="utf-8") as f: | |||
|
81 | f.write(u"# coding: utf-8\n") | |||
|
82 | f.write(py3compat.cast_unicode(cmds)) | |||
|
83 | print 'The following commands were written to file `%s`:' % fname | |||
|
84 | print cmds | |||
|
85 | ||||
|
86 | @line_magic | |||
|
87 | def pastebin(self, parameter_s=''): | |||
|
88 | """Upload code to Github's Gist paste bin, returning the URL. | |||
|
89 | ||||
|
90 | Usage:\\ | |||
|
91 | %pastebin [-d "Custom description"] 1-7 | |||
|
92 | ||||
|
93 | The argument can be an input history range, a filename, or the name of a | |||
|
94 | string or macro. | |||
|
95 | ||||
|
96 | Options: | |||
|
97 | ||||
|
98 | -d: Pass a custom description for the gist. The default will say | |||
|
99 | "Pasted from IPython". | |||
|
100 | """ | |||
|
101 | opts, args = self.parse_options(parameter_s, 'd:') | |||
|
102 | ||||
|
103 | try: | |||
|
104 | code = self.shell.find_user_code(args) | |||
|
105 | except (ValueError, TypeError) as e: | |||
|
106 | print e.args[0] | |||
|
107 | return | |||
|
108 | ||||
|
109 | post_data = json.dumps({ | |||
|
110 | "description": opts.get('d', "Pasted from IPython"), | |||
|
111 | "public": True, | |||
|
112 | "files": { | |||
|
113 | "file1.py": { | |||
|
114 | "content": code | |||
|
115 | } | |||
|
116 | } | |||
|
117 | }).encode('utf-8') | |||
|
118 | ||||
|
119 | response = urlopen("https://api.github.com/gists", post_data) | |||
|
120 | response_data = json.loads(response.read().decode('utf-8')) | |||
|
121 | return response_data['html_url'] | |||
|
122 | ||||
|
123 | @line_magic | |||
|
124 | def loadpy(self, arg_s): | |||
|
125 | """Load a .py python script into the GUI console. | |||
|
126 | ||||
|
127 | This magic command can either take a local filename or a url:: | |||
|
128 | ||||
|
129 | %loadpy myscript.py | |||
|
130 | %loadpy http://www.example.com/myscript.py | |||
|
131 | """ | |||
|
132 | arg_s = unquote_filename(arg_s) | |||
|
133 | remote_url = arg_s.startswith(('http://', 'https://')) | |||
|
134 | local_url = not remote_url | |||
|
135 | if local_url and not arg_s.endswith('.py'): | |||
|
136 | # Local files must be .py; for remote URLs it's possible that the | |||
|
137 | # fetch URL doesn't have a .py in it (many servers have an opaque | |||
|
138 | # URL, such as scipy-central.org). | |||
|
139 | raise ValueError('%%loadpy only works with .py files: %s' % arg_s) | |||
|
140 | ||||
|
141 | # openpy takes care of finding the source encoding (per PEP 263) | |||
|
142 | if remote_url: | |||
|
143 | contents = openpy.read_py_url(arg_s, skip_encoding_cookie=True) | |||
|
144 | else: | |||
|
145 | contents = openpy.read_py_file(arg_s, skip_encoding_cookie=True) | |||
|
146 | ||||
|
147 | self.shell.set_next_input(contents) | |||
|
148 | ||||
|
149 | def _find_edit_target(self, args, opts, last_call): | |||
|
150 | """Utility method used by magic_edit to find what to edit.""" | |||
|
151 | ||||
|
152 | def make_filename(arg): | |||
|
153 | "Make a filename from the given args" | |||
|
154 | arg = unquote_filename(arg) | |||
|
155 | try: | |||
|
156 | filename = get_py_filename(arg) | |||
|
157 | except IOError: | |||
|
158 | # If it ends with .py but doesn't already exist, assume we want | |||
|
159 | # a new file. | |||
|
160 | if arg.endswith('.py'): | |||
|
161 | filename = arg | |||
|
162 | else: | |||
|
163 | filename = None | |||
|
164 | return filename | |||
|
165 | ||||
|
166 | # Set a few locals from the options for convenience: | |||
|
167 | opts_prev = 'p' in opts | |||
|
168 | opts_raw = 'r' in opts | |||
|
169 | ||||
|
170 | # custom exceptions | |||
|
171 | class DataIsObject(Exception): pass | |||
|
172 | ||||
|
173 | # Default line number value | |||
|
174 | lineno = opts.get('n',None) | |||
|
175 | ||||
|
176 | if opts_prev: | |||
|
177 | args = '_%s' % last_call[0] | |||
|
178 | if not self.shell.user_ns.has_key(args): | |||
|
179 | args = last_call[1] | |||
|
180 | ||||
|
181 | # use last_call to remember the state of the previous call, but don't | |||
|
182 | # let it be clobbered by successive '-p' calls. | |||
|
183 | try: | |||
|
184 | last_call[0] = self.shell.displayhook.prompt_count | |||
|
185 | if not opts_prev: | |||
|
186 | last_call[1] = args | |||
|
187 | except: | |||
|
188 | pass | |||
|
189 | ||||
|
190 | # by default this is done with temp files, except when the given | |||
|
191 | # arg is a filename | |||
|
192 | use_temp = True | |||
|
193 | ||||
|
194 | data = '' | |||
|
195 | ||||
|
196 | # First, see if the arguments should be a filename. | |||
|
197 | filename = make_filename(args) | |||
|
198 | if filename: | |||
|
199 | use_temp = False | |||
|
200 | elif args: | |||
|
201 | # Mode where user specifies ranges of lines, like in %macro. | |||
|
202 | data = self.shell.extract_input_lines(args, opts_raw) | |||
|
203 | if not data: | |||
|
204 | try: | |||
|
205 | # Load the parameter given as a variable. If not a string, | |||
|
206 | # process it as an object instead (below) | |||
|
207 | ||||
|
208 | #print '*** args',args,'type',type(args) # dbg | |||
|
209 | data = eval(args, self.shell.user_ns) | |||
|
210 | if not isinstance(data, basestring): | |||
|
211 | raise DataIsObject | |||
|
212 | ||||
|
213 | except (NameError,SyntaxError): | |||
|
214 | # given argument is not a variable, try as a filename | |||
|
215 | filename = make_filename(args) | |||
|
216 | if filename is None: | |||
|
217 | warn("Argument given (%s) can't be found as a variable " | |||
|
218 | "or as a filename." % args) | |||
|
219 | return | |||
|
220 | use_temp = False | |||
|
221 | ||||
|
222 | except DataIsObject: | |||
|
223 | # macros have a special edit function | |||
|
224 | if isinstance(data, Macro): | |||
|
225 | raise MacroToEdit(data) | |||
|
226 | ||||
|
227 | # For objects, try to edit the file where they are defined | |||
|
228 | try: | |||
|
229 | filename = inspect.getabsfile(data) | |||
|
230 | if 'fakemodule' in filename.lower() and \ | |||
|
231 | inspect.isclass(data): | |||
|
232 | # class created by %edit? Try to find source | |||
|
233 | # by looking for method definitions instead, the | |||
|
234 | # __module__ in those classes is FakeModule. | |||
|
235 | attrs = [getattr(data, aname) for aname in dir(data)] | |||
|
236 | for attr in attrs: | |||
|
237 | if not inspect.ismethod(attr): | |||
|
238 | continue | |||
|
239 | filename = inspect.getabsfile(attr) | |||
|
240 | if filename and \ | |||
|
241 | 'fakemodule' not in filename.lower(): | |||
|
242 | # change the attribute to be the edit | |||
|
243 | # target instead | |||
|
244 | data = attr | |||
|
245 | break | |||
|
246 | ||||
|
247 | datafile = 1 | |||
|
248 | except TypeError: | |||
|
249 | filename = make_filename(args) | |||
|
250 | datafile = 1 | |||
|
251 | warn('Could not find file where `%s` is defined.\n' | |||
|
252 | 'Opening a file named `%s`' % (args, filename)) | |||
|
253 | # Now, make sure we can actually read the source (if it was | |||
|
254 | # in a temp file it's gone by now). | |||
|
255 | if datafile: | |||
|
256 | try: | |||
|
257 | if lineno is None: | |||
|
258 | lineno = inspect.getsourcelines(data)[1] | |||
|
259 | except IOError: | |||
|
260 | filename = make_filename(args) | |||
|
261 | if filename is None: | |||
|
262 | warn('The file `%s` where `%s` was defined ' | |||
|
263 | 'cannot be read.' % (filename, data)) | |||
|
264 | return | |||
|
265 | use_temp = False | |||
|
266 | ||||
|
267 | if use_temp: | |||
|
268 | filename = self.shell.mktempfile(data) | |||
|
269 | print 'IPython will make a temporary file named:',filename | |||
|
270 | ||||
|
271 | return filename, lineno, use_temp | |||
|
272 | ||||
|
273 | def _edit_macro(self,mname,macro): | |||
|
274 | """open an editor with the macro data in a file""" | |||
|
275 | filename = self.shell.mktempfile(macro.value) | |||
|
276 | self.shell.hooks.editor(filename) | |||
|
277 | ||||
|
278 | # and make a new macro object, to replace the old one | |||
|
279 | mfile = open(filename) | |||
|
280 | mvalue = mfile.read() | |||
|
281 | mfile.close() | |||
|
282 | self.shell.user_ns[mname] = Macro(mvalue) | |||
|
283 | ||||
|
284 | @line_magic | |||
|
285 | def ed(self, parameter_s=''): | |||
|
286 | """Alias to %edit.""" | |||
|
287 | return self.edit(parameter_s) | |||
|
288 | ||||
|
289 | @skip_doctest | |||
|
290 | @line_magic | |||
|
291 | def edit(self, parameter_s='',last_call=['','']): | |||
|
292 | """Bring up an editor and execute the resulting code. | |||
|
293 | ||||
|
294 | Usage: | |||
|
295 | %edit [options] [args] | |||
|
296 | ||||
|
297 | %edit runs IPython's editor hook. The default version of this hook is | |||
|
298 | set to call the editor specified by your $EDITOR environment variable. | |||
|
299 | If this isn't found, it will default to vi under Linux/Unix and to | |||
|
300 | notepad under Windows. See the end of this docstring for how to change | |||
|
301 | the editor hook. | |||
|
302 | ||||
|
303 | You can also set the value of this editor via the | |||
|
304 | ``TerminalInteractiveShell.editor`` option in your configuration file. | |||
|
305 | This is useful if you wish to use a different editor from your typical | |||
|
306 | default with IPython (and for Windows users who typically don't set | |||
|
307 | environment variables). | |||
|
308 | ||||
|
309 | This command allows you to conveniently edit multi-line code right in | |||
|
310 | your IPython session. | |||
|
311 | ||||
|
312 | If called without arguments, %edit opens up an empty editor with a | |||
|
313 | temporary file and will execute the contents of this file when you | |||
|
314 | close it (don't forget to save it!). | |||
|
315 | ||||
|
316 | ||||
|
317 | Options: | |||
|
318 | ||||
|
319 | -n <number>: open the editor at a specified line number. By default, | |||
|
320 | the IPython editor hook uses the unix syntax 'editor +N filename', but | |||
|
321 | you can configure this by providing your own modified hook if your | |||
|
322 | favorite editor supports line-number specifications with a different | |||
|
323 | syntax. | |||
|
324 | ||||
|
325 | -p: this will call the editor with the same data as the previous time | |||
|
326 | it was used, regardless of how long ago (in your current session) it | |||
|
327 | was. | |||
|
328 | ||||
|
329 | -r: use 'raw' input. This option only applies to input taken from the | |||
|
330 | user's history. By default, the 'processed' history is used, so that | |||
|
331 | magics are loaded in their transformed version to valid Python. If | |||
|
332 | this option is given, the raw input as typed as the command line is | |||
|
333 | used instead. When you exit the editor, it will be executed by | |||
|
334 | IPython's own processor. | |||
|
335 | ||||
|
336 | -x: do not execute the edited code immediately upon exit. This is | |||
|
337 | mainly useful if you are editing programs which need to be called with | |||
|
338 | command line arguments, which you can then do using %run. | |||
|
339 | ||||
|
340 | ||||
|
341 | Arguments: | |||
|
342 | ||||
|
343 | If arguments are given, the following possibilities exist: | |||
|
344 | ||||
|
345 | - If the argument is a filename, IPython will load that into the | |||
|
346 | editor. It will execute its contents with execfile() when you exit, | |||
|
347 | loading any code in the file into your interactive namespace. | |||
|
348 | ||||
|
349 | - The arguments are ranges of input history, e.g. "7 ~1/4-6". | |||
|
350 | The syntax is the same as in the %history magic. | |||
|
351 | ||||
|
352 | - If the argument is a string variable, its contents are loaded | |||
|
353 | into the editor. You can thus edit any string which contains | |||
|
354 | python code (including the result of previous edits). | |||
|
355 | ||||
|
356 | - If the argument is the name of an object (other than a string), | |||
|
357 | IPython will try to locate the file where it was defined and open the | |||
|
358 | editor at the point where it is defined. You can use `%edit function` | |||
|
359 | to load an editor exactly at the point where 'function' is defined, | |||
|
360 | edit it and have the file be executed automatically. | |||
|
361 | ||||
|
362 | - If the object is a macro (see %macro for details), this opens up your | |||
|
363 | specified editor with a temporary file containing the macro's data. | |||
|
364 | Upon exit, the macro is reloaded with the contents of the file. | |||
|
365 | ||||
|
366 | Note: opening at an exact line is only supported under Unix, and some | |||
|
367 | editors (like kedit and gedit up to Gnome 2.8) do not understand the | |||
|
368 | '+NUMBER' parameter necessary for this feature. Good editors like | |||
|
369 | (X)Emacs, vi, jed, pico and joe all do. | |||
|
370 | ||||
|
371 | After executing your code, %edit will return as output the code you | |||
|
372 | typed in the editor (except when it was an existing file). This way | |||
|
373 | you can reload the code in further invocations of %edit as a variable, | |||
|
374 | via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of | |||
|
375 | the output. | |||
|
376 | ||||
|
377 | Note that %edit is also available through the alias %ed. | |||
|
378 | ||||
|
379 | This is an example of creating a simple function inside the editor and | |||
|
380 | then modifying it. First, start up the editor:: | |||
|
381 | ||||
|
382 | In [1]: ed | |||
|
383 | Editing... done. Executing edited code... | |||
|
384 | Out[1]: 'def foo():\\n print "foo() was defined in an editing | |||
|
385 | session"\\n' | |||
|
386 | ||||
|
387 | We can then call the function foo():: | |||
|
388 | ||||
|
389 | In [2]: foo() | |||
|
390 | foo() was defined in an editing session | |||
|
391 | ||||
|
392 | Now we edit foo. IPython automatically loads the editor with the | |||
|
393 | (temporary) file where foo() was previously defined:: | |||
|
394 | ||||
|
395 | In [3]: ed foo | |||
|
396 | Editing... done. Executing edited code... | |||
|
397 | ||||
|
398 | And if we call foo() again we get the modified version:: | |||
|
399 | ||||
|
400 | In [4]: foo() | |||
|
401 | foo() has now been changed! | |||
|
402 | ||||
|
403 | Here is an example of how to edit a code snippet successive | |||
|
404 | times. First we call the editor:: | |||
|
405 | ||||
|
406 | In [5]: ed | |||
|
407 | Editing... done. Executing edited code... | |||
|
408 | hello | |||
|
409 | Out[5]: "print 'hello'\\n" | |||
|
410 | ||||
|
411 | Now we call it again with the previous output (stored in _):: | |||
|
412 | ||||
|
413 | In [6]: ed _ | |||
|
414 | Editing... done. Executing edited code... | |||
|
415 | hello world | |||
|
416 | Out[6]: "print 'hello world'\\n" | |||
|
417 | ||||
|
418 | Now we call it with the output #8 (stored in _8, also as Out[8]):: | |||
|
419 | ||||
|
420 | In [7]: ed _8 | |||
|
421 | Editing... done. Executing edited code... | |||
|
422 | hello again | |||
|
423 | Out[7]: "print 'hello again'\\n" | |||
|
424 | ||||
|
425 | ||||
|
426 | Changing the default editor hook: | |||
|
427 | ||||
|
428 | If you wish to write your own editor hook, you can put it in a | |||
|
429 | configuration file which you load at startup time. The default hook | |||
|
430 | is defined in the IPython.core.hooks module, and you can use that as a | |||
|
431 | starting example for further modifications. That file also has | |||
|
432 | general instructions on how to set a new hook for use once you've | |||
|
433 | defined it.""" | |||
|
434 | opts,args = self.parse_options(parameter_s,'prxn:') | |||
|
435 | ||||
|
436 | try: | |||
|
437 | filename, lineno, is_temp = self._find_edit_target(args, opts, last_call) | |||
|
438 | except MacroToEdit as e: | |||
|
439 | self._edit_macro(args, e.args[0]) | |||
|
440 | return | |||
|
441 | ||||
|
442 | # do actual editing here | |||
|
443 | print 'Editing...', | |||
|
444 | sys.stdout.flush() | |||
|
445 | try: | |||
|
446 | # Quote filenames that may have spaces in them | |||
|
447 | if ' ' in filename: | |||
|
448 | filename = "'%s'" % filename | |||
|
449 | self.shell.hooks.editor(filename,lineno) | |||
|
450 | except TryNext: | |||
|
451 | warn('Could not open editor') | |||
|
452 | return | |||
|
453 | ||||
|
454 | # XXX TODO: should this be generalized for all string vars? | |||
|
455 | # For now, this is special-cased to blocks created by cpaste | |||
|
456 | if args.strip() == 'pasted_block': | |||
|
457 | self.shell.user_ns['pasted_block'] = file_read(filename) | |||
|
458 | ||||
|
459 | if 'x' in opts: # -x prevents actual execution | |||
|
460 | ||||
|
461 | else: | |||
|
462 | print 'done. Executing edited code...' | |||
|
463 | if 'r' in opts: # Untranslated IPython code | |||
|
464 | self.shell.run_cell(file_read(filename), | |||
|
465 | store_history=False) | |||
|
466 | else: | |||
|
467 | self.shell.safe_execfile(filename, self.shell.user_ns, | |||
|
468 | self.shell.user_ns) | |||
|
469 | ||||
|
470 | if is_temp: | |||
|
471 | try: | |||
|
472 | return open(filename).read() | |||
|
473 | except IOError,msg: | |||
|
474 | if msg.filename == filename: | |||
|
475 | warn('File not found. Did you forget to save?') | |||
|
476 | return | |||
|
477 | else: | |||
|
478 | self.shell.showtraceback() |
@@ -0,0 +1,146 b'' | |||||
|
1 | """Implementation of configuration-related magic functions. | |||
|
2 | """ | |||
|
3 | #----------------------------------------------------------------------------- | |||
|
4 | # Copyright (c) 2012 The IPython Development Team. | |||
|
5 | # | |||
|
6 | # Distributed under the terms of the Modified BSD License. | |||
|
7 | # | |||
|
8 | # The full license is in the file COPYING.txt, distributed with this software. | |||
|
9 | #----------------------------------------------------------------------------- | |||
|
10 | ||||
|
11 | #----------------------------------------------------------------------------- | |||
|
12 | # Imports | |||
|
13 | #----------------------------------------------------------------------------- | |||
|
14 | ||||
|
15 | # Stdlib | |||
|
16 | import re | |||
|
17 | ||||
|
18 | # Our own packages | |||
|
19 | from IPython.core.error import UsageError | |||
|
20 | from IPython.core.magic import Magics, magics_class, line_magic | |||
|
21 | from IPython.utils.warn import error | |||
|
22 | ||||
|
23 | #----------------------------------------------------------------------------- | |||
|
24 | # Magic implementation classes | |||
|
25 | #----------------------------------------------------------------------------- | |||
|
26 | ||||
|
27 | @magics_class | |||
|
28 | class ConfigMagics(Magics): | |||
|
29 | ||||
|
30 | def __init__(self, shell): | |||
|
31 | super(ConfigMagics, self).__init__(shell) | |||
|
32 | self.configurables = [] | |||
|
33 | ||||
|
34 | @line_magic | |||
|
35 | def config(self, s): | |||
|
36 | """configure IPython | |||
|
37 | ||||
|
38 | %config Class[.trait=value] | |||
|
39 | ||||
|
40 | This magic exposes most of the IPython config system. Any | |||
|
41 | Configurable class should be able to be configured with the simple | |||
|
42 | line:: | |||
|
43 | ||||
|
44 | %config Class.trait=value | |||
|
45 | ||||
|
46 | Where `value` will be resolved in the user's namespace, if it is an | |||
|
47 | expression or variable name. | |||
|
48 | ||||
|
49 | Examples | |||
|
50 | -------- | |||
|
51 | ||||
|
52 | To see what classes are available for config, pass no arguments:: | |||
|
53 | ||||
|
54 | In [1]: %config | |||
|
55 | Available objects for config: | |||
|
56 | TerminalInteractiveShell | |||
|
57 | HistoryManager | |||
|
58 | PrefilterManager | |||
|
59 | AliasManager | |||
|
60 | IPCompleter | |||
|
61 | PromptManager | |||
|
62 | DisplayFormatter | |||
|
63 | ||||
|
64 | To view what is configurable on a given class, just pass the class | |||
|
65 | name:: | |||
|
66 | ||||
|
67 | In [2]: %config IPCompleter | |||
|
68 | IPCompleter options | |||
|
69 | ----------------- | |||
|
70 | IPCompleter.omit__names=<Enum> | |||
|
71 | Current: 2 | |||
|
72 | Choices: (0, 1, 2) | |||
|
73 | Instruct the completer to omit private method names | |||
|
74 | Specifically, when completing on ``object.<tab>``. | |||
|
75 | When 2 [default]: all names that start with '_' will be excluded. | |||
|
76 | When 1: all 'magic' names (``__foo__``) will be excluded. | |||
|
77 | When 0: nothing will be excluded. | |||
|
78 | IPCompleter.merge_completions=<CBool> | |||
|
79 | Current: True | |||
|
80 | Whether to merge completion results into a single list | |||
|
81 | If False, only the completion results from the first non-empty | |||
|
82 | completer will be returned. | |||
|
83 | IPCompleter.limit_to__all__=<CBool> | |||
|
84 | Current: False | |||
|
85 | Instruct the completer to use __all__ for the completion | |||
|
86 | Specifically, when completing on ``object.<tab>``. | |||
|
87 | When True: only those names in obj.__all__ will be included. | |||
|
88 | When False [default]: the __all__ attribute is ignored | |||
|
89 | IPCompleter.greedy=<CBool> | |||
|
90 | Current: False | |||
|
91 | Activate greedy completion | |||
|
92 | This will enable completion on elements of lists, results of | |||
|
93 | function calls, etc., but can be unsafe because the code is | |||
|
94 | actually evaluated on TAB. | |||
|
95 | ||||
|
96 | but the real use is in setting values:: | |||
|
97 | ||||
|
98 | In [3]: %config IPCompleter.greedy = True | |||
|
99 | ||||
|
100 | and these values are read from the user_ns if they are variables:: | |||
|
101 | ||||
|
102 | In [4]: feeling_greedy=False | |||
|
103 | ||||
|
104 | In [5]: %config IPCompleter.greedy = feeling_greedy | |||
|
105 | ||||
|
106 | """ | |||
|
107 | from IPython.config.loader import Config | |||
|
108 | # some IPython objects are Configurable, but do not yet have | |||
|
109 | # any configurable traits. Exclude them from the effects of | |||
|
110 | # this magic, as their presence is just noise: | |||
|
111 | configurables = [ c for c in self.shell.configurables | |||
|
112 | if c.__class__.class_traits(config=True) ] | |||
|
113 | classnames = [ c.__class__.__name__ for c in configurables ] | |||
|
114 | ||||
|
115 | line = s.strip() | |||
|
116 | if not line: | |||
|
117 | # print available configurable names | |||
|
118 | print "Available objects for config:" | |||
|
119 | for name in classnames: | |||
|
120 | print " ", name | |||
|
121 | return | |||
|
122 | elif line in classnames: | |||
|
123 | # `%config TerminalInteractiveShell` will print trait info for | |||
|
124 | # TerminalInteractiveShell | |||
|
125 | c = configurables[classnames.index(line)] | |||
|
126 | cls = c.__class__ | |||
|
127 | help = cls.class_get_help(c) | |||
|
128 | # strip leading '--' from cl-args: | |||
|
129 | help = re.sub(re.compile(r'^--', re.MULTILINE), '', help) | |||
|
130 | print help | |||
|
131 | return | |||
|
132 | elif '=' not in line: | |||
|
133 | raise UsageError("Invalid config statement: %r, " | |||
|
134 | "should be Class.trait = value" % line) | |||
|
135 | ||||
|
136 | # otherwise, assume we are setting configurables. | |||
|
137 | # leave quotes on args when splitting, because we want | |||
|
138 | # unquoted args to eval in user_ns | |||
|
139 | cfg = Config() | |||
|
140 | exec "cfg."+line in locals(), self.shell.user_ns | |||
|
141 | ||||
|
142 | for configurable in configurables: | |||
|
143 | try: | |||
|
144 | configurable.update_config(cfg) | |||
|
145 | except Exception as e: | |||
|
146 | error(e) |
@@ -0,0 +1,45 b'' | |||||
|
1 | """Deprecated Magic functions. | |||
|
2 | """ | |||
|
3 | #----------------------------------------------------------------------------- | |||
|
4 | # Copyright (c) 2012 The IPython Development Team. | |||
|
5 | # | |||
|
6 | # Distributed under the terms of the Modified BSD License. | |||
|
7 | # | |||
|
8 | # The full license is in the file COPYING.txt, distributed with this software. | |||
|
9 | #----------------------------------------------------------------------------- | |||
|
10 | ||||
|
11 | #----------------------------------------------------------------------------- | |||
|
12 | # Imports | |||
|
13 | #----------------------------------------------------------------------------- | |||
|
14 | ||||
|
15 | # Our own packages | |||
|
16 | from IPython.core.magic import Magics, magics_class, line_magic | |||
|
17 | ||||
|
18 | #----------------------------------------------------------------------------- | |||
|
19 | # Magic implementation classes | |||
|
20 | #----------------------------------------------------------------------------- | |||
|
21 | ||||
|
22 | @magics_class | |||
|
23 | class DeprecatedMagics(Magics): | |||
|
24 | """Magics slated for later removal.""" | |||
|
25 | ||||
|
26 | @line_magic | |||
|
27 | def install_profiles(self, parameter_s=''): | |||
|
28 | """%install_profiles has been deprecated.""" | |||
|
29 | print '\n'.join([ | |||
|
30 | "%install_profiles has been deprecated.", | |||
|
31 | "Use `ipython profile list` to view available profiles.", | |||
|
32 | "Requesting a profile with `ipython profile create <name>`", | |||
|
33 | "or `ipython --profile=<name>` will start with the bundled", | |||
|
34 | "profile of that name if it exists." | |||
|
35 | ]) | |||
|
36 | ||||
|
37 | @line_magic | |||
|
38 | def install_default_config(self, parameter_s=''): | |||
|
39 | """%install_default_config has been deprecated.""" | |||
|
40 | print '\n'.join([ | |||
|
41 | "%install_default_config has been deprecated.", | |||
|
42 | "Use `ipython profile create <name>` to initialize a profile", | |||
|
43 | "with the default config files.", | |||
|
44 | "Add `--reset` to overwrite already existing config files with defaults." | |||
|
45 | ]) |
This diff has been collapsed as it changes many lines, (985 lines changed) Show them Hide them | |||||
@@ -0,0 +1,985 b'' | |||||
|
1 | """Implementation of execution-related magic functions. | |||
|
2 | """ | |||
|
3 | #----------------------------------------------------------------------------- | |||
|
4 | # Copyright (c) 2012 The IPython Development Team. | |||
|
5 | # | |||
|
6 | # Distributed under the terms of the Modified BSD License. | |||
|
7 | # | |||
|
8 | # The full license is in the file COPYING.txt, distributed with this software. | |||
|
9 | #----------------------------------------------------------------------------- | |||
|
10 | ||||
|
11 | #----------------------------------------------------------------------------- | |||
|
12 | # Imports | |||
|
13 | #----------------------------------------------------------------------------- | |||
|
14 | ||||
|
15 | # Stdlib | |||
|
16 | import __builtin__ as builtin_mod | |||
|
17 | import bdb | |||
|
18 | import os | |||
|
19 | import sys | |||
|
20 | import time | |||
|
21 | from StringIO import StringIO | |||
|
22 | ||||
|
23 | # cProfile was added in Python2.5 | |||
|
24 | try: | |||
|
25 | import cProfile as profile | |||
|
26 | import pstats | |||
|
27 | except ImportError: | |||
|
28 | # profile isn't bundled by default in Debian for license reasons | |||
|
29 | try: | |||
|
30 | import profile, pstats | |||
|
31 | except ImportError: | |||
|
32 | profile = pstats = None | |||
|
33 | ||||
|
34 | # Our own packages | |||
|
35 | from IPython.core import debugger, oinspect | |||
|
36 | from IPython.core import page | |||
|
37 | from IPython.core.error import UsageError | |||
|
38 | from IPython.core.macro import Macro | |||
|
39 | from IPython.core.magic import (Magics, magics_class, line_magic, | |||
|
40 | line_cell_magic, on_off, needs_local_scope) | |||
|
41 | from IPython.testing.skipdoctest import skip_doctest | |||
|
42 | from IPython.utils import py3compat | |||
|
43 | from IPython.utils.ipstruct import Struct | |||
|
44 | from IPython.utils.module_paths import find_mod | |||
|
45 | from IPython.utils.path import get_py_filename, unquote_filename | |||
|
46 | from IPython.utils.timing import clock, clock2 | |||
|
47 | from IPython.utils.warn import warn, error | |||
|
48 | ||||
|
49 | #----------------------------------------------------------------------------- | |||
|
50 | # Magic implementation classes | |||
|
51 | #----------------------------------------------------------------------------- | |||
|
52 | ||||
|
53 | @magics_class | |||
|
54 | class ExecutionMagics(Magics): | |||
|
55 | """Magics related to code execution, debugging, profiling, etc. | |||
|
56 | ||||
|
57 | """ | |||
|
58 | ||||
|
59 | def __init__(self, shell): | |||
|
60 | super(ExecutionMagics, self).__init__(shell) | |||
|
61 | if profile is None: | |||
|
62 | self.prun = self.profile_missing_notice | |||
|
63 | # Default execution function used to actually run user code. | |||
|
64 | self.default_runner = None | |||
|
65 | ||||
|
66 | def profile_missing_notice(self, *args, **kwargs): | |||
|
67 | error("""\ | |||
|
68 | The profile module could not be found. It has been removed from the standard | |||
|
69 | python packages because of its non-free license. To use profiling, install the | |||
|
70 | python-profiler package from non-free.""") | |||
|
71 | ||||
|
72 | @skip_doctest | |||
|
73 | @line_cell_magic | |||
|
74 | def prun(self, parameter_s='', cell=None, user_mode=True, | |||
|
75 | opts=None,arg_lst=None,prog_ns=None): | |||
|
76 | ||||
|
77 | """Run a statement through the python code profiler. | |||
|
78 | ||||
|
79 | Usage, in line mode: | |||
|
80 | %prun [options] statement | |||
|
81 | ||||
|
82 | Usage, in cell mode: | |||
|
83 | %%prun [options] [statement] | |||
|
84 | code... | |||
|
85 | code... | |||
|
86 | ||||
|
87 | In cell mode, the additional code lines are appended to the (possibly | |||
|
88 | empty) statement in the first line. Cell mode allows you to easily | |||
|
89 | profile multiline blocks without having to put them in a separate | |||
|
90 | function. | |||
|
91 | ||||
|
92 | The given statement (which doesn't require quote marks) is run via the | |||
|
93 | python profiler in a manner similar to the profile.run() function. | |||
|
94 | Namespaces are internally managed to work correctly; profile.run | |||
|
95 | cannot be used in IPython because it makes certain assumptions about | |||
|
96 | namespaces which do not hold under IPython. | |||
|
97 | ||||
|
98 | Options: | |||
|
99 | ||||
|
100 | -l <limit>: you can place restrictions on what or how much of the | |||
|
101 | profile gets printed. The limit value can be: | |||
|
102 | ||||
|
103 | * A string: only information for function names containing this string | |||
|
104 | is printed. | |||
|
105 | ||||
|
106 | * An integer: only these many lines are printed. | |||
|
107 | ||||
|
108 | * A float (between 0 and 1): this fraction of the report is printed | |||
|
109 | (for example, use a limit of 0.4 to see the topmost 40% only). | |||
|
110 | ||||
|
111 | You can combine several limits with repeated use of the option. For | |||
|
112 | example, '-l __init__ -l 5' will print only the topmost 5 lines of | |||
|
113 | information about class constructors. | |||
|
114 | ||||
|
115 | -r: return the pstats.Stats object generated by the profiling. This | |||
|
116 | object has all the information about the profile in it, and you can | |||
|
117 | later use it for further analysis or in other functions. | |||
|
118 | ||||
|
119 | -s <key>: sort profile by given key. You can provide more than one key | |||
|
120 | by using the option several times: '-s key1 -s key2 -s key3...'. The | |||
|
121 | default sorting key is 'time'. | |||
|
122 | ||||
|
123 | The following is copied verbatim from the profile documentation | |||
|
124 | referenced below: | |||
|
125 | ||||
|
126 | When more than one key is provided, additional keys are used as | |||
|
127 | secondary criteria when the there is equality in all keys selected | |||
|
128 | before them. | |||
|
129 | ||||
|
130 | Abbreviations can be used for any key names, as long as the | |||
|
131 | abbreviation is unambiguous. The following are the keys currently | |||
|
132 | defined: | |||
|
133 | ||||
|
134 | Valid Arg Meaning | |||
|
135 | "calls" call count | |||
|
136 | "cumulative" cumulative time | |||
|
137 | "file" file name | |||
|
138 | "module" file name | |||
|
139 | "pcalls" primitive call count | |||
|
140 | "line" line number | |||
|
141 | "name" function name | |||
|
142 | "nfl" name/file/line | |||
|
143 | "stdname" standard name | |||
|
144 | "time" internal time | |||
|
145 | ||||
|
146 | Note that all sorts on statistics are in descending order (placing | |||
|
147 | most time consuming items first), where as name, file, and line number | |||
|
148 | searches are in ascending order (i.e., alphabetical). The subtle | |||
|
149 | distinction between "nfl" and "stdname" is that the standard name is a | |||
|
150 | sort of the name as printed, which means that the embedded line | |||
|
151 | numbers get compared in an odd way. For example, lines 3, 20, and 40 | |||
|
152 | would (if the file names were the same) appear in the string order | |||
|
153 | "20" "3" and "40". In contrast, "nfl" does a numeric compare of the | |||
|
154 | line numbers. In fact, sort_stats("nfl") is the same as | |||
|
155 | sort_stats("name", "file", "line"). | |||
|
156 | ||||
|
157 | -T <filename>: save profile results as shown on screen to a text | |||
|
158 | file. The profile is still shown on screen. | |||
|
159 | ||||
|
160 | -D <filename>: save (via dump_stats) profile statistics to given | |||
|
161 | filename. This data is in a format understood by the pstats module, and | |||
|
162 | is generated by a call to the dump_stats() method of profile | |||
|
163 | objects. The profile is still shown on screen. | |||
|
164 | ||||
|
165 | -q: suppress output to the pager. Best used with -T and/or -D above. | |||
|
166 | ||||
|
167 | If you want to run complete programs under the profiler's control, use | |||
|
168 | '%run -p [prof_opts] filename.py [args to program]' where prof_opts | |||
|
169 | contains profiler specific options as described here. | |||
|
170 | ||||
|
171 | You can read the complete documentation for the profile module with:: | |||
|
172 | ||||
|
173 | In [1]: import profile; profile.help() | |||
|
174 | """ | |||
|
175 | ||||
|
176 | opts_def = Struct(D=[''],l=[],s=['time'],T=['']) | |||
|
177 | ||||
|
178 | if user_mode: # regular user call | |||
|
179 | opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:q', | |||
|
180 | list_all=True, posix=False) | |||
|
181 | namespace = self.shell.user_ns | |||
|
182 | if cell is not None: | |||
|
183 | arg_str += '\n' + cell | |||
|
184 | else: # called to run a program by %run -p | |||
|
185 | try: | |||
|
186 | filename = get_py_filename(arg_lst[0]) | |||
|
187 | except IOError as e: | |||
|
188 | try: | |||
|
189 | msg = str(e) | |||
|
190 | except UnicodeError: | |||
|
191 | msg = e.message | |||
|
192 | error(msg) | |||
|
193 | return | |||
|
194 | ||||
|
195 | arg_str = 'execfile(filename,prog_ns)' | |||
|
196 | namespace = { | |||
|
197 | 'execfile': self.shell.safe_execfile, | |||
|
198 | 'prog_ns': prog_ns, | |||
|
199 | 'filename': filename | |||
|
200 | } | |||
|
201 | ||||
|
202 | opts.merge(opts_def) | |||
|
203 | ||||
|
204 | prof = profile.Profile() | |||
|
205 | try: | |||
|
206 | prof = prof.runctx(arg_str,namespace,namespace) | |||
|
207 | sys_exit = '' | |||
|
208 | except SystemExit: | |||
|
209 | sys_exit = """*** SystemExit exception caught in code being profiled.""" | |||
|
210 | ||||
|
211 | stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s) | |||
|
212 | ||||
|
213 | lims = opts.l | |||
|
214 | if lims: | |||
|
215 | lims = [] # rebuild lims with ints/floats/strings | |||
|
216 | for lim in opts.l: | |||
|
217 | try: | |||
|
218 | lims.append(int(lim)) | |||
|
219 | except ValueError: | |||
|
220 | try: | |||
|
221 | lims.append(float(lim)) | |||
|
222 | except ValueError: | |||
|
223 | lims.append(lim) | |||
|
224 | ||||
|
225 | # Trap output. | |||
|
226 | stdout_trap = StringIO() | |||
|
227 | ||||
|
228 | if hasattr(stats,'stream'): | |||
|
229 | # In newer versions of python, the stats object has a 'stream' | |||
|
230 | # attribute to write into. | |||
|
231 | stats.stream = stdout_trap | |||
|
232 | stats.print_stats(*lims) | |||
|
233 | else: | |||
|
234 | # For older versions, we manually redirect stdout during printing | |||
|
235 | sys_stdout = sys.stdout | |||
|
236 | try: | |||
|
237 | sys.stdout = stdout_trap | |||
|
238 | stats.print_stats(*lims) | |||
|
239 | finally: | |||
|
240 | sys.stdout = sys_stdout | |||
|
241 | ||||
|
242 | output = stdout_trap.getvalue() | |||
|
243 | output = output.rstrip() | |||
|
244 | ||||
|
245 | if 'q' not in opts: | |||
|
246 | page.page(output) | |||
|
247 | print sys_exit, | |||
|
248 | ||||
|
249 | dump_file = opts.D[0] | |||
|
250 | text_file = opts.T[0] | |||
|
251 | if dump_file: | |||
|
252 | dump_file = unquote_filename(dump_file) | |||
|
253 | prof.dump_stats(dump_file) | |||
|
254 | print '\n*** Profile stats marshalled to file',\ | |||
|
255 | `dump_file`+'.',sys_exit | |||
|
256 | if text_file: | |||
|
257 | text_file = unquote_filename(text_file) | |||
|
258 | pfile = open(text_file,'w') | |||
|
259 | pfile.write(output) | |||
|
260 | pfile.close() | |||
|
261 | print '\n*** Profile printout saved to text file',\ | |||
|
262 | `text_file`+'.',sys_exit | |||
|
263 | ||||
|
264 | if opts.has_key('r'): | |||
|
265 | return stats | |||
|
266 | else: | |||
|
267 | return None | |||
|
268 | ||||
|
269 | @line_magic | |||
|
270 | def pdb(self, parameter_s=''): | |||
|
271 | """Control the automatic calling of the pdb interactive debugger. | |||
|
272 | ||||
|
273 | Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without | |||
|
274 | argument it works as a toggle. | |||
|
275 | ||||
|
276 | When an exception is triggered, IPython can optionally call the | |||
|
277 | interactive pdb debugger after the traceback printout. %pdb toggles | |||
|
278 | this feature on and off. | |||
|
279 | ||||
|
280 | The initial state of this feature is set in your configuration | |||
|
281 | file (the option is ``InteractiveShell.pdb``). | |||
|
282 | ||||
|
283 | If you want to just activate the debugger AFTER an exception has fired, | |||
|
284 | without having to type '%pdb on' and rerunning your code, you can use | |||
|
285 | the %debug magic.""" | |||
|
286 | ||||
|
287 | par = parameter_s.strip().lower() | |||
|
288 | ||||
|
289 | if par: | |||
|
290 | try: | |||
|
291 | new_pdb = {'off':0,'0':0,'on':1,'1':1}[par] | |||
|
292 | except KeyError: | |||
|
293 | print ('Incorrect argument. Use on/1, off/0, ' | |||
|
294 | 'or nothing for a toggle.') | |||
|
295 | return | |||
|
296 | else: | |||
|
297 | # toggle | |||
|
298 | new_pdb = not self.shell.call_pdb | |||
|
299 | ||||
|
300 | # set on the shell | |||
|
301 | self.shell.call_pdb = new_pdb | |||
|
302 | print 'Automatic pdb calling has been turned',on_off(new_pdb) | |||
|
303 | ||||
|
304 | @line_magic | |||
|
305 | def debug(self, parameter_s=''): | |||
|
306 | """Activate the interactive debugger in post-mortem mode. | |||
|
307 | ||||
|
308 | If an exception has just occurred, this lets you inspect its stack | |||
|
309 | frames interactively. Note that this will always work only on the last | |||
|
310 | traceback that occurred, so you must call this quickly after an | |||
|
311 | exception that you wish to inspect has fired, because if another one | |||
|
312 | occurs, it clobbers the previous one. | |||
|
313 | ||||
|
314 | If you want IPython to automatically do this on every exception, see | |||
|
315 | the %pdb magic for more details. | |||
|
316 | """ | |||
|
317 | self.shell.debugger(force=True) | |||
|
318 | ||||
|
319 | @line_magic | |||
|
320 | def tb(self, s): | |||
|
321 | """Print the last traceback with the currently active exception mode. | |||
|
322 | ||||
|
323 | See %xmode for changing exception reporting modes.""" | |||
|
324 | self.shell.showtraceback() | |||
|
325 | ||||
|
326 | @skip_doctest | |||
|
327 | @line_magic | |||
|
328 | def run(self, parameter_s='', runner=None, | |||
|
329 | file_finder=get_py_filename): | |||
|
330 | """Run the named file inside IPython as a program. | |||
|
331 | ||||
|
332 | Usage:\\ | |||
|
333 | %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args] | |||
|
334 | ||||
|
335 | Parameters after the filename are passed as command-line arguments to | |||
|
336 | the program (put in sys.argv). Then, control returns to IPython's | |||
|
337 | prompt. | |||
|
338 | ||||
|
339 | This is similar to running at a system prompt:\\ | |||
|
340 | $ python file args\\ | |||
|
341 | but with the advantage of giving you IPython's tracebacks, and of | |||
|
342 | loading all variables into your interactive namespace for further use | |||
|
343 | (unless -p is used, see below). | |||
|
344 | ||||
|
345 | The file is executed in a namespace initially consisting only of | |||
|
346 | __name__=='__main__' and sys.argv constructed as indicated. It thus | |||
|
347 | sees its environment as if it were being run as a stand-alone program | |||
|
348 | (except for sharing global objects such as previously imported | |||
|
349 | modules). But after execution, the IPython interactive namespace gets | |||
|
350 | updated with all variables defined in the program (except for __name__ | |||
|
351 | and sys.argv). This allows for very convenient loading of code for | |||
|
352 | interactive work, while giving each program a 'clean sheet' to run in. | |||
|
353 | ||||
|
354 | Options: | |||
|
355 | ||||
|
356 | -n: __name__ is NOT set to '__main__', but to the running file's name | |||
|
357 | without extension (as python does under import). This allows running | |||
|
358 | scripts and reloading the definitions in them without calling code | |||
|
359 | protected by an ' if __name__ == "__main__" ' clause. | |||
|
360 | ||||
|
361 | -i: run the file in IPython's namespace instead of an empty one. This | |||
|
362 | is useful if you are experimenting with code written in a text editor | |||
|
363 | which depends on variables defined interactively. | |||
|
364 | ||||
|
365 | -e: ignore sys.exit() calls or SystemExit exceptions in the script | |||
|
366 | being run. This is particularly useful if IPython is being used to | |||
|
367 | run unittests, which always exit with a sys.exit() call. In such | |||
|
368 | cases you are interested in the output of the test results, not in | |||
|
369 | seeing a traceback of the unittest module. | |||
|
370 | ||||
|
371 | -t: print timing information at the end of the run. IPython will give | |||
|
372 | you an estimated CPU time consumption for your script, which under | |||
|
373 | Unix uses the resource module to avoid the wraparound problems of | |||
|
374 | time.clock(). Under Unix, an estimate of time spent on system tasks | |||
|
375 | is also given (for Windows platforms this is reported as 0.0). | |||
|
376 | ||||
|
377 | If -t is given, an additional -N<N> option can be given, where <N> | |||
|
378 | must be an integer indicating how many times you want the script to | |||
|
379 | run. The final timing report will include total and per run results. | |||
|
380 | ||||
|
381 | For example (testing the script uniq_stable.py):: | |||
|
382 | ||||
|
383 | In [1]: run -t uniq_stable | |||
|
384 | ||||
|
385 | IPython CPU timings (estimated):\\ | |||
|
386 | User : 0.19597 s.\\ | |||
|
387 | System: 0.0 s.\\ | |||
|
388 | ||||
|
389 | In [2]: run -t -N5 uniq_stable | |||
|
390 | ||||
|
391 | IPython CPU timings (estimated):\\ | |||
|
392 | Total runs performed: 5\\ | |||
|
393 | Times : Total Per run\\ | |||
|
394 | User : 0.910862 s, 0.1821724 s.\\ | |||
|
395 | System: 0.0 s, 0.0 s. | |||
|
396 | ||||
|
397 | -d: run your program under the control of pdb, the Python debugger. | |||
|
398 | This allows you to execute your program step by step, watch variables, | |||
|
399 | etc. Internally, what IPython does is similar to calling: | |||
|
400 | ||||
|
401 | pdb.run('execfile("YOURFILENAME")') | |||
|
402 | ||||
|
403 | with a breakpoint set on line 1 of your file. You can change the line | |||
|
404 | number for this automatic breakpoint to be <N> by using the -bN option | |||
|
405 | (where N must be an integer). For example:: | |||
|
406 | ||||
|
407 | %run -d -b40 myscript | |||
|
408 | ||||
|
409 | will set the first breakpoint at line 40 in myscript.py. Note that | |||
|
410 | the first breakpoint must be set on a line which actually does | |||
|
411 | something (not a comment or docstring) for it to stop execution. | |||
|
412 | ||||
|
413 | When the pdb debugger starts, you will see a (Pdb) prompt. You must | |||
|
414 | first enter 'c' (without quotes) to start execution up to the first | |||
|
415 | breakpoint. | |||
|
416 | ||||
|
417 | Entering 'help' gives information about the use of the debugger. You | |||
|
418 | can easily see pdb's full documentation with "import pdb;pdb.help()" | |||
|
419 | at a prompt. | |||
|
420 | ||||
|
421 | -p: run program under the control of the Python profiler module (which | |||
|
422 | prints a detailed report of execution times, function calls, etc). | |||
|
423 | ||||
|
424 | You can pass other options after -p which affect the behavior of the | |||
|
425 | profiler itself. See the docs for %prun for details. | |||
|
426 | ||||
|
427 | In this mode, the program's variables do NOT propagate back to the | |||
|
428 | IPython interactive namespace (because they remain in the namespace | |||
|
429 | where the profiler executes them). | |||
|
430 | ||||
|
431 | Internally this triggers a call to %prun, see its documentation for | |||
|
432 | details on the options available specifically for profiling. | |||
|
433 | ||||
|
434 | There is one special usage for which the text above doesn't apply: | |||
|
435 | if the filename ends with .ipy, the file is run as ipython script, | |||
|
436 | just as if the commands were written on IPython prompt. | |||
|
437 | ||||
|
438 | -m: specify module name to load instead of script path. Similar to | |||
|
439 | the -m option for the python interpreter. Use this option last if you | |||
|
440 | want to combine with other %run options. Unlike the python interpreter | |||
|
441 | only source modules are allowed no .pyc or .pyo files. | |||
|
442 | For example:: | |||
|
443 | ||||
|
444 | %run -m example | |||
|
445 | ||||
|
446 | will run the example module. | |||
|
447 | ||||
|
448 | """ | |||
|
449 | ||||
|
450 | # get arguments and set sys.argv for program to be run. | |||
|
451 | opts, arg_lst = self.parse_options(parameter_s, 'nidtN:b:pD:l:rs:T:em:', | |||
|
452 | mode='list', list_all=1) | |||
|
453 | if "m" in opts: | |||
|
454 | modulename = opts["m"][0] | |||
|
455 | modpath = find_mod(modulename) | |||
|
456 | if modpath is None: | |||
|
457 | warn('%r is not a valid modulename on sys.path'%modulename) | |||
|
458 | return | |||
|
459 | arg_lst = [modpath] + arg_lst | |||
|
460 | try: | |||
|
461 | filename = file_finder(arg_lst[0]) | |||
|
462 | except IndexError: | |||
|
463 | warn('you must provide at least a filename.') | |||
|
464 | print '\n%run:\n', oinspect.getdoc(self.run) | |||
|
465 | return | |||
|
466 | except IOError as e: | |||
|
467 | try: | |||
|
468 | msg = str(e) | |||
|
469 | except UnicodeError: | |||
|
470 | msg = e.message | |||
|
471 | error(msg) | |||
|
472 | return | |||
|
473 | ||||
|
474 | if filename.lower().endswith('.ipy'): | |||
|
475 | self.shell.safe_execfile_ipy(filename) | |||
|
476 | return | |||
|
477 | ||||
|
478 | # Control the response to exit() calls made by the script being run | |||
|
479 | exit_ignore = 'e' in opts | |||
|
480 | ||||
|
481 | # Make sure that the running script gets a proper sys.argv as if it | |||
|
482 | # were run from a system shell. | |||
|
483 | save_argv = sys.argv # save it for later restoring | |||
|
484 | ||||
|
485 | # simulate shell expansion on arguments, at least tilde expansion | |||
|
486 | args = [ os.path.expanduser(a) for a in arg_lst[1:] ] | |||
|
487 | ||||
|
488 | sys.argv = [filename] + args # put in the proper filename | |||
|
489 | # protect sys.argv from potential unicode strings on Python 2: | |||
|
490 | if not py3compat.PY3: | |||
|
491 | sys.argv = [ py3compat.cast_bytes(a) for a in sys.argv ] | |||
|
492 | ||||
|
493 | if 'i' in opts: | |||
|
494 | # Run in user's interactive namespace | |||
|
495 | prog_ns = self.shell.user_ns | |||
|
496 | __name__save = self.shell.user_ns['__name__'] | |||
|
497 | prog_ns['__name__'] = '__main__' | |||
|
498 | main_mod = self.shell.new_main_mod(prog_ns) | |||
|
499 | else: | |||
|
500 | # Run in a fresh, empty namespace | |||
|
501 | if 'n' in opts: | |||
|
502 | name = os.path.splitext(os.path.basename(filename))[0] | |||
|
503 | else: | |||
|
504 | name = '__main__' | |||
|
505 | ||||
|
506 | main_mod = self.shell.new_main_mod() | |||
|
507 | prog_ns = main_mod.__dict__ | |||
|
508 | prog_ns['__name__'] = name | |||
|
509 | ||||
|
510 | # Since '%run foo' emulates 'python foo.py' at the cmd line, we must | |||
|
511 | # set the __file__ global in the script's namespace | |||
|
512 | prog_ns['__file__'] = filename | |||
|
513 | ||||
|
514 | # pickle fix. See interactiveshell for an explanation. But we need to | |||
|
515 | # make sure that, if we overwrite __main__, we replace it at the end | |||
|
516 | main_mod_name = prog_ns['__name__'] | |||
|
517 | ||||
|
518 | if main_mod_name == '__main__': | |||
|
519 | restore_main = sys.modules['__main__'] | |||
|
520 | else: | |||
|
521 | restore_main = False | |||
|
522 | ||||
|
523 | # This needs to be undone at the end to prevent holding references to | |||
|
524 | # every single object ever created. | |||
|
525 | sys.modules[main_mod_name] = main_mod | |||
|
526 | ||||
|
527 | try: | |||
|
528 | stats = None | |||
|
529 | with self.shell.readline_no_record: | |||
|
530 | if 'p' in opts: | |||
|
531 | stats = self.prun('', None, False, opts, arg_lst, prog_ns) | |||
|
532 | else: | |||
|
533 | if 'd' in opts: | |||
|
534 | deb = debugger.Pdb(self.shell.colors) | |||
|
535 | # reset Breakpoint state, which is moronically kept | |||
|
536 | # in a class | |||
|
537 | bdb.Breakpoint.next = 1 | |||
|
538 | bdb.Breakpoint.bplist = {} | |||
|
539 | bdb.Breakpoint.bpbynumber = [None] | |||
|
540 | # Set an initial breakpoint to stop execution | |||
|
541 | maxtries = 10 | |||
|
542 | bp = int(opts.get('b', [1])[0]) | |||
|
543 | checkline = deb.checkline(filename, bp) | |||
|
544 | if not checkline: | |||
|
545 | for bp in range(bp + 1, bp + maxtries + 1): | |||
|
546 | if deb.checkline(filename, bp): | |||
|
547 | break | |||
|
548 | else: | |||
|
549 | msg = ("\nI failed to find a valid line to set " | |||
|
550 | "a breakpoint\n" | |||
|
551 | "after trying up to line: %s.\n" | |||
|
552 | "Please set a valid breakpoint manually " | |||
|
553 | "with the -b option." % bp) | |||
|
554 | error(msg) | |||
|
555 | return | |||
|
556 | # if we find a good linenumber, set the breakpoint | |||
|
557 | deb.do_break('%s:%s' % (filename, bp)) | |||
|
558 | # Start file run | |||
|
559 | print "NOTE: Enter 'c' at the", | |||
|
560 | print "%s prompt to start your script." % deb.prompt | |||
|
561 | ns = {'execfile': py3compat.execfile, 'prog_ns': prog_ns} | |||
|
562 | try: | |||
|
563 | deb.run('execfile("%s", prog_ns)' % filename, ns) | |||
|
564 | ||||
|
565 | except: | |||
|
566 | etype, value, tb = sys.exc_info() | |||
|
567 | # Skip three frames in the traceback: the %run one, | |||
|
568 | # one inside bdb.py, and the command-line typed by the | |||
|
569 | # user (run by exec in pdb itself). | |||
|
570 | self.shell.InteractiveTB(etype, value, tb, tb_offset=3) | |||
|
571 | else: | |||
|
572 | if runner is None: | |||
|
573 | runner = self.default_runner | |||
|
574 | if runner is None: | |||
|
575 | runner = self.shell.safe_execfile | |||
|
576 | if 't' in opts: | |||
|
577 | # timed execution | |||
|
578 | try: | |||
|
579 | nruns = int(opts['N'][0]) | |||
|
580 | if nruns < 1: | |||
|
581 | error('Number of runs must be >=1') | |||
|
582 | return | |||
|
583 | except (KeyError): | |||
|
584 | nruns = 1 | |||
|
585 | twall0 = time.time() | |||
|
586 | if nruns == 1: | |||
|
587 | t0 = clock2() | |||
|
588 | runner(filename, prog_ns, prog_ns, | |||
|
589 | exit_ignore=exit_ignore) | |||
|
590 | t1 = clock2() | |||
|
591 | t_usr = t1[0] - t0[0] | |||
|
592 | t_sys = t1[1] - t0[1] | |||
|
593 | print "\nIPython CPU timings (estimated):" | |||
|
594 | print " User : %10.2f s." % t_usr | |||
|
595 | print " System : %10.2f s." % t_sys | |||
|
596 | else: | |||
|
597 | runs = range(nruns) | |||
|
598 | t0 = clock2() | |||
|
599 | for nr in runs: | |||
|
600 | runner(filename, prog_ns, prog_ns, | |||
|
601 | exit_ignore=exit_ignore) | |||
|
602 | t1 = clock2() | |||
|
603 | t_usr = t1[0] - t0[0] | |||
|
604 | t_sys = t1[1] - t0[1] | |||
|
605 | print "\nIPython CPU timings (estimated):" | |||
|
606 | print "Total runs performed:", nruns | |||
|
607 | print " Times : %10.2f %10.2f" % ('Total', 'Per run') | |||
|
608 | print " User : %10.2f s, %10.2f s." % (t_usr, t_usr / nruns) | |||
|
609 | print " System : %10.2f s, %10.2f s." % (t_sys, t_sys / nruns) | |||
|
610 | twall1 = time.time() | |||
|
611 | print "Wall time: %10.2f s." % (twall1 - twall0) | |||
|
612 | ||||
|
613 | else: | |||
|
614 | # regular execution | |||
|
615 | runner(filename, prog_ns, prog_ns, exit_ignore=exit_ignore) | |||
|
616 | ||||
|
617 | if 'i' in opts: | |||
|
618 | self.shell.user_ns['__name__'] = __name__save | |||
|
619 | else: | |||
|
620 | # The shell MUST hold a reference to prog_ns so after %run | |||
|
621 | # exits, the python deletion mechanism doesn't zero it out | |||
|
622 | # (leaving dangling references). | |||
|
623 | self.shell.cache_main_mod(prog_ns, filename) | |||
|
624 | # update IPython interactive namespace | |||
|
625 | ||||
|
626 | # Some forms of read errors on the file may mean the | |||
|
627 | # __name__ key was never set; using pop we don't have to | |||
|
628 | # worry about a possible KeyError. | |||
|
629 | prog_ns.pop('__name__', None) | |||
|
630 | ||||
|
631 | self.shell.user_ns.update(prog_ns) | |||
|
632 | finally: | |||
|
633 | # It's a bit of a mystery why, but __builtins__ can change from | |||
|
634 | # being a module to becoming a dict missing some key data after | |||
|
635 | # %run. As best I can see, this is NOT something IPython is doing | |||
|
636 | # at all, and similar problems have been reported before: | |||
|
637 | # http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-10/0188.html | |||
|
638 | # Since this seems to be done by the interpreter itself, the best | |||
|
639 | # we can do is to at least restore __builtins__ for the user on | |||
|
640 | # exit. | |||
|
641 | self.shell.user_ns['__builtins__'] = builtin_mod | |||
|
642 | ||||
|
643 | # Ensure key global structures are restored | |||
|
644 | sys.argv = save_argv | |||
|
645 | if restore_main: | |||
|
646 | sys.modules['__main__'] = restore_main | |||
|
647 | else: | |||
|
648 | # Remove from sys.modules the reference to main_mod we'd | |||
|
649 | # added. Otherwise it will trap references to objects | |||
|
650 | # contained therein. | |||
|
651 | del sys.modules[main_mod_name] | |||
|
652 | ||||
|
653 | return stats | |||
|
654 | ||||
|
655 | @skip_doctest | |||
|
656 | @line_cell_magic | |||
|
657 | def timeit(self, line='', cell=None): | |||
|
658 | """Time execution of a Python statement or expression | |||
|
659 | ||||
|
660 | Usage, in line mode: | |||
|
661 | %timeit [-n<N> -r<R> [-t|-c]] statement | |||
|
662 | or in cell mode: | |||
|
663 | %%timeit [-n<N> -r<R> [-t|-c]] setup_code | |||
|
664 | code | |||
|
665 | code... | |||
|
666 | ||||
|
667 | Time execution of a Python statement or expression using the timeit | |||
|
668 | module. This function can be used both as a line and cell magic: | |||
|
669 | ||||
|
670 | - In line mode you can time a single-line statement (though multiple | |||
|
671 | ones can be chained with using semicolons). | |||
|
672 | ||||
|
673 | - In cell mode, the statement in the first line is used as setup code | |||
|
674 | (executed but not timed) and the body of the cell is timed. The cell | |||
|
675 | body has access to any variables created in the setup code. | |||
|
676 | ||||
|
677 | Options: | |||
|
678 | -n<N>: execute the given statement <N> times in a loop. If this value | |||
|
679 | is not given, a fitting value is chosen. | |||
|
680 | ||||
|
681 | -r<R>: repeat the loop iteration <R> times and take the best result. | |||
|
682 | Default: 3 | |||
|
683 | ||||
|
684 | -t: use time.time to measure the time, which is the default on Unix. | |||
|
685 | This function measures wall time. | |||
|
686 | ||||
|
687 | -c: use time.clock to measure the time, which is the default on | |||
|
688 | Windows and measures wall time. On Unix, resource.getrusage is used | |||
|
689 | instead and returns the CPU user time. | |||
|
690 | ||||
|
691 | -p<P>: use a precision of <P> digits to display the timing result. | |||
|
692 | Default: 3 | |||
|
693 | ||||
|
694 | ||||
|
695 | Examples | |||
|
696 | -------- | |||
|
697 | :: | |||
|
698 | ||||
|
699 | In [1]: %timeit pass | |||
|
700 | 10000000 loops, best of 3: 53.3 ns per loop | |||
|
701 | ||||
|
702 | In [2]: u = None | |||
|
703 | ||||
|
704 | In [3]: %timeit u is None | |||
|
705 | 10000000 loops, best of 3: 184 ns per loop | |||
|
706 | ||||
|
707 | In [4]: %timeit -r 4 u == None | |||
|
708 | 1000000 loops, best of 4: 242 ns per loop | |||
|
709 | ||||
|
710 | In [5]: import time | |||
|
711 | ||||
|
712 | In [6]: %timeit -n1 time.sleep(2) | |||
|
713 | 1 loops, best of 3: 2 s per loop | |||
|
714 | ||||
|
715 | ||||
|
716 | The times reported by %timeit will be slightly higher than those | |||
|
717 | reported by the timeit.py script when variables are accessed. This is | |||
|
718 | due to the fact that %timeit executes the statement in the namespace | |||
|
719 | of the shell, compared with timeit.py, which uses a single setup | |||
|
720 | statement to import function or create variables. Generally, the bias | |||
|
721 | does not matter as long as results from timeit.py are not mixed with | |||
|
722 | those from %timeit.""" | |||
|
723 | ||||
|
724 | import timeit | |||
|
725 | import math | |||
|
726 | ||||
|
727 | # XXX: Unfortunately the unicode 'micro' symbol can cause problems in | |||
|
728 | # certain terminals. Until we figure out a robust way of | |||
|
729 | # auto-detecting if the terminal can deal with it, use plain 'us' for | |||
|
730 | # microseconds. I am really NOT happy about disabling the proper | |||
|
731 | # 'micro' prefix, but crashing is worse... If anyone knows what the | |||
|
732 | # right solution for this is, I'm all ears... | |||
|
733 | # | |||
|
734 | # Note: using | |||
|
735 | # | |||
|
736 | # s = u'\xb5' | |||
|
737 | # s.encode(sys.getdefaultencoding()) | |||
|
738 | # | |||
|
739 | # is not sufficient, as I've seen terminals where that fails but | |||
|
740 | # print s | |||
|
741 | # | |||
|
742 | # succeeds | |||
|
743 | # | |||
|
744 | # See bug: https://bugs.launchpad.net/ipython/+bug/348466 | |||
|
745 | ||||
|
746 | #units = [u"s", u"ms",u'\xb5',"ns"] | |||
|
747 | units = [u"s", u"ms",u'us',"ns"] | |||
|
748 | ||||
|
749 | scaling = [1, 1e3, 1e6, 1e9] | |||
|
750 | ||||
|
751 | opts, stmt = self.parse_options(line,'n:r:tcp:', | |||
|
752 | posix=False, strict=False) | |||
|
753 | if stmt == "": | |||
|
754 | return | |||
|
755 | timefunc = timeit.default_timer | |||
|
756 | number = int(getattr(opts, "n", 0)) | |||
|
757 | repeat = int(getattr(opts, "r", timeit.default_repeat)) | |||
|
758 | precision = int(getattr(opts, "p", 3)) | |||
|
759 | if hasattr(opts, "t"): | |||
|
760 | timefunc = time.time | |||
|
761 | if hasattr(opts, "c"): | |||
|
762 | timefunc = clock | |||
|
763 | ||||
|
764 | timer = timeit.Timer(timer=timefunc) | |||
|
765 | # this code has tight coupling to the inner workings of timeit.Timer, | |||
|
766 | # but is there a better way to achieve that the code stmt has access | |||
|
767 | # to the shell namespace? | |||
|
768 | ||||
|
769 | if cell is None: | |||
|
770 | # called as line magic | |||
|
771 | setup = 'pass' | |||
|
772 | stmt = timeit.reindent(stmt, 8) | |||
|
773 | else: | |||
|
774 | setup = timeit.reindent(stmt, 4) | |||
|
775 | stmt = timeit.reindent(cell, 8) | |||
|
776 | ||||
|
777 | src = timeit.template % dict(stmt=stmt, setup=setup) | |||
|
778 | # Track compilation time so it can be reported if too long | |||
|
779 | # Minimum time above which compilation time will be reported | |||
|
780 | tc_min = 0.1 | |||
|
781 | ||||
|
782 | t0 = clock() | |||
|
783 | code = compile(src, "<magic-timeit>", "exec") | |||
|
784 | tc = clock()-t0 | |||
|
785 | ||||
|
786 | ns = {} | |||
|
787 | exec code in self.shell.user_ns, ns | |||
|
788 | timer.inner = ns["inner"] | |||
|
789 | ||||
|
790 | if number == 0: | |||
|
791 | # determine number so that 0.2 <= total time < 2.0 | |||
|
792 | number = 1 | |||
|
793 | for i in range(1, 10): | |||
|
794 | if timer.timeit(number) >= 0.2: | |||
|
795 | break | |||
|
796 | number *= 10 | |||
|
797 | ||||
|
798 | best = min(timer.repeat(repeat, number)) / number | |||
|
799 | ||||
|
800 | if best > 0.0 and best < 1000.0: | |||
|
801 | order = min(-int(math.floor(math.log10(best)) // 3), 3) | |||
|
802 | elif best >= 1000.0: | |||
|
803 | order = 0 | |||
|
804 | else: | |||
|
805 | order = 3 | |||
|
806 | print u"%d loops, best of %d: %.*g %s per loop" % (number, repeat, | |||
|
807 | precision, | |||
|
808 | best * scaling[order], | |||
|
809 | units[order]) | |||
|
810 | if tc > tc_min: | |||
|
811 | print "Compiler time: %.2f s" % tc | |||
|
812 | ||||
|
813 | @skip_doctest | |||
|
814 | @needs_local_scope | |||
|
815 | @line_magic | |||
|
816 | def time(self,parameter_s, user_locals): | |||
|
817 | """Time execution of a Python statement or expression. | |||
|
818 | ||||
|
819 | The CPU and wall clock times are printed, and the value of the | |||
|
820 | expression (if any) is returned. Note that under Win32, system time | |||
|
821 | is always reported as 0, since it can not be measured. | |||
|
822 | ||||
|
823 | This function provides very basic timing functionality. In Python | |||
|
824 | 2.3, the timeit module offers more control and sophistication, so this | |||
|
825 | could be rewritten to use it (patches welcome). | |||
|
826 | ||||
|
827 | Examples | |||
|
828 | -------- | |||
|
829 | :: | |||
|
830 | ||||
|
831 | In [1]: time 2**128 | |||
|
832 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s | |||
|
833 | Wall time: 0.00 | |||
|
834 | Out[1]: 340282366920938463463374607431768211456L | |||
|
835 | ||||
|
836 | In [2]: n = 1000000 | |||
|
837 | ||||
|
838 | In [3]: time sum(range(n)) | |||
|
839 | CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s | |||
|
840 | Wall time: 1.37 | |||
|
841 | Out[3]: 499999500000L | |||
|
842 | ||||
|
843 | In [4]: time print 'hello world' | |||
|
844 | hello world | |||
|
845 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s | |||
|
846 | Wall time: 0.00 | |||
|
847 | ||||
|
848 | Note that the time needed by Python to compile the given expression | |||
|
849 | will be reported if it is more than 0.1s. In this example, the | |||
|
850 | actual exponentiation is done by Python at compilation time, so while | |||
|
851 | the expression can take a noticeable amount of time to compute, that | |||
|
852 | time is purely due to the compilation: | |||
|
853 | ||||
|
854 | In [5]: time 3**9999; | |||
|
855 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s | |||
|
856 | Wall time: 0.00 s | |||
|
857 | ||||
|
858 | In [6]: time 3**999999; | |||
|
859 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s | |||
|
860 | Wall time: 0.00 s | |||
|
861 | Compiler : 0.78 s | |||
|
862 | """ | |||
|
863 | ||||
|
864 | # fail immediately if the given expression can't be compiled | |||
|
865 | ||||
|
866 | expr = self.shell.prefilter(parameter_s,False) | |||
|
867 | ||||
|
868 | # Minimum time above which compilation time will be reported | |||
|
869 | tc_min = 0.1 | |||
|
870 | ||||
|
871 | try: | |||
|
872 | mode = 'eval' | |||
|
873 | t0 = clock() | |||
|
874 | code = compile(expr,'<timed eval>',mode) | |||
|
875 | tc = clock()-t0 | |||
|
876 | except SyntaxError: | |||
|
877 | mode = 'exec' | |||
|
878 | t0 = clock() | |||
|
879 | code = compile(expr,'<timed exec>',mode) | |||
|
880 | tc = clock()-t0 | |||
|
881 | # skew measurement as little as possible | |||
|
882 | glob = self.shell.user_ns | |||
|
883 | wtime = time.time | |||
|
884 | # time execution | |||
|
885 | wall_st = wtime() | |||
|
886 | if mode=='eval': | |||
|
887 | st = clock2() | |||
|
888 | out = eval(code, glob, user_locals) | |||
|
889 | end = clock2() | |||
|
890 | else: | |||
|
891 | st = clock2() | |||
|
892 | exec code in glob, user_locals | |||
|
893 | end = clock2() | |||
|
894 | out = None | |||
|
895 | wall_end = wtime() | |||
|
896 | # Compute actual times and report | |||
|
897 | wall_time = wall_end-wall_st | |||
|
898 | cpu_user = end[0]-st[0] | |||
|
899 | cpu_sys = end[1]-st[1] | |||
|
900 | cpu_tot = cpu_user+cpu_sys | |||
|
901 | print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \ | |||
|
902 | (cpu_user,cpu_sys,cpu_tot) | |||
|
903 | print "Wall time: %.2f s" % wall_time | |||
|
904 | if tc > tc_min: | |||
|
905 | print "Compiler : %.2f s" % tc | |||
|
906 | return out | |||
|
907 | ||||
|
908 | @skip_doctest | |||
|
909 | @line_magic | |||
|
910 | def macro(self, parameter_s=''): | |||
|
911 | """Define a macro for future re-execution. It accepts ranges of history, | |||
|
912 | filenames or string objects. | |||
|
913 | ||||
|
914 | Usage:\\ | |||
|
915 | %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ... | |||
|
916 | ||||
|
917 | Options: | |||
|
918 | ||||
|
919 | -r: use 'raw' input. By default, the 'processed' history is used, | |||
|
920 | so that magics are loaded in their transformed version to valid | |||
|
921 | Python. If this option is given, the raw input as typed as the | |||
|
922 | command line is used instead. | |||
|
923 | ||||
|
924 | This will define a global variable called `name` which is a string | |||
|
925 | made of joining the slices and lines you specify (n1,n2,... numbers | |||
|
926 | above) from your input history into a single string. This variable | |||
|
927 | acts like an automatic function which re-executes those lines as if | |||
|
928 | you had typed them. You just type 'name' at the prompt and the code | |||
|
929 | executes. | |||
|
930 | ||||
|
931 | The syntax for indicating input ranges is described in %history. | |||
|
932 | ||||
|
933 | Note: as a 'hidden' feature, you can also use traditional python slice | |||
|
934 | notation, where N:M means numbers N through M-1. | |||
|
935 | ||||
|
936 | For example, if your history contains (%hist prints it):: | |||
|
937 | ||||
|
938 | 44: x=1 | |||
|
939 | 45: y=3 | |||
|
940 | 46: z=x+y | |||
|
941 | 47: print x | |||
|
942 | 48: a=5 | |||
|
943 | 49: print 'x',x,'y',y | |||
|
944 | ||||
|
945 | you can create a macro with lines 44 through 47 (included) and line 49 | |||
|
946 | called my_macro with:: | |||
|
947 | ||||
|
948 | In [55]: %macro my_macro 44-47 49 | |||
|
949 | ||||
|
950 | Now, typing `my_macro` (without quotes) will re-execute all this code | |||
|
951 | in one pass. | |||
|
952 | ||||
|
953 | You don't need to give the line-numbers in order, and any given line | |||
|
954 | number can appear multiple times. You can assemble macros with any | |||
|
955 | lines from your input history in any order. | |||
|
956 | ||||
|
957 | The macro is a simple object which holds its value in an attribute, | |||
|
958 | but IPython's display system checks for macros and executes them as | |||
|
959 | code instead of printing them when you type their name. | |||
|
960 | ||||
|
961 | You can view a macro's contents by explicitly printing it with:: | |||
|
962 | ||||
|
963 | print macro_name | |||
|
964 | ||||
|
965 | """ | |||
|
966 | opts,args = self.parse_options(parameter_s,'r',mode='list') | |||
|
967 | if not args: # List existing macros | |||
|
968 | return sorted(k for k,v in self.shell.user_ns.iteritems() if\ | |||
|
969 | isinstance(v, Macro)) | |||
|
970 | if len(args) == 1: | |||
|
971 | raise UsageError( | |||
|
972 | "%macro insufficient args; usage '%macro name n1-n2 n3-4...") | |||
|
973 | name, codefrom = args[0], " ".join(args[1:]) | |||
|
974 | ||||
|
975 | #print 'rng',ranges # dbg | |||
|
976 | try: | |||
|
977 | lines = self.shell.find_user_code(codefrom, 'r' in opts) | |||
|
978 | except (ValueError, TypeError) as e: | |||
|
979 | print e.args[0] | |||
|
980 | return | |||
|
981 | macro = Macro(lines) | |||
|
982 | self.shell.define_macro(name, macro) | |||
|
983 | print 'Macro `%s` created. To execute, type its name (without quotes).' % name | |||
|
984 | print '=== Macro contents: ===' | |||
|
985 | print macro, |
@@ -0,0 +1,69 b'' | |||||
|
1 | """Implementation of magic functions for the extension machinery. | |||
|
2 | """ | |||
|
3 | #----------------------------------------------------------------------------- | |||
|
4 | # Copyright (c) 2012 The IPython Development Team. | |||
|
5 | # | |||
|
6 | # Distributed under the terms of the Modified BSD License. | |||
|
7 | # | |||
|
8 | # The full license is in the file COPYING.txt, distributed with this software. | |||
|
9 | #----------------------------------------------------------------------------- | |||
|
10 | ||||
|
11 | #----------------------------------------------------------------------------- | |||
|
12 | # Imports | |||
|
13 | #----------------------------------------------------------------------------- | |||
|
14 | ||||
|
15 | # Stdlib | |||
|
16 | import os | |||
|
17 | ||||
|
18 | # Our own packages | |||
|
19 | from IPython.core.magic import Magics, magics_class, line_magic | |||
|
20 | ||||
|
21 | #----------------------------------------------------------------------------- | |||
|
22 | # Magic implementation classes | |||
|
23 | #----------------------------------------------------------------------------- | |||
|
24 | ||||
|
25 | @magics_class | |||
|
26 | class ExtensionMagics(Magics): | |||
|
27 | """Magics to manage the IPython extensions system.""" | |||
|
28 | ||||
|
29 | @line_magic | |||
|
30 | def install_ext(self, parameter_s=''): | |||
|
31 | """Download and install an extension from a URL, e.g.:: | |||
|
32 | ||||
|
33 | %install_ext https://bitbucket.org/birkenfeld/ipython-physics/raw/d1310a2ab15d/physics.py | |||
|
34 | ||||
|
35 | The URL should point to an importable Python module - either a .py file | |||
|
36 | or a .zip file. | |||
|
37 | ||||
|
38 | Parameters: | |||
|
39 | ||||
|
40 | -n filename : Specify a name for the file, rather than taking it from | |||
|
41 | the URL. | |||
|
42 | """ | |||
|
43 | opts, args = self.parse_options(parameter_s, 'n:') | |||
|
44 | try: | |||
|
45 | filename = self.shell.extension_manager.install_extension(args, | |||
|
46 | opts.get('n')) | |||
|
47 | except ValueError as e: | |||
|
48 | print e | |||
|
49 | return | |||
|
50 | ||||
|
51 | filename = os.path.basename(filename) | |||
|
52 | print "Installed %s. To use it, type:" % filename | |||
|
53 | print " %%load_ext %s" % os.path.splitext(filename)[0] | |||
|
54 | ||||
|
55 | ||||
|
56 | @line_magic | |||
|
57 | def load_ext(self, module_str): | |||
|
58 | """Load an IPython extension by its module name.""" | |||
|
59 | return self.shell.extension_manager.load_extension(module_str) | |||
|
60 | ||||
|
61 | @line_magic | |||
|
62 | def unload_ext(self, module_str): | |||
|
63 | """Unload an IPython extension by its module name.""" | |||
|
64 | self.shell.extension_manager.unload_extension(module_str) | |||
|
65 | ||||
|
66 | @line_magic | |||
|
67 | def reload_ext(self, module_str): | |||
|
68 | """Reload an IPython extension by its module name.""" | |||
|
69 | self.shell.extension_manager.reload_extension(module_str) |
@@ -0,0 +1,294 b'' | |||||
|
1 | """Implementation of magic functions related to History. | |||
|
2 | """ | |||
|
3 | #----------------------------------------------------------------------------- | |||
|
4 | # Copyright (c) 2012, IPython Development Team. | |||
|
5 | # | |||
|
6 | # Distributed under the terms of the Modified BSD License. | |||
|
7 | # | |||
|
8 | # The full license is in the file COPYING.txt, distributed with this software. | |||
|
9 | #----------------------------------------------------------------------------- | |||
|
10 | ||||
|
11 | #----------------------------------------------------------------------------- | |||
|
12 | # Imports | |||
|
13 | #----------------------------------------------------------------------------- | |||
|
14 | from __future__ import print_function | |||
|
15 | ||||
|
16 | # Stdlib | |||
|
17 | import os | |||
|
18 | from io import open as io_open | |||
|
19 | ||||
|
20 | # Our own packages | |||
|
21 | from IPython.core.error import StdinNotImplementedError | |||
|
22 | from IPython.core.magic import Magics, magics_class, line_magic | |||
|
23 | from IPython.testing.skipdoctest import skip_doctest | |||
|
24 | from IPython.utils import io | |||
|
25 | ||||
|
26 | #----------------------------------------------------------------------------- | |||
|
27 | # Magics class implementation | |||
|
28 | #----------------------------------------------------------------------------- | |||
|
29 | ||||
|
30 | @magics_class | |||
|
31 | class HistoryMagics(Magics): | |||
|
32 | ||||
|
33 | @skip_doctest | |||
|
34 | @line_magic | |||
|
35 | def history(self, parameter_s = ''): | |||
|
36 | """Print input history (_i<n> variables), with most recent last. | |||
|
37 | ||||
|
38 | %history [-o -p -t -n] [-f filename] [range | -g pattern | -l number] | |||
|
39 | ||||
|
40 | By default, input history is printed without line numbers so it can be | |||
|
41 | directly pasted into an editor. Use -n to show them. | |||
|
42 | ||||
|
43 | By default, all input history from the current session is displayed. | |||
|
44 | Ranges of history can be indicated using the syntax: | |||
|
45 | 4 : Line 4, current session | |||
|
46 | 4-6 : Lines 4-6, current session | |||
|
47 | 243/1-5: Lines 1-5, session 243 | |||
|
48 | ~2/7 : Line 7, session 2 before current | |||
|
49 | ~8/1-~6/5 : From the first line of 8 sessions ago, to the fifth line | |||
|
50 | of 6 sessions ago. | |||
|
51 | Multiple ranges can be entered, separated by spaces | |||
|
52 | ||||
|
53 | The same syntax is used by %macro, %save, %edit, %rerun | |||
|
54 | ||||
|
55 | Options: | |||
|
56 | ||||
|
57 | -n: print line numbers for each input. | |||
|
58 | This feature is only available if numbered prompts are in use. | |||
|
59 | ||||
|
60 | -o: also print outputs for each input. | |||
|
61 | ||||
|
62 | -p: print classic '>>>' python prompts before each input. This is | |||
|
63 | useful for making documentation, and in conjunction with -o, for | |||
|
64 | producing doctest-ready output. | |||
|
65 | ||||
|
66 | -r: (default) print the 'raw' history, i.e. the actual commands you | |||
|
67 | typed. | |||
|
68 | ||||
|
69 | -t: print the 'translated' history, as IPython understands it. | |||
|
70 | IPython filters your input and converts it all into valid Python | |||
|
71 | source before executing it (things like magics or aliases are turned | |||
|
72 | into function calls, for example). With this option, you'll see the | |||
|
73 | native history instead of the user-entered version: '%cd /' will be | |||
|
74 | seen as 'get_ipython().magic("%cd /")' instead of '%cd /'. | |||
|
75 | ||||
|
76 | -g: treat the arg as a pattern to grep for in (full) history. | |||
|
77 | This includes the saved history (almost all commands ever written). | |||
|
78 | Use '%hist -g' to show full saved history (may be very long). | |||
|
79 | ||||
|
80 | -l: get the last n lines from all sessions. Specify n as a single | |||
|
81 | arg, or the default is the last 10 lines. | |||
|
82 | ||||
|
83 | -f FILENAME: instead of printing the output to the screen, redirect | |||
|
84 | it to the given file. The file is always overwritten, though *when | |||
|
85 | it can*, IPython asks for confirmation first. In particular, running | |||
|
86 | the command 'history -f FILENAME' from the IPython Notebook | |||
|
87 | interface will replace FILENAME even if it already exists *without* | |||
|
88 | confirmation. | |||
|
89 | ||||
|
90 | Examples | |||
|
91 | -------- | |||
|
92 | :: | |||
|
93 | ||||
|
94 | In [6]: %hist -n 4-6 | |||
|
95 | 4:a = 12 | |||
|
96 | 5:print a**2 | |||
|
97 | 6:%hist -n 4-6 | |||
|
98 | ||||
|
99 | """ | |||
|
100 | ||||
|
101 | if not self.shell.displayhook.do_full_cache: | |||
|
102 | print('This feature is only available if numbered prompts ' | |||
|
103 | 'are in use.') | |||
|
104 | return | |||
|
105 | opts,args = self.parse_options(parameter_s,'noprtglf:',mode='string') | |||
|
106 | ||||
|
107 | # For brevity | |||
|
108 | history_manager = self.shell.history_manager | |||
|
109 | ||||
|
110 | def _format_lineno(session, line): | |||
|
111 | """Helper function to format line numbers properly.""" | |||
|
112 | if session in (0, history_manager.session_number): | |||
|
113 | return str(line) | |||
|
114 | return "%s/%s" % (session, line) | |||
|
115 | ||||
|
116 | # Check if output to specific file was requested. | |||
|
117 | try: | |||
|
118 | outfname = opts['f'] | |||
|
119 | except KeyError: | |||
|
120 | outfile = io.stdout # default | |||
|
121 | # We don't want to close stdout at the end! | |||
|
122 | close_at_end = False | |||
|
123 | else: | |||
|
124 | if os.path.exists(outfname): | |||
|
125 | try: | |||
|
126 | ans = io.ask_yes_no("File %r exists. Overwrite?" % outfname) | |||
|
127 | except StdinNotImplementedError: | |||
|
128 | ans = True | |||
|
129 | if not ans: | |||
|
130 | print('Aborting.') | |||
|
131 | return | |||
|
132 | print("Overwriting file.") | |||
|
133 | outfile = io_open(outfname, 'w', encoding='utf-8') | |||
|
134 | close_at_end = True | |||
|
135 | ||||
|
136 | print_nums = 'n' in opts | |||
|
137 | get_output = 'o' in opts | |||
|
138 | pyprompts = 'p' in opts | |||
|
139 | # Raw history is the default | |||
|
140 | raw = not('t' in opts) | |||
|
141 | ||||
|
142 | pattern = None | |||
|
143 | ||||
|
144 | if 'g' in opts: # Glob search | |||
|
145 | pattern = "*" + args + "*" if args else "*" | |||
|
146 | hist = history_manager.search(pattern, raw=raw, output=get_output) | |||
|
147 | print_nums = True | |||
|
148 | elif 'l' in opts: # Get 'tail' | |||
|
149 | try: | |||
|
150 | n = int(args) | |||
|
151 | except (ValueError, IndexError): | |||
|
152 | n = 10 | |||
|
153 | hist = history_manager.get_tail(n, raw=raw, output=get_output) | |||
|
154 | else: | |||
|
155 | if args: # Get history by ranges | |||
|
156 | hist = history_manager.get_range_by_str(args, raw, get_output) | |||
|
157 | else: # Just get history for the current session | |||
|
158 | hist = history_manager.get_range(raw=raw, output=get_output) | |||
|
159 | ||||
|
160 | # We could be displaying the entire history, so let's not try to pull | |||
|
161 | # it into a list in memory. Anything that needs more space will just | |||
|
162 | # misalign. | |||
|
163 | width = 4 | |||
|
164 | ||||
|
165 | for session, lineno, inline in hist: | |||
|
166 | # Print user history with tabs expanded to 4 spaces. The GUI | |||
|
167 | # clients use hard tabs for easier usability in auto-indented code, | |||
|
168 | # but we want to produce PEP-8 compliant history for safe pasting | |||
|
169 | # into an editor. | |||
|
170 | if get_output: | |||
|
171 | inline, output = inline | |||
|
172 | inline = inline.expandtabs(4).rstrip() | |||
|
173 | ||||
|
174 | multiline = "\n" in inline | |||
|
175 | line_sep = '\n' if multiline else ' ' | |||
|
176 | if print_nums: | |||
|
177 | print(u'%s:%s' % (_format_lineno(session, lineno).rjust(width), | |||
|
178 | line_sep), file=outfile, end=u'') | |||
|
179 | if pyprompts: | |||
|
180 | print(u">>> ", end=u"", file=outfile) | |||
|
181 | if multiline: | |||
|
182 | inline = "\n... ".join(inline.splitlines()) + "\n..." | |||
|
183 | print(inline, file=outfile) | |||
|
184 | if get_output and output: | |||
|
185 | print(output, file=outfile) | |||
|
186 | ||||
|
187 | if close_at_end: | |||
|
188 | outfile.close() | |||
|
189 | ||||
|
190 | # For a long time we've had %hist as well as %history | |||
|
191 | @line_magic | |||
|
192 | def hist(self, arg): | |||
|
193 | return self.history(arg) | |||
|
194 | ||||
|
195 | hist.__doc__ = history.__doc__ | |||
|
196 | ||||
|
197 | @line_magic | |||
|
198 | def rep(self, arg): | |||
|
199 | r"""Repeat a command, or get command to input line for editing. | |||
|
200 | ||||
|
201 | %recall and %rep are equivalent. | |||
|
202 | ||||
|
203 | - %recall (no arguments): | |||
|
204 | ||||
|
205 | Place a string version of last computation result (stored in the | |||
|
206 | special '_' variable) to the next input prompt. Allows you to create | |||
|
207 | elaborate command lines without using copy-paste:: | |||
|
208 | ||||
|
209 | In[1]: l = ["hei", "vaan"] | |||
|
210 | In[2]: "".join(l) | |||
|
211 | Out[2]: heivaan | |||
|
212 | In[3]: %rep | |||
|
213 | In[4]: heivaan_ <== cursor blinking | |||
|
214 | ||||
|
215 | %recall 45 | |||
|
216 | ||||
|
217 | Place history line 45 on the next input prompt. Use %hist to find | |||
|
218 | out the number. | |||
|
219 | ||||
|
220 | %recall 1-4 | |||
|
221 | ||||
|
222 | Combine the specified lines into one cell, and place it on the next | |||
|
223 | input prompt. See %history for the slice syntax. | |||
|
224 | ||||
|
225 | %recall foo+bar | |||
|
226 | ||||
|
227 | If foo+bar can be evaluated in the user namespace, the result is | |||
|
228 | placed at the next input prompt. Otherwise, the history is searched | |||
|
229 | for lines which contain that substring, and the most recent one is | |||
|
230 | placed at the next input prompt. | |||
|
231 | """ | |||
|
232 | if not arg: # Last output | |||
|
233 | self.shell.set_next_input(str(self.shell.user_ns["_"])) | |||
|
234 | return | |||
|
235 | # Get history range | |||
|
236 | histlines = self.shell.history_manager.get_range_by_str(arg) | |||
|
237 | cmd = "\n".join(x[2] for x in histlines) | |||
|
238 | if cmd: | |||
|
239 | self.shell.set_next_input(cmd.rstrip()) | |||
|
240 | return | |||
|
241 | ||||
|
242 | try: # Variable in user namespace | |||
|
243 | cmd = str(eval(arg, self.shell.user_ns)) | |||
|
244 | except Exception: # Search for term in history | |||
|
245 | histlines = self.shell.history_manager.search("*"+arg+"*") | |||
|
246 | for h in reversed([x[2] for x in histlines]): | |||
|
247 | if 'rep' in h: | |||
|
248 | continue | |||
|
249 | self.shell.set_next_input(h.rstrip()) | |||
|
250 | return | |||
|
251 | else: | |||
|
252 | self.shell.set_next_input(cmd.rstrip()) | |||
|
253 | print("Couldn't evaluate or find in history:", arg) | |||
|
254 | ||||
|
255 | @line_magic | |||
|
256 | def rerun(self, parameter_s=''): | |||
|
257 | """Re-run previous input | |||
|
258 | ||||
|
259 | By default, you can specify ranges of input history to be repeated | |||
|
260 | (as with %history). With no arguments, it will repeat the last line. | |||
|
261 | ||||
|
262 | Options: | |||
|
263 | ||||
|
264 | -l <n> : Repeat the last n lines of input, not including the | |||
|
265 | current command. | |||
|
266 | ||||
|
267 | -g foo : Repeat the most recent line which contains foo | |||
|
268 | """ | |||
|
269 | opts, args = self.parse_options(parameter_s, 'l:g:', mode='string') | |||
|
270 | if "l" in opts: # Last n lines | |||
|
271 | n = int(opts['l']) | |||
|
272 | hist = self.shell.history_manager.get_tail(n) | |||
|
273 | elif "g" in opts: # Search | |||
|
274 | p = "*"+opts['g']+"*" | |||
|
275 | hist = list(self.shell.history_manager.search(p)) | |||
|
276 | for l in reversed(hist): | |||
|
277 | if "rerun" not in l[2]: | |||
|
278 | hist = [l] # The last match which isn't a %rerun | |||
|
279 | break | |||
|
280 | else: | |||
|
281 | hist = [] # No matches except %rerun | |||
|
282 | elif args: # Specify history ranges | |||
|
283 | hist = self.shell.history_manager.get_range_by_str(args) | |||
|
284 | else: # Last line | |||
|
285 | hist = self.shell.history_manager.get_tail(1) | |||
|
286 | hist = [x[2] for x in hist] | |||
|
287 | if not hist: | |||
|
288 | print("No lines in history match specification") | |||
|
289 | return | |||
|
290 | histlines = "\n".join(hist) | |||
|
291 | print("=== Executing: ===") | |||
|
292 | print(histlines) | |||
|
293 | print("=== Output: ===") | |||
|
294 | self.shell.run_cell("\n".join(hist), store_history=False) |
@@ -0,0 +1,169 b'' | |||||
|
1 | """Implementation of magic functions for IPython's own logging. | |||
|
2 | """ | |||
|
3 | #----------------------------------------------------------------------------- | |||
|
4 | # Copyright (c) 2012 The IPython Development Team. | |||
|
5 | # | |||
|
6 | # Distributed under the terms of the Modified BSD License. | |||
|
7 | # | |||
|
8 | # The full license is in the file COPYING.txt, distributed with this software. | |||
|
9 | #----------------------------------------------------------------------------- | |||
|
10 | ||||
|
11 | #----------------------------------------------------------------------------- | |||
|
12 | # Imports | |||
|
13 | #----------------------------------------------------------------------------- | |||
|
14 | ||||
|
15 | # Stdlib | |||
|
16 | import os | |||
|
17 | import sys | |||
|
18 | ||||
|
19 | # Our own packages | |||
|
20 | from IPython.core.magic import Magics, magics_class, line_magic | |||
|
21 | from IPython.utils.warn import warn | |||
|
22 | ||||
|
23 | #----------------------------------------------------------------------------- | |||
|
24 | # Magic implementation classes | |||
|
25 | #----------------------------------------------------------------------------- | |||
|
26 | ||||
|
27 | @magics_class | |||
|
28 | class LoggingMagics(Magics): | |||
|
29 | """Magics related to all logging machinery.""" | |||
|
30 | ||||
|
31 | @line_magic | |||
|
32 | def logstart(self, parameter_s=''): | |||
|
33 | """Start logging anywhere in a session. | |||
|
34 | ||||
|
35 | %logstart [-o|-r|-t] [log_name [log_mode]] | |||
|
36 | ||||
|
37 | If no name is given, it defaults to a file named 'ipython_log.py' in your | |||
|
38 | current directory, in 'rotate' mode (see below). | |||
|
39 | ||||
|
40 | '%logstart name' saves to file 'name' in 'backup' mode. It saves your | |||
|
41 | history up to that point and then continues logging. | |||
|
42 | ||||
|
43 | %logstart takes a second optional parameter: logging mode. This can be one | |||
|
44 | of (note that the modes are given unquoted):\\ | |||
|
45 | append: well, that says it.\\ | |||
|
46 | backup: rename (if exists) to name~ and start name.\\ | |||
|
47 | global: single logfile in your home dir, appended to.\\ | |||
|
48 | over : overwrite existing log.\\ | |||
|
49 | rotate: create rotating logs name.1~, name.2~, etc. | |||
|
50 | ||||
|
51 | Options: | |||
|
52 | ||||
|
53 | -o: log also IPython's output. In this mode, all commands which | |||
|
54 | generate an Out[NN] prompt are recorded to the logfile, right after | |||
|
55 | their corresponding input line. The output lines are always | |||
|
56 | prepended with a '#[Out]# ' marker, so that the log remains valid | |||
|
57 | Python code. | |||
|
58 | ||||
|
59 | Since this marker is always the same, filtering only the output from | |||
|
60 | a log is very easy, using for example a simple awk call:: | |||
|
61 | ||||
|
62 | awk -F'#\\[Out\\]# ' '{if($2) {print $2}}' ipython_log.py | |||
|
63 | ||||
|
64 | -r: log 'raw' input. Normally, IPython's logs contain the processed | |||
|
65 | input, so that user lines are logged in their final form, converted | |||
|
66 | into valid Python. For example, %Exit is logged as | |||
|
67 | _ip.magic("Exit"). If the -r flag is given, all input is logged | |||
|
68 | exactly as typed, with no transformations applied. | |||
|
69 | ||||
|
70 | -t: put timestamps before each input line logged (these are put in | |||
|
71 | comments).""" | |||
|
72 | ||||
|
73 | opts,par = self.parse_options(parameter_s,'ort') | |||
|
74 | log_output = 'o' in opts | |||
|
75 | log_raw_input = 'r' in opts | |||
|
76 | timestamp = 't' in opts | |||
|
77 | ||||
|
78 | logger = self.shell.logger | |||
|
79 | ||||
|
80 | # if no args are given, the defaults set in the logger constructor by | |||
|
81 | # ipython remain valid | |||
|
82 | if par: | |||
|
83 | try: | |||
|
84 | logfname,logmode = par.split() | |||
|
85 | except: | |||
|
86 | logfname = par | |||
|
87 | logmode = 'backup' | |||
|
88 | else: | |||
|
89 | logfname = logger.logfname | |||
|
90 | logmode = logger.logmode | |||
|
91 | # put logfname into rc struct as if it had been called on the command | |||
|
92 | # line, so it ends up saved in the log header Save it in case we need | |||
|
93 | # to restore it... | |||
|
94 | old_logfile = self.shell.logfile | |||
|
95 | if logfname: | |||
|
96 | logfname = os.path.expanduser(logfname) | |||
|
97 | self.shell.logfile = logfname | |||
|
98 | ||||
|
99 | loghead = '# IPython log file\n\n' | |||
|
100 | try: | |||
|
101 | logger.logstart(logfname, loghead, logmode, log_output, timestamp, | |||
|
102 | log_raw_input) | |||
|
103 | except: | |||
|
104 | self.shell.logfile = old_logfile | |||
|
105 | warn("Couldn't start log: %s" % sys.exc_info()[1]) | |||
|
106 | else: | |||
|
107 | # log input history up to this point, optionally interleaving | |||
|
108 | # output if requested | |||
|
109 | ||||
|
110 | if timestamp: | |||
|
111 | # disable timestamping for the previous history, since we've | |||
|
112 | # lost those already (no time machine here). | |||
|
113 | logger.timestamp = False | |||
|
114 | ||||
|
115 | if log_raw_input: | |||
|
116 | input_hist = self.shell.history_manager.input_hist_raw | |||
|
117 | else: | |||
|
118 | input_hist = self.shell.history_manager.input_hist_parsed | |||
|
119 | ||||
|
120 | if log_output: | |||
|
121 | log_write = logger.log_write | |||
|
122 | output_hist = self.shell.history_manager.output_hist | |||
|
123 | for n in range(1,len(input_hist)-1): | |||
|
124 | log_write(input_hist[n].rstrip() + '\n') | |||
|
125 | if n in output_hist: | |||
|
126 | log_write(repr(output_hist[n]),'output') | |||
|
127 | else: | |||
|
128 | logger.log_write('\n'.join(input_hist[1:])) | |||
|
129 | logger.log_write('\n') | |||
|
130 | if timestamp: | |||
|
131 | # re-enable timestamping | |||
|
132 | logger.timestamp = True | |||
|
133 | ||||
|
134 | print ('Activating auto-logging. ' | |||
|
135 | 'Current session state plus future input saved.') | |||
|
136 | logger.logstate() | |||
|
137 | ||||
|
138 | @line_magic | |||
|
139 | def logstop(self, parameter_s=''): | |||
|
140 | """Fully stop logging and close log file. | |||
|
141 | ||||
|
142 | In order to start logging again, a new %logstart call needs to be made, | |||
|
143 | possibly (though not necessarily) with a new filename, mode and other | |||
|
144 | options.""" | |||
|
145 | self.logger.logstop() | |||
|
146 | ||||
|
147 | @line_magic | |||
|
148 | def logoff(self, parameter_s=''): | |||
|
149 | """Temporarily stop logging. | |||
|
150 | ||||
|
151 | You must have previously started logging.""" | |||
|
152 | self.shell.logger.switch_log(0) | |||
|
153 | ||||
|
154 | @line_magic | |||
|
155 | def logon(self, parameter_s=''): | |||
|
156 | """Restart logging. | |||
|
157 | ||||
|
158 | This function is for restarting logging which you've temporarily | |||
|
159 | stopped with %logoff. For starting logging for the first time, you | |||
|
160 | must use the %logstart function, which allows you to specify an | |||
|
161 | optional log filename.""" | |||
|
162 | ||||
|
163 | self.shell.logger.switch_log(1) | |||
|
164 | ||||
|
165 | @line_magic | |||
|
166 | def logstate(self, parameter_s=''): | |||
|
167 | """Print the status of the logging system.""" | |||
|
168 | ||||
|
169 | self.shell.logger.logstate() |
This diff has been collapsed as it changes many lines, (700 lines changed) Show them Hide them | |||||
@@ -0,0 +1,700 b'' | |||||
|
1 | """Implementation of namespace-related magic functions. | |||
|
2 | """ | |||
|
3 | #----------------------------------------------------------------------------- | |||
|
4 | # Copyright (c) 2012 The IPython Development Team. | |||
|
5 | # | |||
|
6 | # Distributed under the terms of the Modified BSD License. | |||
|
7 | # | |||
|
8 | # The full license is in the file COPYING.txt, distributed with this software. | |||
|
9 | #----------------------------------------------------------------------------- | |||
|
10 | ||||
|
11 | #----------------------------------------------------------------------------- | |||
|
12 | # Imports | |||
|
13 | #----------------------------------------------------------------------------- | |||
|
14 | ||||
|
15 | # Stdlib | |||
|
16 | import gc | |||
|
17 | import re | |||
|
18 | import sys | |||
|
19 | ||||
|
20 | # Our own packages | |||
|
21 | from IPython.core import page | |||
|
22 | from IPython.core.error import StdinNotImplementedError | |||
|
23 | from IPython.core.magic import Magics, magics_class, line_magic | |||
|
24 | from IPython.testing.skipdoctest import skip_doctest | |||
|
25 | from IPython.utils.encoding import DEFAULT_ENCODING | |||
|
26 | from IPython.utils.path import get_py_filename | |||
|
27 | ||||
|
28 | #----------------------------------------------------------------------------- | |||
|
29 | # Magic implementation classes | |||
|
30 | #----------------------------------------------------------------------------- | |||
|
31 | ||||
|
32 | @magics_class | |||
|
33 | class NamespaceMagics(Magics): | |||
|
34 | """Magics to manage various aspects of the user's namespace. | |||
|
35 | ||||
|
36 | These include listing variables, introspecting into them, etc. | |||
|
37 | """ | |||
|
38 | ||||
|
39 | @line_magic | |||
|
40 | def pinfo(self, parameter_s='', namespaces=None): | |||
|
41 | """Provide detailed information about an object. | |||
|
42 | ||||
|
43 | '%pinfo object' is just a synonym for object? or ?object.""" | |||
|
44 | ||||
|
45 | #print 'pinfo par: <%s>' % parameter_s # dbg | |||
|
46 | # detail_level: 0 -> obj? , 1 -> obj?? | |||
|
47 | detail_level = 0 | |||
|
48 | # We need to detect if we got called as 'pinfo pinfo foo', which can | |||
|
49 | # happen if the user types 'pinfo foo?' at the cmd line. | |||
|
50 | pinfo,qmark1,oname,qmark2 = \ | |||
|
51 | re.match('(pinfo )?(\?*)(.*?)(\??$)',parameter_s).groups() | |||
|
52 | if pinfo or qmark1 or qmark2: | |||
|
53 | detail_level = 1 | |||
|
54 | if "*" in oname: | |||
|
55 | self.psearch(oname) | |||
|
56 | else: | |||
|
57 | self.shell._inspect('pinfo', oname, detail_level=detail_level, | |||
|
58 | namespaces=namespaces) | |||
|
59 | ||||
|
60 | @line_magic | |||
|
61 | def pinfo2(self, parameter_s='', namespaces=None): | |||
|
62 | """Provide extra detailed information about an object. | |||
|
63 | ||||
|
64 | '%pinfo2 object' is just a synonym for object?? or ??object.""" | |||
|
65 | self.shell._inspect('pinfo', parameter_s, detail_level=1, | |||
|
66 | namespaces=namespaces) | |||
|
67 | ||||
|
68 | @skip_doctest | |||
|
69 | @line_magic | |||
|
70 | def pdef(self, parameter_s='', namespaces=None): | |||
|
71 | """Print the definition header for any callable object. | |||
|
72 | ||||
|
73 | If the object is a class, print the constructor information. | |||
|
74 | ||||
|
75 | Examples | |||
|
76 | -------- | |||
|
77 | :: | |||
|
78 | ||||
|
79 | In [3]: %pdef urllib.urlopen | |||
|
80 | urllib.urlopen(url, data=None, proxies=None) | |||
|
81 | """ | |||
|
82 | self._inspect('pdef',parameter_s, namespaces) | |||
|
83 | ||||
|
84 | @line_magic | |||
|
85 | def pdoc(self, parameter_s='', namespaces=None): | |||
|
86 | """Print the docstring for an object. | |||
|
87 | ||||
|
88 | If the given object is a class, it will print both the class and the | |||
|
89 | constructor docstrings.""" | |||
|
90 | self._inspect('pdoc',parameter_s, namespaces) | |||
|
91 | ||||
|
92 | @line_magic | |||
|
93 | def psource(self, parameter_s='', namespaces=None): | |||
|
94 | """Print (or run through pager) the source code for an object.""" | |||
|
95 | self._inspect('psource',parameter_s, namespaces) | |||
|
96 | ||||
|
97 | @line_magic | |||
|
98 | def pfile(self, parameter_s=''): | |||
|
99 | """Print (or run through pager) the file where an object is defined. | |||
|
100 | ||||
|
101 | The file opens at the line where the object definition begins. IPython | |||
|
102 | will honor the environment variable PAGER if set, and otherwise will | |||
|
103 | do its best to print the file in a convenient form. | |||
|
104 | ||||
|
105 | If the given argument is not an object currently defined, IPython will | |||
|
106 | try to interpret it as a filename (automatically adding a .py extension | |||
|
107 | if needed). You can thus use %pfile as a syntax highlighting code | |||
|
108 | viewer.""" | |||
|
109 | ||||
|
110 | # first interpret argument as an object name | |||
|
111 | out = self._inspect('pfile',parameter_s) | |||
|
112 | # if not, try the input as a filename | |||
|
113 | if out == 'not found': | |||
|
114 | try: | |||
|
115 | filename = get_py_filename(parameter_s) | |||
|
116 | except IOError,msg: | |||
|
117 | print msg | |||
|
118 | return | |||
|
119 | page.page(self.shell.inspector.format(open(filename).read())) | |||
|
120 | ||||
|
121 | @line_magic | |||
|
122 | def psearch(self, parameter_s=''): | |||
|
123 | """Search for object in namespaces by wildcard. | |||
|
124 | ||||
|
125 | %psearch [options] PATTERN [OBJECT TYPE] | |||
|
126 | ||||
|
127 | Note: ? can be used as a synonym for %psearch, at the beginning or at | |||
|
128 | the end: both a*? and ?a* are equivalent to '%psearch a*'. Still, the | |||
|
129 | rest of the command line must be unchanged (options come first), so | |||
|
130 | for example the following forms are equivalent | |||
|
131 | ||||
|
132 | %psearch -i a* function | |||
|
133 | -i a* function? | |||
|
134 | ?-i a* function | |||
|
135 | ||||
|
136 | Arguments: | |||
|
137 | ||||
|
138 | PATTERN | |||
|
139 | ||||
|
140 | where PATTERN is a string containing * as a wildcard similar to its | |||
|
141 | use in a shell. The pattern is matched in all namespaces on the | |||
|
142 | search path. By default objects starting with a single _ are not | |||
|
143 | matched, many IPython generated objects have a single | |||
|
144 | underscore. The default is case insensitive matching. Matching is | |||
|
145 | also done on the attributes of objects and not only on the objects | |||
|
146 | in a module. | |||
|
147 | ||||
|
148 | [OBJECT TYPE] | |||
|
149 | ||||
|
150 | Is the name of a python type from the types module. The name is | |||
|
151 | given in lowercase without the ending type, ex. StringType is | |||
|
152 | written string. By adding a type here only objects matching the | |||
|
153 | given type are matched. Using all here makes the pattern match all | |||
|
154 | types (this is the default). | |||
|
155 | ||||
|
156 | Options: | |||
|
157 | ||||
|
158 | -a: makes the pattern match even objects whose names start with a | |||
|
159 | single underscore. These names are normally omitted from the | |||
|
160 | search. | |||
|
161 | ||||
|
162 | -i/-c: make the pattern case insensitive/sensitive. If neither of | |||
|
163 | these options are given, the default is read from your configuration | |||
|
164 | file, with the option ``InteractiveShell.wildcards_case_sensitive``. | |||
|
165 | If this option is not specified in your configuration file, IPython's | |||
|
166 | internal default is to do a case sensitive search. | |||
|
167 | ||||
|
168 | -e/-s NAMESPACE: exclude/search a given namespace. The pattern you | |||
|
169 | specify can be searched in any of the following namespaces: | |||
|
170 | 'builtin', 'user', 'user_global','internal', 'alias', where | |||
|
171 | 'builtin' and 'user' are the search defaults. Note that you should | |||
|
172 | not use quotes when specifying namespaces. | |||
|
173 | ||||
|
174 | 'Builtin' contains the python module builtin, 'user' contains all | |||
|
175 | user data, 'alias' only contain the shell aliases and no python | |||
|
176 | objects, 'internal' contains objects used by IPython. The | |||
|
177 | 'user_global' namespace is only used by embedded IPython instances, | |||
|
178 | and it contains module-level globals. You can add namespaces to the | |||
|
179 | search with -s or exclude them with -e (these options can be given | |||
|
180 | more than once). | |||
|
181 | ||||
|
182 | Examples | |||
|
183 | -------- | |||
|
184 | :: | |||
|
185 | ||||
|
186 | %psearch a* -> objects beginning with an a | |||
|
187 | %psearch -e builtin a* -> objects NOT in the builtin space starting in a | |||
|
188 | %psearch a* function -> all functions beginning with an a | |||
|
189 | %psearch re.e* -> objects beginning with an e in module re | |||
|
190 | %psearch r*.e* -> objects that start with e in modules starting in r | |||
|
191 | %psearch r*.* string -> all strings in modules beginning with r | |||
|
192 | ||||
|
193 | Case sensitive search:: | |||
|
194 | ||||
|
195 | %psearch -c a* list all object beginning with lower case a | |||
|
196 | ||||
|
197 | Show objects beginning with a single _:: | |||
|
198 | ||||
|
199 | %psearch -a _* list objects beginning with a single underscore | |||
|
200 | """ | |||
|
201 | try: | |||
|
202 | parameter_s.encode('ascii') | |||
|
203 | except UnicodeEncodeError: | |||
|
204 | print 'Python identifiers can only contain ascii characters.' | |||
|
205 | return | |||
|
206 | ||||
|
207 | # default namespaces to be searched | |||
|
208 | def_search = ['user_local', 'user_global', 'builtin'] | |||
|
209 | ||||
|
210 | # Process options/args | |||
|
211 | opts,args = self.parse_options(parameter_s,'cias:e:',list_all=True) | |||
|
212 | opt = opts.get | |||
|
213 | shell = self.shell | |||
|
214 | psearch = shell.inspector.psearch | |||
|
215 | ||||
|
216 | # select case options | |||
|
217 | if opts.has_key('i'): | |||
|
218 | ignore_case = True | |||
|
219 | elif opts.has_key('c'): | |||
|
220 | ignore_case = False | |||
|
221 | else: | |||
|
222 | ignore_case = not shell.wildcards_case_sensitive | |||
|
223 | ||||
|
224 | # Build list of namespaces to search from user options | |||
|
225 | def_search.extend(opt('s',[])) | |||
|
226 | ns_exclude = ns_exclude=opt('e',[]) | |||
|
227 | ns_search = [nm for nm in def_search if nm not in ns_exclude] | |||
|
228 | ||||
|
229 | # Call the actual search | |||
|
230 | try: | |||
|
231 | psearch(args,shell.ns_table,ns_search, | |||
|
232 | show_all=opt('a'),ignore_case=ignore_case) | |||
|
233 | except: | |||
|
234 | shell.showtraceback() | |||
|
235 | ||||
|
236 | @skip_doctest | |||
|
237 | @line_magic | |||
|
238 | def who_ls(self, parameter_s=''): | |||
|
239 | """Return a sorted list of all interactive variables. | |||
|
240 | ||||
|
241 | If arguments are given, only variables of types matching these | |||
|
242 | arguments are returned. | |||
|
243 | ||||
|
244 | Examples | |||
|
245 | -------- | |||
|
246 | ||||
|
247 | Define two variables and list them with who_ls:: | |||
|
248 | ||||
|
249 | In [1]: alpha = 123 | |||
|
250 | ||||
|
251 | In [2]: beta = 'test' | |||
|
252 | ||||
|
253 | In [3]: %who_ls | |||
|
254 | Out[3]: ['alpha', 'beta'] | |||
|
255 | ||||
|
256 | In [4]: %who_ls int | |||
|
257 | Out[4]: ['alpha'] | |||
|
258 | ||||
|
259 | In [5]: %who_ls str | |||
|
260 | Out[5]: ['beta'] | |||
|
261 | """ | |||
|
262 | ||||
|
263 | user_ns = self.shell.user_ns | |||
|
264 | user_ns_hidden = self.shell.user_ns_hidden | |||
|
265 | out = [ i for i in user_ns | |||
|
266 | if not i.startswith('_') \ | |||
|
267 | and not i in user_ns_hidden ] | |||
|
268 | ||||
|
269 | typelist = parameter_s.split() | |||
|
270 | if typelist: | |||
|
271 | typeset = set(typelist) | |||
|
272 | out = [i for i in out if type(user_ns[i]).__name__ in typeset] | |||
|
273 | ||||
|
274 | out.sort() | |||
|
275 | return out | |||
|
276 | ||||
|
277 | @skip_doctest | |||
|
278 | @line_magic | |||
|
279 | def who(self, parameter_s=''): | |||
|
280 | """Print all interactive variables, with some minimal formatting. | |||
|
281 | ||||
|
282 | If any arguments are given, only variables whose type matches one of | |||
|
283 | these are printed. For example:: | |||
|
284 | ||||
|
285 | %who function str | |||
|
286 | ||||
|
287 | will only list functions and strings, excluding all other types of | |||
|
288 | variables. To find the proper type names, simply use type(var) at a | |||
|
289 | command line to see how python prints type names. For example: | |||
|
290 | ||||
|
291 | :: | |||
|
292 | ||||
|
293 | In [1]: type('hello')\\ | |||
|
294 | Out[1]: <type 'str'> | |||
|
295 | ||||
|
296 | indicates that the type name for strings is 'str'. | |||
|
297 | ||||
|
298 | ``%who`` always excludes executed names loaded through your configuration | |||
|
299 | file and things which are internal to IPython. | |||
|
300 | ||||
|
301 | This is deliberate, as typically you may load many modules and the | |||
|
302 | purpose of %who is to show you only what you've manually defined. | |||
|
303 | ||||
|
304 | Examples | |||
|
305 | -------- | |||
|
306 | ||||
|
307 | Define two variables and list them with who:: | |||
|
308 | ||||
|
309 | In [1]: alpha = 123 | |||
|
310 | ||||
|
311 | In [2]: beta = 'test' | |||
|
312 | ||||
|
313 | In [3]: %who | |||
|
314 | alpha beta | |||
|
315 | ||||
|
316 | In [4]: %who int | |||
|
317 | alpha | |||
|
318 | ||||
|
319 | In [5]: %who str | |||
|
320 | beta | |||
|
321 | """ | |||
|
322 | ||||
|
323 | varlist = self.who_ls(parameter_s) | |||
|
324 | if not varlist: | |||
|
325 | if parameter_s: | |||
|
326 | print 'No variables match your requested type.' | |||
|
327 | else: | |||
|
328 | print 'Interactive namespace is empty.' | |||
|
329 | return | |||
|
330 | ||||
|
331 | # if we have variables, move on... | |||
|
332 | count = 0 | |||
|
333 | for i in varlist: | |||
|
334 | print i+'\t', | |||
|
335 | count += 1 | |||
|
336 | if count > 8: | |||
|
337 | count = 0 | |||
|
338 | ||||
|
339 | ||||
|
340 | ||||
|
341 | @skip_doctest | |||
|
342 | @line_magic | |||
|
343 | def whos(self, parameter_s=''): | |||
|
344 | """Like %who, but gives some extra information about each variable. | |||
|
345 | ||||
|
346 | The same type filtering of %who can be applied here. | |||
|
347 | ||||
|
348 | For all variables, the type is printed. Additionally it prints: | |||
|
349 | ||||
|
350 | - For {},[],(): their length. | |||
|
351 | ||||
|
352 | - For numpy arrays, a summary with shape, number of | |||
|
353 | elements, typecode and size in memory. | |||
|
354 | ||||
|
355 | - Everything else: a string representation, snipping their middle if | |||
|
356 | too long. | |||
|
357 | ||||
|
358 | Examples | |||
|
359 | -------- | |||
|
360 | ||||
|
361 | Define two variables and list them with whos:: | |||
|
362 | ||||
|
363 | In [1]: alpha = 123 | |||
|
364 | ||||
|
365 | In [2]: beta = 'test' | |||
|
366 | ||||
|
367 | In [3]: %whos | |||
|
368 | Variable Type Data/Info | |||
|
369 | -------------------------------- | |||
|
370 | alpha int 123 | |||
|
371 | beta str test | |||
|
372 | """ | |||
|
373 | ||||
|
374 | varnames = self.who_ls(parameter_s) | |||
|
375 | if not varnames: | |||
|
376 | if parameter_s: | |||
|
377 | print 'No variables match your requested type.' | |||
|
378 | else: | |||
|
379 | print 'Interactive namespace is empty.' | |||
|
380 | return | |||
|
381 | ||||
|
382 | # if we have variables, move on... | |||
|
383 | ||||
|
384 | # for these types, show len() instead of data: | |||
|
385 | seq_types = ['dict', 'list', 'tuple'] | |||
|
386 | ||||
|
387 | # for numpy arrays, display summary info | |||
|
388 | ndarray_type = None | |||
|
389 | if 'numpy' in sys.modules: | |||
|
390 | try: | |||
|
391 | from numpy import ndarray | |||
|
392 | except ImportError: | |||
|
393 | pass | |||
|
394 | else: | |||
|
395 | ndarray_type = ndarray.__name__ | |||
|
396 | ||||
|
397 | # Find all variable names and types so we can figure out column sizes | |||
|
398 | def get_vars(i): | |||
|
399 | return self.shell.user_ns[i] | |||
|
400 | ||||
|
401 | # some types are well known and can be shorter | |||
|
402 | abbrevs = {'IPython.core.macro.Macro' : 'Macro'} | |||
|
403 | def type_name(v): | |||
|
404 | tn = type(v).__name__ | |||
|
405 | return abbrevs.get(tn,tn) | |||
|
406 | ||||
|
407 | varlist = map(get_vars,varnames) | |||
|
408 | ||||
|
409 | typelist = [] | |||
|
410 | for vv in varlist: | |||
|
411 | tt = type_name(vv) | |||
|
412 | ||||
|
413 | if tt=='instance': | |||
|
414 | typelist.append( abbrevs.get(str(vv.__class__), | |||
|
415 | str(vv.__class__))) | |||
|
416 | else: | |||
|
417 | typelist.append(tt) | |||
|
418 | ||||
|
419 | # column labels and # of spaces as separator | |||
|
420 | varlabel = 'Variable' | |||
|
421 | typelabel = 'Type' | |||
|
422 | datalabel = 'Data/Info' | |||
|
423 | colsep = 3 | |||
|
424 | # variable format strings | |||
|
425 | vformat = "{0:<{varwidth}}{1:<{typewidth}}" | |||
|
426 | aformat = "%s: %s elems, type `%s`, %s bytes" | |||
|
427 | # find the size of the columns to format the output nicely | |||
|
428 | varwidth = max(max(map(len,varnames)), len(varlabel)) + colsep | |||
|
429 | typewidth = max(max(map(len,typelist)), len(typelabel)) + colsep | |||
|
430 | # table header | |||
|
431 | print varlabel.ljust(varwidth) + typelabel.ljust(typewidth) + \ | |||
|
432 | ' '+datalabel+'\n' + '-'*(varwidth+typewidth+len(datalabel)+1) | |||
|
433 | # and the table itself | |||
|
434 | kb = 1024 | |||
|
435 | Mb = 1048576 # kb**2 | |||
|
436 | for vname,var,vtype in zip(varnames,varlist,typelist): | |||
|
437 | print vformat.format(vname, vtype, varwidth=varwidth, typewidth=typewidth), | |||
|
438 | if vtype in seq_types: | |||
|
439 | print "n="+str(len(var)) | |||
|
440 | elif vtype == ndarray_type: | |||
|
441 | vshape = str(var.shape).replace(',','').replace(' ','x')[1:-1] | |||
|
442 | if vtype==ndarray_type: | |||
|
443 | # numpy | |||
|
444 | vsize = var.size | |||
|
445 | vbytes = vsize*var.itemsize | |||
|
446 | vdtype = var.dtype | |||
|
447 | ||||
|
448 | if vbytes < 100000: | |||
|
449 | print aformat % (vshape, vsize, vdtype, vbytes) | |||
|
450 | else: | |||
|
451 | print aformat % (vshape, vsize, vdtype, vbytes), | |||
|
452 | if vbytes < Mb: | |||
|
453 | print '(%s kb)' % (vbytes/kb,) | |||
|
454 | else: | |||
|
455 | print '(%s Mb)' % (vbytes/Mb,) | |||
|
456 | else: | |||
|
457 | try: | |||
|
458 | vstr = str(var) | |||
|
459 | except UnicodeEncodeError: | |||
|
460 | vstr = unicode(var).encode(DEFAULT_ENCODING, | |||
|
461 | 'backslashreplace') | |||
|
462 | except: | |||
|
463 | vstr = "<object with id %d (str() failed)>" % id(var) | |||
|
464 | vstr = vstr.replace('\n', '\\n') | |||
|
465 | if len(vstr) < 50: | |||
|
466 | print vstr | |||
|
467 | else: | |||
|
468 | print vstr[:25] + "<...>" + vstr[-25:] | |||
|
469 | ||||
|
470 | @line_magic | |||
|
471 | def reset(self, parameter_s=''): | |||
|
472 | """Resets the namespace by removing all names defined by the user, if | |||
|
473 | called without arguments, or by removing some types of objects, such | |||
|
474 | as everything currently in IPython's In[] and Out[] containers (see | |||
|
475 | the parameters for details). | |||
|
476 | ||||
|
477 | Parameters | |||
|
478 | ---------- | |||
|
479 | -f : force reset without asking for confirmation. | |||
|
480 | ||||
|
481 | -s : 'Soft' reset: Only clears your namespace, leaving history intact. | |||
|
482 | References to objects may be kept. By default (without this option), | |||
|
483 | we do a 'hard' reset, giving you a new session and removing all | |||
|
484 | references to objects from the current session. | |||
|
485 | ||||
|
486 | in : reset input history | |||
|
487 | ||||
|
488 | out : reset output history | |||
|
489 | ||||
|
490 | dhist : reset directory history | |||
|
491 | ||||
|
492 | array : reset only variables that are NumPy arrays | |||
|
493 | ||||
|
494 | See Also | |||
|
495 | -------- | |||
|
496 | magic_reset_selective : invoked as ``%reset_selective`` | |||
|
497 | ||||
|
498 | Examples | |||
|
499 | -------- | |||
|
500 | :: | |||
|
501 | ||||
|
502 | In [6]: a = 1 | |||
|
503 | ||||
|
504 | In [7]: a | |||
|
505 | Out[7]: 1 | |||
|
506 | ||||
|
507 | In [8]: 'a' in _ip.user_ns | |||
|
508 | Out[8]: True | |||
|
509 | ||||
|
510 | In [9]: %reset -f | |||
|
511 | ||||
|
512 | In [1]: 'a' in _ip.user_ns | |||
|
513 | Out[1]: False | |||
|
514 | ||||
|
515 | In [2]: %reset -f in | |||
|
516 | Flushing input history | |||
|
517 | ||||
|
518 | In [3]: %reset -f dhist in | |||
|
519 | Flushing directory history | |||
|
520 | Flushing input history | |||
|
521 | ||||
|
522 | Notes | |||
|
523 | ----- | |||
|
524 | Calling this magic from clients that do not implement standard input, | |||
|
525 | such as the ipython notebook interface, will reset the namespace | |||
|
526 | without confirmation. | |||
|
527 | """ | |||
|
528 | opts, args = self.parse_options(parameter_s,'sf', mode='list') | |||
|
529 | if 'f' in opts: | |||
|
530 | ans = True | |||
|
531 | else: | |||
|
532 | try: | |||
|
533 | ans = self.shell.ask_yes_no( | |||
|
534 | "Once deleted, variables cannot be recovered. Proceed (y/[n])?", | |||
|
535 | default='n') | |||
|
536 | except StdinNotImplementedError: | |||
|
537 | ans = True | |||
|
538 | if not ans: | |||
|
539 | print 'Nothing done.' | |||
|
540 | return | |||
|
541 | ||||
|
542 | if 's' in opts: # Soft reset | |||
|
543 | user_ns = self.shell.user_ns | |||
|
544 | for i in self.who_ls(): | |||
|
545 | del(user_ns[i]) | |||
|
546 | elif len(args) == 0: # Hard reset | |||
|
547 | self.shell.reset(new_session = False) | |||
|
548 | ||||
|
549 | # reset in/out/dhist/array: previously extensinions/clearcmd.py | |||
|
550 | ip = self.shell | |||
|
551 | user_ns = self.shell.user_ns # local lookup, heavily used | |||
|
552 | ||||
|
553 | for target in args: | |||
|
554 | target = target.lower() # make matches case insensitive | |||
|
555 | if target == 'out': | |||
|
556 | print "Flushing output cache (%d entries)" % len(user_ns['_oh']) | |||
|
557 | self.shell.displayhook.flush() | |||
|
558 | ||||
|
559 | elif target == 'in': | |||
|
560 | print "Flushing input history" | |||
|
561 | pc = self.shell.displayhook.prompt_count + 1 | |||
|
562 | for n in range(1, pc): | |||
|
563 | key = '_i'+repr(n) | |||
|
564 | user_ns.pop(key,None) | |||
|
565 | user_ns.update(dict(_i=u'',_ii=u'',_iii=u'')) | |||
|
566 | hm = ip.history_manager | |||
|
567 | # don't delete these, as %save and %macro depending on the | |||
|
568 | # length of these lists to be preserved | |||
|
569 | hm.input_hist_parsed[:] = [''] * pc | |||
|
570 | hm.input_hist_raw[:] = [''] * pc | |||
|
571 | # hm has internal machinery for _i,_ii,_iii, clear it out | |||
|
572 | hm._i = hm._ii = hm._iii = hm._i00 = u'' | |||
|
573 | ||||
|
574 | elif target == 'array': | |||
|
575 | # Support cleaning up numpy arrays | |||
|
576 | try: | |||
|
577 | from numpy import ndarray | |||
|
578 | # This must be done with items and not iteritems because | |||
|
579 | # we're going to modify the dict in-place. | |||
|
580 | for x,val in user_ns.items(): | |||
|
581 | if isinstance(val,ndarray): | |||
|
582 | del user_ns[x] | |||
|
583 | except ImportError: | |||
|
584 | print "reset array only works if Numpy is available." | |||
|
585 | ||||
|
586 | elif target == 'dhist': | |||
|
587 | print "Flushing directory history" | |||
|
588 | del user_ns['_dh'][:] | |||
|
589 | ||||
|
590 | else: | |||
|
591 | print "Don't know how to reset ", | |||
|
592 | print target + ", please run `%reset?` for details" | |||
|
593 | ||||
|
594 | gc.collect() | |||
|
595 | ||||
|
596 | @line_magic | |||
|
597 | def reset_selective(self, parameter_s=''): | |||
|
598 | """Resets the namespace by removing names defined by the user. | |||
|
599 | ||||
|
600 | Input/Output history are left around in case you need them. | |||
|
601 | ||||
|
602 | %reset_selective [-f] regex | |||
|
603 | ||||
|
604 | No action is taken if regex is not included | |||
|
605 | ||||
|
606 | Options | |||
|
607 | -f : force reset without asking for confirmation. | |||
|
608 | ||||
|
609 | See Also | |||
|
610 | -------- | |||
|
611 | magic_reset : invoked as ``%reset`` | |||
|
612 | ||||
|
613 | Examples | |||
|
614 | -------- | |||
|
615 | ||||
|
616 | We first fully reset the namespace so your output looks identical to | |||
|
617 | this example for pedagogical reasons; in practice you do not need a | |||
|
618 | full reset:: | |||
|
619 | ||||
|
620 | In [1]: %reset -f | |||
|
621 | ||||
|
622 | Now, with a clean namespace we can make a few variables and use | |||
|
623 | ``%reset_selective`` to only delete names that match our regexp:: | |||
|
624 | ||||
|
625 | In [2]: a=1; b=2; c=3; b1m=4; b2m=5; b3m=6; b4m=7; b2s=8 | |||
|
626 | ||||
|
627 | In [3]: who_ls | |||
|
628 | Out[3]: ['a', 'b', 'b1m', 'b2m', 'b2s', 'b3m', 'b4m', 'c'] | |||
|
629 | ||||
|
630 | In [4]: %reset_selective -f b[2-3]m | |||
|
631 | ||||
|
632 | In [5]: who_ls | |||
|
633 | Out[5]: ['a', 'b', 'b1m', 'b2s', 'b4m', 'c'] | |||
|
634 | ||||
|
635 | In [6]: %reset_selective -f d | |||
|
636 | ||||
|
637 | In [7]: who_ls | |||
|
638 | Out[7]: ['a', 'b', 'b1m', 'b2s', 'b4m', 'c'] | |||
|
639 | ||||
|
640 | In [8]: %reset_selective -f c | |||
|
641 | ||||
|
642 | In [9]: who_ls | |||
|
643 | Out[9]: ['a', 'b', 'b1m', 'b2s', 'b4m'] | |||
|
644 | ||||
|
645 | In [10]: %reset_selective -f b | |||
|
646 | ||||
|
647 | In [11]: who_ls | |||
|
648 | Out[11]: ['a'] | |||
|
649 | ||||
|
650 | Notes | |||
|
651 | ----- | |||
|
652 | Calling this magic from clients that do not implement standard input, | |||
|
653 | such as the ipython notebook interface, will reset the namespace | |||
|
654 | without confirmation. | |||
|
655 | """ | |||
|
656 | ||||
|
657 | opts, regex = self.parse_options(parameter_s,'f') | |||
|
658 | ||||
|
659 | if opts.has_key('f'): | |||
|
660 | ans = True | |||
|
661 | else: | |||
|
662 | try: | |||
|
663 | ans = self.shell.ask_yes_no( | |||
|
664 | "Once deleted, variables cannot be recovered. Proceed (y/[n])? ", | |||
|
665 | default='n') | |||
|
666 | except StdinNotImplementedError: | |||
|
667 | ans = True | |||
|
668 | if not ans: | |||
|
669 | print 'Nothing done.' | |||
|
670 | return | |||
|
671 | user_ns = self.shell.user_ns | |||
|
672 | if not regex: | |||
|
673 | print 'No regex pattern specified. Nothing done.' | |||
|
674 | return | |||
|
675 | else: | |||
|
676 | try: | |||
|
677 | m = re.compile(regex) | |||
|
678 | except TypeError: | |||
|
679 | raise TypeError('regex must be a string or compiled pattern') | |||
|
680 | for i in self.who_ls(): | |||
|
681 | if m.search(i): | |||
|
682 | del(user_ns[i]) | |||
|
683 | ||||
|
684 | @line_magic | |||
|
685 | def xdel(self, parameter_s=''): | |||
|
686 | """Delete a variable, trying to clear it from anywhere that | |||
|
687 | IPython's machinery has references to it. By default, this uses | |||
|
688 | the identity of the named object in the user namespace to remove | |||
|
689 | references held under other names. The object is also removed | |||
|
690 | from the output history. | |||
|
691 | ||||
|
692 | Options | |||
|
693 | -n : Delete the specified name from all namespaces, without | |||
|
694 | checking their identity. | |||
|
695 | """ | |||
|
696 | opts, varname = self.parse_options(parameter_s,'n') | |||
|
697 | try: | |||
|
698 | self.shell.del_var(varname, ('n' in opts)) | |||
|
699 | except (NameError, ValueError) as e: | |||
|
700 | print type(e).__name__ +": "+ str(e) |
This diff has been collapsed as it changes many lines, (674 lines changed) Show them Hide them | |||||
@@ -0,0 +1,674 b'' | |||||
|
1 | """Implementation of magic functions for interaction with the OS. | |||
|
2 | ||||
|
3 | Note: this module is named 'osm' instead of 'os' to avoid a collision with the | |||
|
4 | builtin. | |||
|
5 | """ | |||
|
6 | #----------------------------------------------------------------------------- | |||
|
7 | # Copyright (c) 2012 The IPython Development Team. | |||
|
8 | # | |||
|
9 | # Distributed under the terms of the Modified BSD License. | |||
|
10 | # | |||
|
11 | # The full license is in the file COPYING.txt, distributed with this software. | |||
|
12 | #----------------------------------------------------------------------------- | |||
|
13 | ||||
|
14 | #----------------------------------------------------------------------------- | |||
|
15 | # Imports | |||
|
16 | #----------------------------------------------------------------------------- | |||
|
17 | ||||
|
18 | # Stdlib | |||
|
19 | import os | |||
|
20 | import re | |||
|
21 | import sys | |||
|
22 | from pprint import pformat | |||
|
23 | ||||
|
24 | # Our own packages | |||
|
25 | from IPython.core import oinspect | |||
|
26 | from IPython.core import page | |||
|
27 | from IPython.core.error import UsageError | |||
|
28 | from IPython.core.magic import (Magics, compress_dhist, magics_class, | |||
|
29 | line_magic) | |||
|
30 | from IPython.testing.skipdoctest import skip_doctest | |||
|
31 | from IPython.utils.io import file_read, nlprint | |||
|
32 | from IPython.utils.path import get_py_filename, unquote_filename | |||
|
33 | from IPython.utils.process import abbrev_cwd | |||
|
34 | from IPython.utils.terminal import set_term_title | |||
|
35 | #----------------------------------------------------------------------------- | |||
|
36 | # Magic implementation classes | |||
|
37 | #----------------------------------------------------------------------------- | |||
|
38 | @magics_class | |||
|
39 | class OSMagics(Magics): | |||
|
40 | """Magics to interact with the underlying OS (shell-type functionality). | |||
|
41 | """ | |||
|
42 | ||||
|
43 | @skip_doctest | |||
|
44 | @line_magic | |||
|
45 | def alias(self, parameter_s=''): | |||
|
46 | """Define an alias for a system command. | |||
|
47 | ||||
|
48 | '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd' | |||
|
49 | ||||
|
50 | Then, typing 'alias_name params' will execute the system command 'cmd | |||
|
51 | params' (from your underlying operating system). | |||
|
52 | ||||
|
53 | Aliases have lower precedence than magic functions and Python normal | |||
|
54 | variables, so if 'foo' is both a Python variable and an alias, the | |||
|
55 | alias can not be executed until 'del foo' removes the Python variable. | |||
|
56 | ||||
|
57 | You can use the %l specifier in an alias definition to represent the | |||
|
58 | whole line when the alias is called. For example:: | |||
|
59 | ||||
|
60 | In [2]: alias bracket echo "Input in brackets: <%l>" | |||
|
61 | In [3]: bracket hello world | |||
|
62 | Input in brackets: <hello world> | |||
|
63 | ||||
|
64 | You can also define aliases with parameters using %s specifiers (one | |||
|
65 | per parameter):: | |||
|
66 | ||||
|
67 | In [1]: alias parts echo first %s second %s | |||
|
68 | In [2]: %parts A B | |||
|
69 | first A second B | |||
|
70 | In [3]: %parts A | |||
|
71 | Incorrect number of arguments: 2 expected. | |||
|
72 | parts is an alias to: 'echo first %s second %s' | |||
|
73 | ||||
|
74 | Note that %l and %s are mutually exclusive. You can only use one or | |||
|
75 | the other in your aliases. | |||
|
76 | ||||
|
77 | Aliases expand Python variables just like system calls using ! or !! | |||
|
78 | do: all expressions prefixed with '$' get expanded. For details of | |||
|
79 | the semantic rules, see PEP-215: | |||
|
80 | http://www.python.org/peps/pep-0215.html. This is the library used by | |||
|
81 | IPython for variable expansion. If you want to access a true shell | |||
|
82 | variable, an extra $ is necessary to prevent its expansion by | |||
|
83 | IPython:: | |||
|
84 | ||||
|
85 | In [6]: alias show echo | |||
|
86 | In [7]: PATH='A Python string' | |||
|
87 | In [8]: show $PATH | |||
|
88 | A Python string | |||
|
89 | In [9]: show $$PATH | |||
|
90 | /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:... | |||
|
91 | ||||
|
92 | You can use the alias facility to acess all of $PATH. See the %rehash | |||
|
93 | and %rehashx functions, which automatically create aliases for the | |||
|
94 | contents of your $PATH. | |||
|
95 | ||||
|
96 | If called with no parameters, %alias prints the current alias table.""" | |||
|
97 | ||||
|
98 | par = parameter_s.strip() | |||
|
99 | if not par: | |||
|
100 | aliases = sorted(self.shell.alias_manager.aliases) | |||
|
101 | # stored = self.shell.db.get('stored_aliases', {} ) | |||
|
102 | # for k, v in stored: | |||
|
103 | # atab.append(k, v[0]) | |||
|
104 | ||||
|
105 | print "Total number of aliases:", len(aliases) | |||
|
106 | sys.stdout.flush() | |||
|
107 | return aliases | |||
|
108 | ||||
|
109 | # Now try to define a new one | |||
|
110 | try: | |||
|
111 | alias,cmd = par.split(None, 1) | |||
|
112 | except: | |||
|
113 | print oinspect.getdoc(self.alias) | |||
|
114 | else: | |||
|
115 | self.shell.alias_manager.soft_define_alias(alias, cmd) | |||
|
116 | # end magic_alias | |||
|
117 | ||||
|
118 | @line_magic | |||
|
119 | def unalias(self, parameter_s=''): | |||
|
120 | """Remove an alias""" | |||
|
121 | ||||
|
122 | aname = parameter_s.strip() | |||
|
123 | self.shell.alias_manager.undefine_alias(aname) | |||
|
124 | stored = self.shell.db.get('stored_aliases', {} ) | |||
|
125 | if aname in stored: | |||
|
126 | print "Removing %stored alias",aname | |||
|
127 | del stored[aname] | |||
|
128 | self.shell.db['stored_aliases'] = stored | |||
|
129 | ||||
|
130 | @line_magic | |||
|
131 | def rehashx(self, parameter_s=''): | |||
|
132 | """Update the alias table with all executable files in $PATH. | |||
|
133 | ||||
|
134 | This version explicitly checks that every entry in $PATH is a file | |||
|
135 | with execute access (os.X_OK), so it is much slower than %rehash. | |||
|
136 | ||||
|
137 | Under Windows, it checks executability as a match against a | |||
|
138 | '|'-separated string of extensions, stored in the IPython config | |||
|
139 | variable win_exec_ext. This defaults to 'exe|com|bat'. | |||
|
140 | ||||
|
141 | This function also resets the root module cache of module completer, | |||
|
142 | used on slow filesystems. | |||
|
143 | """ | |||
|
144 | from IPython.core.alias import InvalidAliasError | |||
|
145 | ||||
|
146 | # for the benefit of module completer in ipy_completers.py | |||
|
147 | del self.shell.db['rootmodules'] | |||
|
148 | ||||
|
149 | path = [os.path.abspath(os.path.expanduser(p)) for p in | |||
|
150 | os.environ.get('PATH','').split(os.pathsep)] | |||
|
151 | path = filter(os.path.isdir,path) | |||
|
152 | ||||
|
153 | syscmdlist = [] | |||
|
154 | # Now define isexec in a cross platform manner. | |||
|
155 | if os.name == 'posix': | |||
|
156 | isexec = lambda fname:os.path.isfile(fname) and \ | |||
|
157 | os.access(fname,os.X_OK) | |||
|
158 | else: | |||
|
159 | try: | |||
|
160 | winext = os.environ['pathext'].replace(';','|').replace('.','') | |||
|
161 | except KeyError: | |||
|
162 | winext = 'exe|com|bat|py' | |||
|
163 | if 'py' not in winext: | |||
|
164 | winext += '|py' | |||
|
165 | execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE) | |||
|
166 | isexec = lambda fname:os.path.isfile(fname) and execre.match(fname) | |||
|
167 | savedir = os.getcwdu() | |||
|
168 | ||||
|
169 | # Now walk the paths looking for executables to alias. | |||
|
170 | try: | |||
|
171 | # write the whole loop for posix/Windows so we don't have an if in | |||
|
172 | # the innermost part | |||
|
173 | if os.name == 'posix': | |||
|
174 | for pdir in path: | |||
|
175 | os.chdir(pdir) | |||
|
176 | for ff in os.listdir(pdir): | |||
|
177 | if isexec(ff): | |||
|
178 | try: | |||
|
179 | # Removes dots from the name since ipython | |||
|
180 | # will assume names with dots to be python. | |||
|
181 | self.shell.alias_manager.define_alias( | |||
|
182 | ff.replace('.',''), ff) | |||
|
183 | except InvalidAliasError: | |||
|
184 | pass | |||
|
185 | else: | |||
|
186 | syscmdlist.append(ff) | |||
|
187 | else: | |||
|
188 | no_alias = self.shell.alias_manager.no_alias | |||
|
189 | for pdir in path: | |||
|
190 | os.chdir(pdir) | |||
|
191 | for ff in os.listdir(pdir): | |||
|
192 | base, ext = os.path.splitext(ff) | |||
|
193 | if isexec(ff) and base.lower() not in no_alias: | |||
|
194 | if ext.lower() == '.exe': | |||
|
195 | ff = base | |||
|
196 | try: | |||
|
197 | # Removes dots from the name since ipython | |||
|
198 | # will assume names with dots to be python. | |||
|
199 | self.shell.alias_manager.define_alias( | |||
|
200 | base.lower().replace('.',''), ff) | |||
|
201 | except InvalidAliasError: | |||
|
202 | pass | |||
|
203 | syscmdlist.append(ff) | |||
|
204 | self.shell.db['syscmdlist'] = syscmdlist | |||
|
205 | finally: | |||
|
206 | os.chdir(savedir) | |||
|
207 | ||||
|
208 | @skip_doctest | |||
|
209 | @line_magic | |||
|
210 | def pwd(self, parameter_s=''): | |||
|
211 | """Return the current working directory path. | |||
|
212 | ||||
|
213 | Examples | |||
|
214 | -------- | |||
|
215 | :: | |||
|
216 | ||||
|
217 | In [9]: pwd | |||
|
218 | Out[9]: '/home/tsuser/sprint/ipython' | |||
|
219 | """ | |||
|
220 | return os.getcwdu() | |||
|
221 | ||||
|
222 | @skip_doctest | |||
|
223 | @line_magic | |||
|
224 | def cd(self, parameter_s=''): | |||
|
225 | """Change the current working directory. | |||
|
226 | ||||
|
227 | This command automatically maintains an internal list of directories | |||
|
228 | you visit during your IPython session, in the variable _dh. The | |||
|
229 | command %dhist shows this history nicely formatted. You can also | |||
|
230 | do 'cd -<tab>' to see directory history conveniently. | |||
|
231 | ||||
|
232 | Usage: | |||
|
233 | ||||
|
234 | cd 'dir': changes to directory 'dir'. | |||
|
235 | ||||
|
236 | cd -: changes to the last visited directory. | |||
|
237 | ||||
|
238 | cd -<n>: changes to the n-th directory in the directory history. | |||
|
239 | ||||
|
240 | cd --foo: change to directory that matches 'foo' in history | |||
|
241 | ||||
|
242 | cd -b <bookmark_name>: jump to a bookmark set by %bookmark | |||
|
243 | (note: cd <bookmark_name> is enough if there is no | |||
|
244 | directory <bookmark_name>, but a bookmark with the name exists.) | |||
|
245 | 'cd -b <tab>' allows you to tab-complete bookmark names. | |||
|
246 | ||||
|
247 | Options: | |||
|
248 | ||||
|
249 | -q: quiet. Do not print the working directory after the cd command is | |||
|
250 | executed. By default IPython's cd command does print this directory, | |||
|
251 | since the default prompts do not display path information. | |||
|
252 | ||||
|
253 | Note that !cd doesn't work for this purpose because the shell where | |||
|
254 | !command runs is immediately discarded after executing 'command'. | |||
|
255 | ||||
|
256 | Examples | |||
|
257 | -------- | |||
|
258 | :: | |||
|
259 | ||||
|
260 | In [10]: cd parent/child | |||
|
261 | /home/tsuser/parent/child | |||
|
262 | """ | |||
|
263 | ||||
|
264 | oldcwd = os.getcwdu() | |||
|
265 | numcd = re.match(r'(-)(\d+)$',parameter_s) | |||
|
266 | # jump in directory history by number | |||
|
267 | if numcd: | |||
|
268 | nn = int(numcd.group(2)) | |||
|
269 | try: | |||
|
270 | ps = self.shell.user_ns['_dh'][nn] | |||
|
271 | except IndexError: | |||
|
272 | print 'The requested directory does not exist in history.' | |||
|
273 | return | |||
|
274 | else: | |||
|
275 | opts = {} | |||
|
276 | elif parameter_s.startswith('--'): | |||
|
277 | ps = None | |||
|
278 | fallback = None | |||
|
279 | pat = parameter_s[2:] | |||
|
280 | dh = self.shell.user_ns['_dh'] | |||
|
281 | # first search only by basename (last component) | |||
|
282 | for ent in reversed(dh): | |||
|
283 | if pat in os.path.basename(ent) and os.path.isdir(ent): | |||
|
284 | ps = ent | |||
|
285 | break | |||
|
286 | ||||
|
287 | if fallback is None and pat in ent and os.path.isdir(ent): | |||
|
288 | fallback = ent | |||
|
289 | ||||
|
290 | # if we have no last part match, pick the first full path match | |||
|
291 | if ps is None: | |||
|
292 | ps = fallback | |||
|
293 | ||||
|
294 | if ps is None: | |||
|
295 | print "No matching entry in directory history" | |||
|
296 | return | |||
|
297 | else: | |||
|
298 | opts = {} | |||
|
299 | ||||
|
300 | ||||
|
301 | else: | |||
|
302 | #turn all non-space-escaping backslashes to slashes, | |||
|
303 | # for c:\windows\directory\names\ | |||
|
304 | parameter_s = re.sub(r'\\(?! )','/', parameter_s) | |||
|
305 | opts,ps = self.parse_options(parameter_s,'qb',mode='string') | |||
|
306 | # jump to previous | |||
|
307 | if ps == '-': | |||
|
308 | try: | |||
|
309 | ps = self.shell.user_ns['_dh'][-2] | |||
|
310 | except IndexError: | |||
|
311 | raise UsageError('%cd -: No previous directory to change to.') | |||
|
312 | # jump to bookmark if needed | |||
|
313 | else: | |||
|
314 | if not os.path.isdir(ps) or 'b' in opts: | |||
|
315 | bkms = self.shell.db.get('bookmarks', {}) | |||
|
316 | ||||
|
317 | if ps in bkms: | |||
|
318 | target = bkms[ps] | |||
|
319 | print '(bookmark:%s) -> %s' % (ps, target) | |||
|
320 | ps = target | |||
|
321 | else: | |||
|
322 | if 'b' in opts: | |||
|
323 | raise UsageError("Bookmark '%s' not found. " | |||
|
324 | "Use '%%bookmark -l' to see your bookmarks." % ps) | |||
|
325 | ||||
|
326 | # strip extra quotes on Windows, because os.chdir doesn't like them | |||
|
327 | ps = unquote_filename(ps) | |||
|
328 | # at this point ps should point to the target dir | |||
|
329 | if ps: | |||
|
330 | try: | |||
|
331 | os.chdir(os.path.expanduser(ps)) | |||
|
332 | if hasattr(self.shell, 'term_title') and self.shell.term_title: | |||
|
333 | set_term_title('IPython: ' + abbrev_cwd()) | |||
|
334 | except OSError: | |||
|
335 | print sys.exc_info()[1] | |||
|
336 | else: | |||
|
337 | cwd = os.getcwdu() | |||
|
338 | dhist = self.shell.user_ns['_dh'] | |||
|
339 | if oldcwd != cwd: | |||
|
340 | dhist.append(cwd) | |||
|
341 | self.shell.db['dhist'] = compress_dhist(dhist)[-100:] | |||
|
342 | ||||
|
343 | else: | |||
|
344 | os.chdir(self.shell.home_dir) | |||
|
345 | if hasattr(self.shell, 'term_title') and self.shell.term_title: | |||
|
346 | set_term_title('IPython: ' + '~') | |||
|
347 | cwd = os.getcwdu() | |||
|
348 | dhist = self.shell.user_ns['_dh'] | |||
|
349 | ||||
|
350 | if oldcwd != cwd: | |||
|
351 | dhist.append(cwd) | |||
|
352 | self.shell.db['dhist'] = compress_dhist(dhist)[-100:] | |||
|
353 | if not 'q' in opts and self.shell.user_ns['_dh']: | |||
|
354 | print self.shell.user_ns['_dh'][-1] | |||
|
355 | ||||
|
356 | ||||
|
357 | @line_magic | |||
|
358 | def env(self, parameter_s=''): | |||
|
359 | """List environment variables.""" | |||
|
360 | ||||
|
361 | return dict(os.environ) | |||
|
362 | ||||
|
363 | @line_magic | |||
|
364 | def pushd(self, parameter_s=''): | |||
|
365 | """Place the current dir on stack and change directory. | |||
|
366 | ||||
|
367 | Usage:\\ | |||
|
368 | %pushd ['dirname'] | |||
|
369 | """ | |||
|
370 | ||||
|
371 | dir_s = self.shell.dir_stack | |||
|
372 | tgt = os.path.expanduser(unquote_filename(parameter_s)) | |||
|
373 | cwd = os.getcwdu().replace(self.shell.home_dir,'~') | |||
|
374 | if tgt: | |||
|
375 | self.cd(parameter_s) | |||
|
376 | dir_s.insert(0,cwd) | |||
|
377 | return self.shell.magic('dirs') | |||
|
378 | ||||
|
379 | @line_magic | |||
|
380 | def popd(self, parameter_s=''): | |||
|
381 | """Change to directory popped off the top of the stack. | |||
|
382 | """ | |||
|
383 | if not self.shell.dir_stack: | |||
|
384 | raise UsageError("%popd on empty stack") | |||
|
385 | top = self.shell.dir_stack.pop(0) | |||
|
386 | self.cd(top) | |||
|
387 | print "popd ->",top | |||
|
388 | ||||
|
389 | @line_magic | |||
|
390 | def dirs(self, parameter_s=''): | |||
|
391 | """Return the current directory stack.""" | |||
|
392 | ||||
|
393 | return self.shell.dir_stack | |||
|
394 | ||||
|
395 | @line_magic | |||
|
396 | def dhist(self, parameter_s=''): | |||
|
397 | """Print your history of visited directories. | |||
|
398 | ||||
|
399 | %dhist -> print full history\\ | |||
|
400 | %dhist n -> print last n entries only\\ | |||
|
401 | %dhist n1 n2 -> print entries between n1 and n2 (n1 not included)\\ | |||
|
402 | ||||
|
403 | This history is automatically maintained by the %cd command, and | |||
|
404 | always available as the global list variable _dh. You can use %cd -<n> | |||
|
405 | to go to directory number <n>. | |||
|
406 | ||||
|
407 | Note that most of time, you should view directory history by entering | |||
|
408 | cd -<TAB>. | |||
|
409 | ||||
|
410 | """ | |||
|
411 | ||||
|
412 | dh = self.shell.user_ns['_dh'] | |||
|
413 | if parameter_s: | |||
|
414 | try: | |||
|
415 | args = map(int,parameter_s.split()) | |||
|
416 | except: | |||
|
417 | self.arg_err(self.dhist) | |||
|
418 | return | |||
|
419 | if len(args) == 1: | |||
|
420 | ini,fin = max(len(dh)-(args[0]),0),len(dh) | |||
|
421 | elif len(args) == 2: | |||
|
422 | ini,fin = args | |||
|
423 | else: | |||
|
424 | self.arg_err(self.dhist) | |||
|
425 | return | |||
|
426 | else: | |||
|
427 | ini,fin = 0,len(dh) | |||
|
428 | nlprint(dh, | |||
|
429 | header = 'Directory history (kept in _dh)', | |||
|
430 | start=ini,stop=fin) | |||
|
431 | ||||
|
432 | @skip_doctest | |||
|
433 | @line_magic | |||
|
434 | def sc(self, parameter_s=''): | |||
|
435 | """Shell capture - execute a shell command and capture its output. | |||
|
436 | ||||
|
437 | DEPRECATED. Suboptimal, retained for backwards compatibility. | |||
|
438 | ||||
|
439 | You should use the form 'var = !command' instead. Example: | |||
|
440 | ||||
|
441 | "%sc -l myfiles = ls ~" should now be written as | |||
|
442 | ||||
|
443 | "myfiles = !ls ~" | |||
|
444 | ||||
|
445 | myfiles.s, myfiles.l and myfiles.n still apply as documented | |||
|
446 | below. | |||
|
447 | ||||
|
448 | -- | |||
|
449 | %sc [options] varname=command | |||
|
450 | ||||
|
451 | IPython will run the given command using commands.getoutput(), and | |||
|
452 | will then update the user's interactive namespace with a variable | |||
|
453 | called varname, containing the value of the call. Your command can | |||
|
454 | contain shell wildcards, pipes, etc. | |||
|
455 | ||||
|
456 | The '=' sign in the syntax is mandatory, and the variable name you | |||
|
457 | supply must follow Python's standard conventions for valid names. | |||
|
458 | ||||
|
459 | (A special format without variable name exists for internal use) | |||
|
460 | ||||
|
461 | Options: | |||
|
462 | ||||
|
463 | -l: list output. Split the output on newlines into a list before | |||
|
464 | assigning it to the given variable. By default the output is stored | |||
|
465 | as a single string. | |||
|
466 | ||||
|
467 | -v: verbose. Print the contents of the variable. | |||
|
468 | ||||
|
469 | In most cases you should not need to split as a list, because the | |||
|
470 | returned value is a special type of string which can automatically | |||
|
471 | provide its contents either as a list (split on newlines) or as a | |||
|
472 | space-separated string. These are convenient, respectively, either | |||
|
473 | for sequential processing or to be passed to a shell command. | |||
|
474 | ||||
|
475 | For example:: | |||
|
476 | ||||
|
477 | # Capture into variable a | |||
|
478 | In [1]: sc a=ls *py | |||
|
479 | ||||
|
480 | # a is a string with embedded newlines | |||
|
481 | In [2]: a | |||
|
482 | Out[2]: 'setup.py\\nwin32_manual_post_install.py' | |||
|
483 | ||||
|
484 | # which can be seen as a list: | |||
|
485 | In [3]: a.l | |||
|
486 | Out[3]: ['setup.py', 'win32_manual_post_install.py'] | |||
|
487 | ||||
|
488 | # or as a whitespace-separated string: | |||
|
489 | In [4]: a.s | |||
|
490 | Out[4]: 'setup.py win32_manual_post_install.py' | |||
|
491 | ||||
|
492 | # a.s is useful to pass as a single command line: | |||
|
493 | In [5]: !wc -l $a.s | |||
|
494 | 146 setup.py | |||
|
495 | 130 win32_manual_post_install.py | |||
|
496 | 276 total | |||
|
497 | ||||
|
498 | # while the list form is useful to loop over: | |||
|
499 | In [6]: for f in a.l: | |||
|
500 | ...: !wc -l $f | |||
|
501 | ...: | |||
|
502 | 146 setup.py | |||
|
503 | 130 win32_manual_post_install.py | |||
|
504 | ||||
|
505 | Similarly, the lists returned by the -l option are also special, in | |||
|
506 | the sense that you can equally invoke the .s attribute on them to | |||
|
507 | automatically get a whitespace-separated string from their contents:: | |||
|
508 | ||||
|
509 | In [7]: sc -l b=ls *py | |||
|
510 | ||||
|
511 | In [8]: b | |||
|
512 | Out[8]: ['setup.py', 'win32_manual_post_install.py'] | |||
|
513 | ||||
|
514 | In [9]: b.s | |||
|
515 | Out[9]: 'setup.py win32_manual_post_install.py' | |||
|
516 | ||||
|
517 | In summary, both the lists and strings used for output capture have | |||
|
518 | the following special attributes:: | |||
|
519 | ||||
|
520 | .l (or .list) : value as list. | |||
|
521 | .n (or .nlstr): value as newline-separated string. | |||
|
522 | .s (or .spstr): value as space-separated string. | |||
|
523 | """ | |||
|
524 | ||||
|
525 | opts,args = self.parse_options(parameter_s, 'lv') | |||
|
526 | # Try to get a variable name and command to run | |||
|
527 | try: | |||
|
528 | # the variable name must be obtained from the parse_options | |||
|
529 | # output, which uses shlex.split to strip options out. | |||
|
530 | var,_ = args.split('=', 1) | |||
|
531 | var = var.strip() | |||
|
532 | # But the command has to be extracted from the original input | |||
|
533 | # parameter_s, not on what parse_options returns, to avoid the | |||
|
534 | # quote stripping which shlex.split performs on it. | |||
|
535 | _,cmd = parameter_s.split('=', 1) | |||
|
536 | except ValueError: | |||
|
537 | var,cmd = '','' | |||
|
538 | # If all looks ok, proceed | |||
|
539 | split = 'l' in opts | |||
|
540 | out = self.shell.getoutput(cmd, split=split) | |||
|
541 | if 'v' in opts: | |||
|
542 | print '%s ==\n%s' % (var, pformat(out)) | |||
|
543 | if var: | |||
|
544 | self.shell.user_ns.update({var:out}) | |||
|
545 | else: | |||
|
546 | return out | |||
|
547 | ||||
|
548 | @line_magic | |||
|
549 | def sx(self, parameter_s=''): | |||
|
550 | """Shell execute - run a shell command and capture its output. | |||
|
551 | ||||
|
552 | %sx command | |||
|
553 | ||||
|
554 | IPython will run the given command using commands.getoutput(), and | |||
|
555 | return the result formatted as a list (split on '\\n'). Since the | |||
|
556 | output is _returned_, it will be stored in ipython's regular output | |||
|
557 | cache Out[N] and in the '_N' automatic variables. | |||
|
558 | ||||
|
559 | Notes: | |||
|
560 | ||||
|
561 | 1) If an input line begins with '!!', then %sx is automatically | |||
|
562 | invoked. That is, while:: | |||
|
563 | ||||
|
564 | !ls | |||
|
565 | ||||
|
566 | causes ipython to simply issue system('ls'), typing:: | |||
|
567 | ||||
|
568 | !!ls | |||
|
569 | ||||
|
570 | is a shorthand equivalent to:: | |||
|
571 | ||||
|
572 | %sx ls | |||
|
573 | ||||
|
574 | 2) %sx differs from %sc in that %sx automatically splits into a list, | |||
|
575 | like '%sc -l'. The reason for this is to make it as easy as possible | |||
|
576 | to process line-oriented shell output via further python commands. | |||
|
577 | %sc is meant to provide much finer control, but requires more | |||
|
578 | typing. | |||
|
579 | ||||
|
580 | 3) Just like %sc -l, this is a list with special attributes: | |||
|
581 | :: | |||
|
582 | ||||
|
583 | .l (or .list) : value as list. | |||
|
584 | .n (or .nlstr): value as newline-separated string. | |||
|
585 | .s (or .spstr): value as whitespace-separated string. | |||
|
586 | ||||
|
587 | This is very useful when trying to use such lists as arguments to | |||
|
588 | system commands.""" | |||
|
589 | ||||
|
590 | if parameter_s: | |||
|
591 | return self.shell.getoutput(parameter_s) | |||
|
592 | ||||
|
593 | ||||
|
594 | @line_magic | |||
|
595 | def bookmark(self, parameter_s=''): | |||
|
596 | """Manage IPython's bookmark system. | |||
|
597 | ||||
|
598 | %bookmark <name> - set bookmark to current dir | |||
|
599 | %bookmark <name> <dir> - set bookmark to <dir> | |||
|
600 | %bookmark -l - list all bookmarks | |||
|
601 | %bookmark -d <name> - remove bookmark | |||
|
602 | %bookmark -r - remove all bookmarks | |||
|
603 | ||||
|
604 | You can later on access a bookmarked folder with:: | |||
|
605 | ||||
|
606 | %cd -b <name> | |||
|
607 | ||||
|
608 | or simply '%cd <name>' if there is no directory called <name> AND | |||
|
609 | there is such a bookmark defined. | |||
|
610 | ||||
|
611 | Your bookmarks persist through IPython sessions, but they are | |||
|
612 | associated with each profile.""" | |||
|
613 | ||||
|
614 | opts,args = self.parse_options(parameter_s,'drl',mode='list') | |||
|
615 | if len(args) > 2: | |||
|
616 | raise UsageError("%bookmark: too many arguments") | |||
|
617 | ||||
|
618 | bkms = self.shell.db.get('bookmarks',{}) | |||
|
619 | ||||
|
620 | if 'd' in opts: | |||
|
621 | try: | |||
|
622 | todel = args[0] | |||
|
623 | except IndexError: | |||
|
624 | raise UsageError( | |||
|
625 | "%bookmark -d: must provide a bookmark to delete") | |||
|
626 | else: | |||
|
627 | try: | |||
|
628 | del bkms[todel] | |||
|
629 | except KeyError: | |||
|
630 | raise UsageError( | |||
|
631 | "%%bookmark -d: Can't delete bookmark '%s'" % todel) | |||
|
632 | ||||
|
633 | elif 'r' in opts: | |||
|
634 | bkms = {} | |||
|
635 | elif 'l' in opts: | |||
|
636 | bks = bkms.keys() | |||
|
637 | bks.sort() | |||
|
638 | if bks: | |||
|
639 | size = max(map(len, bks)) | |||
|
640 | else: | |||
|
641 | size = 0 | |||
|
642 | fmt = '%-'+str(size)+'s -> %s' | |||
|
643 | print 'Current bookmarks:' | |||
|
644 | for bk in bks: | |||
|
645 | print fmt % (bk, bkms[bk]) | |||
|
646 | else: | |||
|
647 | if not args: | |||
|
648 | raise UsageError("%bookmark: You must specify the bookmark name") | |||
|
649 | elif len(args)==1: | |||
|
650 | bkms[args[0]] = os.getcwdu() | |||
|
651 | elif len(args)==2: | |||
|
652 | bkms[args[0]] = args[1] | |||
|
653 | self.shell.db['bookmarks'] = bkms | |||
|
654 | ||||
|
655 | @line_magic | |||
|
656 | def pycat(self, parameter_s=''): | |||
|
657 | """Show a syntax-highlighted file through a pager. | |||
|
658 | ||||
|
659 | This magic is similar to the cat utility, but it will assume the file | |||
|
660 | to be Python source and will show it with syntax highlighting. """ | |||
|
661 | ||||
|
662 | try: | |||
|
663 | filename = get_py_filename(parameter_s) | |||
|
664 | cont = file_read(filename) | |||
|
665 | except IOError: | |||
|
666 | try: | |||
|
667 | cont = eval(parameter_s, self.shell.user_ns) | |||
|
668 | except NameError: | |||
|
669 | cont = None | |||
|
670 | if cont is None: | |||
|
671 | print "Error: no such file or variable" | |||
|
672 | return | |||
|
673 | ||||
|
674 | page.page(self.shell.pycolorize(cont)) |
@@ -0,0 +1,88 b'' | |||||
|
1 | """Implementation of magic functions for matplotlib/pylab support. | |||
|
2 | """ | |||
|
3 | #----------------------------------------------------------------------------- | |||
|
4 | # Copyright (c) 2012 The IPython Development Team. | |||
|
5 | # | |||
|
6 | # Distributed under the terms of the Modified BSD License. | |||
|
7 | # | |||
|
8 | # The full license is in the file COPYING.txt, distributed with this software. | |||
|
9 | #----------------------------------------------------------------------------- | |||
|
10 | ||||
|
11 | #----------------------------------------------------------------------------- | |||
|
12 | # Imports | |||
|
13 | #----------------------------------------------------------------------------- | |||
|
14 | ||||
|
15 | # Our own packages | |||
|
16 | from IPython.config.application import Application | |||
|
17 | from IPython.core.magic import Magics, magics_class, line_magic | |||
|
18 | from IPython.testing.skipdoctest import skip_doctest | |||
|
19 | ||||
|
20 | #----------------------------------------------------------------------------- | |||
|
21 | # Magic implementation classes | |||
|
22 | #----------------------------------------------------------------------------- | |||
|
23 | ||||
|
24 | @magics_class | |||
|
25 | class PylabMagics(Magics): | |||
|
26 | """Magics related to matplotlib's pylab support""" | |||
|
27 | ||||
|
28 | @skip_doctest | |||
|
29 | @line_magic | |||
|
30 | def pylab(self, parameter_s=''): | |||
|
31 | """Load numpy and matplotlib to work interactively. | |||
|
32 | ||||
|
33 | %pylab [GUINAME] | |||
|
34 | ||||
|
35 | This function lets you activate pylab (matplotlib, numpy and | |||
|
36 | interactive support) at any point during an IPython session. | |||
|
37 | ||||
|
38 | It will import at the top level numpy as np, pyplot as plt, matplotlib, | |||
|
39 | pylab and mlab, as well as all names from numpy and pylab. | |||
|
40 | ||||
|
41 | If you are using the inline matplotlib backend for embedded figures, | |||
|
42 | you can adjust its behavior via the %config magic:: | |||
|
43 | ||||
|
44 | # enable SVG figures, necessary for SVG+XHTML export in the qtconsole | |||
|
45 | In [1]: %config InlineBackend.figure_format = 'svg' | |||
|
46 | ||||
|
47 | # change the behavior of closing all figures at the end of each | |||
|
48 | # execution (cell), or allowing reuse of active figures across | |||
|
49 | # cells: | |||
|
50 | In [2]: %config InlineBackend.close_figures = False | |||
|
51 | ||||
|
52 | Parameters | |||
|
53 | ---------- | |||
|
54 | guiname : optional | |||
|
55 | One of the valid arguments to the %gui magic ('qt', 'wx', 'gtk', | |||
|
56 | 'osx' or 'tk'). If given, the corresponding Matplotlib backend is | |||
|
57 | used, otherwise matplotlib's default (which you can override in your | |||
|
58 | matplotlib config file) is used. | |||
|
59 | ||||
|
60 | Examples | |||
|
61 | -------- | |||
|
62 | In this case, where the MPL default is TkAgg:: | |||
|
63 | ||||
|
64 | In [2]: %pylab | |||
|
65 | ||||
|
66 | Welcome to pylab, a matplotlib-based Python environment. | |||
|
67 | Backend in use: TkAgg | |||
|
68 | For more information, type 'help(pylab)'. | |||
|
69 | ||||
|
70 | But you can explicitly request a different backend:: | |||
|
71 | ||||
|
72 | In [3]: %pylab qt | |||
|
73 | ||||
|
74 | Welcome to pylab, a matplotlib-based Python environment. | |||
|
75 | Backend in use: Qt4Agg | |||
|
76 | For more information, type 'help(pylab)'. | |||
|
77 | """ | |||
|
78 | ||||
|
79 | if Application.initialized(): | |||
|
80 | app = Application.instance() | |||
|
81 | try: | |||
|
82 | import_all_status = app.pylab_import_all | |||
|
83 | except AttributeError: | |||
|
84 | import_all_status = True | |||
|
85 | else: | |||
|
86 | import_all_status = True | |||
|
87 | ||||
|
88 | self.shell.enable_pylab(parameter_s, import_all=import_all_status) |
@@ -53,15 +53,14 b' used, and this module (and the readline module) are silently inactive.' | |||||
53 | # proper procedure is to maintain its copyright as belonging to the Python |
|
53 | # proper procedure is to maintain its copyright as belonging to the Python | |
54 | # Software Foundation (in addition to my own, for all new code). |
|
54 | # Software Foundation (in addition to my own, for all new code). | |
55 | # |
|
55 | # | |
56 |
# Copyright (C) 2008 |
|
56 | # Copyright (C) 2008 IPython Development Team | |
57 |
# Copyright (C) 2001 |
|
57 | # Copyright (C) 2001 Fernando Perez. <fperez@colorado.edu> | |
58 | # Copyright (C) 2001 Python Software Foundation, www.python.org |
|
58 | # Copyright (C) 2001 Python Software Foundation, www.python.org | |
59 | # |
|
59 | # | |
60 | # Distributed under the terms of the BSD License. The full license is in |
|
60 | # Distributed under the terms of the BSD License. The full license is in | |
61 | # the file COPYING, distributed as part of this software. |
|
61 | # the file COPYING, distributed as part of this software. | |
62 | # |
|
62 | # | |
63 | #***************************************************************************** |
|
63 | #***************************************************************************** | |
64 | from __future__ import print_function |
|
|||
65 |
|
64 | |||
66 | #----------------------------------------------------------------------------- |
|
65 | #----------------------------------------------------------------------------- | |
67 | # Imports |
|
66 | # Imports | |
@@ -178,11 +177,14 b' def compress_user(path, tilde_expand, tilde_val):' | |||||
178 | else: |
|
177 | else: | |
179 | return path |
|
178 | return path | |
180 |
|
179 | |||
|
180 | ||||
181 | class Bunch(object): pass |
|
181 | class Bunch(object): pass | |
182 |
|
182 | |||
|
183 | ||||
183 | DELIMS = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?' |
|
184 | DELIMS = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?' | |
184 | GREEDY_DELIMS = ' \r\n' |
|
185 | GREEDY_DELIMS = ' \r\n' | |
185 |
|
186 | |||
|
187 | ||||
186 | class CompletionSplitter(object): |
|
188 | class CompletionSplitter(object): | |
187 | """An object to split an input line in a manner similar to readline. |
|
189 | """An object to split an input line in a manner similar to readline. | |
188 |
|
190 | |||
@@ -194,7 +196,7 b' class CompletionSplitter(object):' | |||||
194 |
|
196 | |||
195 | What characters are used as splitting delimiters can be controlled by |
|
197 | What characters are used as splitting delimiters can be controlled by | |
196 | setting the `delims` attribute (this is a property that internally |
|
198 | setting the `delims` attribute (this is a property that internally | |
197 | automatically builds the necessary """ |
|
199 | automatically builds the necessary regular expression)""" | |
198 |
|
200 | |||
199 | # Private interface |
|
201 | # Private interface | |
200 |
|
202 | |||
@@ -212,19 +214,21 b' class CompletionSplitter(object):' | |||||
212 |
|
214 | |||
213 | def __init__(self, delims=None): |
|
215 | def __init__(self, delims=None): | |
214 | delims = CompletionSplitter._delims if delims is None else delims |
|
216 | delims = CompletionSplitter._delims if delims is None else delims | |
215 |
self. |
|
217 | self.delims = delims | |
|
218 | ||||
|
219 | @property | |||
|
220 | def delims(self): | |||
|
221 | """Return the string of delimiter characters.""" | |||
|
222 | return self._delims | |||
216 |
|
223 | |||
217 | def set_delims(self, delims): |
|
224 | @delims.setter | |
|
225 | def delims(self, delims): | |||
218 | """Set the delimiters for line splitting.""" |
|
226 | """Set the delimiters for line splitting.""" | |
219 | expr = '[' + ''.join('\\'+ c for c in delims) + ']' |
|
227 | expr = '[' + ''.join('\\'+ c for c in delims) + ']' | |
220 | self._delim_re = re.compile(expr) |
|
228 | self._delim_re = re.compile(expr) | |
221 | self._delims = delims |
|
229 | self._delims = delims | |
222 | self._delim_expr = expr |
|
230 | self._delim_expr = expr | |
223 |
|
231 | |||
224 | def get_delims(self): |
|
|||
225 | """Return the string of delimiter characters.""" |
|
|||
226 | return self._delims |
|
|||
227 |
|
||||
228 | def split_line(self, line, cursor_pos=None): |
|
232 | def split_line(self, line, cursor_pos=None): | |
229 | """Split a line of text with a cursor at the given position. |
|
233 | """Split a line of text with a cursor at the given position. | |
230 | """ |
|
234 | """ | |
@@ -377,7 +381,7 b' class Completer(Configurable):' | |||||
377 | def get__all__entries(obj): |
|
381 | def get__all__entries(obj): | |
378 | """returns the strings in the __all__ attribute""" |
|
382 | """returns the strings in the __all__ attribute""" | |
379 | try: |
|
383 | try: | |
380 | words = getattr(obj,'__all__') |
|
384 | words = getattr(obj, '__all__') | |
381 | except: |
|
385 | except: | |
382 | return [] |
|
386 | return [] | |
383 |
|
387 | |||
@@ -390,12 +394,12 b' class IPCompleter(Completer):' | |||||
390 | def _greedy_changed(self, name, old, new): |
|
394 | def _greedy_changed(self, name, old, new): | |
391 | """update the splitter and readline delims when greedy is changed""" |
|
395 | """update the splitter and readline delims when greedy is changed""" | |
392 | if new: |
|
396 | if new: | |
393 |
self.splitter. |
|
397 | self.splitter.delims = GREEDY_DELIMS | |
394 | else: |
|
398 | else: | |
395 |
self.splitter. |
|
399 | self.splitter.delims = DELIMS | |
396 |
|
400 | |||
397 | if self.readline: |
|
401 | if self.readline: | |
398 |
self.readline.set_completer_delims(self.splitter. |
|
402 | self.readline.set_completer_delims(self.splitter.delims) | |
399 |
|
403 | |||
400 | merge_completions = CBool(True, config=True, |
|
404 | merge_completions = CBool(True, config=True, | |
401 | help="""Whether to merge completion results into a single list |
|
405 | help="""Whether to merge completion results into a single list | |
@@ -472,7 +476,7 b' class IPCompleter(Completer):' | |||||
472 |
|
476 | |||
473 | # List where completion matches will be stored |
|
477 | # List where completion matches will be stored | |
474 | self.matches = [] |
|
478 | self.matches = [] | |
475 |
self.shell = shell |
|
479 | self.shell = shell | |
476 | if alias_table is None: |
|
480 | if alias_table is None: | |
477 | alias_table = {} |
|
481 | alias_table = {} | |
478 | self.alias_table = alias_table |
|
482 | self.alias_table = alias_table | |
@@ -601,11 +605,23 b' class IPCompleter(Completer):' | |||||
601 | """Match magics""" |
|
605 | """Match magics""" | |
602 | #print 'Completer->magic_matches:',text,'lb',self.text_until_cursor # dbg |
|
606 | #print 'Completer->magic_matches:',text,'lb',self.text_until_cursor # dbg | |
603 | # Get all shell magics now rather than statically, so magics loaded at |
|
607 | # Get all shell magics now rather than statically, so magics loaded at | |
604 | # runtime show up too |
|
608 | # runtime show up too. | |
605 |
m |
|
609 | lsm = self.shell.magics_manager.lsmagic() | |
|
610 | line_magics = lsm['line'] | |||
|
611 | cell_magics = lsm['cell'] | |||
606 | pre = self.magic_escape |
|
612 | pre = self.magic_escape | |
607 | baretext = text.lstrip(pre) |
|
613 | pre2 = pre+pre | |
608 | return [ pre+m for m in magics if m.startswith(baretext)] |
|
614 | ||
|
615 | # Completion logic: | |||
|
616 | # - user gives %%: only do cell magics | |||
|
617 | # - user gives %: do both line and cell magics | |||
|
618 | # - no prefix: do both | |||
|
619 | # In other words, line magics are skipped if the user gives %% explicitly | |||
|
620 | bare_text = text.lstrip(pre) | |||
|
621 | comp = [ pre2+m for m in cell_magics if m.startswith(bare_text)] | |||
|
622 | if not text.startswith(pre2): | |||
|
623 | comp += [ pre+m for m in line_magics if m.startswith(bare_text)] | |||
|
624 | return comp | |||
609 |
|
625 | |||
610 | def alias_matches(self, text): |
|
626 | def alias_matches(self, text): | |
611 | """Match internal system aliases""" |
|
627 | """Match internal system aliases""" | |
@@ -820,7 +836,7 b' class IPCompleter(Completer):' | |||||
820 |
|
836 | |||
821 | self.line_buffer = line_buffer |
|
837 | self.line_buffer = line_buffer | |
822 | self.text_until_cursor = self.line_buffer[:cursor_pos] |
|
838 | self.text_until_cursor = self.line_buffer[:cursor_pos] | |
823 |
#io.rprint(' |
|
839 | #io.rprint('COMP2 %r %r %r' % (text, line_buffer, cursor_pos)) # dbg | |
824 |
|
840 | |||
825 | # Start with a clean slate of completions |
|
841 | # Start with a clean slate of completions | |
826 | self.matches[:] = [] |
|
842 | self.matches[:] = [] |
@@ -15,7 +15,6 b' from __future__ import print_function' | |||||
15 | # Stdlib imports |
|
15 | # Stdlib imports | |
16 | import atexit |
|
16 | import atexit | |
17 | import datetime |
|
17 | import datetime | |
18 | from io import open as io_open |
|
|||
19 | import os |
|
18 | import os | |
20 | import re |
|
19 | import re | |
21 | try: |
|
20 | try: | |
@@ -25,11 +24,8 b' except ImportError:' | |||||
25 | import threading |
|
24 | import threading | |
26 |
|
25 | |||
27 | # Our own packages |
|
26 | # Our own packages | |
28 | from IPython.core.error import StdinNotImplementedError |
|
|||
29 | from IPython.config.configurable import Configurable |
|
27 | from IPython.config.configurable import Configurable | |
30 | from IPython.external.decorator import decorator |
|
28 | from IPython.external.decorator import decorator | |
31 | from IPython.testing.skipdoctest import skip_doctest |
|
|||
32 | from IPython.utils import io |
|
|||
33 | from IPython.utils.path import locate_profile |
|
29 | from IPython.utils.path import locate_profile | |
34 | from IPython.utils.traitlets import Bool, Dict, Instance, Integer, List, Unicode |
|
30 | from IPython.utils.traitlets import Bool, Dict, Instance, Integer, List, Unicode | |
35 | from IPython.utils.warn import warn |
|
31 | from IPython.utils.warn import warn | |
@@ -53,7 +49,8 b' class DummyDB(object):' | |||||
53 |
|
49 | |||
54 | def __exit__(self, *args, **kwargs): |
|
50 | def __exit__(self, *args, **kwargs): | |
55 | pass |
|
51 | pass | |
56 |
|
52 | |||
|
53 | ||||
57 | @decorator |
|
54 | @decorator | |
58 | def needs_sqlite(f,*a,**kw): |
|
55 | def needs_sqlite(f,*a,**kw): | |
59 | """return an empty list in the absence of sqlite""" |
|
56 | """return an empty list in the absence of sqlite""" | |
@@ -62,6 +59,7 b' def needs_sqlite(f,*a,**kw):' | |||||
62 | else: |
|
59 | else: | |
63 | return f(*a,**kw) |
|
60 | return f(*a,**kw) | |
64 |
|
61 | |||
|
62 | ||||
65 | class HistoryAccessor(Configurable): |
|
63 | class HistoryAccessor(Configurable): | |
66 | """Access the history database without adding to it. |
|
64 | """Access the history database without adding to it. | |
67 |
|
65 | |||
@@ -72,19 +70,18 b' class HistoryAccessor(Configurable):' | |||||
72 | hist_file = Unicode(config=True, |
|
70 | hist_file = Unicode(config=True, | |
73 | help="""Path to file to use for SQLite history database. |
|
71 | help="""Path to file to use for SQLite history database. | |
74 |
|
72 | |||
75 |
By default, IPython will put the history database in the IPython |
|
73 | By default, IPython will put the history database in the IPython | |
76 |
directory. If you would rather share one history among |
|
74 | profile directory. If you would rather share one history among | |
77 | you ca set this value in each, so that they are consistent. |
|
75 | profiles, you can set this value in each, so that they are consistent. | |
78 |
|
76 | |||
79 |
Due to an issue with fcntl, SQLite is known to misbehave on some NFS |
|
77 | Due to an issue with fcntl, SQLite is known to misbehave on some NFS | |
80 |
If you see IPython hanging, try setting this to something on a |
|
78 | mounts. If you see IPython hanging, try setting this to something on a | |
81 | e.g:: |
|
79 | local disk, e.g:: | |
82 |
|
80 | |||
83 | ipython --HistoryManager.hist_file=/tmp/ipython_hist.sqlite |
|
81 | ipython --HistoryManager.hist_file=/tmp/ipython_hist.sqlite | |
84 |
|
82 | |||
85 | """) |
|
83 | """) | |
86 |
|
84 | |||
87 |
|
||||
88 | # The SQLite database |
|
85 | # The SQLite database | |
89 | if sqlite3: |
|
86 | if sqlite3: | |
90 | db = Instance(sqlite3.Connection) |
|
87 | db = Instance(sqlite3.Connection) | |
@@ -152,7 +149,8 b' class HistoryAccessor(Configurable):' | |||||
152 | def init_db(self): |
|
149 | def init_db(self): | |
153 | """Connect to the database, and create tables if necessary.""" |
|
150 | """Connect to the database, and create tables if necessary.""" | |
154 | # use detect_types so that timestamps return datetime objects |
|
151 | # use detect_types so that timestamps return datetime objects | |
155 |
self.db = sqlite3.connect(self.hist_file, |
|
152 | self.db = sqlite3.connect(self.hist_file, | |
|
153 | detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES) | |||
156 | self.db.execute("""CREATE TABLE IF NOT EXISTS sessions (session integer |
|
154 | self.db.execute("""CREATE TABLE IF NOT EXISTS sessions (session integer | |
157 | primary key autoincrement, start timestamp, |
|
155 | primary key autoincrement, start timestamp, | |
158 | end timestamp, num_cmds integer, remark text)""") |
|
156 | end timestamp, num_cmds integer, remark text)""") | |
@@ -215,7 +213,8 b' class HistoryAccessor(Configurable):' | |||||
215 | Returns |
|
213 | Returns | |
216 | ------- |
|
214 | ------- | |
217 |
|
215 | |||
218 |
(session_id [int], start [datetime], end [datetime], num_cmds [int], |
|
216 | (session_id [int], start [datetime], end [datetime], num_cmds [int], | |
|
217 | remark [unicode]) | |||
219 |
|
218 | |||
220 | Sessions that are running or did not exit cleanly will have `end=None` |
|
219 | Sessions that are running or did not exit cleanly will have `end=None` | |
221 | and `num_cmds=None`. |
|
220 | and `num_cmds=None`. | |
@@ -511,7 +510,8 b' class HistoryManager(HistoryAccessor):' | |||||
511 | session += self.session_number |
|
510 | session += self.session_number | |
512 | if session==self.session_number: # Current session |
|
511 | if session==self.session_number: # Current session | |
513 | return self._get_range_session(start, stop, raw, output) |
|
512 | return self._get_range_session(start, stop, raw, output) | |
514 |
return super(HistoryManager, self).get_range(session, start, stop, raw, |
|
513 | return super(HistoryManager, self).get_range(session, start, stop, raw, | |
|
514 | output) | |||
515 |
|
515 | |||
516 | ## ---------------------------- |
|
516 | ## ---------------------------- | |
517 | ## Methods for storing history: |
|
517 | ## Methods for storing history: | |
@@ -610,7 +610,9 b' class HistoryManager(HistoryAccessor):' | |||||
610 | print("ERROR! Session/line number was not unique in", |
|
610 | print("ERROR! Session/line number was not unique in", | |
611 | "database. History logging moved to new session", |
|
611 | "database. History logging moved to new session", | |
612 | self.session_number) |
|
612 | self.session_number) | |
613 | try: # Try writing to the new session. If this fails, don't recurse |
|
613 | try: | |
|
614 | # Try writing to the new session. If this fails, don't | |||
|
615 | # recurse | |||
614 | self._writeout_input_cache(conn) |
|
616 | self._writeout_input_cache(conn) | |
615 | except sqlite3.IntegrityError: |
|
617 | except sqlite3.IntegrityError: | |
616 | pass |
|
618 | pass | |
@@ -676,6 +678,7 b' range_re = re.compile(r"""' | |||||
676 | (?P<end>\d+))? |
|
678 | (?P<end>\d+))? | |
677 | $""", re.VERBOSE) |
|
679 | $""", re.VERBOSE) | |
678 |
|
680 | |||
|
681 | ||||
679 | def extract_hist_ranges(ranges_str): |
|
682 | def extract_hist_ranges(ranges_str): | |
680 | """Turn a string of history ranges into 3-tuples of (session, start, stop). |
|
683 | """Turn a string of history ranges into 3-tuples of (session, start, stop). | |
681 |
|
684 | |||
@@ -708,270 +711,11 b' def extract_hist_ranges(ranges_str):' | |||||
708 | yield (sess, 1, None) |
|
711 | yield (sess, 1, None) | |
709 | yield (endsess, 1, end) |
|
712 | yield (endsess, 1, end) | |
710 |
|
713 | |||
|
714 | ||||
711 | def _format_lineno(session, line): |
|
715 | def _format_lineno(session, line): | |
712 | """Helper function to format line numbers properly.""" |
|
716 | """Helper function to format line numbers properly.""" | |
713 | if session == 0: |
|
717 | if session == 0: | |
714 | return str(line) |
|
718 | return str(line) | |
715 | return "%s#%s" % (session, line) |
|
719 | return "%s#%s" % (session, line) | |
716 |
|
720 | |||
717 | @skip_doctest |
|
|||
718 | def magic_history(self, parameter_s = ''): |
|
|||
719 | """Print input history (_i<n> variables), with most recent last. |
|
|||
720 |
|
||||
721 | %history [-o -p -t -n] [-f filename] [range | -g pattern | -l number] |
|
|||
722 |
|
||||
723 | By default, input history is printed without line numbers so it can be |
|
|||
724 | directly pasted into an editor. Use -n to show them. |
|
|||
725 |
|
||||
726 | By default, all input history from the current session is displayed. |
|
|||
727 | Ranges of history can be indicated using the syntax: |
|
|||
728 | 4 : Line 4, current session |
|
|||
729 | 4-6 : Lines 4-6, current session |
|
|||
730 | 243/1-5: Lines 1-5, session 243 |
|
|||
731 | ~2/7 : Line 7, session 2 before current |
|
|||
732 | ~8/1-~6/5 : From the first line of 8 sessions ago, to the fifth line |
|
|||
733 | of 6 sessions ago. |
|
|||
734 | Multiple ranges can be entered, separated by spaces |
|
|||
735 |
|
||||
736 | The same syntax is used by %macro, %save, %edit, %rerun |
|
|||
737 |
|
||||
738 | Options: |
|
|||
739 |
|
||||
740 | -n: print line numbers for each input. |
|
|||
741 | This feature is only available if numbered prompts are in use. |
|
|||
742 |
|
||||
743 | -o: also print outputs for each input. |
|
|||
744 |
|
||||
745 | -p: print classic '>>>' python prompts before each input. This is useful |
|
|||
746 | for making documentation, and in conjunction with -o, for producing |
|
|||
747 | doctest-ready output. |
|
|||
748 |
|
||||
749 | -r: (default) print the 'raw' history, i.e. the actual commands you typed. |
|
|||
750 |
|
||||
751 | -t: print the 'translated' history, as IPython understands it. IPython |
|
|||
752 | filters your input and converts it all into valid Python source before |
|
|||
753 | executing it (things like magics or aliases are turned into function |
|
|||
754 | calls, for example). With this option, you'll see the native history |
|
|||
755 | instead of the user-entered version: '%cd /' will be seen as |
|
|||
756 | 'get_ipython().magic("%cd /")' instead of '%cd /'. |
|
|||
757 |
|
||||
758 | -g: treat the arg as a pattern to grep for in (full) history. |
|
|||
759 | This includes the saved history (almost all commands ever written). |
|
|||
760 | Use '%hist -g' to show full saved history (may be very long). |
|
|||
761 |
|
||||
762 | -l: get the last n lines from all sessions. Specify n as a single arg, or |
|
|||
763 | the default is the last 10 lines. |
|
|||
764 |
|
||||
765 | -f FILENAME: instead of printing the output to the screen, redirect it to |
|
|||
766 | the given file. The file is always overwritten, though *when it can*, |
|
|||
767 | IPython asks for confirmation first. In particular, running the command |
|
|||
768 | "history -f FILENAME" from the IPython Notebook interface will replace |
|
|||
769 | FILENAME even if it already exists *without* confirmation. |
|
|||
770 |
|
||||
771 | Examples |
|
|||
772 | -------- |
|
|||
773 | :: |
|
|||
774 |
|
||||
775 | In [6]: %hist -n 4-6 |
|
|||
776 | 4:a = 12 |
|
|||
777 | 5:print a**2 |
|
|||
778 | 6:%hist -n 4-6 |
|
|||
779 |
|
||||
780 | """ |
|
|||
781 |
|
||||
782 | if not self.shell.displayhook.do_full_cache: |
|
|||
783 | print('This feature is only available if numbered prompts are in use.') |
|
|||
784 | return |
|
|||
785 | opts,args = self.parse_options(parameter_s,'noprtglf:',mode='string') |
|
|||
786 |
|
||||
787 | # For brevity |
|
|||
788 | history_manager = self.shell.history_manager |
|
|||
789 |
|
||||
790 | def _format_lineno(session, line): |
|
|||
791 | """Helper function to format line numbers properly.""" |
|
|||
792 | if session in (0, history_manager.session_number): |
|
|||
793 | return str(line) |
|
|||
794 | return "%s/%s" % (session, line) |
|
|||
795 |
|
||||
796 | # Check if output to specific file was requested. |
|
|||
797 | try: |
|
|||
798 | outfname = opts['f'] |
|
|||
799 | except KeyError: |
|
|||
800 | outfile = io.stdout # default |
|
|||
801 | # We don't want to close stdout at the end! |
|
|||
802 | close_at_end = False |
|
|||
803 | else: |
|
|||
804 | if os.path.exists(outfname): |
|
|||
805 | try: |
|
|||
806 | ans = io.ask_yes_no("File %r exists. Overwrite?" % outfname) |
|
|||
807 | except StdinNotImplementedError: |
|
|||
808 | ans = True |
|
|||
809 | if not ans: |
|
|||
810 | print('Aborting.') |
|
|||
811 | return |
|
|||
812 | print("Overwriting file.") |
|
|||
813 | outfile = io_open(outfname, 'w', encoding='utf-8') |
|
|||
814 | close_at_end = True |
|
|||
815 |
|
||||
816 | print_nums = 'n' in opts |
|
|||
817 | get_output = 'o' in opts |
|
|||
818 | pyprompts = 'p' in opts |
|
|||
819 | # Raw history is the default |
|
|||
820 | raw = not('t' in opts) |
|
|||
821 |
|
||||
822 | default_length = 40 |
|
|||
823 | pattern = None |
|
|||
824 |
|
||||
825 | if 'g' in opts: # Glob search |
|
|||
826 | pattern = "*" + args + "*" if args else "*" |
|
|||
827 | hist = history_manager.search(pattern, raw=raw, output=get_output) |
|
|||
828 | print_nums = True |
|
|||
829 | elif 'l' in opts: # Get 'tail' |
|
|||
830 | try: |
|
|||
831 | n = int(args) |
|
|||
832 | except ValueError, IndexError: |
|
|||
833 | n = 10 |
|
|||
834 | hist = history_manager.get_tail(n, raw=raw, output=get_output) |
|
|||
835 | else: |
|
|||
836 | if args: # Get history by ranges |
|
|||
837 | hist = history_manager.get_range_by_str(args, raw, get_output) |
|
|||
838 | else: # Just get history for the current session |
|
|||
839 | hist = history_manager.get_range(raw=raw, output=get_output) |
|
|||
840 |
|
||||
841 | # We could be displaying the entire history, so let's not try to pull it |
|
|||
842 | # into a list in memory. Anything that needs more space will just misalign. |
|
|||
843 | width = 4 |
|
|||
844 |
|
||||
845 | for session, lineno, inline in hist: |
|
|||
846 | # Print user history with tabs expanded to 4 spaces. The GUI clients |
|
|||
847 | # use hard tabs for easier usability in auto-indented code, but we want |
|
|||
848 | # to produce PEP-8 compliant history for safe pasting into an editor. |
|
|||
849 | if get_output: |
|
|||
850 | inline, output = inline |
|
|||
851 | inline = inline.expandtabs(4).rstrip() |
|
|||
852 |
|
||||
853 | multiline = "\n" in inline |
|
|||
854 | line_sep = '\n' if multiline else ' ' |
|
|||
855 | if print_nums: |
|
|||
856 | print(u'%s:%s' % (_format_lineno(session, lineno).rjust(width), |
|
|||
857 | line_sep), file=outfile, end=u'') |
|
|||
858 | if pyprompts: |
|
|||
859 | print(u">>> ", end=u"", file=outfile) |
|
|||
860 | if multiline: |
|
|||
861 | inline = "\n... ".join(inline.splitlines()) + "\n..." |
|
|||
862 | print(inline, file=outfile) |
|
|||
863 | if get_output and output: |
|
|||
864 | print(output, file=outfile) |
|
|||
865 |
|
||||
866 | if close_at_end: |
|
|||
867 | outfile.close() |
|
|||
868 |
|
||||
869 |
|
||||
870 | def magic_rep(self, arg): |
|
|||
871 | r"""Repeat a command, or get command to input line for editing. |
|
|||
872 |
|
||||
873 | %recall and %rep are equivalent. |
|
|||
874 |
|
||||
875 | - %recall (no arguments): |
|
|||
876 |
|
||||
877 | Place a string version of last computation result (stored in the special '_' |
|
|||
878 | variable) to the next input prompt. Allows you to create elaborate command |
|
|||
879 | lines without using copy-paste:: |
|
|||
880 |
|
||||
881 | In[1]: l = ["hei", "vaan"] |
|
|||
882 | In[2]: "".join(l) |
|
|||
883 | Out[2]: heivaan |
|
|||
884 | In[3]: %rep |
|
|||
885 | In[4]: heivaan_ <== cursor blinking |
|
|||
886 |
|
||||
887 | %recall 45 |
|
|||
888 |
|
||||
889 | Place history line 45 on the next input prompt. Use %hist to find |
|
|||
890 | out the number. |
|
|||
891 |
|
||||
892 | %recall 1-4 |
|
|||
893 |
|
721 | |||
894 | Combine the specified lines into one cell, and place it on the next |
|
|||
895 | input prompt. See %history for the slice syntax. |
|
|||
896 |
|
||||
897 | %recall foo+bar |
|
|||
898 |
|
||||
899 | If foo+bar can be evaluated in the user namespace, the result is |
|
|||
900 | placed at the next input prompt. Otherwise, the history is searched |
|
|||
901 | for lines which contain that substring, and the most recent one is |
|
|||
902 | placed at the next input prompt. |
|
|||
903 | """ |
|
|||
904 | if not arg: # Last output |
|
|||
905 | self.set_next_input(str(self.shell.user_ns["_"])) |
|
|||
906 | return |
|
|||
907 | # Get history range |
|
|||
908 | histlines = self.history_manager.get_range_by_str(arg) |
|
|||
909 | cmd = "\n".join(x[2] for x in histlines) |
|
|||
910 | if cmd: |
|
|||
911 | self.set_next_input(cmd.rstrip()) |
|
|||
912 | return |
|
|||
913 |
|
||||
914 | try: # Variable in user namespace |
|
|||
915 | cmd = str(eval(arg, self.shell.user_ns)) |
|
|||
916 | except Exception: # Search for term in history |
|
|||
917 | histlines = self.history_manager.search("*"+arg+"*") |
|
|||
918 | for h in reversed([x[2] for x in histlines]): |
|
|||
919 | if 'rep' in h: |
|
|||
920 | continue |
|
|||
921 | self.set_next_input(h.rstrip()) |
|
|||
922 | return |
|
|||
923 | else: |
|
|||
924 | self.set_next_input(cmd.rstrip()) |
|
|||
925 | print("Couldn't evaluate or find in history:", arg) |
|
|||
926 |
|
||||
927 | def magic_rerun(self, parameter_s=''): |
|
|||
928 | """Re-run previous input |
|
|||
929 |
|
||||
930 | By default, you can specify ranges of input history to be repeated |
|
|||
931 | (as with %history). With no arguments, it will repeat the last line. |
|
|||
932 |
|
||||
933 | Options: |
|
|||
934 |
|
||||
935 | -l <n> : Repeat the last n lines of input, not including the |
|
|||
936 | current command. |
|
|||
937 |
|
||||
938 | -g foo : Repeat the most recent line which contains foo |
|
|||
939 | """ |
|
|||
940 | opts, args = self.parse_options(parameter_s, 'l:g:', mode='string') |
|
|||
941 | if "l" in opts: # Last n lines |
|
|||
942 | n = int(opts['l']) |
|
|||
943 | hist = self.history_manager.get_tail(n) |
|
|||
944 | elif "g" in opts: # Search |
|
|||
945 | p = "*"+opts['g']+"*" |
|
|||
946 | hist = list(self.history_manager.search(p)) |
|
|||
947 | for l in reversed(hist): |
|
|||
948 | if "rerun" not in l[2]: |
|
|||
949 | hist = [l] # The last match which isn't a %rerun |
|
|||
950 | break |
|
|||
951 | else: |
|
|||
952 | hist = [] # No matches except %rerun |
|
|||
953 | elif args: # Specify history ranges |
|
|||
954 | hist = self.history_manager.get_range_by_str(args) |
|
|||
955 | else: # Last line |
|
|||
956 | hist = self.history_manager.get_tail(1) |
|
|||
957 | hist = [x[2] for x in hist] |
|
|||
958 | if not hist: |
|
|||
959 | print("No lines in history match specification") |
|
|||
960 | return |
|
|||
961 | histlines = "\n".join(hist) |
|
|||
962 | print("=== Executing: ===") |
|
|||
963 | print(histlines) |
|
|||
964 | print("=== Output: ===") |
|
|||
965 | self.run_cell("\n".join(hist), store_history=False) |
|
|||
966 |
|
||||
967 |
|
||||
968 | def init_ipython(ip): |
|
|||
969 | ip.define_magic("rep", magic_rep) |
|
|||
970 | ip.define_magic("recall", magic_rep) |
|
|||
971 | ip.define_magic("rerun", magic_rerun) |
|
|||
972 | ip.define_magic("hist",magic_history) # Alternative name |
|
|||
973 | ip.define_magic("history",magic_history) |
|
|||
974 |
|
||||
975 | # XXX - ipy_completers are in quarantine, need to be updated to new apis |
|
|||
976 | #import ipy_completers |
|
|||
977 | #ipy_completers.quick_completer('%hist' ,'-g -t -r -n') |
|
@@ -128,9 +128,9 b' class CommandChainDispatcher:' | |||||
128 | def __call__(self,*args, **kw): |
|
128 | def __call__(self,*args, **kw): | |
129 | """ Command chain is called just like normal func. |
|
129 | """ Command chain is called just like normal func. | |
130 |
|
130 | |||
131 |
This will call all funcs in chain with the same args as were given to |
|
131 | This will call all funcs in chain with the same args as were given to | |
132 | function, and return the result of first func that didn't raise |
|
132 | this function, and return the result of first func that didn't raise | |
133 |
TryNext |
|
133 | TryNext""" | |
134 |
|
134 | |||
135 | for prio,cmd in self.chain: |
|
135 | for prio,cmd in self.chain: | |
136 | #print "prio",prio,"cmd",cmd #dbg |
|
136 | #print "prio",prio,"cmd",cmd #dbg |
@@ -55,12 +55,11 b' Authors' | |||||
55 | * Brian Granger |
|
55 | * Brian Granger | |
56 | """ |
|
56 | """ | |
57 | #----------------------------------------------------------------------------- |
|
57 | #----------------------------------------------------------------------------- | |
58 |
# Copyright (C) 2010 |
|
58 | # Copyright (C) 2010 The IPython Development Team | |
59 | # |
|
59 | # | |
60 | # Distributed under the terms of the BSD License. The full license is in |
|
60 | # Distributed under the terms of the BSD License. The full license is in | |
61 | # the file COPYING, distributed as part of this software. |
|
61 | # the file COPYING, distributed as part of this software. | |
62 | #----------------------------------------------------------------------------- |
|
62 | #----------------------------------------------------------------------------- | |
63 | from __future__ import print_function |
|
|||
64 |
|
63 | |||
65 | #----------------------------------------------------------------------------- |
|
64 | #----------------------------------------------------------------------------- | |
66 | # Imports |
|
65 | # Imports | |
@@ -141,6 +140,46 b' def num_ini_spaces(s):' | |||||
141 | else: |
|
140 | else: | |
142 | return 0 |
|
141 | return 0 | |
143 |
|
142 | |||
|
143 | def last_blank(src): | |||
|
144 | """Determine if the input source ends in a blank. | |||
|
145 | ||||
|
146 | A blank is either a newline or a line consisting of whitespace. | |||
|
147 | ||||
|
148 | Parameters | |||
|
149 | ---------- | |||
|
150 | src : string | |||
|
151 | A single or multiline string. | |||
|
152 | """ | |||
|
153 | if not src: return False | |||
|
154 | ll = src.splitlines()[-1] | |||
|
155 | return (ll == '') or ll.isspace() | |||
|
156 | ||||
|
157 | ||||
|
158 | last_two_blanks_re = re.compile(r'\n\s*\n\s*$', re.MULTILINE) | |||
|
159 | last_two_blanks_re2 = re.compile(r'.+\n\s*\n\s+$', re.MULTILINE) | |||
|
160 | ||||
|
161 | def last_two_blanks(src): | |||
|
162 | """Determine if the input source ends in two blanks. | |||
|
163 | ||||
|
164 | A blank is either a newline or a line consisting of whitespace. | |||
|
165 | ||||
|
166 | Parameters | |||
|
167 | ---------- | |||
|
168 | src : string | |||
|
169 | A single or multiline string. | |||
|
170 | """ | |||
|
171 | if not src: return False | |||
|
172 | # The logic here is tricky: I couldn't get a regexp to work and pass all | |||
|
173 | # the tests, so I took a different approach: split the source by lines, | |||
|
174 | # grab the last two and prepend '###\n' as a stand-in for whatever was in | |||
|
175 | # the body before the last two lines. Then, with that structure, it's | |||
|
176 | # possible to analyze with two regexps. Not the most elegant solution, but | |||
|
177 | # it works. If anyone tries to change this logic, make sure to validate | |||
|
178 | # the whole test suite first! | |||
|
179 | new_src = '\n'.join(['###\n'] + src.splitlines()[-2:]) | |||
|
180 | return (bool(last_two_blanks_re.match(new_src)) or | |||
|
181 | bool(last_two_blanks_re2.match(new_src)) ) | |||
|
182 | ||||
144 |
|
183 | |||
145 | def remove_comments(src): |
|
184 | def remove_comments(src): | |
146 | """Remove all comments from input source. |
|
185 | """Remove all comments from input source. | |
@@ -558,20 +597,23 b' def _make_help_call(target, esc, lspace, next_input=None):' | |||||
558 | else 'psearch' if '*' in target \ |
|
597 | else 'psearch' if '*' in target \ | |
559 | else 'pinfo' |
|
598 | else 'pinfo' | |
560 | arg = " ".join([method, target]) |
|
599 | arg = " ".join([method, target]) | |
561 |
|
600 | if next_input is None: | ||
562 | if next_input: |
|
|||
563 | tpl = '%sget_ipython().magic(%r, next_input=%r)' |
|
|||
564 | return tpl % (lspace, arg, next_input) |
|
|||
565 | else: |
|
|||
566 | return '%sget_ipython().magic(%r)' % (lspace, arg) |
|
601 | return '%sget_ipython().magic(%r)' % (lspace, arg) | |
|
602 | else: | |||
|
603 | return '%sget_ipython().set_next_input(%r);get_ipython().magic(%r)' % \ | |||
|
604 | (lspace, next_input, arg) | |||
|
605 | ||||
567 |
|
606 | |||
568 | _initial_space_re = re.compile(r'\s*') |
|
607 | _initial_space_re = re.compile(r'\s*') | |
569 | _help_end_re = re.compile(r"""(%? |
|
608 | ||
|
609 | _help_end_re = re.compile(r"""(%{0,2} | |||
570 | [a-zA-Z_*][\w*]* # Variable name |
|
610 | [a-zA-Z_*][\w*]* # Variable name | |
571 | (\.[a-zA-Z_*][\w*]*)* # .etc.etc |
|
611 | (\.[a-zA-Z_*][\w*]*)* # .etc.etc | |
572 | ) |
|
612 | ) | |
573 | (\?\??)$ # ? or ??""", |
|
613 | (\?\??)$ # ? or ??""", | |
574 | re.VERBOSE) |
|
614 | re.VERBOSE) | |
|
615 | ||||
|
616 | ||||
575 | def transform_help_end(line): |
|
617 | def transform_help_end(line): | |
576 | """Translate lines with ?/?? at the end""" |
|
618 | """Translate lines with ?/?? at the end""" | |
577 | m = _help_end_re.search(line) |
|
619 | m = _help_end_re.search(line) | |
@@ -681,20 +723,31 b' class IPythonInputSplitter(InputSplitter):' | |||||
681 | # String with raw, untransformed input. |
|
723 | # String with raw, untransformed input. | |
682 | source_raw = '' |
|
724 | source_raw = '' | |
683 |
|
725 | |||
|
726 | # Flag to track when we're in the middle of processing a cell magic, since | |||
|
727 | # the logic has to change. In that case, we apply no transformations at | |||
|
728 | # all. | |||
|
729 | processing_cell_magic = False | |||
|
730 | ||||
|
731 | # Storage for all blocks of input that make up a cell magic | |||
|
732 | cell_magic_parts = [] | |||
|
733 | ||||
684 | # Private attributes |
|
734 | # Private attributes | |
685 |
|
735 | |||
686 | # List with lines of raw input accumulated so far. |
|
736 | # List with lines of raw input accumulated so far. | |
687 | _buffer_raw = None |
|
737 | _buffer_raw = None | |
688 |
|
738 | |||
689 | def __init__(self, input_mode=None): |
|
739 | def __init__(self, input_mode=None): | |
690 |
InputSplitter.__init__( |
|
740 | super(IPythonInputSplitter, self).__init__(input_mode) | |
691 | self._buffer_raw = [] |
|
741 | self._buffer_raw = [] | |
|
742 | self._validate = True | |||
692 |
|
743 | |||
693 | def reset(self): |
|
744 | def reset(self): | |
694 | """Reset the input buffer and associated state.""" |
|
745 | """Reset the input buffer and associated state.""" | |
695 |
InputSplitter.reset( |
|
746 | super(IPythonInputSplitter, self).reset() | |
696 | self._buffer_raw[:] = [] |
|
747 | self._buffer_raw[:] = [] | |
697 | self.source_raw = '' |
|
748 | self.source_raw = '' | |
|
749 | self.cell_magic_parts = [] | |||
|
750 | self.processing_cell_magic = False | |||
698 |
|
751 | |||
699 | def source_raw_reset(self): |
|
752 | def source_raw_reset(self): | |
700 | """Return input and raw source and perform a full reset. |
|
753 | """Return input and raw source and perform a full reset. | |
@@ -704,8 +757,79 b' class IPythonInputSplitter(InputSplitter):' | |||||
704 | self.reset() |
|
757 | self.reset() | |
705 | return out, out_r |
|
758 | return out, out_r | |
706 |
|
759 | |||
|
760 | def push_accepts_more(self): | |||
|
761 | if self.processing_cell_magic: | |||
|
762 | return not self._is_complete | |||
|
763 | else: | |||
|
764 | return super(IPythonInputSplitter, self).push_accepts_more() | |||
|
765 | ||||
|
766 | def _handle_cell_magic(self, lines): | |||
|
767 | """Process lines when they start with %%, which marks cell magics. | |||
|
768 | """ | |||
|
769 | self.processing_cell_magic = True | |||
|
770 | first, _, body = lines.partition('\n') | |||
|
771 | magic_name, _, line = first.partition(' ') | |||
|
772 | magic_name = magic_name.lstrip(ESC_MAGIC) | |||
|
773 | # We store the body of the cell and create a call to a method that | |||
|
774 | # will use this stored value. This is ugly, but it's a first cut to | |||
|
775 | # get it all working, as right now changing the return API of our | |||
|
776 | # methods would require major refactoring. | |||
|
777 | self.cell_magic_parts = [body] | |||
|
778 | tpl = 'get_ipython()._run_cached_cell_magic(%r, %r)' | |||
|
779 | tlines = tpl % (magic_name, line) | |||
|
780 | self._store(tlines) | |||
|
781 | self._store(lines, self._buffer_raw, 'source_raw') | |||
|
782 | # We can actually choose whether to allow for single blank lines here | |||
|
783 | # during input for clients that use cell mode to decide when to stop | |||
|
784 | # pushing input (currently only the Qt console). | |||
|
785 | # My first implementation did that, and then I realized it wasn't | |||
|
786 | # consistent with the terminal behavior, so I've reverted it to one | |||
|
787 | # line. But I'm leaving it here so we can easily test both behaviors, | |||
|
788 | # I kind of liked having full blank lines allowed in the cell magics... | |||
|
789 | #self._is_complete = last_two_blanks(lines) | |||
|
790 | self._is_complete = last_blank(lines) | |||
|
791 | return self._is_complete | |||
|
792 | ||||
|
793 | def _line_mode_cell_append(self, lines): | |||
|
794 | """Append new content for a cell magic in line mode. | |||
|
795 | """ | |||
|
796 | # Only store the raw input. Lines beyond the first one are only only | |||
|
797 | # stored for history purposes; for execution the caller will grab the | |||
|
798 | # magic pieces from cell_magic_parts and will assemble the cell body | |||
|
799 | self._store(lines, self._buffer_raw, 'source_raw') | |||
|
800 | self.cell_magic_parts.append(lines) | |||
|
801 | # Find out if the last stored block has a whitespace line as its | |||
|
802 | # last line and also this line is whitespace, case in which we're | |||
|
803 | # done (two contiguous blank lines signal termination). Note that | |||
|
804 | # the storage logic *enforces* that every stored block is | |||
|
805 | # newline-terminated, so we grab everything but the last character | |||
|
806 | # so we can have the body of the block alone. | |||
|
807 | last_block = self.cell_magic_parts[-1] | |||
|
808 | self._is_complete = last_blank(last_block) and lines.isspace() | |||
|
809 | return self._is_complete | |||
|
810 | ||||
707 | def push(self, lines): |
|
811 | def push(self, lines): | |
708 | """Push one or more lines of IPython input. |
|
812 | """Push one or more lines of IPython input. | |
|
813 | ||||
|
814 | This stores the given lines and returns a status code indicating | |||
|
815 | whether the code forms a complete Python block or not, after processing | |||
|
816 | all input lines for special IPython syntax. | |||
|
817 | ||||
|
818 | Any exceptions generated in compilation are swallowed, but if an | |||
|
819 | exception was produced, the method returns True. | |||
|
820 | ||||
|
821 | Parameters | |||
|
822 | ---------- | |||
|
823 | lines : string | |||
|
824 | One or more lines of Python input. | |||
|
825 | ||||
|
826 | Returns | |||
|
827 | ------- | |||
|
828 | is_complete : boolean | |||
|
829 | True if the current input source (the result of the current input | |||
|
830 | plus prior inputs) forms a complete Python execution block. Note that | |||
|
831 | this value is also stored as a private attribute (_is_complete), so it | |||
|
832 | can be queried at any time. | |||
709 | """ |
|
833 | """ | |
710 | if not lines: |
|
834 | if not lines: | |
711 | return super(IPythonInputSplitter, self).push(lines) |
|
835 | return super(IPythonInputSplitter, self).push(lines) | |
@@ -713,6 +837,18 b' class IPythonInputSplitter(InputSplitter):' | |||||
713 | # We must ensure all input is pure unicode |
|
837 | # We must ensure all input is pure unicode | |
714 | lines = cast_unicode(lines, self.encoding) |
|
838 | lines = cast_unicode(lines, self.encoding) | |
715 |
|
839 | |||
|
840 | # If the entire input block is a cell magic, return after handling it | |||
|
841 | # as the rest of the transformation logic should be skipped. | |||
|
842 | if lines.startswith('%%') and not \ | |||
|
843 | (len(lines.splitlines()) == 1 and lines.strip().endswith('?')): | |||
|
844 | return self._handle_cell_magic(lines) | |||
|
845 | ||||
|
846 | # In line mode, a cell magic can arrive in separate pieces | |||
|
847 | if self.input_mode == 'line' and self.processing_cell_magic: | |||
|
848 | return self._line_mode_cell_append(lines) | |||
|
849 | ||||
|
850 | # The rest of the processing is for 'normal' content, i.e. IPython | |||
|
851 | # source that we process through our transformations pipeline. | |||
716 | lines_list = lines.splitlines() |
|
852 | lines_list = lines.splitlines() | |
717 |
|
853 | |||
718 | transforms = [transform_ipy_prompt, transform_classic_prompt, |
|
854 | transforms = [transform_ipy_prompt, transform_classic_prompt, | |
@@ -755,8 +891,7 b' class IPythonInputSplitter(InputSplitter):' | |||||
755 | buf = self._buffer |
|
891 | buf = self._buffer | |
756 | for line in lines_list: |
|
892 | for line in lines_list: | |
757 | if self._is_complete or not buf or \ |
|
893 | if self._is_complete or not buf or \ | |
758 |
(buf and |
|
894 | (buf and buf[-1].rstrip().endswith((':', ','))): | |
759 | buf[-1].rstrip().endswith(',')) ): |
|
|||
760 | for f in transforms: |
|
895 | for f in transforms: | |
761 | line = f(line) |
|
896 | line = f(line) | |
762 |
|
897 |
@@ -28,11 +28,18 b' import runpy' | |||||
28 | import sys |
|
28 | import sys | |
29 | import tempfile |
|
29 | import tempfile | |
30 | import types |
|
30 | import types | |
31 | import urllib |
|
31 | ||
32 | from io import open as io_open |
|
32 | # We need to use nested to support python 2.6, once we move to >=2.7, we can | |
|
33 | # use the with keyword's new builtin support for nested managers | |||
|
34 | try: | |||
|
35 | from contextlib import nested | |||
|
36 | except: | |||
|
37 | from IPython.utils.nested_context import nested | |||
33 |
|
38 | |||
34 | from IPython.config.configurable import SingletonConfigurable |
|
39 | from IPython.config.configurable import SingletonConfigurable | |
35 | from IPython.core import debugger, oinspect |
|
40 | from IPython.core import debugger, oinspect | |
|
41 | from IPython.core import history as ipcorehist | |||
|
42 | from IPython.core import magic | |||
36 | from IPython.core import page |
|
43 | from IPython.core import page | |
37 | from IPython.core import prefilter |
|
44 | from IPython.core import prefilter | |
38 | from IPython.core import shadowns |
|
45 | from IPython.core import shadowns | |
@@ -52,7 +59,6 b' from IPython.core.history import HistoryManager' | |||||
52 | from IPython.core.inputsplitter import IPythonInputSplitter |
|
59 | from IPython.core.inputsplitter import IPythonInputSplitter | |
53 | from IPython.core.logger import Logger |
|
60 | from IPython.core.logger import Logger | |
54 | from IPython.core.macro import Macro |
|
61 | from IPython.core.macro import Macro | |
55 | from IPython.core.magic import Magic |
|
|||
56 | from IPython.core.payload import PayloadManager |
|
62 | from IPython.core.payload import PayloadManager | |
57 | from IPython.core.plugin import PluginManager |
|
63 | from IPython.core.plugin import PluginManager | |
58 | from IPython.core.prefilter import PrefilterManager, ESC_MAGIC |
|
64 | from IPython.core.prefilter import PrefilterManager, ESC_MAGIC | |
@@ -187,7 +193,7 b' class ReadlineNoRecord(object):' | |||||
187 | # Main IPython class |
|
193 | # Main IPython class | |
188 | #----------------------------------------------------------------------------- |
|
194 | #----------------------------------------------------------------------------- | |
189 |
|
195 | |||
190 |
class InteractiveShell(SingletonConfigurable |
|
196 | class InteractiveShell(SingletonConfigurable): | |
191 | """An enhanced, interactive shell for Python.""" |
|
197 | """An enhanced, interactive shell for Python.""" | |
192 |
|
198 | |||
193 | _instance = None |
|
199 | _instance = None | |
@@ -380,6 +386,7 b' class InteractiveShell(SingletonConfigurable, Magic):' | |||||
380 | plugin_manager = Instance('IPython.core.plugin.PluginManager') |
|
386 | plugin_manager = Instance('IPython.core.plugin.PluginManager') | |
381 | payload_manager = Instance('IPython.core.payload.PayloadManager') |
|
387 | payload_manager = Instance('IPython.core.payload.PayloadManager') | |
382 | history_manager = Instance('IPython.core.history.HistoryManager') |
|
388 | history_manager = Instance('IPython.core.history.HistoryManager') | |
|
389 | magics_manager = Instance('IPython.core.magic.MagicsManager') | |||
383 |
|
390 | |||
384 | profile_dir = Instance('IPython.core.application.ProfileDir') |
|
391 | profile_dir = Instance('IPython.core.application.ProfileDir') | |
385 | @property |
|
392 | @property | |
@@ -430,8 +437,6 b' class InteractiveShell(SingletonConfigurable, Magic):' | |||||
430 | self.init_encoding() |
|
437 | self.init_encoding() | |
431 | self.init_prefilter() |
|
438 | self.init_prefilter() | |
432 |
|
439 | |||
433 | Magic.__init__(self, self) |
|
|||
434 |
|
||||
435 | self.init_syntax_highlighting() |
|
440 | self.init_syntax_highlighting() | |
436 | self.init_hooks() |
|
441 | self.init_hooks() | |
437 | self.init_pushd_popd_magic() |
|
442 | self.init_pushd_popd_magic() | |
@@ -588,11 +593,11 b' class InteractiveShell(SingletonConfigurable, Magic):' | |||||
588 | """Initialize logging in case it was requested at the command line. |
|
593 | """Initialize logging in case it was requested at the command line. | |
589 | """ |
|
594 | """ | |
590 | if self.logappend: |
|
595 | if self.logappend: | |
591 |
self.magic |
|
596 | self.magic('logstart %s append' % self.logappend) | |
592 | elif self.logfile: |
|
597 | elif self.logfile: | |
593 |
self.magic |
|
598 | self.magic('logstart %' % self.logfile) | |
594 | elif self.logstart: |
|
599 | elif self.logstart: | |
595 |
self.magic |
|
600 | self.magic('logstart') | |
596 |
|
601 | |||
597 | def init_builtins(self): |
|
602 | def init_builtins(self): | |
598 | # A single, static flag that we set to True. Its presence indicates |
|
603 | # A single, static flag that we set to True. Its presence indicates | |
@@ -1396,8 +1401,10 b' class InteractiveShell(SingletonConfigurable, Magic):' | |||||
1396 | # Try to see if it's magic |
|
1401 | # Try to see if it's magic | |
1397 | if not found: |
|
1402 | if not found: | |
1398 | if oname.startswith(ESC_MAGIC): |
|
1403 | if oname.startswith(ESC_MAGIC): | |
1399 |
oname = oname |
|
1404 | oname = oname.lstrip(ESC_MAGIC) | |
1400 |
obj = |
|
1405 | obj = self.find_line_magic(oname) | |
|
1406 | if obj is None: | |||
|
1407 | obj = self.find_cell_magic(oname) | |||
1401 | if obj is not None: |
|
1408 | if obj is not None: | |
1402 | found = True |
|
1409 | found = True | |
1403 | ospace = 'IPython internal' |
|
1410 | ospace = 'IPython internal' | |
@@ -1993,16 +2000,113 b' class InteractiveShell(SingletonConfigurable, Magic):' | |||||
1993 | #------------------------------------------------------------------------- |
|
2000 | #------------------------------------------------------------------------- | |
1994 |
|
2001 | |||
1995 | def init_magics(self): |
|
2002 | def init_magics(self): | |
|
2003 | from IPython.core import magics as m | |||
|
2004 | self.magics_manager = magic.MagicsManager(shell=self, | |||
|
2005 | confg=self.config, | |||
|
2006 | user_magics=m.UserMagics(self)) | |||
|
2007 | self.configurables.append(self.magics_manager) | |||
|
2008 | ||||
|
2009 | # Expose as public API from the magics manager | |||
|
2010 | self.register_magics = self.magics_manager.register | |||
|
2011 | self.register_magic_function = self.magics_manager.register_function | |||
|
2012 | self.define_magic = self.magics_manager.define_magic | |||
|
2013 | ||||
|
2014 | self.register_magics(m.AutoMagics, m.BasicMagics, m.CodeMagics, | |||
|
2015 | m.ConfigMagics, m.DeprecatedMagics, m.ExecutionMagics, | |||
|
2016 | m.ExtensionMagics, m.HistoryMagics, m.LoggingMagics, | |||
|
2017 | m.NamespaceMagics, m.OSMagics, m.PylabMagics ) | |||
|
2018 | ||||
1996 | # FIXME: Move the color initialization to the DisplayHook, which |
|
2019 | # FIXME: Move the color initialization to the DisplayHook, which | |
1997 | # should be split into a prompt manager and displayhook. We probably |
|
2020 | # should be split into a prompt manager and displayhook. We probably | |
1998 | # even need a centralize colors management object. |
|
2021 | # even need a centralize colors management object. | |
1999 |
self.magic |
|
2022 | self.magic('colors %s' % self.colors) | |
2000 | # History was moved to a separate module |
|
2023 | ||
2001 | from IPython.core import history |
|
2024 | def run_line_magic(self, magic_name, line): | |
2002 | history.init_ipython(self) |
|
2025 | """Execute the given line magic. | |
|
2026 | ||||
|
2027 | Parameters | |||
|
2028 | ---------- | |||
|
2029 | magic_name : str | |||
|
2030 | Name of the desired magic function, without '%' prefix. | |||
|
2031 | ||||
|
2032 | line : str | |||
|
2033 | The rest of the input line as a single string. | |||
|
2034 | """ | |||
|
2035 | fn = self.find_line_magic(magic_name) | |||
|
2036 | if fn is None: | |||
|
2037 | cm = self.find_cell_magic(magic_name) | |||
|
2038 | etpl = "Line magic function `%%%s` not found%s." | |||
|
2039 | extra = '' if cm is None else (' (But cell magic `%%%%%s` exists, ' | |||
|
2040 | 'did you mean that instead?)' % magic_name ) | |||
|
2041 | error(etpl % (magic_name, extra)) | |||
|
2042 | else: | |||
|
2043 | # Note: this is the distance in the stack to the user's frame. | |||
|
2044 | # This will need to be updated if the internal calling logic gets | |||
|
2045 | # refactored, or else we'll be expanding the wrong variables. | |||
|
2046 | stack_depth = 2 | |||
|
2047 | magic_arg_s = self.var_expand(line, stack_depth) | |||
|
2048 | # Put magic args in a list so we can call with f(*a) syntax | |||
|
2049 | args = [magic_arg_s] | |||
|
2050 | # Grab local namespace if we need it: | |||
|
2051 | if getattr(fn, "needs_local_scope", False): | |||
|
2052 | args.append(sys._getframe(stack_depth).f_locals) | |||
|
2053 | with self.builtin_trap: | |||
|
2054 | result = fn(*args) | |||
|
2055 | return result | |||
|
2056 | ||||
|
2057 | def run_cell_magic(self, magic_name, line, cell): | |||
|
2058 | """Execute the given cell magic. | |||
|
2059 | ||||
|
2060 | Parameters | |||
|
2061 | ---------- | |||
|
2062 | magic_name : str | |||
|
2063 | Name of the desired magic function, without '%' prefix. | |||
|
2064 | ||||
|
2065 | line : str | |||
|
2066 | The rest of the first input line as a single string. | |||
|
2067 | ||||
|
2068 | cell : str | |||
|
2069 | The body of the cell as a (possibly multiline) string. | |||
|
2070 | """ | |||
|
2071 | fn = self.find_cell_magic(magic_name) | |||
|
2072 | if fn is None: | |||
|
2073 | lm = self.find_line_magic(magic_name) | |||
|
2074 | etpl = "Cell magic function `%%%%%s` not found%s." | |||
|
2075 | extra = '' if lm is None else (' (But line magic `%%%s` exists, ' | |||
|
2076 | 'did you mean that instead?)' % magic_name ) | |||
|
2077 | error(etpl % (magic_name, extra)) | |||
|
2078 | else: | |||
|
2079 | # Note: this is the distance in the stack to the user's frame. | |||
|
2080 | # This will need to be updated if the internal calling logic gets | |||
|
2081 | # refactored, or else we'll be expanding the wrong variables. | |||
|
2082 | stack_depth = 2 | |||
|
2083 | magic_arg_s = self.var_expand(line, stack_depth) | |||
|
2084 | with self.builtin_trap: | |||
|
2085 | result = fn(line, cell) | |||
|
2086 | return result | |||
|
2087 | ||||
|
2088 | def find_line_magic(self, magic_name): | |||
|
2089 | """Find and return a line magic by name. | |||
|
2090 | ||||
|
2091 | Returns None if the magic isn't found.""" | |||
|
2092 | return self.magics_manager.magics['line'].get(magic_name) | |||
|
2093 | ||||
|
2094 | def find_cell_magic(self, magic_name): | |||
|
2095 | """Find and return a cell magic by name. | |||
2003 |
|
|
2096 | ||
2004 | def magic(self, arg_s, next_input=None): |
|
2097 | Returns None if the magic isn't found.""" | |
2005 | """Call a magic function by name. |
|
2098 | return self.magics_manager.magics['cell'].get(magic_name) | |
|
2099 | ||||
|
2100 | def find_magic(self, magic_name, magic_kind='line'): | |||
|
2101 | """Find and return a magic of the given type by name. | |||
|
2102 | ||||
|
2103 | Returns None if the magic isn't found.""" | |||
|
2104 | return self.magics_manager.magics[magic_kind].get(magic_name) | |||
|
2105 | ||||
|
2106 | def magic(self, arg_s): | |||
|
2107 | """DEPRECATED. Use run_line_magic() instead. | |||
|
2108 | ||||
|
2109 | Call a magic function by name. | |||
2006 |
|
2110 | |||
2007 | Input: a string containing the name of the magic function to call and |
|
2111 | Input: a string containing the name of the magic function to call and | |
2008 | any additional arguments to be passed to the magic. |
|
2112 | any additional arguments to be passed to the magic. | |
@@ -2018,45 +2122,10 b' class InteractiveShell(SingletonConfigurable, Magic):' | |||||
2018 | valid Python code you can type at the interpreter, including loops and |
|
2122 | valid Python code you can type at the interpreter, including loops and | |
2019 | compound statements. |
|
2123 | compound statements. | |
2020 | """ |
|
2124 | """ | |
2021 | # Allow setting the next input - this is used if the user does `a=abs?`. |
|
2125 | # TODO: should we issue a loud deprecation warning here? | |
2022 | # We do this first so that magic functions can override it. |
|
2126 | magic_name, _, magic_arg_s = arg_s.partition(' ') | |
2023 | if next_input: |
|
|||
2024 | self.set_next_input(next_input) |
|
|||
2025 |
|
||||
2026 | magic_name, _, magic_args = arg_s.partition(' ') |
|
|||
2027 | magic_name = magic_name.lstrip(prefilter.ESC_MAGIC) |
|
2127 | magic_name = magic_name.lstrip(prefilter.ESC_MAGIC) | |
2028 |
|
2128 | return self.run_line_magic(magic_name, magic_arg_s) | ||
2029 | fn = getattr(self,'magic_'+magic_name,None) |
|
|||
2030 | if fn is None: |
|
|||
2031 | error("Magic function `%s` not found." % magic_name) |
|
|||
2032 | else: |
|
|||
2033 | magic_args = self.var_expand(magic_args,1) |
|
|||
2034 | # Grab local namespace if we need it: |
|
|||
2035 | if getattr(fn, "needs_local_scope", False): |
|
|||
2036 | self._magic_locals = sys._getframe(1).f_locals |
|
|||
2037 | with self.builtin_trap: |
|
|||
2038 | result = fn(magic_args) |
|
|||
2039 | # Ensure we're not keeping object references around: |
|
|||
2040 | self._magic_locals = {} |
|
|||
2041 | return result |
|
|||
2042 |
|
||||
2043 | def define_magic(self, magicname, func): |
|
|||
2044 | """Expose own function as magic function for ipython |
|
|||
2045 |
|
||||
2046 | Example:: |
|
|||
2047 |
|
||||
2048 | def foo_impl(self,parameter_s=''): |
|
|||
2049 | 'My very own magic!. (Use docstrings, IPython reads them).' |
|
|||
2050 | print 'Magic function. Passed parameter is between < >:' |
|
|||
2051 | print '<%s>' % parameter_s |
|
|||
2052 | print 'The self object is:', self |
|
|||
2053 |
|
||||
2054 | ip.define_magic('foo',foo_impl) |
|
|||
2055 | """ |
|
|||
2056 | im = types.MethodType(func,self) |
|
|||
2057 | old = getattr(self, "magic_" + magicname, None) |
|
|||
2058 | setattr(self, "magic_" + magicname, im) |
|
|||
2059 | return old |
|
|||
2060 |
|
2129 | |||
2061 | #------------------------------------------------------------------------- |
|
2130 | #------------------------------------------------------------------------- | |
2062 | # Things related to macros |
|
2131 | # Things related to macros | |
@@ -2426,6 +2495,13 b' class InteractiveShell(SingletonConfigurable, Magic):' | |||||
2426 | self.showtraceback() |
|
2495 | self.showtraceback() | |
2427 | warn('Unknown failure executing module: <%s>' % mod_name) |
|
2496 | warn('Unknown failure executing module: <%s>' % mod_name) | |
2428 |
|
2497 | |||
|
2498 | def _run_cached_cell_magic(self, magic_name, line): | |||
|
2499 | """Special method to call a cell magic with the data stored in self. | |||
|
2500 | """ | |||
|
2501 | cell = self._current_cell_magic_body | |||
|
2502 | self._current_cell_magic_body = None | |||
|
2503 | return self.run_cell_magic(magic_name, line, cell) | |||
|
2504 | ||||
2429 | def run_cell(self, raw_cell, store_history=False, silent=False): |
|
2505 | def run_cell(self, raw_cell, store_history=False, silent=False): | |
2430 | """Run a complete IPython cell. |
|
2506 | """Run a complete IPython cell. | |
2431 |
|
2507 | |||
@@ -2447,8 +2523,15 b' class InteractiveShell(SingletonConfigurable, Magic):' | |||||
2447 | if silent: |
|
2523 | if silent: | |
2448 | store_history = False |
|
2524 | store_history = False | |
2449 |
|
2525 | |||
2450 | for line in raw_cell.splitlines(): |
|
2526 | self.input_splitter.push(raw_cell) | |
2451 | self.input_splitter.push(line) |
|
2527 | ||
|
2528 | # Check for cell magics, which leave state behind. This interface is | |||
|
2529 | # ugly, we need to do something cleaner later... Now the logic is | |||
|
2530 | # simply that the input_splitter remembers if there was a cell magic, | |||
|
2531 | # and in that case we grab the cell body. | |||
|
2532 | if self.input_splitter.cell_magic_parts: | |||
|
2533 | self._current_cell_magic_body = \ | |||
|
2534 | ''.join(self.input_splitter.cell_magic_parts) | |||
2452 | cell = self.input_splitter.source_reset() |
|
2535 | cell = self.input_splitter.source_reset() | |
2453 |
|
2536 | |||
2454 | with self.builtin_trap: |
|
2537 | with self.builtin_trap: | |
@@ -2479,7 +2562,8 b' class InteractiveShell(SingletonConfigurable, Magic):' | |||||
2479 |
|
2562 | |||
2480 | with self.display_trap: |
|
2563 | with self.display_trap: | |
2481 | try: |
|
2564 | try: | |
2482 |
code_ast = self.compile.ast_parse(cell, |
|
2565 | code_ast = self.compile.ast_parse(cell, | |
|
2566 | filename=cell_name) | |||
2483 | except IndentationError: |
|
2567 | except IndentationError: | |
2484 | self.showindentationerror() |
|
2568 | self.showindentationerror() | |
2485 | if store_history: |
|
2569 | if store_history: | |
@@ -2669,7 +2753,7 b' class InteractiveShell(SingletonConfigurable, Magic):' | |||||
2669 | make sense in all contexts, for example a terminal ipython can't |
|
2753 | make sense in all contexts, for example a terminal ipython can't | |
2670 | display figures inline. |
|
2754 | display figures inline. | |
2671 | """ |
|
2755 | """ | |
2672 |
|
2756 | from IPython.core.pylabtools import mpl_runner | ||
2673 | # We want to prevent the loading of pylab to pollute the user's |
|
2757 | # We want to prevent the loading of pylab to pollute the user's | |
2674 | # namespace as shown by the %who* magics, so we execute the activation |
|
2758 | # namespace as shown by the %who* magics, so we execute the activation | |
2675 | # code in an empty namespace, and we update *both* user_ns and |
|
2759 | # code in an empty namespace, and we update *both* user_ns and | |
@@ -2685,7 +2769,8 b' class InteractiveShell(SingletonConfigurable, Magic):' | |||||
2685 | # Now we must activate the gui pylab wants to use, and fix %run to take |
|
2769 | # Now we must activate the gui pylab wants to use, and fix %run to take | |
2686 | # plot updates into account |
|
2770 | # plot updates into account | |
2687 | self.enable_gui(gui) |
|
2771 | self.enable_gui(gui) | |
2688 | self.magic_run = self._pylab_magic_run |
|
2772 | self.magics_manager.registry['ExecutionMagics'].default_runner = \ | |
|
2773 | mpl_runner(self.safe_execfile) | |||
2689 |
|
2774 | |||
2690 | #------------------------------------------------------------------------- |
|
2775 | #------------------------------------------------------------------------- | |
2691 | # Utilities |
|
2776 | # Utilities | |
@@ -2749,6 +2834,29 b' class InteractiveShell(SingletonConfigurable, Magic):' | |||||
2749 | """Show a usage message""" |
|
2834 | """Show a usage message""" | |
2750 | page.page(IPython.core.usage.interactive_usage) |
|
2835 | page.page(IPython.core.usage.interactive_usage) | |
2751 |
|
2836 | |||
|
2837 | def extract_input_lines(self, range_str, raw=False): | |||
|
2838 | """Return as a string a set of input history slices. | |||
|
2839 | ||||
|
2840 | Parameters | |||
|
2841 | ---------- | |||
|
2842 | range_str : string | |||
|
2843 | The set of slices is given as a string, like "~5/6-~4/2 4:8 9", | |||
|
2844 | since this function is for use by magic functions which get their | |||
|
2845 | arguments as strings. The number before the / is the session | |||
|
2846 | number: ~n goes n back from the current session. | |||
|
2847 | ||||
|
2848 | Optional Parameters: | |||
|
2849 | - raw(False): by default, the processed input is used. If this is | |||
|
2850 | true, the raw input history is used instead. | |||
|
2851 | ||||
|
2852 | Note that slices can be called with two notations: | |||
|
2853 | ||||
|
2854 | N:M -> standard python form, means including items N...(M-1). | |||
|
2855 | ||||
|
2856 | N-M -> include items N..M (closed endpoint).""" | |||
|
2857 | lines = self.history_manager.get_range_by_str(range_str, raw=raw) | |||
|
2858 | return "\n".join(x for _, _, x in lines) | |||
|
2859 | ||||
2752 | def find_user_code(self, target, raw=True, py_only=False): |
|
2860 | def find_user_code(self, target, raw=True, py_only=False): | |
2753 | """Get a code string from history, file, url, or a string or macro. |
|
2861 | """Get a code string from history, file, url, or a string or macro. | |
2754 |
|
2862 |
This diff has been collapsed as it changes many lines, (4087 lines changed) Show them Hide them | |||||
@@ -4,8 +4,8 b'' | |||||
4 |
|
4 | |||
5 | #----------------------------------------------------------------------------- |
|
5 | #----------------------------------------------------------------------------- | |
6 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and |
|
6 | # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and | |
7 |
# Copyright (C) 2001 |
|
7 | # Copyright (C) 2001 Fernando Perez <fperez@colorado.edu> | |
8 |
# Copyright (C) 2008 |
|
8 | # Copyright (C) 2008 The IPython Development Team | |
9 |
|
9 | |||
10 | # Distributed under the terms of the BSD License. The full license is in |
|
10 | # Distributed under the terms of the BSD License. The full license is in | |
11 | # the file COPYING, distributed as part of this software. |
|
11 | # the file COPYING, distributed as part of this software. | |
@@ -14,67 +14,58 b'' | |||||
14 | #----------------------------------------------------------------------------- |
|
14 | #----------------------------------------------------------------------------- | |
15 | # Imports |
|
15 | # Imports | |
16 | #----------------------------------------------------------------------------- |
|
16 | #----------------------------------------------------------------------------- | |
17 |
|
17 | # Stdlib | ||
18 | import __builtin__ as builtin_mod |
|
|||
19 | import __future__ |
|
|||
20 | import bdb |
|
|||
21 | import inspect |
|
|||
22 | import io |
|
|||
23 | import json |
|
|||
24 | import os |
|
18 | import os | |
25 | import sys |
|
|||
26 | import re |
|
19 | import re | |
27 |
import |
|
20 | import sys | |
28 |
import |
|
21 | import types | |
29 | from StringIO import StringIO |
|
22 | from getopt import getopt, GetoptError | |
30 | from getopt import getopt,GetoptError |
|
23 | ||
31 | from pprint import pformat |
|
24 | # Our own | |
32 | from urllib2 import urlopen |
|
25 | from IPython.config.configurable import Configurable | |
33 |
|
26 | from IPython.core import oinspect | ||
34 | # cProfile was added in Python2.5 |
|
|||
35 | try: |
|
|||
36 | import cProfile as profile |
|
|||
37 | import pstats |
|
|||
38 | except ImportError: |
|
|||
39 | # profile isn't bundled by default in Debian for license reasons |
|
|||
40 | try: |
|
|||
41 | import profile,pstats |
|
|||
42 | except ImportError: |
|
|||
43 | profile = pstats = None |
|
|||
44 |
|
||||
45 | from IPython.core import debugger, oinspect |
|
|||
46 | from IPython.core.error import TryNext |
|
|||
47 | from IPython.core.error import UsageError |
|
27 | from IPython.core.error import UsageError | |
48 | from IPython.core.error import StdinNotImplementedError |
|
|||
49 | from IPython.core.macro import Macro |
|
|||
50 | from IPython.core import magic_arguments, page |
|
|||
51 | from IPython.core.prefilter import ESC_MAGIC |
|
28 | from IPython.core.prefilter import ESC_MAGIC | |
52 |
from IPython. |
|
29 | from IPython.external.decorator import decorator | |
53 | from IPython.testing.skipdoctest import skip_doctest |
|
|||
54 | from IPython.utils import py3compat |
|
|||
55 | from IPython.utils.encoding import DEFAULT_ENCODING |
|
|||
56 | from IPython.utils.io import file_read, nlprint |
|
|||
57 | from IPython.utils.module_paths import find_mod |
|
|||
58 | from IPython.utils.path import get_py_filename, unquote_filename |
|
|||
59 | from IPython.utils.process import arg_split, abbrev_cwd |
|
|||
60 | from IPython.utils.terminal import set_term_title |
|
|||
61 | from IPython.utils.text import format_screen |
|
|||
62 | from IPython.utils.timing import clock, clock2 |
|
|||
63 | from IPython.utils.warn import warn, error |
|
|||
64 | from IPython.utils.ipstruct import Struct |
|
30 | from IPython.utils.ipstruct import Struct | |
65 |
from IPython. |
|
31 | from IPython.utils.process import arg_split | |
|
32 | from IPython.utils.text import dedent | |||
|
33 | from IPython.utils.traitlets import Bool, Dict, Instance | |||
|
34 | from IPython.utils.warn import error, warn | |||
|
35 | ||||
|
36 | #----------------------------------------------------------------------------- | |||
|
37 | # Globals | |||
|
38 | #----------------------------------------------------------------------------- | |||
|
39 | ||||
|
40 | # A dict we'll use for each class that has magics, used as temporary storage to | |||
|
41 | # pass information between the @line/cell_magic method decorators and the | |||
|
42 | # @magics_class class decorator, because the method decorators have no | |||
|
43 | # access to the class when they run. See for more details: | |||
|
44 | # http://stackoverflow.com/questions/2366713/can-a-python-decorator-of-an-instance-method-access-the-class | |||
|
45 | ||||
|
46 | magics = dict(line={}, cell={}) | |||
|
47 | ||||
|
48 | magic_kinds = ('line', 'cell') | |||
|
49 | magic_spec = ('line', 'cell', 'line_cell') | |||
66 |
|
50 | |||
67 | #----------------------------------------------------------------------------- |
|
51 | #----------------------------------------------------------------------------- | |
68 | # Utility functions |
|
52 | # Utility classes and functions | |
69 | #----------------------------------------------------------------------------- |
|
53 | #----------------------------------------------------------------------------- | |
70 |
|
54 | |||
|
55 | class Bunch: pass | |||
|
56 | ||||
|
57 | ||||
71 | def on_off(tag): |
|
58 | def on_off(tag): | |
72 | """Return an ON/OFF string for a 1/0 input. Simple utility function.""" |
|
59 | """Return an ON/OFF string for a 1/0 input. Simple utility function.""" | |
73 | return ['OFF','ON'][tag] |
|
60 | return ['OFF','ON'][tag] | |
74 |
|
61 | |||
75 | class Bunch: pass |
|
|||
76 |
|
62 | |||
77 | def compress_dhist(dh): |
|
63 | def compress_dhist(dh): | |
|
64 | """Compress a directory history into a new one with at most 20 entries. | |||
|
65 | ||||
|
66 | Return a new list made from the first and last 10 elements of dhist after | |||
|
67 | removal of duplicates. | |||
|
68 | """ | |||
78 | head, tail = dh[:-10], dh[-10:] |
|
69 | head, tail = dh[:-10], dh[-10:] | |
79 |
|
70 | |||
80 | newhead = [] |
|
71 | newhead = [] | |
@@ -87,127 +78,400 b' def compress_dhist(dh):' | |||||
87 |
|
78 | |||
88 | return newhead + tail |
|
79 | return newhead + tail | |
89 |
|
80 | |||
|
81 | ||||
90 | def needs_local_scope(func): |
|
82 | def needs_local_scope(func): | |
91 | """Decorator to mark magic functions which need to local scope to run.""" |
|
83 | """Decorator to mark magic functions which need to local scope to run.""" | |
92 | func.needs_local_scope = True |
|
84 | func.needs_local_scope = True | |
93 | return func |
|
85 | return func | |
94 |
|
86 | |||
|
87 | #----------------------------------------------------------------------------- | |||
|
88 | # Class and method decorators for registering magics | |||
|
89 | #----------------------------------------------------------------------------- | |||
|
90 | ||||
|
91 | def magics_class(cls): | |||
|
92 | """Class decorator for all subclasses of the main Magics class. | |||
|
93 | ||||
|
94 | Any class that subclasses Magics *must* also apply this decorator, to | |||
|
95 | ensure that all the methods that have been decorated as line/cell magics | |||
|
96 | get correctly registered in the class instance. This is necessary because | |||
|
97 | when method decorators run, the class does not exist yet, so they | |||
|
98 | temporarily store their information into a module global. Application of | |||
|
99 | this class decorator copies that global data to the class instance and | |||
|
100 | clears the global. | |||
|
101 | ||||
|
102 | Obviously, this mechanism is not thread-safe, which means that the | |||
|
103 | *creation* of subclasses of Magic should only be done in a single-thread | |||
|
104 | context. Instantiation of the classes has no restrictions. Given that | |||
|
105 | these classes are typically created at IPython startup time and before user | |||
|
106 | application code becomes active, in practice this should not pose any | |||
|
107 | problems. | |||
|
108 | """ | |||
|
109 | cls.registered = True | |||
|
110 | cls.magics = dict(line = magics['line'], | |||
|
111 | cell = magics['cell']) | |||
|
112 | magics['line'] = {} | |||
|
113 | magics['cell'] = {} | |||
|
114 | return cls | |||
|
115 | ||||
|
116 | ||||
|
117 | def record_magic(dct, magic_kind, magic_name, func): | |||
|
118 | """Utility function to store a function as a magic of a specific kind. | |||
|
119 | ||||
|
120 | Parameters | |||
|
121 | ---------- | |||
|
122 | dct : dict | |||
|
123 | A dictionary with 'line' and 'cell' subdicts. | |||
|
124 | ||||
|
125 | magic_kind : str | |||
|
126 | Kind of magic to be stored. | |||
|
127 | ||||
|
128 | magic_name : str | |||
|
129 | Key to store the magic as. | |||
|
130 | ||||
|
131 | func : function | |||
|
132 | Callable object to store. | |||
|
133 | """ | |||
|
134 | if magic_kind == 'line_cell': | |||
|
135 | dct['line'][magic_name] = dct['cell'][magic_name] = func | |||
|
136 | else: | |||
|
137 | dct[magic_kind][magic_name] = func | |||
|
138 | ||||
|
139 | ||||
|
140 | def validate_type(magic_kind): | |||
|
141 | """Ensure that the given magic_kind is valid. | |||
|
142 | ||||
|
143 | Check that the given magic_kind is one of the accepted spec types (stored | |||
|
144 | in the global `magic_spec`), raise ValueError otherwise. | |||
|
145 | """ | |||
|
146 | if magic_kind not in magic_spec: | |||
|
147 | raise ValueError('magic_kind must be one of %s, %s given' % | |||
|
148 | magic_kinds, magic_kind) | |||
|
149 | ||||
|
150 | ||||
|
151 | # The docstrings for the decorator below will be fairly similar for the two | |||
|
152 | # types (method and function), so we generate them here once and reuse the | |||
|
153 | # templates below. | |||
|
154 | _docstring_template = \ | |||
|
155 | """Decorate the given {0} as {1} magic. | |||
|
156 | ||||
|
157 | The decorator can be used with or without arguments, as follows. | |||
|
158 | ||||
|
159 | i) without arguments: it will create a {1} magic named as the {0} being | |||
|
160 | decorated:: | |||
|
161 | ||||
|
162 | @deco | |||
|
163 | def foo(...) | |||
|
164 | ||||
|
165 | will create a {1} magic named `foo`. | |||
|
166 | ||||
|
167 | ii) with one string argument: which will be used as the actual name of the | |||
|
168 | resulting magic:: | |||
|
169 | ||||
|
170 | @deco('bar') | |||
|
171 | def foo(...) | |||
|
172 | ||||
|
173 | will create a {1} magic named `bar`. | |||
|
174 | """ | |||
|
175 | ||||
|
176 | # These two are decorator factories. While they are conceptually very similar, | |||
|
177 | # there are enough differences in the details that it's simpler to have them | |||
|
178 | # written as completely standalone functions rather than trying to share code | |||
|
179 | # and make a single one with convoluted logic. | |||
|
180 | ||||
|
181 | def _method_magic_marker(magic_kind): | |||
|
182 | """Decorator factory for methods in Magics subclasses. | |||
|
183 | """ | |||
|
184 | ||||
|
185 | validate_type(magic_kind) | |||
|
186 | ||||
|
187 | # This is a closure to capture the magic_kind. We could also use a class, | |||
|
188 | # but it's overkill for just that one bit of state. | |||
|
189 | def magic_deco(arg): | |||
|
190 | call = lambda f, *a, **k: f(*a, **k) | |||
|
191 | ||||
|
192 | if callable(arg): | |||
|
193 | # "Naked" decorator call (just @foo, no args) | |||
|
194 | func = arg | |||
|
195 | name = func.func_name | |||
|
196 | retval = decorator(call, func) | |||
|
197 | record_magic(magics, magic_kind, name, name) | |||
|
198 | elif isinstance(arg, basestring): | |||
|
199 | # Decorator called with arguments (@foo('bar')) | |||
|
200 | name = arg | |||
|
201 | def mark(func, *a, **kw): | |||
|
202 | record_magic(magics, magic_kind, name, func.func_name) | |||
|
203 | return decorator(call, func) | |||
|
204 | retval = mark | |||
|
205 | else: | |||
|
206 | raise ValueError("Decorator can only be called with " | |||
|
207 | "string or function") | |||
|
208 | return retval | |||
|
209 | ||||
|
210 | # Ensure the resulting decorator has a usable docstring | |||
|
211 | magic_deco.__doc__ = _docstring_template.format('method', magic_kind) | |||
|
212 | return magic_deco | |||
|
213 | ||||
|
214 | ||||
|
215 | def _function_magic_marker(magic_kind): | |||
|
216 | """Decorator factory for standalone functions. | |||
|
217 | """ | |||
|
218 | validate_type(magic_kind) | |||
|
219 | ||||
|
220 | # This is a closure to capture the magic_kind. We could also use a class, | |||
|
221 | # but it's overkill for just that one bit of state. | |||
|
222 | def magic_deco(arg): | |||
|
223 | call = lambda f, *a, **k: f(*a, **k) | |||
|
224 | ||||
|
225 | # Find get_ipython() in the caller's namespace | |||
|
226 | caller = sys._getframe(1) | |||
|
227 | for ns in ['f_locals', 'f_globals', 'f_builtins']: | |||
|
228 | get_ipython = getattr(caller, ns).get('get_ipython') | |||
|
229 | if get_ipython is not None: | |||
|
230 | break | |||
|
231 | else: | |||
|
232 | raise('Decorator can only run in context where `get_ipython` exists') | |||
|
233 | ||||
|
234 | ip = get_ipython() | |||
|
235 | ||||
|
236 | if callable(arg): | |||
|
237 | # "Naked" decorator call (just @foo, no args) | |||
|
238 | func = arg | |||
|
239 | name = func.func_name | |||
|
240 | ip.register_magic_function(func, magic_kind, name) | |||
|
241 | retval = decorator(call, func) | |||
|
242 | elif isinstance(arg, basestring): | |||
|
243 | # Decorator called with arguments (@foo('bar')) | |||
|
244 | name = arg | |||
|
245 | def mark(func, *a, **kw): | |||
|
246 | ip.register_magic_function(func, magic_kind, name) | |||
|
247 | return decorator(call, func) | |||
|
248 | retval = mark | |||
|
249 | else: | |||
|
250 | raise ValueError("Decorator can only be called with " | |||
|
251 | "string or function") | |||
|
252 | return retval | |||
|
253 | ||||
|
254 | # Ensure the resulting decorator has a usable docstring | |||
|
255 | ds = _docstring_template.format('function', magic_kind) | |||
|
256 | ||||
|
257 | ds += dedent(""" | |||
|
258 | Note: this decorator can only be used in a context where IPython is already | |||
|
259 | active, so that the `get_ipython()` call succeeds. You can therefore use | |||
|
260 | it in your startup files loaded after IPython initializes, but *not* in the | |||
|
261 | IPython configuration file itself, which is executed before IPython is | |||
|
262 | fully up and running. Any file located in the `startup` subdirectory of | |||
|
263 | your configuration profile will be OK in this sense. | |||
|
264 | """) | |||
95 |
|
265 | |||
96 | # Used for exception handling in magic_edit |
|
266 | magic_deco.__doc__ = ds | |
97 | class MacroToEdit(ValueError): pass |
|
267 | return magic_deco | |
98 |
|
268 | |||
99 | #*************************************************************************** |
|
|||
100 | # Main class implementing Magic functionality |
|
|||
101 |
|
269 | |||
102 | # XXX - for some odd reason, if Magic is made a new-style class, we get errors |
|
270 | # Create the actual decorators for public use | |
103 | # on construction of the main InteractiveShell object. Something odd is going |
|
|||
104 | # on with super() calls, Configurable and the MRO... For now leave it as-is, but |
|
|||
105 | # eventually this needs to be clarified. |
|
|||
106 | # BG: This is because InteractiveShell inherits from this, but is itself a |
|
|||
107 | # Configurable. This messes up the MRO in some way. The fix is that we need to |
|
|||
108 | # make Magic a configurable that InteractiveShell does not subclass. |
|
|||
109 |
|
271 | |||
110 | class Magic: |
|
272 | # These three are used to decorate methods in class definitions | |
111 | """Magic functions for InteractiveShell. |
|
273 | line_magic = _method_magic_marker('line') | |
|
274 | cell_magic = _method_magic_marker('cell') | |||
|
275 | line_cell_magic = _method_magic_marker('line_cell') | |||
112 |
|
276 | |||
113 | Shell functions which can be reached as %function_name. All magic |
|
277 | # These three decorate standalone functions and perform the decoration | |
114 | functions should accept a string, which they can parse for their own |
|
278 | # immediately. They can only run where get_ipython() works | |
115 | needs. This can make some functions easier to type, eg `%cd ../` |
|
279 | register_line_magic = _function_magic_marker('line') | |
116 | vs. `%cd("../")` |
|
280 | register_cell_magic = _function_magic_marker('cell') | |
|
281 | register_line_cell_magic = _function_magic_marker('line_cell') | |||
117 |
|
282 | |||
118 | ALL definitions MUST begin with the prefix magic_. The user won't need it |
|
283 | #----------------------------------------------------------------------------- | |
119 | at the command line, but it is is needed in the definition. """ |
|
284 | # Core Magic classes | |
|
285 | #----------------------------------------------------------------------------- | |||
120 |
|
286 | |||
121 | # class globals |
|
287 | class MagicsManager(Configurable): | |
122 | auto_status = ['Automagic is OFF, % prefix IS needed for magic functions.', |
|
288 | """Object that handles all magic-related functionality for IPython. | |
123 | 'Automagic is ON, % prefix NOT needed for magic functions.'] |
|
289 | """ | |
|
290 | # Non-configurable class attributes | |||
124 |
|
291 | |||
|
292 | # A two-level dict, first keyed by magic type, then by magic function, and | |||
|
293 | # holding the actual callable object as value. This is the dict used for | |||
|
294 | # magic function dispatch | |||
|
295 | magics = Dict | |||
125 |
|
296 | |||
126 | configurables = None |
|
297 | # A registry of the original objects that we've been given holding magics. | |
127 | #...................................................................... |
|
298 | registry = Dict | |
128 | # some utility functions |
|
|||
129 |
|
299 | |||
130 | def __init__(self,shell): |
|
300 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC') | |
131 |
|
301 | |||
132 | self.options_table = {} |
|
302 | auto_magic = Bool(True, config=True, help= | |
133 | if profile is None: |
|
303 | "Automatically call line magics without requiring explicit % prefix") | |
134 | self.magic_prun = self.profile_missing_notice |
|
304 | ||
135 | self.shell = shell |
|
305 | _auto_status = [ | |
136 | if self.configurables is None: |
|
306 | 'Automagic is OFF, % prefix IS needed for line magics.', | |
137 | self.configurables = [] |
|
307 | 'Automagic is ON, % prefix IS NOT needed for line magics.'] | |
138 |
|
308 | |||
139 | # namespace for holding state we may need |
|
309 | user_magics = Instance('IPython.core.magics.UserMagics') | |
140 | self._magic_state = Bunch() |
|
|||
141 |
|
310 | |||
142 | def profile_missing_notice(self, *args, **kwargs): |
|
311 | def __init__(self, shell=None, config=None, user_magics=None, **traits): | |
143 | error("""\ |
|
|||
144 | The profile module could not be found. It has been removed from the standard |
|
|||
145 | python packages because of its non-free license. To use profiling, install the |
|
|||
146 | python-profiler package from non-free.""") |
|
|||
147 |
|
312 | |||
148 | def default_option(self,fn,optstr): |
|
313 | super(MagicsManager, self).__init__(shell=shell, config=config, | |
149 | """Make an entry in the options_table for fn, with value optstr""" |
|
314 | user_magics=user_magics, **traits) | |
|
315 | self.magics = dict(line={}, cell={}) | |||
|
316 | # Let's add the user_magics to the registry for uniformity, so *all* | |||
|
317 | # registered magic containers can be found there. | |||
|
318 | self.registry[user_magics.__class__.__name__] = user_magics | |||
150 |
|
319 | |||
151 | if fn not in self.lsmagic(): |
|
320 | def auto_status(self): | |
152 | error("%s is not a magic function" % fn) |
|
321 | """Return descriptive string with automagic status.""" | |
153 | self.options_table[fn] = optstr |
|
322 | return self._auto_status[self.auto_magic] | |
154 |
|
323 | |||
155 | def lsmagic(self): |
|
324 | def lsmagic(self): | |
156 |
"""Return a |
|
325 | """Return a dict of currently available magic functions. | |
157 |
|
326 | |||
158 | Gives a list of the bare names after mangling (['ls','cd', ...], not |
|
327 | The return dict has the keys 'line' and 'cell', corresponding to the | |
159 | ['magic_ls','magic_cd',...]""" |
|
328 | two types of magics we support. Each value is a list of names. | |
160 |
|
329 | """ | ||
161 | # FIXME. This needs a cleanup, in the way the magics list is built. |
|
330 | return self.magics | |
162 |
|
331 | |||
163 | # magics in class definition |
|
332 | def register(self, *magic_objects): | |
164 | class_magic = lambda fn: fn.startswith('magic_') and \ |
|
333 | """Register one or more instances of Magics. | |
165 | callable(Magic.__dict__[fn]) |
|
334 | ||
166 | # in instance namespace (run-time user additions) |
|
335 | Take one or more classes or instances of classes that subclass the main | |
167 | inst_magic = lambda fn: fn.startswith('magic_') and \ |
|
336 | `core.Magic` class, and register them with IPython to use the magic | |
168 | callable(self.__dict__[fn]) |
|
337 | functions they provide. The registration process will then ensure that | |
169 | # and bound magics by user (so they can access self): |
|
338 | any methods that have decorated to provide line and/or cell magics will | |
170 | inst_bound_magic = lambda fn: fn.startswith('magic_') and \ |
|
339 | be recognized with the `%x`/`%%x` syntax as a line/cell magic | |
171 | callable(self.__class__.__dict__[fn]) |
|
340 | respectively. | |
172 | magics = filter(class_magic,Magic.__dict__.keys()) + \ |
|
341 | ||
173 | filter(inst_magic,self.__dict__.keys()) + \ |
|
342 | If classes are given, they will be instantiated with the default | |
174 | filter(inst_bound_magic,self.__class__.__dict__.keys()) |
|
343 | constructor. If your classes need a custom constructor, you should | |
175 | out = [] |
|
344 | instanitate them first and pass the instance. | |
176 | for fn in set(magics): |
|
345 | ||
177 | out.append(fn.replace('magic_','',1)) |
|
346 | The provided arguments can be an arbitrary mix of classes and instances. | |
178 | out.sort() |
|
347 | ||
179 |
|
|
348 | Parameters | |
180 |
|
349 | ---------- | ||
181 | def extract_input_lines(self, range_str, raw=False): |
|
350 | magic_objects : one or more classes or instances | |
182 | """Return as a string a set of input history slices. |
|
351 | """ | |
|
352 | # Start by validating them to ensure they have all had their magic | |||
|
353 | # methods registered at the instance level | |||
|
354 | for m in magic_objects: | |||
|
355 | if not m.registered: | |||
|
356 | raise ValueError("Class of magics %r was constructed without " | |||
|
357 | "the @register_macics class decorator") | |||
|
358 | if type(m) is type: | |||
|
359 | # If we're given an uninstantiated class | |||
|
360 | m = m(self.shell) | |||
|
361 | ||||
|
362 | # Now that we have an instance, we can register it and update the | |||
|
363 | # table of callables | |||
|
364 | self.registry[m.__class__.__name__] = m | |||
|
365 | for mtype in magic_kinds: | |||
|
366 | self.magics[mtype].update(m.magics[mtype]) | |||
|
367 | ||||
|
368 | def register_function(self, func, magic_kind='line', magic_name=None): | |||
|
369 | """Expose a standalone function as magic function for IPython. | |||
|
370 | ||||
|
371 | This will create an IPython magic (line, cell or both) from a | |||
|
372 | standalone function. The functions should have the following | |||
|
373 | signatures: | |||
|
374 | ||||
|
375 | * For line magics: `def f(line)` | |||
|
376 | * For cell magics: `def f(line, cell)` | |||
|
377 | * For a function that does both: `def f(line, cell=None)` | |||
|
378 | ||||
|
379 | In the latter case, the function will be called with `cell==None` when | |||
|
380 | invoked as `%f`, and with cell as a string when invoked as `%%f`. | |||
183 |
|
381 | |||
184 | Parameters |
|
382 | Parameters | |
185 | ---------- |
|
383 | ---------- | |
186 | range_str : string |
|
384 | func : callable | |
187 | The set of slices is given as a string, like "~5/6-~4/2 4:8 9", |
|
385 | Function to be registered as a magic. | |
188 | since this function is for use by magic functions which get their |
|
|||
189 | arguments as strings. The number before the / is the session |
|
|||
190 | number: ~n goes n back from the current session. |
|
|||
191 |
|
386 | |||
192 | Optional Parameters: |
|
387 | magic_kind : str | |
193 | - raw(False): by default, the processed input is used. If this is |
|
388 | Kind of magic, one of 'line', 'cell' or 'line_cell' | |
194 | true, the raw input history is used instead. |
|
|||
195 |
|
389 | |||
196 | Note that slices can be called with two notations: |
|
390 | magic_name : optional str | |
|
391 | If given, the name the magic will have in the IPython namespace. By | |||
|
392 | default, the name of the function itself is used. | |||
|
393 | """ | |||
|
394 | ||||
|
395 | # Create the new method in the user_magics and register it in the | |||
|
396 | # global table | |||
|
397 | validate_type(magic_kind) | |||
|
398 | magic_name = func.func_name if magic_name is None else magic_name | |||
|
399 | setattr(self.user_magics, magic_name, func) | |||
|
400 | record_magic(self.magics, magic_kind, magic_name, func) | |||
|
401 | ||||
|
402 | def define_magic(self, name, func): | |||
|
403 | """[Deprecated] Expose own function as magic function for IPython. | |||
|
404 | ||||
|
405 | Example:: | |||
|
406 | ||||
|
407 | def foo_impl(self, parameter_s=''): | |||
|
408 | 'My very own magic!. (Use docstrings, IPython reads them).' | |||
|
409 | print 'Magic function. Passed parameter is between < >:' | |||
|
410 | print '<%s>' % parameter_s | |||
|
411 | print 'The self object is:', self | |||
|
412 | ||||
|
413 | ip.define_magic('foo',foo_impl) | |||
|
414 | """ | |||
|
415 | meth = types.MethodType(func, self.user_magics) | |||
|
416 | setattr(self.user_magics, name, meth) | |||
|
417 | record_magic(self.magics, 'line', name, meth) | |||
|
418 | ||||
|
419 | # Key base class that provides the central functionality for magics. | |||
197 |
|
420 | |||
198 | N:M -> standard python form, means including items N...(M-1). |
|
421 | class Magics(object): | |
|
422 | """Base class for implementing magic functions. | |||
199 |
|
423 | |||
200 | N-M -> include items N..M (closed endpoint).""" |
|
424 | Shell functions which can be reached as %function_name. All magic | |
201 | lines = self.shell.history_manager.\ |
|
425 | functions should accept a string, which they can parse for their own | |
202 | get_range_by_str(range_str, raw=raw) |
|
426 | needs. This can make some functions easier to type, eg `%cd ../` | |
203 | return "\n".join(x for _, _, x in lines) |
|
427 | vs. `%cd("../")` | |
|
428 | ||||
|
429 | Classes providing magic functions need to subclass this class, and they | |||
|
430 | MUST: | |||
|
431 | ||||
|
432 | - Use the method decorators `@line_magic` and `@cell_magic` to decorate | |||
|
433 | individual methods as magic functions, AND | |||
|
434 | ||||
|
435 | - Use the class decorator `@magics_class` to ensure that the magic | |||
|
436 | methods are properly registered at the instance level upon instance | |||
|
437 | initialization. | |||
|
438 | ||||
|
439 | See :mod:`magic_functions` for examples of actual implementation classes. | |||
|
440 | """ | |||
|
441 | # Dict holding all command-line options for each magic. | |||
|
442 | options_table = None | |||
|
443 | # Dict for the mapping of magic names to methods, set by class decorator | |||
|
444 | magics = None | |||
|
445 | # Flag to check that the class decorator was properly applied | |||
|
446 | registered = False | |||
|
447 | # Instance of IPython shell | |||
|
448 | shell = None | |||
|
449 | ||||
|
450 | def __init__(self, shell): | |||
|
451 | if not(self.__class__.registered): | |||
|
452 | raise ValueError('Magics subclass without registration - ' | |||
|
453 | 'did you forget to apply @magics_class?') | |||
|
454 | self.shell = shell | |||
|
455 | self.options_table = {} | |||
|
456 | # The method decorators are run when the instance doesn't exist yet, so | |||
|
457 | # they can only record the names of the methods they are supposed to | |||
|
458 | # grab. Only now, that the instance exists, can we create the proper | |||
|
459 | # mapping to bound methods. So we read the info off the original names | |||
|
460 | # table and replace each method name by the actual bound method. | |||
|
461 | for mtype in magic_kinds: | |||
|
462 | tab = self.magics[mtype] | |||
|
463 | # must explicitly use keys, as we're mutating this puppy | |||
|
464 | for magic_name in tab.keys(): | |||
|
465 | meth_name = tab[magic_name] | |||
|
466 | if isinstance(meth_name, basestring): | |||
|
467 | tab[magic_name] = getattr(self, meth_name) | |||
204 |
|
468 | |||
205 | def arg_err(self,func): |
|
469 | def arg_err(self,func): | |
206 | """Print docstring if incorrect arguments were passed""" |
|
470 | """Print docstring if incorrect arguments were passed""" | |
207 | print 'Error in arguments:' |
|
471 | print 'Error in arguments:' | |
208 | print oinspect.getdoc(func) |
|
472 | print oinspect.getdoc(func) | |
209 |
|
473 | |||
210 | def format_latex(self,strng): |
|
474 | def format_latex(self, strng): | |
211 | """Format a string for latex inclusion.""" |
|
475 | """Format a string for latex inclusion.""" | |
212 |
|
476 | |||
213 | # Characters that need to be escaped for latex: |
|
477 | # Characters that need to be escaped for latex: | |
@@ -234,7 +498,7 b' python-profiler package from non-free.""")' | |||||
234 | strng = newline_re.sub(r'\\textbackslash{}n',strng) |
|
498 | strng = newline_re.sub(r'\\textbackslash{}n',strng) | |
235 | return strng |
|
499 | return strng | |
236 |
|
500 | |||
237 | def parse_options(self,arg_str,opt_str,*long_opts,**kw): |
|
501 | def parse_options(self, arg_str, opt_str, *long_opts, **kw): | |
238 | """Parse options passed to an argument string. |
|
502 | """Parse options passed to an argument string. | |
239 |
|
503 | |||
240 | The interface is similar to that of getopt(), but it returns back a |
|
504 | The interface is similar to that of getopt(), but it returns back a | |
@@ -257,7 +521,7 b' python-profiler package from non-free.""")' | |||||
257 | standard library.""" |
|
521 | standard library.""" | |
258 |
|
522 | |||
259 | # inject default options at the beginning of the input line |
|
523 | # inject default options at the beginning of the input line | |
260 |
caller = sys._getframe(1).f_code.co_name |
|
524 | caller = sys._getframe(1).f_code.co_name | |
261 | arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str) |
|
525 | arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str) | |
262 |
|
526 | |||
263 | mode = kw.get('mode','string') |
|
527 | mode = kw.get('mode','string') | |
@@ -303,3550 +567,9 b' python-profiler package from non-free.""")' | |||||
303 |
|
567 | |||
304 | return opts,args |
|
568 | return opts,args | |
305 |
|
569 | |||
306 | #...................................................................... |
|
570 | def default_option(self, fn, optstr): | |
307 | # And now the actual magic functions |
|
571 | """Make an entry in the options_table for fn, with value optstr""" | |
308 |
|
||||
309 | # Functions for IPython shell work (vars,funcs, config, etc) |
|
|||
310 | def magic_lsmagic(self, parameter_s = ''): |
|
|||
311 | """List currently available magic functions.""" |
|
|||
312 | mesc = ESC_MAGIC |
|
|||
313 | print 'Available magic functions:\n'+mesc+\ |
|
|||
314 | (' '+mesc).join(self.lsmagic()) |
|
|||
315 | print '\n' + Magic.auto_status[self.shell.automagic] |
|
|||
316 | return None |
|
|||
317 |
|
||||
318 | def magic_magic(self, parameter_s = ''): |
|
|||
319 | """Print information about the magic function system. |
|
|||
320 |
|
||||
321 | Supported formats: -latex, -brief, -rest |
|
|||
322 | """ |
|
|||
323 |
|
||||
324 | mode = '' |
|
|||
325 | try: |
|
|||
326 | if parameter_s.split()[0] == '-latex': |
|
|||
327 | mode = 'latex' |
|
|||
328 | if parameter_s.split()[0] == '-brief': |
|
|||
329 | mode = 'brief' |
|
|||
330 | if parameter_s.split()[0] == '-rest': |
|
|||
331 | mode = 'rest' |
|
|||
332 | rest_docs = [] |
|
|||
333 | except: |
|
|||
334 | pass |
|
|||
335 |
|
||||
336 | magic_docs = [] |
|
|||
337 | for fname in self.lsmagic(): |
|
|||
338 | mname = 'magic_' + fname |
|
|||
339 | for space in (Magic,self,self.__class__): |
|
|||
340 | try: |
|
|||
341 | fn = space.__dict__[mname] |
|
|||
342 | except KeyError: |
|
|||
343 | pass |
|
|||
344 | else: |
|
|||
345 | break |
|
|||
346 | if mode == 'brief': |
|
|||
347 | # only first line |
|
|||
348 | if fn.__doc__: |
|
|||
349 | fndoc = fn.__doc__.split('\n',1)[0] |
|
|||
350 | else: |
|
|||
351 | fndoc = 'No documentation' |
|
|||
352 | else: |
|
|||
353 | if fn.__doc__: |
|
|||
354 | fndoc = fn.__doc__.rstrip() |
|
|||
355 | else: |
|
|||
356 | fndoc = 'No documentation' |
|
|||
357 |
|
||||
358 |
|
||||
359 | if mode == 'rest': |
|
|||
360 | rest_docs.append('**%s%s**::\n\n\t%s\n\n' %(ESC_MAGIC, |
|
|||
361 | fname,fndoc)) |
|
|||
362 |
|
||||
363 | else: |
|
|||
364 | magic_docs.append('%s%s:\n\t%s\n' %(ESC_MAGIC, |
|
|||
365 | fname,fndoc)) |
|
|||
366 |
|
||||
367 | magic_docs = ''.join(magic_docs) |
|
|||
368 |
|
||||
369 | if mode == 'rest': |
|
|||
370 | return "".join(rest_docs) |
|
|||
371 |
|
||||
372 | if mode == 'latex': |
|
|||
373 | print self.format_latex(magic_docs) |
|
|||
374 | return |
|
|||
375 | else: |
|
|||
376 | magic_docs = format_screen(magic_docs) |
|
|||
377 | if mode == 'brief': |
|
|||
378 | return magic_docs |
|
|||
379 |
|
||||
380 | outmsg = """ |
|
|||
381 | IPython's 'magic' functions |
|
|||
382 | =========================== |
|
|||
383 |
|
||||
384 | The magic function system provides a series of functions which allow you to |
|
|||
385 | control the behavior of IPython itself, plus a lot of system-type |
|
|||
386 | features. All these functions are prefixed with a % character, but parameters |
|
|||
387 | are given without parentheses or quotes. |
|
|||
388 |
|
||||
389 | NOTE: If you have 'automagic' enabled (via the command line option or with the |
|
|||
390 | %automagic function), you don't need to type in the % explicitly. By default, |
|
|||
391 | IPython ships with automagic on, so you should only rarely need the % escape. |
|
|||
392 |
|
||||
393 | Example: typing '%cd mydir' (without the quotes) changes you working directory |
|
|||
394 | to 'mydir', if it exists. |
|
|||
395 |
|
||||
396 | For a list of the available magic functions, use %lsmagic. For a description |
|
|||
397 | of any of them, type %magic_name?, e.g. '%cd?'. |
|
|||
398 |
|
||||
399 | Currently the magic system has the following functions:\n""" |
|
|||
400 |
|
||||
401 | mesc = ESC_MAGIC |
|
|||
402 | outmsg = ("%s\n%s\n\nSummary of magic functions (from %slsmagic):" |
|
|||
403 | "\n\n%s%s\n\n%s" % (outmsg, |
|
|||
404 | magic_docs,mesc,mesc, |
|
|||
405 | (' '+mesc).join(self.lsmagic()), |
|
|||
406 | Magic.auto_status[self.shell.automagic] ) ) |
|
|||
407 | page.page(outmsg) |
|
|||
408 |
|
||||
409 | def magic_automagic(self, parameter_s = ''): |
|
|||
410 | """Make magic functions callable without having to type the initial %. |
|
|||
411 |
|
||||
412 | Without argumentsl toggles on/off (when off, you must call it as |
|
|||
413 | %automagic, of course). With arguments it sets the value, and you can |
|
|||
414 | use any of (case insensitive): |
|
|||
415 |
|
||||
416 | - on,1,True: to activate |
|
|||
417 |
|
||||
418 | - off,0,False: to deactivate. |
|
|||
419 |
|
||||
420 | Note that magic functions have lowest priority, so if there's a |
|
|||
421 | variable whose name collides with that of a magic fn, automagic won't |
|
|||
422 | work for that function (you get the variable instead). However, if you |
|
|||
423 | delete the variable (del var), the previously shadowed magic function |
|
|||
424 | becomes visible to automagic again.""" |
|
|||
425 |
|
||||
426 | arg = parameter_s.lower() |
|
|||
427 | if parameter_s in ('on','1','true'): |
|
|||
428 | self.shell.automagic = True |
|
|||
429 | elif parameter_s in ('off','0','false'): |
|
|||
430 | self.shell.automagic = False |
|
|||
431 | else: |
|
|||
432 | self.shell.automagic = not self.shell.automagic |
|
|||
433 | print '\n' + Magic.auto_status[self.shell.automagic] |
|
|||
434 |
|
||||
435 | @skip_doctest |
|
|||
436 | def magic_autocall(self, parameter_s = ''): |
|
|||
437 | """Make functions callable without having to type parentheses. |
|
|||
438 |
|
||||
439 | Usage: |
|
|||
440 |
|
||||
441 | %autocall [mode] |
|
|||
442 |
|
||||
443 | The mode can be one of: 0->Off, 1->Smart, 2->Full. If not given, the |
|
|||
444 | value is toggled on and off (remembering the previous state). |
|
|||
445 |
|
||||
446 | In more detail, these values mean: |
|
|||
447 |
|
||||
448 | 0 -> fully disabled |
|
|||
449 |
|
||||
450 | 1 -> active, but do not apply if there are no arguments on the line. |
|
|||
451 |
|
||||
452 | In this mode, you get:: |
|
|||
453 |
|
||||
454 | In [1]: callable |
|
|||
455 | Out[1]: <built-in function callable> |
|
|||
456 |
|
||||
457 | In [2]: callable 'hello' |
|
|||
458 | ------> callable('hello') |
|
|||
459 | Out[2]: False |
|
|||
460 |
|
||||
461 | 2 -> Active always. Even if no arguments are present, the callable |
|
|||
462 | object is called:: |
|
|||
463 |
|
||||
464 | In [2]: float |
|
|||
465 | ------> float() |
|
|||
466 | Out[2]: 0.0 |
|
|||
467 |
|
||||
468 | Note that even with autocall off, you can still use '/' at the start of |
|
|||
469 | a line to treat the first argument on the command line as a function |
|
|||
470 | and add parentheses to it:: |
|
|||
471 |
|
||||
472 | In [8]: /str 43 |
|
|||
473 | ------> str(43) |
|
|||
474 | Out[8]: '43' |
|
|||
475 |
|
||||
476 | # all-random (note for auto-testing) |
|
|||
477 | """ |
|
|||
478 |
|
||||
479 | if parameter_s: |
|
|||
480 | arg = int(parameter_s) |
|
|||
481 | else: |
|
|||
482 | arg = 'toggle' |
|
|||
483 |
|
||||
484 | if not arg in (0,1,2,'toggle'): |
|
|||
485 | error('Valid modes: (0->Off, 1->Smart, 2->Full') |
|
|||
486 | return |
|
|||
487 |
|
||||
488 | if arg in (0,1,2): |
|
|||
489 | self.shell.autocall = arg |
|
|||
490 | else: # toggle |
|
|||
491 | if self.shell.autocall: |
|
|||
492 | self._magic_state.autocall_save = self.shell.autocall |
|
|||
493 | self.shell.autocall = 0 |
|
|||
494 | else: |
|
|||
495 | try: |
|
|||
496 | self.shell.autocall = self._magic_state.autocall_save |
|
|||
497 | except AttributeError: |
|
|||
498 | self.shell.autocall = self._magic_state.autocall_save = 1 |
|
|||
499 |
|
||||
500 | print "Automatic calling is:",['OFF','Smart','Full'][self.shell.autocall] |
|
|||
501 |
|
||||
502 |
|
||||
503 | def magic_page(self, parameter_s=''): |
|
|||
504 | """Pretty print the object and display it through a pager. |
|
|||
505 |
|
||||
506 | %page [options] OBJECT |
|
|||
507 |
|
||||
508 | If no object is given, use _ (last output). |
|
|||
509 |
|
||||
510 | Options: |
|
|||
511 |
|
||||
512 | -r: page str(object), don't pretty-print it.""" |
|
|||
513 |
|
||||
514 | # After a function contributed by Olivier Aubert, slightly modified. |
|
|||
515 |
|
||||
516 | # Process options/args |
|
|||
517 | opts,args = self.parse_options(parameter_s,'r') |
|
|||
518 | raw = 'r' in opts |
|
|||
519 |
|
||||
520 | oname = args and args or '_' |
|
|||
521 | info = self._ofind(oname) |
|
|||
522 | if info['found']: |
|
|||
523 | txt = (raw and str or pformat)( info['obj'] ) |
|
|||
524 | page.page(txt) |
|
|||
525 | else: |
|
|||
526 | print 'Object `%s` not found' % oname |
|
|||
527 |
|
||||
528 | def magic_profile(self, parameter_s=''): |
|
|||
529 | """Print your currently active IPython profile.""" |
|
|||
530 | from IPython.core.application import BaseIPythonApplication |
|
|||
531 | if BaseIPythonApplication.initialized(): |
|
|||
532 | print BaseIPythonApplication.instance().profile |
|
|||
533 | else: |
|
|||
534 | error("profile is an application-level value, but you don't appear to be in an IPython application") |
|
|||
535 |
|
||||
536 | def magic_pinfo(self, parameter_s='', namespaces=None): |
|
|||
537 | """Provide detailed information about an object. |
|
|||
538 |
|
||||
539 | '%pinfo object' is just a synonym for object? or ?object.""" |
|
|||
540 |
|
||||
541 | #print 'pinfo par: <%s>' % parameter_s # dbg |
|
|||
542 |
|
||||
543 |
|
||||
544 | # detail_level: 0 -> obj? , 1 -> obj?? |
|
|||
545 | detail_level = 0 |
|
|||
546 | # We need to detect if we got called as 'pinfo pinfo foo', which can |
|
|||
547 | # happen if the user types 'pinfo foo?' at the cmd line. |
|
|||
548 | pinfo,qmark1,oname,qmark2 = \ |
|
|||
549 | re.match('(pinfo )?(\?*)(.*?)(\??$)',parameter_s).groups() |
|
|||
550 | if pinfo or qmark1 or qmark2: |
|
|||
551 | detail_level = 1 |
|
|||
552 | if "*" in oname: |
|
|||
553 | self.magic_psearch(oname) |
|
|||
554 | else: |
|
|||
555 | self.shell._inspect('pinfo', oname, detail_level=detail_level, |
|
|||
556 | namespaces=namespaces) |
|
|||
557 |
|
||||
558 | def magic_pinfo2(self, parameter_s='', namespaces=None): |
|
|||
559 | """Provide extra detailed information about an object. |
|
|||
560 |
|
||||
561 | '%pinfo2 object' is just a synonym for object?? or ??object.""" |
|
|||
562 | self.shell._inspect('pinfo', parameter_s, detail_level=1, |
|
|||
563 | namespaces=namespaces) |
|
|||
564 |
|
||||
565 | @skip_doctest |
|
|||
566 | def magic_pdef(self, parameter_s='', namespaces=None): |
|
|||
567 | """Print the definition header for any callable object. |
|
|||
568 |
|
||||
569 | If the object is a class, print the constructor information. |
|
|||
570 |
|
||||
571 | Examples |
|
|||
572 | -------- |
|
|||
573 | :: |
|
|||
574 |
|
||||
575 | In [3]: %pdef urllib.urlopen |
|
|||
576 | urllib.urlopen(url, data=None, proxies=None) |
|
|||
577 | """ |
|
|||
578 | self._inspect('pdef',parameter_s, namespaces) |
|
|||
579 |
|
||||
580 | def magic_pdoc(self, parameter_s='', namespaces=None): |
|
|||
581 | """Print the docstring for an object. |
|
|||
582 |
|
||||
583 | If the given object is a class, it will print both the class and the |
|
|||
584 | constructor docstrings.""" |
|
|||
585 | self._inspect('pdoc',parameter_s, namespaces) |
|
|||
586 |
|
||||
587 | def magic_psource(self, parameter_s='', namespaces=None): |
|
|||
588 | """Print (or run through pager) the source code for an object.""" |
|
|||
589 | self._inspect('psource',parameter_s, namespaces) |
|
|||
590 |
|
||||
591 | def magic_pfile(self, parameter_s=''): |
|
|||
592 | """Print (or run through pager) the file where an object is defined. |
|
|||
593 |
|
||||
594 | The file opens at the line where the object definition begins. IPython |
|
|||
595 | will honor the environment variable PAGER if set, and otherwise will |
|
|||
596 | do its best to print the file in a convenient form. |
|
|||
597 |
|
||||
598 | If the given argument is not an object currently defined, IPython will |
|
|||
599 | try to interpret it as a filename (automatically adding a .py extension |
|
|||
600 | if needed). You can thus use %pfile as a syntax highlighting code |
|
|||
601 | viewer.""" |
|
|||
602 |
|
||||
603 | # first interpret argument as an object name |
|
|||
604 | out = self._inspect('pfile',parameter_s) |
|
|||
605 | # if not, try the input as a filename |
|
|||
606 | if out == 'not found': |
|
|||
607 | try: |
|
|||
608 | filename = get_py_filename(parameter_s) |
|
|||
609 | except IOError,msg: |
|
|||
610 | print msg |
|
|||
611 | return |
|
|||
612 | page.page(self.shell.inspector.format(open(filename).read())) |
|
|||
613 |
|
||||
614 | def magic_psearch(self, parameter_s=''): |
|
|||
615 | """Search for object in namespaces by wildcard. |
|
|||
616 |
|
||||
617 | %psearch [options] PATTERN [OBJECT TYPE] |
|
|||
618 |
|
||||
619 | Note: ? can be used as a synonym for %psearch, at the beginning or at |
|
|||
620 | the end: both a*? and ?a* are equivalent to '%psearch a*'. Still, the |
|
|||
621 | rest of the command line must be unchanged (options come first), so |
|
|||
622 | for example the following forms are equivalent |
|
|||
623 |
|
||||
624 | %psearch -i a* function |
|
|||
625 | -i a* function? |
|
|||
626 | ?-i a* function |
|
|||
627 |
|
||||
628 | Arguments: |
|
|||
629 |
|
||||
630 | PATTERN |
|
|||
631 |
|
||||
632 | where PATTERN is a string containing * as a wildcard similar to its |
|
|||
633 | use in a shell. The pattern is matched in all namespaces on the |
|
|||
634 | search path. By default objects starting with a single _ are not |
|
|||
635 | matched, many IPython generated objects have a single |
|
|||
636 | underscore. The default is case insensitive matching. Matching is |
|
|||
637 | also done on the attributes of objects and not only on the objects |
|
|||
638 | in a module. |
|
|||
639 |
|
||||
640 | [OBJECT TYPE] |
|
|||
641 |
|
||||
642 | Is the name of a python type from the types module. The name is |
|
|||
643 | given in lowercase without the ending type, ex. StringType is |
|
|||
644 | written string. By adding a type here only objects matching the |
|
|||
645 | given type are matched. Using all here makes the pattern match all |
|
|||
646 | types (this is the default). |
|
|||
647 |
|
||||
648 | Options: |
|
|||
649 |
|
||||
650 | -a: makes the pattern match even objects whose names start with a |
|
|||
651 | single underscore. These names are normally omitted from the |
|
|||
652 | search. |
|
|||
653 |
|
||||
654 | -i/-c: make the pattern case insensitive/sensitive. If neither of |
|
|||
655 | these options are given, the default is read from your configuration |
|
|||
656 | file, with the option ``InteractiveShell.wildcards_case_sensitive``. |
|
|||
657 | If this option is not specified in your configuration file, IPython's |
|
|||
658 | internal default is to do a case sensitive search. |
|
|||
659 |
|
||||
660 | -e/-s NAMESPACE: exclude/search a given namespace. The pattern you |
|
|||
661 | specify can be searched in any of the following namespaces: |
|
|||
662 | 'builtin', 'user', 'user_global','internal', 'alias', where |
|
|||
663 | 'builtin' and 'user' are the search defaults. Note that you should |
|
|||
664 | not use quotes when specifying namespaces. |
|
|||
665 |
|
||||
666 | 'Builtin' contains the python module builtin, 'user' contains all |
|
|||
667 | user data, 'alias' only contain the shell aliases and no python |
|
|||
668 | objects, 'internal' contains objects used by IPython. The |
|
|||
669 | 'user_global' namespace is only used by embedded IPython instances, |
|
|||
670 | and it contains module-level globals. You can add namespaces to the |
|
|||
671 | search with -s or exclude them with -e (these options can be given |
|
|||
672 | more than once). |
|
|||
673 |
|
||||
674 | Examples |
|
|||
675 | -------- |
|
|||
676 | :: |
|
|||
677 |
|
||||
678 | %psearch a* -> objects beginning with an a |
|
|||
679 | %psearch -e builtin a* -> objects NOT in the builtin space starting in a |
|
|||
680 | %psearch a* function -> all functions beginning with an a |
|
|||
681 | %psearch re.e* -> objects beginning with an e in module re |
|
|||
682 | %psearch r*.e* -> objects that start with e in modules starting in r |
|
|||
683 | %psearch r*.* string -> all strings in modules beginning with r |
|
|||
684 |
|
||||
685 | Case sensitive search:: |
|
|||
686 |
|
||||
687 | %psearch -c a* list all object beginning with lower case a |
|
|||
688 |
|
||||
689 | Show objects beginning with a single _:: |
|
|||
690 |
|
||||
691 | %psearch -a _* list objects beginning with a single underscore""" |
|
|||
692 | try: |
|
|||
693 | parameter_s.encode('ascii') |
|
|||
694 | except UnicodeEncodeError: |
|
|||
695 | print 'Python identifiers can only contain ascii characters.' |
|
|||
696 | return |
|
|||
697 |
|
||||
698 | # default namespaces to be searched |
|
|||
699 | def_search = ['user_local', 'user_global', 'builtin'] |
|
|||
700 |
|
||||
701 | # Process options/args |
|
|||
702 | opts,args = self.parse_options(parameter_s,'cias:e:',list_all=True) |
|
|||
703 | opt = opts.get |
|
|||
704 | shell = self.shell |
|
|||
705 | psearch = shell.inspector.psearch |
|
|||
706 |
|
||||
707 | # select case options |
|
|||
708 | if opts.has_key('i'): |
|
|||
709 | ignore_case = True |
|
|||
710 | elif opts.has_key('c'): |
|
|||
711 | ignore_case = False |
|
|||
712 | else: |
|
|||
713 | ignore_case = not shell.wildcards_case_sensitive |
|
|||
714 |
|
||||
715 | # Build list of namespaces to search from user options |
|
|||
716 | def_search.extend(opt('s',[])) |
|
|||
717 | ns_exclude = ns_exclude=opt('e',[]) |
|
|||
718 | ns_search = [nm for nm in def_search if nm not in ns_exclude] |
|
|||
719 |
|
||||
720 | # Call the actual search |
|
|||
721 | try: |
|
|||
722 | psearch(args,shell.ns_table,ns_search, |
|
|||
723 | show_all=opt('a'),ignore_case=ignore_case) |
|
|||
724 | except: |
|
|||
725 | shell.showtraceback() |
|
|||
726 |
|
||||
727 | @skip_doctest |
|
|||
728 | def magic_who_ls(self, parameter_s=''): |
|
|||
729 | """Return a sorted list of all interactive variables. |
|
|||
730 |
|
||||
731 | If arguments are given, only variables of types matching these |
|
|||
732 | arguments are returned. |
|
|||
733 |
|
||||
734 | Examples |
|
|||
735 | -------- |
|
|||
736 |
|
||||
737 | Define two variables and list them with who_ls:: |
|
|||
738 |
|
||||
739 | In [1]: alpha = 123 |
|
|||
740 |
|
||||
741 | In [2]: beta = 'test' |
|
|||
742 |
|
||||
743 | In [3]: %who_ls |
|
|||
744 | Out[3]: ['alpha', 'beta'] |
|
|||
745 |
|
||||
746 | In [4]: %who_ls int |
|
|||
747 | Out[4]: ['alpha'] |
|
|||
748 |
|
||||
749 | In [5]: %who_ls str |
|
|||
750 | Out[5]: ['beta'] |
|
|||
751 | """ |
|
|||
752 |
|
||||
753 | user_ns = self.shell.user_ns |
|
|||
754 | user_ns_hidden = self.shell.user_ns_hidden |
|
|||
755 | out = [ i for i in user_ns |
|
|||
756 | if not i.startswith('_') \ |
|
|||
757 | and not i in user_ns_hidden ] |
|
|||
758 |
|
||||
759 | typelist = parameter_s.split() |
|
|||
760 | if typelist: |
|
|||
761 | typeset = set(typelist) |
|
|||
762 | out = [i for i in out if type(user_ns[i]).__name__ in typeset] |
|
|||
763 |
|
||||
764 | out.sort() |
|
|||
765 | return out |
|
|||
766 |
|
||||
767 | @skip_doctest |
|
|||
768 | def magic_who(self, parameter_s=''): |
|
|||
769 | """Print all interactive variables, with some minimal formatting. |
|
|||
770 |
|
||||
771 | If any arguments are given, only variables whose type matches one of |
|
|||
772 | these are printed. For example:: |
|
|||
773 |
|
||||
774 | %who function str |
|
|||
775 |
|
||||
776 | will only list functions and strings, excluding all other types of |
|
|||
777 | variables. To find the proper type names, simply use type(var) at a |
|
|||
778 | command line to see how python prints type names. For example: |
|
|||
779 |
|
||||
780 | :: |
|
|||
781 |
|
||||
782 | In [1]: type('hello')\\ |
|
|||
783 | Out[1]: <type 'str'> |
|
|||
784 |
|
||||
785 | indicates that the type name for strings is 'str'. |
|
|||
786 |
|
||||
787 | ``%who`` always excludes executed names loaded through your configuration |
|
|||
788 | file and things which are internal to IPython. |
|
|||
789 |
|
||||
790 | This is deliberate, as typically you may load many modules and the |
|
|||
791 | purpose of %who is to show you only what you've manually defined. |
|
|||
792 |
|
||||
793 | Examples |
|
|||
794 | -------- |
|
|||
795 |
|
||||
796 | Define two variables and list them with who:: |
|
|||
797 |
|
||||
798 | In [1]: alpha = 123 |
|
|||
799 |
|
||||
800 | In [2]: beta = 'test' |
|
|||
801 |
|
||||
802 | In [3]: %who |
|
|||
803 | alpha beta |
|
|||
804 |
|
||||
805 | In [4]: %who int |
|
|||
806 | alpha |
|
|||
807 |
|
||||
808 | In [5]: %who str |
|
|||
809 | beta |
|
|||
810 | """ |
|
|||
811 |
|
||||
812 | varlist = self.magic_who_ls(parameter_s) |
|
|||
813 | if not varlist: |
|
|||
814 | if parameter_s: |
|
|||
815 | print 'No variables match your requested type.' |
|
|||
816 | else: |
|
|||
817 | print 'Interactive namespace is empty.' |
|
|||
818 | return |
|
|||
819 |
|
||||
820 | # if we have variables, move on... |
|
|||
821 | count = 0 |
|
|||
822 | for i in varlist: |
|
|||
823 | print i+'\t', |
|
|||
824 | count += 1 |
|
|||
825 | if count > 8: |
|
|||
826 | count = 0 |
|
|||
827 |
|
||||
828 |
|
||||
829 |
|
||||
830 | @skip_doctest |
|
|||
831 | def magic_whos(self, parameter_s=''): |
|
|||
832 | """Like %who, but gives some extra information about each variable. |
|
|||
833 |
|
||||
834 | The same type filtering of %who can be applied here. |
|
|||
835 |
|
||||
836 | For all variables, the type is printed. Additionally it prints: |
|
|||
837 |
|
||||
838 | - For {},[],(): their length. |
|
|||
839 |
|
||||
840 | - For numpy arrays, a summary with shape, number of |
|
|||
841 | elements, typecode and size in memory. |
|
|||
842 |
|
||||
843 | - Everything else: a string representation, snipping their middle if |
|
|||
844 | too long. |
|
|||
845 |
|
||||
846 | Examples |
|
|||
847 | -------- |
|
|||
848 |
|
||||
849 | Define two variables and list them with whos:: |
|
|||
850 |
|
||||
851 | In [1]: alpha = 123 |
|
|||
852 |
|
||||
853 | In [2]: beta = 'test' |
|
|||
854 |
|
||||
855 | In [3]: %whos |
|
|||
856 | Variable Type Data/Info |
|
|||
857 | -------------------------------- |
|
|||
858 | alpha int 123 |
|
|||
859 | beta str test |
|
|||
860 | """ |
|
|||
861 |
|
||||
862 | varnames = self.magic_who_ls(parameter_s) |
|
|||
863 | if not varnames: |
|
|||
864 | if parameter_s: |
|
|||
865 | print 'No variables match your requested type.' |
|
|||
866 | else: |
|
|||
867 | print 'Interactive namespace is empty.' |
|
|||
868 | return |
|
|||
869 |
|
||||
870 | # if we have variables, move on... |
|
|||
871 |
|
||||
872 | # for these types, show len() instead of data: |
|
|||
873 | seq_types = ['dict', 'list', 'tuple'] |
|
|||
874 |
|
||||
875 | # for numpy arrays, display summary info |
|
|||
876 | ndarray_type = None |
|
|||
877 | if 'numpy' in sys.modules: |
|
|||
878 | try: |
|
|||
879 | from numpy import ndarray |
|
|||
880 | except ImportError: |
|
|||
881 | pass |
|
|||
882 | else: |
|
|||
883 | ndarray_type = ndarray.__name__ |
|
|||
884 |
|
||||
885 | # Find all variable names and types so we can figure out column sizes |
|
|||
886 | def get_vars(i): |
|
|||
887 | return self.shell.user_ns[i] |
|
|||
888 |
|
||||
889 | # some types are well known and can be shorter |
|
|||
890 | abbrevs = {'IPython.core.macro.Macro' : 'Macro'} |
|
|||
891 | def type_name(v): |
|
|||
892 | tn = type(v).__name__ |
|
|||
893 | return abbrevs.get(tn,tn) |
|
|||
894 |
|
||||
895 | varlist = map(get_vars,varnames) |
|
|||
896 |
|
||||
897 | typelist = [] |
|
|||
898 | for vv in varlist: |
|
|||
899 | tt = type_name(vv) |
|
|||
900 |
|
||||
901 | if tt=='instance': |
|
|||
902 | typelist.append( abbrevs.get(str(vv.__class__), |
|
|||
903 | str(vv.__class__))) |
|
|||
904 | else: |
|
|||
905 | typelist.append(tt) |
|
|||
906 |
|
||||
907 | # column labels and # of spaces as separator |
|
|||
908 | varlabel = 'Variable' |
|
|||
909 | typelabel = 'Type' |
|
|||
910 | datalabel = 'Data/Info' |
|
|||
911 | colsep = 3 |
|
|||
912 | # variable format strings |
|
|||
913 | vformat = "{0:<{varwidth}}{1:<{typewidth}}" |
|
|||
914 | aformat = "%s: %s elems, type `%s`, %s bytes" |
|
|||
915 | # find the size of the columns to format the output nicely |
|
|||
916 | varwidth = max(max(map(len,varnames)), len(varlabel)) + colsep |
|
|||
917 | typewidth = max(max(map(len,typelist)), len(typelabel)) + colsep |
|
|||
918 | # table header |
|
|||
919 | print varlabel.ljust(varwidth) + typelabel.ljust(typewidth) + \ |
|
|||
920 | ' '+datalabel+'\n' + '-'*(varwidth+typewidth+len(datalabel)+1) |
|
|||
921 | # and the table itself |
|
|||
922 | kb = 1024 |
|
|||
923 | Mb = 1048576 # kb**2 |
|
|||
924 | for vname,var,vtype in zip(varnames,varlist,typelist): |
|
|||
925 | print vformat.format(vname, vtype, varwidth=varwidth, typewidth=typewidth), |
|
|||
926 | if vtype in seq_types: |
|
|||
927 | print "n="+str(len(var)) |
|
|||
928 | elif vtype == ndarray_type: |
|
|||
929 | vshape = str(var.shape).replace(',','').replace(' ','x')[1:-1] |
|
|||
930 | if vtype==ndarray_type: |
|
|||
931 | # numpy |
|
|||
932 | vsize = var.size |
|
|||
933 | vbytes = vsize*var.itemsize |
|
|||
934 | vdtype = var.dtype |
|
|||
935 |
|
||||
936 | if vbytes < 100000: |
|
|||
937 | print aformat % (vshape,vsize,vdtype,vbytes) |
|
|||
938 | else: |
|
|||
939 | print aformat % (vshape,vsize,vdtype,vbytes), |
|
|||
940 | if vbytes < Mb: |
|
|||
941 | print '(%s kb)' % (vbytes/kb,) |
|
|||
942 | else: |
|
|||
943 | print '(%s Mb)' % (vbytes/Mb,) |
|
|||
944 | else: |
|
|||
945 | try: |
|
|||
946 | vstr = str(var) |
|
|||
947 | except UnicodeEncodeError: |
|
|||
948 | vstr = unicode(var).encode(DEFAULT_ENCODING, |
|
|||
949 | 'backslashreplace') |
|
|||
950 | except: |
|
|||
951 | vstr = "<object with id %d (str() failed)>" % id(var) |
|
|||
952 | vstr = vstr.replace('\n','\\n') |
|
|||
953 | if len(vstr) < 50: |
|
|||
954 | print vstr |
|
|||
955 | else: |
|
|||
956 | print vstr[:25] + "<...>" + vstr[-25:] |
|
|||
957 |
|
||||
958 | def magic_reset(self, parameter_s=''): |
|
|||
959 | """Resets the namespace by removing all names defined by the user, if |
|
|||
960 | called without arguments, or by removing some types of objects, such |
|
|||
961 | as everything currently in IPython's In[] and Out[] containers (see |
|
|||
962 | the parameters for details). |
|
|||
963 |
|
||||
964 | Parameters |
|
|||
965 | ---------- |
|
|||
966 | -f : force reset without asking for confirmation. |
|
|||
967 |
|
||||
968 | -s : 'Soft' reset: Only clears your namespace, leaving history intact. |
|
|||
969 | References to objects may be kept. By default (without this option), |
|
|||
970 | we do a 'hard' reset, giving you a new session and removing all |
|
|||
971 | references to objects from the current session. |
|
|||
972 |
|
||||
973 | in : reset input history |
|
|||
974 |
|
||||
975 | out : reset output history |
|
|||
976 |
|
||||
977 | dhist : reset directory history |
|
|||
978 |
|
||||
979 | array : reset only variables that are NumPy arrays |
|
|||
980 |
|
||||
981 | See Also |
|
|||
982 | -------- |
|
|||
983 | magic_reset_selective : invoked as ``%reset_selective`` |
|
|||
984 |
|
||||
985 | Examples |
|
|||
986 | -------- |
|
|||
987 | :: |
|
|||
988 |
|
||||
989 | In [6]: a = 1 |
|
|||
990 |
|
||||
991 | In [7]: a |
|
|||
992 | Out[7]: 1 |
|
|||
993 |
|
||||
994 | In [8]: 'a' in _ip.user_ns |
|
|||
995 | Out[8]: True |
|
|||
996 |
|
||||
997 | In [9]: %reset -f |
|
|||
998 |
|
||||
999 | In [1]: 'a' in _ip.user_ns |
|
|||
1000 | Out[1]: False |
|
|||
1001 |
|
||||
1002 | In [2]: %reset -f in |
|
|||
1003 | Flushing input history |
|
|||
1004 |
|
||||
1005 | In [3]: %reset -f dhist in |
|
|||
1006 | Flushing directory history |
|
|||
1007 | Flushing input history |
|
|||
1008 |
|
||||
1009 | Notes |
|
|||
1010 | ----- |
|
|||
1011 | Calling this magic from clients that do not implement standard input, |
|
|||
1012 | such as the ipython notebook interface, will reset the namespace |
|
|||
1013 | without confirmation. |
|
|||
1014 | """ |
|
|||
1015 | opts, args = self.parse_options(parameter_s,'sf', mode='list') |
|
|||
1016 | if 'f' in opts: |
|
|||
1017 | ans = True |
|
|||
1018 | else: |
|
|||
1019 | try: |
|
|||
1020 | ans = self.shell.ask_yes_no( |
|
|||
1021 | "Once deleted, variables cannot be recovered. Proceed (y/[n])? ", default='n') |
|
|||
1022 | except StdinNotImplementedError: |
|
|||
1023 | ans = True |
|
|||
1024 | if not ans: |
|
|||
1025 | print 'Nothing done.' |
|
|||
1026 | return |
|
|||
1027 |
|
||||
1028 | if 's' in opts: # Soft reset |
|
|||
1029 | user_ns = self.shell.user_ns |
|
|||
1030 | for i in self.magic_who_ls(): |
|
|||
1031 | del(user_ns[i]) |
|
|||
1032 | elif len(args) == 0: # Hard reset |
|
|||
1033 | self.shell.reset(new_session = False) |
|
|||
1034 |
|
||||
1035 | # reset in/out/dhist/array: previously extensinions/clearcmd.py |
|
|||
1036 | ip = self.shell |
|
|||
1037 | user_ns = self.user_ns # local lookup, heavily used |
|
|||
1038 |
|
||||
1039 | for target in args: |
|
|||
1040 | target = target.lower() # make matches case insensitive |
|
|||
1041 | if target == 'out': |
|
|||
1042 | print "Flushing output cache (%d entries)" % len(user_ns['_oh']) |
|
|||
1043 | self.displayhook.flush() |
|
|||
1044 |
|
||||
1045 | elif target == 'in': |
|
|||
1046 | print "Flushing input history" |
|
|||
1047 | pc = self.displayhook.prompt_count + 1 |
|
|||
1048 | for n in range(1, pc): |
|
|||
1049 | key = '_i'+repr(n) |
|
|||
1050 | user_ns.pop(key,None) |
|
|||
1051 | user_ns.update(dict(_i=u'',_ii=u'',_iii=u'')) |
|
|||
1052 | hm = ip.history_manager |
|
|||
1053 | # don't delete these, as %save and %macro depending on the length |
|
|||
1054 | # of these lists to be preserved |
|
|||
1055 | hm.input_hist_parsed[:] = [''] * pc |
|
|||
1056 | hm.input_hist_raw[:] = [''] * pc |
|
|||
1057 | # hm has internal machinery for _i,_ii,_iii, clear it out |
|
|||
1058 | hm._i = hm._ii = hm._iii = hm._i00 = u'' |
|
|||
1059 |
|
||||
1060 | elif target == 'array': |
|
|||
1061 | # Support cleaning up numpy arrays |
|
|||
1062 | try: |
|
|||
1063 | from numpy import ndarray |
|
|||
1064 | # This must be done with items and not iteritems because we're |
|
|||
1065 | # going to modify the dict in-place. |
|
|||
1066 | for x,val in user_ns.items(): |
|
|||
1067 | if isinstance(val,ndarray): |
|
|||
1068 | del user_ns[x] |
|
|||
1069 | except ImportError: |
|
|||
1070 | print "reset array only works if Numpy is available." |
|
|||
1071 |
|
||||
1072 | elif target == 'dhist': |
|
|||
1073 | print "Flushing directory history" |
|
|||
1074 | del user_ns['_dh'][:] |
|
|||
1075 |
|
||||
1076 | else: |
|
|||
1077 | print "Don't know how to reset ", |
|
|||
1078 | print target + ", please run `%reset?` for details" |
|
|||
1079 |
|
572 | |||
1080 | gc.collect() |
|
573 | if fn not in self.lsmagic(): | |
1081 |
|
574 | error("%s is not a magic function" % fn) | ||
1082 | def magic_reset_selective(self, parameter_s=''): |
|
575 | self.options_table[fn] = optstr | |
1083 | """Resets the namespace by removing names defined by the user. |
|
|||
1084 |
|
||||
1085 | Input/Output history are left around in case you need them. |
|
|||
1086 |
|
||||
1087 | %reset_selective [-f] regex |
|
|||
1088 |
|
||||
1089 | No action is taken if regex is not included |
|
|||
1090 |
|
||||
1091 | Options |
|
|||
1092 | -f : force reset without asking for confirmation. |
|
|||
1093 |
|
||||
1094 | See Also |
|
|||
1095 | -------- |
|
|||
1096 | magic_reset : invoked as ``%reset`` |
|
|||
1097 |
|
||||
1098 | Examples |
|
|||
1099 | -------- |
|
|||
1100 |
|
||||
1101 | We first fully reset the namespace so your output looks identical to |
|
|||
1102 | this example for pedagogical reasons; in practice you do not need a |
|
|||
1103 | full reset:: |
|
|||
1104 |
|
||||
1105 | In [1]: %reset -f |
|
|||
1106 |
|
||||
1107 | Now, with a clean namespace we can make a few variables and use |
|
|||
1108 | ``%reset_selective`` to only delete names that match our regexp:: |
|
|||
1109 |
|
||||
1110 | In [2]: a=1; b=2; c=3; b1m=4; b2m=5; b3m=6; b4m=7; b2s=8 |
|
|||
1111 |
|
||||
1112 | In [3]: who_ls |
|
|||
1113 | Out[3]: ['a', 'b', 'b1m', 'b2m', 'b2s', 'b3m', 'b4m', 'c'] |
|
|||
1114 |
|
||||
1115 | In [4]: %reset_selective -f b[2-3]m |
|
|||
1116 |
|
||||
1117 | In [5]: who_ls |
|
|||
1118 | Out[5]: ['a', 'b', 'b1m', 'b2s', 'b4m', 'c'] |
|
|||
1119 |
|
||||
1120 | In [6]: %reset_selective -f d |
|
|||
1121 |
|
||||
1122 | In [7]: who_ls |
|
|||
1123 | Out[7]: ['a', 'b', 'b1m', 'b2s', 'b4m', 'c'] |
|
|||
1124 |
|
||||
1125 | In [8]: %reset_selective -f c |
|
|||
1126 |
|
||||
1127 | In [9]: who_ls |
|
|||
1128 | Out[9]: ['a', 'b', 'b1m', 'b2s', 'b4m'] |
|
|||
1129 |
|
||||
1130 | In [10]: %reset_selective -f b |
|
|||
1131 |
|
||||
1132 | In [11]: who_ls |
|
|||
1133 | Out[11]: ['a'] |
|
|||
1134 |
|
||||
1135 | Notes |
|
|||
1136 | ----- |
|
|||
1137 | Calling this magic from clients that do not implement standard input, |
|
|||
1138 | such as the ipython notebook interface, will reset the namespace |
|
|||
1139 | without confirmation. |
|
|||
1140 | """ |
|
|||
1141 |
|
||||
1142 | opts, regex = self.parse_options(parameter_s,'f') |
|
|||
1143 |
|
||||
1144 | if opts.has_key('f'): |
|
|||
1145 | ans = True |
|
|||
1146 | else: |
|
|||
1147 | try: |
|
|||
1148 | ans = self.shell.ask_yes_no( |
|
|||
1149 | "Once deleted, variables cannot be recovered. Proceed (y/[n])? ", |
|
|||
1150 | default='n') |
|
|||
1151 | except StdinNotImplementedError: |
|
|||
1152 | ans = True |
|
|||
1153 | if not ans: |
|
|||
1154 | print 'Nothing done.' |
|
|||
1155 | return |
|
|||
1156 | user_ns = self.shell.user_ns |
|
|||
1157 | if not regex: |
|
|||
1158 | print 'No regex pattern specified. Nothing done.' |
|
|||
1159 | return |
|
|||
1160 | else: |
|
|||
1161 | try: |
|
|||
1162 | m = re.compile(regex) |
|
|||
1163 | except TypeError: |
|
|||
1164 | raise TypeError('regex must be a string or compiled pattern') |
|
|||
1165 | for i in self.magic_who_ls(): |
|
|||
1166 | if m.search(i): |
|
|||
1167 | del(user_ns[i]) |
|
|||
1168 |
|
||||
1169 | def magic_xdel(self, parameter_s=''): |
|
|||
1170 | """Delete a variable, trying to clear it from anywhere that |
|
|||
1171 | IPython's machinery has references to it. By default, this uses |
|
|||
1172 | the identity of the named object in the user namespace to remove |
|
|||
1173 | references held under other names. The object is also removed |
|
|||
1174 | from the output history. |
|
|||
1175 |
|
||||
1176 | Options |
|
|||
1177 | -n : Delete the specified name from all namespaces, without |
|
|||
1178 | checking their identity. |
|
|||
1179 | """ |
|
|||
1180 | opts, varname = self.parse_options(parameter_s,'n') |
|
|||
1181 | try: |
|
|||
1182 | self.shell.del_var(varname, ('n' in opts)) |
|
|||
1183 | except (NameError, ValueError) as e: |
|
|||
1184 | print type(e).__name__ +": "+ str(e) |
|
|||
1185 |
|
||||
1186 | def magic_logstart(self,parameter_s=''): |
|
|||
1187 | """Start logging anywhere in a session. |
|
|||
1188 |
|
||||
1189 | %logstart [-o|-r|-t] [log_name [log_mode]] |
|
|||
1190 |
|
||||
1191 | If no name is given, it defaults to a file named 'ipython_log.py' in your |
|
|||
1192 | current directory, in 'rotate' mode (see below). |
|
|||
1193 |
|
||||
1194 | '%logstart name' saves to file 'name' in 'backup' mode. It saves your |
|
|||
1195 | history up to that point and then continues logging. |
|
|||
1196 |
|
||||
1197 | %logstart takes a second optional parameter: logging mode. This can be one |
|
|||
1198 | of (note that the modes are given unquoted):\\ |
|
|||
1199 | append: well, that says it.\\ |
|
|||
1200 | backup: rename (if exists) to name~ and start name.\\ |
|
|||
1201 | global: single logfile in your home dir, appended to.\\ |
|
|||
1202 | over : overwrite existing log.\\ |
|
|||
1203 | rotate: create rotating logs name.1~, name.2~, etc. |
|
|||
1204 |
|
||||
1205 | Options: |
|
|||
1206 |
|
||||
1207 | -o: log also IPython's output. In this mode, all commands which |
|
|||
1208 | generate an Out[NN] prompt are recorded to the logfile, right after |
|
|||
1209 | their corresponding input line. The output lines are always |
|
|||
1210 | prepended with a '#[Out]# ' marker, so that the log remains valid |
|
|||
1211 | Python code. |
|
|||
1212 |
|
||||
1213 | Since this marker is always the same, filtering only the output from |
|
|||
1214 | a log is very easy, using for example a simple awk call:: |
|
|||
1215 |
|
||||
1216 | awk -F'#\\[Out\\]# ' '{if($2) {print $2}}' ipython_log.py |
|
|||
1217 |
|
||||
1218 | -r: log 'raw' input. Normally, IPython's logs contain the processed |
|
|||
1219 | input, so that user lines are logged in their final form, converted |
|
|||
1220 | into valid Python. For example, %Exit is logged as |
|
|||
1221 | _ip.magic("Exit"). If the -r flag is given, all input is logged |
|
|||
1222 | exactly as typed, with no transformations applied. |
|
|||
1223 |
|
||||
1224 | -t: put timestamps before each input line logged (these are put in |
|
|||
1225 | comments).""" |
|
|||
1226 |
|
||||
1227 | opts,par = self.parse_options(parameter_s,'ort') |
|
|||
1228 | log_output = 'o' in opts |
|
|||
1229 | log_raw_input = 'r' in opts |
|
|||
1230 | timestamp = 't' in opts |
|
|||
1231 |
|
||||
1232 | logger = self.shell.logger |
|
|||
1233 |
|
||||
1234 | # if no args are given, the defaults set in the logger constructor by |
|
|||
1235 | # ipython remain valid |
|
|||
1236 | if par: |
|
|||
1237 | try: |
|
|||
1238 | logfname,logmode = par.split() |
|
|||
1239 | except: |
|
|||
1240 | logfname = par |
|
|||
1241 | logmode = 'backup' |
|
|||
1242 | else: |
|
|||
1243 | logfname = logger.logfname |
|
|||
1244 | logmode = logger.logmode |
|
|||
1245 | # put logfname into rc struct as if it had been called on the command |
|
|||
1246 | # line, so it ends up saved in the log header Save it in case we need |
|
|||
1247 | # to restore it... |
|
|||
1248 | old_logfile = self.shell.logfile |
|
|||
1249 | if logfname: |
|
|||
1250 | logfname = os.path.expanduser(logfname) |
|
|||
1251 | self.shell.logfile = logfname |
|
|||
1252 |
|
||||
1253 | loghead = '# IPython log file\n\n' |
|
|||
1254 | try: |
|
|||
1255 | started = logger.logstart(logfname,loghead,logmode, |
|
|||
1256 | log_output,timestamp,log_raw_input) |
|
|||
1257 | except: |
|
|||
1258 | self.shell.logfile = old_logfile |
|
|||
1259 | warn("Couldn't start log: %s" % sys.exc_info()[1]) |
|
|||
1260 | else: |
|
|||
1261 | # log input history up to this point, optionally interleaving |
|
|||
1262 | # output if requested |
|
|||
1263 |
|
||||
1264 | if timestamp: |
|
|||
1265 | # disable timestamping for the previous history, since we've |
|
|||
1266 | # lost those already (no time machine here). |
|
|||
1267 | logger.timestamp = False |
|
|||
1268 |
|
||||
1269 | if log_raw_input: |
|
|||
1270 | input_hist = self.shell.history_manager.input_hist_raw |
|
|||
1271 | else: |
|
|||
1272 | input_hist = self.shell.history_manager.input_hist_parsed |
|
|||
1273 |
|
||||
1274 | if log_output: |
|
|||
1275 | log_write = logger.log_write |
|
|||
1276 | output_hist = self.shell.history_manager.output_hist |
|
|||
1277 | for n in range(1,len(input_hist)-1): |
|
|||
1278 | log_write(input_hist[n].rstrip() + '\n') |
|
|||
1279 | if n in output_hist: |
|
|||
1280 | log_write(repr(output_hist[n]),'output') |
|
|||
1281 | else: |
|
|||
1282 | logger.log_write('\n'.join(input_hist[1:])) |
|
|||
1283 | logger.log_write('\n') |
|
|||
1284 | if timestamp: |
|
|||
1285 | # re-enable timestamping |
|
|||
1286 | logger.timestamp = True |
|
|||
1287 |
|
||||
1288 | print ('Activating auto-logging. ' |
|
|||
1289 | 'Current session state plus future input saved.') |
|
|||
1290 | logger.logstate() |
|
|||
1291 |
|
||||
1292 | def magic_logstop(self,parameter_s=''): |
|
|||
1293 | """Fully stop logging and close log file. |
|
|||
1294 |
|
||||
1295 | In order to start logging again, a new %logstart call needs to be made, |
|
|||
1296 | possibly (though not necessarily) with a new filename, mode and other |
|
|||
1297 | options.""" |
|
|||
1298 | self.logger.logstop() |
|
|||
1299 |
|
||||
1300 | def magic_logoff(self,parameter_s=''): |
|
|||
1301 | """Temporarily stop logging. |
|
|||
1302 |
|
||||
1303 | You must have previously started logging.""" |
|
|||
1304 | self.shell.logger.switch_log(0) |
|
|||
1305 |
|
||||
1306 | def magic_logon(self,parameter_s=''): |
|
|||
1307 | """Restart logging. |
|
|||
1308 |
|
||||
1309 | This function is for restarting logging which you've temporarily |
|
|||
1310 | stopped with %logoff. For starting logging for the first time, you |
|
|||
1311 | must use the %logstart function, which allows you to specify an |
|
|||
1312 | optional log filename.""" |
|
|||
1313 |
|
||||
1314 | self.shell.logger.switch_log(1) |
|
|||
1315 |
|
||||
1316 | def magic_logstate(self,parameter_s=''): |
|
|||
1317 | """Print the status of the logging system.""" |
|
|||
1318 |
|
||||
1319 | self.shell.logger.logstate() |
|
|||
1320 |
|
||||
1321 | def magic_pdb(self, parameter_s=''): |
|
|||
1322 | """Control the automatic calling of the pdb interactive debugger. |
|
|||
1323 |
|
||||
1324 | Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without |
|
|||
1325 | argument it works as a toggle. |
|
|||
1326 |
|
||||
1327 | When an exception is triggered, IPython can optionally call the |
|
|||
1328 | interactive pdb debugger after the traceback printout. %pdb toggles |
|
|||
1329 | this feature on and off. |
|
|||
1330 |
|
||||
1331 | The initial state of this feature is set in your configuration |
|
|||
1332 | file (the option is ``InteractiveShell.pdb``). |
|
|||
1333 |
|
||||
1334 | If you want to just activate the debugger AFTER an exception has fired, |
|
|||
1335 | without having to type '%pdb on' and rerunning your code, you can use |
|
|||
1336 | the %debug magic.""" |
|
|||
1337 |
|
||||
1338 | par = parameter_s.strip().lower() |
|
|||
1339 |
|
||||
1340 | if par: |
|
|||
1341 | try: |
|
|||
1342 | new_pdb = {'off':0,'0':0,'on':1,'1':1}[par] |
|
|||
1343 | except KeyError: |
|
|||
1344 | print ('Incorrect argument. Use on/1, off/0, ' |
|
|||
1345 | 'or nothing for a toggle.') |
|
|||
1346 | return |
|
|||
1347 | else: |
|
|||
1348 | # toggle |
|
|||
1349 | new_pdb = not self.shell.call_pdb |
|
|||
1350 |
|
||||
1351 | # set on the shell |
|
|||
1352 | self.shell.call_pdb = new_pdb |
|
|||
1353 | print 'Automatic pdb calling has been turned',on_off(new_pdb) |
|
|||
1354 |
|
||||
1355 | def magic_debug(self, parameter_s=''): |
|
|||
1356 | """Activate the interactive debugger in post-mortem mode. |
|
|||
1357 |
|
||||
1358 | If an exception has just occurred, this lets you inspect its stack |
|
|||
1359 | frames interactively. Note that this will always work only on the last |
|
|||
1360 | traceback that occurred, so you must call this quickly after an |
|
|||
1361 | exception that you wish to inspect has fired, because if another one |
|
|||
1362 | occurs, it clobbers the previous one. |
|
|||
1363 |
|
||||
1364 | If you want IPython to automatically do this on every exception, see |
|
|||
1365 | the %pdb magic for more details. |
|
|||
1366 | """ |
|
|||
1367 | self.shell.debugger(force=True) |
|
|||
1368 |
|
||||
1369 | @skip_doctest |
|
|||
1370 | def magic_prun(self, parameter_s ='',user_mode=1, |
|
|||
1371 | opts=None,arg_lst=None,prog_ns=None): |
|
|||
1372 |
|
||||
1373 | """Run a statement through the python code profiler. |
|
|||
1374 |
|
||||
1375 | Usage: |
|
|||
1376 | %prun [options] statement |
|
|||
1377 |
|
||||
1378 | The given statement (which doesn't require quote marks) is run via the |
|
|||
1379 | python profiler in a manner similar to the profile.run() function. |
|
|||
1380 | Namespaces are internally managed to work correctly; profile.run |
|
|||
1381 | cannot be used in IPython because it makes certain assumptions about |
|
|||
1382 | namespaces which do not hold under IPython. |
|
|||
1383 |
|
||||
1384 | Options: |
|
|||
1385 |
|
||||
1386 | -l <limit>: you can place restrictions on what or how much of the |
|
|||
1387 | profile gets printed. The limit value can be: |
|
|||
1388 |
|
||||
1389 | * A string: only information for function names containing this string |
|
|||
1390 | is printed. |
|
|||
1391 |
|
||||
1392 | * An integer: only these many lines are printed. |
|
|||
1393 |
|
||||
1394 | * A float (between 0 and 1): this fraction of the report is printed |
|
|||
1395 | (for example, use a limit of 0.4 to see the topmost 40% only). |
|
|||
1396 |
|
||||
1397 | You can combine several limits with repeated use of the option. For |
|
|||
1398 | example, '-l __init__ -l 5' will print only the topmost 5 lines of |
|
|||
1399 | information about class constructors. |
|
|||
1400 |
|
||||
1401 | -r: return the pstats.Stats object generated by the profiling. This |
|
|||
1402 | object has all the information about the profile in it, and you can |
|
|||
1403 | later use it for further analysis or in other functions. |
|
|||
1404 |
|
||||
1405 | -s <key>: sort profile by given key. You can provide more than one key |
|
|||
1406 | by using the option several times: '-s key1 -s key2 -s key3...'. The |
|
|||
1407 | default sorting key is 'time'. |
|
|||
1408 |
|
||||
1409 | The following is copied verbatim from the profile documentation |
|
|||
1410 | referenced below: |
|
|||
1411 |
|
||||
1412 | When more than one key is provided, additional keys are used as |
|
|||
1413 | secondary criteria when the there is equality in all keys selected |
|
|||
1414 | before them. |
|
|||
1415 |
|
||||
1416 | Abbreviations can be used for any key names, as long as the |
|
|||
1417 | abbreviation is unambiguous. The following are the keys currently |
|
|||
1418 | defined: |
|
|||
1419 |
|
||||
1420 | Valid Arg Meaning |
|
|||
1421 | "calls" call count |
|
|||
1422 | "cumulative" cumulative time |
|
|||
1423 | "file" file name |
|
|||
1424 | "module" file name |
|
|||
1425 | "pcalls" primitive call count |
|
|||
1426 | "line" line number |
|
|||
1427 | "name" function name |
|
|||
1428 | "nfl" name/file/line |
|
|||
1429 | "stdname" standard name |
|
|||
1430 | "time" internal time |
|
|||
1431 |
|
||||
1432 | Note that all sorts on statistics are in descending order (placing |
|
|||
1433 | most time consuming items first), where as name, file, and line number |
|
|||
1434 | searches are in ascending order (i.e., alphabetical). The subtle |
|
|||
1435 | distinction between "nfl" and "stdname" is that the standard name is a |
|
|||
1436 | sort of the name as printed, which means that the embedded line |
|
|||
1437 | numbers get compared in an odd way. For example, lines 3, 20, and 40 |
|
|||
1438 | would (if the file names were the same) appear in the string order |
|
|||
1439 | "20" "3" and "40". In contrast, "nfl" does a numeric compare of the |
|
|||
1440 | line numbers. In fact, sort_stats("nfl") is the same as |
|
|||
1441 | sort_stats("name", "file", "line"). |
|
|||
1442 |
|
||||
1443 | -T <filename>: save profile results as shown on screen to a text |
|
|||
1444 | file. The profile is still shown on screen. |
|
|||
1445 |
|
||||
1446 | -D <filename>: save (via dump_stats) profile statistics to given |
|
|||
1447 | filename. This data is in a format understood by the pstats module, and |
|
|||
1448 | is generated by a call to the dump_stats() method of profile |
|
|||
1449 | objects. The profile is still shown on screen. |
|
|||
1450 |
|
||||
1451 | -q: suppress output to the pager. Best used with -T and/or -D above. |
|
|||
1452 |
|
||||
1453 | If you want to run complete programs under the profiler's control, use |
|
|||
1454 | '%run -p [prof_opts] filename.py [args to program]' where prof_opts |
|
|||
1455 | contains profiler specific options as described here. |
|
|||
1456 |
|
||||
1457 | You can read the complete documentation for the profile module with:: |
|
|||
1458 |
|
||||
1459 | In [1]: import profile; profile.help() |
|
|||
1460 | """ |
|
|||
1461 |
|
||||
1462 | opts_def = Struct(D=[''],l=[],s=['time'],T=['']) |
|
|||
1463 |
|
||||
1464 | if user_mode: # regular user call |
|
|||
1465 | opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:q', |
|
|||
1466 | list_all=1, posix=False) |
|
|||
1467 | namespace = self.shell.user_ns |
|
|||
1468 | else: # called to run a program by %run -p |
|
|||
1469 | try: |
|
|||
1470 | filename = get_py_filename(arg_lst[0]) |
|
|||
1471 | except IOError as e: |
|
|||
1472 | try: |
|
|||
1473 | msg = str(e) |
|
|||
1474 | except UnicodeError: |
|
|||
1475 | msg = e.message |
|
|||
1476 | error(msg) |
|
|||
1477 | return |
|
|||
1478 |
|
||||
1479 | arg_str = 'execfile(filename,prog_ns)' |
|
|||
1480 | namespace = { |
|
|||
1481 | 'execfile': self.shell.safe_execfile, |
|
|||
1482 | 'prog_ns': prog_ns, |
|
|||
1483 | 'filename': filename |
|
|||
1484 | } |
|
|||
1485 |
|
||||
1486 | opts.merge(opts_def) |
|
|||
1487 |
|
||||
1488 | prof = profile.Profile() |
|
|||
1489 | try: |
|
|||
1490 | prof = prof.runctx(arg_str,namespace,namespace) |
|
|||
1491 | sys_exit = '' |
|
|||
1492 | except SystemExit: |
|
|||
1493 | sys_exit = """*** SystemExit exception caught in code being profiled.""" |
|
|||
1494 |
|
||||
1495 | stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s) |
|
|||
1496 |
|
||||
1497 | lims = opts.l |
|
|||
1498 | if lims: |
|
|||
1499 | lims = [] # rebuild lims with ints/floats/strings |
|
|||
1500 | for lim in opts.l: |
|
|||
1501 | try: |
|
|||
1502 | lims.append(int(lim)) |
|
|||
1503 | except ValueError: |
|
|||
1504 | try: |
|
|||
1505 | lims.append(float(lim)) |
|
|||
1506 | except ValueError: |
|
|||
1507 | lims.append(lim) |
|
|||
1508 |
|
||||
1509 | # Trap output. |
|
|||
1510 | stdout_trap = StringIO() |
|
|||
1511 |
|
||||
1512 | if hasattr(stats,'stream'): |
|
|||
1513 | # In newer versions of python, the stats object has a 'stream' |
|
|||
1514 | # attribute to write into. |
|
|||
1515 | stats.stream = stdout_trap |
|
|||
1516 | stats.print_stats(*lims) |
|
|||
1517 | else: |
|
|||
1518 | # For older versions, we manually redirect stdout during printing |
|
|||
1519 | sys_stdout = sys.stdout |
|
|||
1520 | try: |
|
|||
1521 | sys.stdout = stdout_trap |
|
|||
1522 | stats.print_stats(*lims) |
|
|||
1523 | finally: |
|
|||
1524 | sys.stdout = sys_stdout |
|
|||
1525 |
|
||||
1526 | output = stdout_trap.getvalue() |
|
|||
1527 | output = output.rstrip() |
|
|||
1528 |
|
||||
1529 | if 'q' not in opts: |
|
|||
1530 | page.page(output) |
|
|||
1531 | print sys_exit, |
|
|||
1532 |
|
||||
1533 | dump_file = opts.D[0] |
|
|||
1534 | text_file = opts.T[0] |
|
|||
1535 | if dump_file: |
|
|||
1536 | dump_file = unquote_filename(dump_file) |
|
|||
1537 | prof.dump_stats(dump_file) |
|
|||
1538 | print '\n*** Profile stats marshalled to file',\ |
|
|||
1539 | `dump_file`+'.',sys_exit |
|
|||
1540 | if text_file: |
|
|||
1541 | text_file = unquote_filename(text_file) |
|
|||
1542 | pfile = open(text_file,'w') |
|
|||
1543 | pfile.write(output) |
|
|||
1544 | pfile.close() |
|
|||
1545 | print '\n*** Profile printout saved to text file',\ |
|
|||
1546 | `text_file`+'.',sys_exit |
|
|||
1547 |
|
||||
1548 | if opts.has_key('r'): |
|
|||
1549 | return stats |
|
|||
1550 | else: |
|
|||
1551 | return None |
|
|||
1552 |
|
||||
1553 | @skip_doctest |
|
|||
1554 | def magic_run(self, parameter_s ='', runner=None, |
|
|||
1555 | file_finder=get_py_filename): |
|
|||
1556 | """Run the named file inside IPython as a program. |
|
|||
1557 |
|
||||
1558 | Usage:\\ |
|
|||
1559 | %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args] |
|
|||
1560 |
|
||||
1561 | Parameters after the filename are passed as command-line arguments to |
|
|||
1562 | the program (put in sys.argv). Then, control returns to IPython's |
|
|||
1563 | prompt. |
|
|||
1564 |
|
||||
1565 | This is similar to running at a system prompt:\\ |
|
|||
1566 | $ python file args\\ |
|
|||
1567 | but with the advantage of giving you IPython's tracebacks, and of |
|
|||
1568 | loading all variables into your interactive namespace for further use |
|
|||
1569 | (unless -p is used, see below). |
|
|||
1570 |
|
||||
1571 | The file is executed in a namespace initially consisting only of |
|
|||
1572 | __name__=='__main__' and sys.argv constructed as indicated. It thus |
|
|||
1573 | sees its environment as if it were being run as a stand-alone program |
|
|||
1574 | (except for sharing global objects such as previously imported |
|
|||
1575 | modules). But after execution, the IPython interactive namespace gets |
|
|||
1576 | updated with all variables defined in the program (except for __name__ |
|
|||
1577 | and sys.argv). This allows for very convenient loading of code for |
|
|||
1578 | interactive work, while giving each program a 'clean sheet' to run in. |
|
|||
1579 |
|
||||
1580 | Options: |
|
|||
1581 |
|
||||
1582 | -n: __name__ is NOT set to '__main__', but to the running file's name |
|
|||
1583 | without extension (as python does under import). This allows running |
|
|||
1584 | scripts and reloading the definitions in them without calling code |
|
|||
1585 | protected by an ' if __name__ == "__main__" ' clause. |
|
|||
1586 |
|
||||
1587 | -i: run the file in IPython's namespace instead of an empty one. This |
|
|||
1588 | is useful if you are experimenting with code written in a text editor |
|
|||
1589 | which depends on variables defined interactively. |
|
|||
1590 |
|
||||
1591 | -e: ignore sys.exit() calls or SystemExit exceptions in the script |
|
|||
1592 | being run. This is particularly useful if IPython is being used to |
|
|||
1593 | run unittests, which always exit with a sys.exit() call. In such |
|
|||
1594 | cases you are interested in the output of the test results, not in |
|
|||
1595 | seeing a traceback of the unittest module. |
|
|||
1596 |
|
||||
1597 | -t: print timing information at the end of the run. IPython will give |
|
|||
1598 | you an estimated CPU time consumption for your script, which under |
|
|||
1599 | Unix uses the resource module to avoid the wraparound problems of |
|
|||
1600 | time.clock(). Under Unix, an estimate of time spent on system tasks |
|
|||
1601 | is also given (for Windows platforms this is reported as 0.0). |
|
|||
1602 |
|
||||
1603 | If -t is given, an additional -N<N> option can be given, where <N> |
|
|||
1604 | must be an integer indicating how many times you want the script to |
|
|||
1605 | run. The final timing report will include total and per run results. |
|
|||
1606 |
|
||||
1607 | For example (testing the script uniq_stable.py):: |
|
|||
1608 |
|
||||
1609 | In [1]: run -t uniq_stable |
|
|||
1610 |
|
||||
1611 | IPython CPU timings (estimated):\\ |
|
|||
1612 | User : 0.19597 s.\\ |
|
|||
1613 | System: 0.0 s.\\ |
|
|||
1614 |
|
||||
1615 | In [2]: run -t -N5 uniq_stable |
|
|||
1616 |
|
||||
1617 | IPython CPU timings (estimated):\\ |
|
|||
1618 | Total runs performed: 5\\ |
|
|||
1619 | Times : Total Per run\\ |
|
|||
1620 | User : 0.910862 s, 0.1821724 s.\\ |
|
|||
1621 | System: 0.0 s, 0.0 s. |
|
|||
1622 |
|
||||
1623 | -d: run your program under the control of pdb, the Python debugger. |
|
|||
1624 | This allows you to execute your program step by step, watch variables, |
|
|||
1625 | etc. Internally, what IPython does is similar to calling: |
|
|||
1626 |
|
||||
1627 | pdb.run('execfile("YOURFILENAME")') |
|
|||
1628 |
|
||||
1629 | with a breakpoint set on line 1 of your file. You can change the line |
|
|||
1630 | number for this automatic breakpoint to be <N> by using the -bN option |
|
|||
1631 | (where N must be an integer). For example:: |
|
|||
1632 |
|
||||
1633 | %run -d -b40 myscript |
|
|||
1634 |
|
||||
1635 | will set the first breakpoint at line 40 in myscript.py. Note that |
|
|||
1636 | the first breakpoint must be set on a line which actually does |
|
|||
1637 | something (not a comment or docstring) for it to stop execution. |
|
|||
1638 |
|
||||
1639 | When the pdb debugger starts, you will see a (Pdb) prompt. You must |
|
|||
1640 | first enter 'c' (without quotes) to start execution up to the first |
|
|||
1641 | breakpoint. |
|
|||
1642 |
|
||||
1643 | Entering 'help' gives information about the use of the debugger. You |
|
|||
1644 | can easily see pdb's full documentation with "import pdb;pdb.help()" |
|
|||
1645 | at a prompt. |
|
|||
1646 |
|
||||
1647 | -p: run program under the control of the Python profiler module (which |
|
|||
1648 | prints a detailed report of execution times, function calls, etc). |
|
|||
1649 |
|
||||
1650 | You can pass other options after -p which affect the behavior of the |
|
|||
1651 | profiler itself. See the docs for %prun for details. |
|
|||
1652 |
|
||||
1653 | In this mode, the program's variables do NOT propagate back to the |
|
|||
1654 | IPython interactive namespace (because they remain in the namespace |
|
|||
1655 | where the profiler executes them). |
|
|||
1656 |
|
||||
1657 | Internally this triggers a call to %prun, see its documentation for |
|
|||
1658 | details on the options available specifically for profiling. |
|
|||
1659 |
|
||||
1660 | There is one special usage for which the text above doesn't apply: |
|
|||
1661 | if the filename ends with .ipy, the file is run as ipython script, |
|
|||
1662 | just as if the commands were written on IPython prompt. |
|
|||
1663 |
|
||||
1664 | -m: specify module name to load instead of script path. Similar to |
|
|||
1665 | the -m option for the python interpreter. Use this option last if you |
|
|||
1666 | want to combine with other %run options. Unlike the python interpreter |
|
|||
1667 | only source modules are allowed no .pyc or .pyo files. |
|
|||
1668 | For example:: |
|
|||
1669 |
|
||||
1670 | %run -m example |
|
|||
1671 |
|
||||
1672 | will run the example module. |
|
|||
1673 |
|
||||
1674 | """ |
|
|||
1675 |
|
||||
1676 | # get arguments and set sys.argv for program to be run. |
|
|||
1677 | opts, arg_lst = self.parse_options(parameter_s, 'nidtN:b:pD:l:rs:T:em:', |
|
|||
1678 | mode='list', list_all=1) |
|
|||
1679 | if "m" in opts: |
|
|||
1680 | modulename = opts["m"][0] |
|
|||
1681 | modpath = find_mod(modulename) |
|
|||
1682 | if modpath is None: |
|
|||
1683 | warn('%r is not a valid modulename on sys.path'%modulename) |
|
|||
1684 | return |
|
|||
1685 | arg_lst = [modpath] + arg_lst |
|
|||
1686 | try: |
|
|||
1687 | filename = file_finder(arg_lst[0]) |
|
|||
1688 | except IndexError: |
|
|||
1689 | warn('you must provide at least a filename.') |
|
|||
1690 | print '\n%run:\n', oinspect.getdoc(self.magic_run) |
|
|||
1691 | return |
|
|||
1692 | except IOError as e: |
|
|||
1693 | try: |
|
|||
1694 | msg = str(e) |
|
|||
1695 | except UnicodeError: |
|
|||
1696 | msg = e.message |
|
|||
1697 | error(msg) |
|
|||
1698 | return |
|
|||
1699 |
|
||||
1700 | if filename.lower().endswith('.ipy'): |
|
|||
1701 | self.shell.safe_execfile_ipy(filename) |
|
|||
1702 | return |
|
|||
1703 |
|
||||
1704 | # Control the response to exit() calls made by the script being run |
|
|||
1705 | exit_ignore = 'e' in opts |
|
|||
1706 |
|
||||
1707 | # Make sure that the running script gets a proper sys.argv as if it |
|
|||
1708 | # were run from a system shell. |
|
|||
1709 | save_argv = sys.argv # save it for later restoring |
|
|||
1710 |
|
||||
1711 | # simulate shell expansion on arguments, at least tilde expansion |
|
|||
1712 | args = [ os.path.expanduser(a) for a in arg_lst[1:] ] |
|
|||
1713 |
|
||||
1714 | sys.argv = [filename] + args # put in the proper filename |
|
|||
1715 | # protect sys.argv from potential unicode strings on Python 2: |
|
|||
1716 | if not py3compat.PY3: |
|
|||
1717 | sys.argv = [ py3compat.cast_bytes(a) for a in sys.argv ] |
|
|||
1718 |
|
||||
1719 | if 'i' in opts: |
|
|||
1720 | # Run in user's interactive namespace |
|
|||
1721 | prog_ns = self.shell.user_ns |
|
|||
1722 | __name__save = self.shell.user_ns['__name__'] |
|
|||
1723 | prog_ns['__name__'] = '__main__' |
|
|||
1724 | main_mod = self.shell.new_main_mod(prog_ns) |
|
|||
1725 | else: |
|
|||
1726 | # Run in a fresh, empty namespace |
|
|||
1727 | if 'n' in opts: |
|
|||
1728 | name = os.path.splitext(os.path.basename(filename))[0] |
|
|||
1729 | else: |
|
|||
1730 | name = '__main__' |
|
|||
1731 |
|
||||
1732 | main_mod = self.shell.new_main_mod() |
|
|||
1733 | prog_ns = main_mod.__dict__ |
|
|||
1734 | prog_ns['__name__'] = name |
|
|||
1735 |
|
||||
1736 | # Since '%run foo' emulates 'python foo.py' at the cmd line, we must |
|
|||
1737 | # set the __file__ global in the script's namespace |
|
|||
1738 | prog_ns['__file__'] = filename |
|
|||
1739 |
|
||||
1740 | # pickle fix. See interactiveshell for an explanation. But we need to make sure |
|
|||
1741 | # that, if we overwrite __main__, we replace it at the end |
|
|||
1742 | main_mod_name = prog_ns['__name__'] |
|
|||
1743 |
|
||||
1744 | if main_mod_name == '__main__': |
|
|||
1745 | restore_main = sys.modules['__main__'] |
|
|||
1746 | else: |
|
|||
1747 | restore_main = False |
|
|||
1748 |
|
||||
1749 | # This needs to be undone at the end to prevent holding references to |
|
|||
1750 | # every single object ever created. |
|
|||
1751 | sys.modules[main_mod_name] = main_mod |
|
|||
1752 |
|
||||
1753 | try: |
|
|||
1754 | stats = None |
|
|||
1755 | with self.readline_no_record: |
|
|||
1756 | if 'p' in opts: |
|
|||
1757 | stats = self.magic_prun('', 0, opts, arg_lst, prog_ns) |
|
|||
1758 | else: |
|
|||
1759 | if 'd' in opts: |
|
|||
1760 | deb = debugger.Pdb(self.shell.colors) |
|
|||
1761 | # reset Breakpoint state, which is moronically kept |
|
|||
1762 | # in a class |
|
|||
1763 | bdb.Breakpoint.next = 1 |
|
|||
1764 | bdb.Breakpoint.bplist = {} |
|
|||
1765 | bdb.Breakpoint.bpbynumber = [None] |
|
|||
1766 | # Set an initial breakpoint to stop execution |
|
|||
1767 | maxtries = 10 |
|
|||
1768 | bp = int(opts.get('b', [1])[0]) |
|
|||
1769 | checkline = deb.checkline(filename, bp) |
|
|||
1770 | if not checkline: |
|
|||
1771 | for bp in range(bp + 1, bp + maxtries + 1): |
|
|||
1772 | if deb.checkline(filename, bp): |
|
|||
1773 | break |
|
|||
1774 | else: |
|
|||
1775 | msg = ("\nI failed to find a valid line to set " |
|
|||
1776 | "a breakpoint\n" |
|
|||
1777 | "after trying up to line: %s.\n" |
|
|||
1778 | "Please set a valid breakpoint manually " |
|
|||
1779 | "with the -b option." % bp) |
|
|||
1780 | error(msg) |
|
|||
1781 | return |
|
|||
1782 | # if we find a good linenumber, set the breakpoint |
|
|||
1783 | deb.do_break('%s:%s' % (filename, bp)) |
|
|||
1784 | # Start file run |
|
|||
1785 | print "NOTE: Enter 'c' at the", |
|
|||
1786 | print "%s prompt to start your script." % deb.prompt |
|
|||
1787 | ns = {'execfile': py3compat.execfile, 'prog_ns': prog_ns} |
|
|||
1788 | try: |
|
|||
1789 | deb.run('execfile("%s", prog_ns)' % filename, ns) |
|
|||
1790 |
|
||||
1791 | except: |
|
|||
1792 | etype, value, tb = sys.exc_info() |
|
|||
1793 | # Skip three frames in the traceback: the %run one, |
|
|||
1794 | # one inside bdb.py, and the command-line typed by the |
|
|||
1795 | # user (run by exec in pdb itself). |
|
|||
1796 | self.shell.InteractiveTB(etype, value, tb, tb_offset=3) |
|
|||
1797 | else: |
|
|||
1798 | if runner is None: |
|
|||
1799 | runner = self.shell.safe_execfile |
|
|||
1800 | if 't' in opts: |
|
|||
1801 | # timed execution |
|
|||
1802 | try: |
|
|||
1803 | nruns = int(opts['N'][0]) |
|
|||
1804 | if nruns < 1: |
|
|||
1805 | error('Number of runs must be >=1') |
|
|||
1806 | return |
|
|||
1807 | except (KeyError): |
|
|||
1808 | nruns = 1 |
|
|||
1809 | twall0 = time.time() |
|
|||
1810 | if nruns == 1: |
|
|||
1811 | t0 = clock2() |
|
|||
1812 | runner(filename, prog_ns, prog_ns, |
|
|||
1813 | exit_ignore=exit_ignore) |
|
|||
1814 | t1 = clock2() |
|
|||
1815 | t_usr = t1[0] - t0[0] |
|
|||
1816 | t_sys = t1[1] - t0[1] |
|
|||
1817 | print "\nIPython CPU timings (estimated):" |
|
|||
1818 | print " User : %10.2f s." % t_usr |
|
|||
1819 | print " System : %10.2f s." % t_sys |
|
|||
1820 | else: |
|
|||
1821 | runs = range(nruns) |
|
|||
1822 | t0 = clock2() |
|
|||
1823 | for nr in runs: |
|
|||
1824 | runner(filename, prog_ns, prog_ns, |
|
|||
1825 | exit_ignore=exit_ignore) |
|
|||
1826 | t1 = clock2() |
|
|||
1827 | t_usr = t1[0] - t0[0] |
|
|||
1828 | t_sys = t1[1] - t0[1] |
|
|||
1829 | print "\nIPython CPU timings (estimated):" |
|
|||
1830 | print "Total runs performed:", nruns |
|
|||
1831 | print " Times : %10.2f %10.2f" % ('Total', 'Per run') |
|
|||
1832 | print " User : %10.2f s, %10.2f s." % (t_usr, t_usr / nruns) |
|
|||
1833 | print " System : %10.2f s, %10.2f s." % (t_sys, t_sys / nruns) |
|
|||
1834 | twall1 = time.time() |
|
|||
1835 | print "Wall time: %10.2f s." % (twall1 - twall0) |
|
|||
1836 |
|
||||
1837 | else: |
|
|||
1838 | # regular execution |
|
|||
1839 | runner(filename, prog_ns, prog_ns, exit_ignore=exit_ignore) |
|
|||
1840 |
|
||||
1841 | if 'i' in opts: |
|
|||
1842 | self.shell.user_ns['__name__'] = __name__save |
|
|||
1843 | else: |
|
|||
1844 | # The shell MUST hold a reference to prog_ns so after %run |
|
|||
1845 | # exits, the python deletion mechanism doesn't zero it out |
|
|||
1846 | # (leaving dangling references). |
|
|||
1847 | self.shell.cache_main_mod(prog_ns, filename) |
|
|||
1848 | # update IPython interactive namespace |
|
|||
1849 |
|
||||
1850 | # Some forms of read errors on the file may mean the |
|
|||
1851 | # __name__ key was never set; using pop we don't have to |
|
|||
1852 | # worry about a possible KeyError. |
|
|||
1853 | prog_ns.pop('__name__', None) |
|
|||
1854 |
|
||||
1855 | self.shell.user_ns.update(prog_ns) |
|
|||
1856 | finally: |
|
|||
1857 | # It's a bit of a mystery why, but __builtins__ can change from |
|
|||
1858 | # being a module to becoming a dict missing some key data after |
|
|||
1859 | # %run. As best I can see, this is NOT something IPython is doing |
|
|||
1860 | # at all, and similar problems have been reported before: |
|
|||
1861 | # http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-10/0188.html |
|
|||
1862 | # Since this seems to be done by the interpreter itself, the best |
|
|||
1863 | # we can do is to at least restore __builtins__ for the user on |
|
|||
1864 | # exit. |
|
|||
1865 | self.shell.user_ns['__builtins__'] = builtin_mod |
|
|||
1866 |
|
||||
1867 | # Ensure key global structures are restored |
|
|||
1868 | sys.argv = save_argv |
|
|||
1869 | if restore_main: |
|
|||
1870 | sys.modules['__main__'] = restore_main |
|
|||
1871 | else: |
|
|||
1872 | # Remove from sys.modules the reference to main_mod we'd |
|
|||
1873 | # added. Otherwise it will trap references to objects |
|
|||
1874 | # contained therein. |
|
|||
1875 | del sys.modules[main_mod_name] |
|
|||
1876 |
|
||||
1877 | return stats |
|
|||
1878 |
|
||||
1879 | @skip_doctest |
|
|||
1880 | def magic_timeit(self, parameter_s =''): |
|
|||
1881 | """Time execution of a Python statement or expression |
|
|||
1882 |
|
||||
1883 | Usage:\\ |
|
|||
1884 | %timeit [-n<N> -r<R> [-t|-c]] statement |
|
|||
1885 |
|
||||
1886 | Time execution of a Python statement or expression using the timeit |
|
|||
1887 | module. |
|
|||
1888 |
|
||||
1889 | Options: |
|
|||
1890 | -n<N>: execute the given statement <N> times in a loop. If this value |
|
|||
1891 | is not given, a fitting value is chosen. |
|
|||
1892 |
|
||||
1893 | -r<R>: repeat the loop iteration <R> times and take the best result. |
|
|||
1894 | Default: 3 |
|
|||
1895 |
|
||||
1896 | -t: use time.time to measure the time, which is the default on Unix. |
|
|||
1897 | This function measures wall time. |
|
|||
1898 |
|
||||
1899 | -c: use time.clock to measure the time, which is the default on |
|
|||
1900 | Windows and measures wall time. On Unix, resource.getrusage is used |
|
|||
1901 | instead and returns the CPU user time. |
|
|||
1902 |
|
||||
1903 | -p<P>: use a precision of <P> digits to display the timing result. |
|
|||
1904 | Default: 3 |
|
|||
1905 |
|
||||
1906 |
|
||||
1907 | Examples |
|
|||
1908 | -------- |
|
|||
1909 | :: |
|
|||
1910 |
|
||||
1911 | In [1]: %timeit pass |
|
|||
1912 | 10000000 loops, best of 3: 53.3 ns per loop |
|
|||
1913 |
|
||||
1914 | In [2]: u = None |
|
|||
1915 |
|
||||
1916 | In [3]: %timeit u is None |
|
|||
1917 | 10000000 loops, best of 3: 184 ns per loop |
|
|||
1918 |
|
||||
1919 | In [4]: %timeit -r 4 u == None |
|
|||
1920 | 1000000 loops, best of 4: 242 ns per loop |
|
|||
1921 |
|
||||
1922 | In [5]: import time |
|
|||
1923 |
|
||||
1924 | In [6]: %timeit -n1 time.sleep(2) |
|
|||
1925 | 1 loops, best of 3: 2 s per loop |
|
|||
1926 |
|
||||
1927 |
|
||||
1928 | The times reported by %timeit will be slightly higher than those |
|
|||
1929 | reported by the timeit.py script when variables are accessed. This is |
|
|||
1930 | due to the fact that %timeit executes the statement in the namespace |
|
|||
1931 | of the shell, compared with timeit.py, which uses a single setup |
|
|||
1932 | statement to import function or create variables. Generally, the bias |
|
|||
1933 | does not matter as long as results from timeit.py are not mixed with |
|
|||
1934 | those from %timeit.""" |
|
|||
1935 |
|
||||
1936 | import timeit |
|
|||
1937 | import math |
|
|||
1938 |
|
||||
1939 | # XXX: Unfortunately the unicode 'micro' symbol can cause problems in |
|
|||
1940 | # certain terminals. Until we figure out a robust way of |
|
|||
1941 | # auto-detecting if the terminal can deal with it, use plain 'us' for |
|
|||
1942 | # microseconds. I am really NOT happy about disabling the proper |
|
|||
1943 | # 'micro' prefix, but crashing is worse... If anyone knows what the |
|
|||
1944 | # right solution for this is, I'm all ears... |
|
|||
1945 | # |
|
|||
1946 | # Note: using |
|
|||
1947 | # |
|
|||
1948 | # s = u'\xb5' |
|
|||
1949 | # s.encode(sys.getdefaultencoding()) |
|
|||
1950 | # |
|
|||
1951 | # is not sufficient, as I've seen terminals where that fails but |
|
|||
1952 | # print s |
|
|||
1953 | # |
|
|||
1954 | # succeeds |
|
|||
1955 | # |
|
|||
1956 | # See bug: https://bugs.launchpad.net/ipython/+bug/348466 |
|
|||
1957 |
|
||||
1958 | #units = [u"s", u"ms",u'\xb5',"ns"] |
|
|||
1959 | units = [u"s", u"ms",u'us',"ns"] |
|
|||
1960 |
|
||||
1961 | scaling = [1, 1e3, 1e6, 1e9] |
|
|||
1962 |
|
||||
1963 | opts, stmt = self.parse_options(parameter_s,'n:r:tcp:', |
|
|||
1964 | posix=False, strict=False) |
|
|||
1965 | if stmt == "": |
|
|||
1966 | return |
|
|||
1967 | timefunc = timeit.default_timer |
|
|||
1968 | number = int(getattr(opts, "n", 0)) |
|
|||
1969 | repeat = int(getattr(opts, "r", timeit.default_repeat)) |
|
|||
1970 | precision = int(getattr(opts, "p", 3)) |
|
|||
1971 | if hasattr(opts, "t"): |
|
|||
1972 | timefunc = time.time |
|
|||
1973 | if hasattr(opts, "c"): |
|
|||
1974 | timefunc = clock |
|
|||
1975 |
|
||||
1976 | timer = timeit.Timer(timer=timefunc) |
|
|||
1977 | # this code has tight coupling to the inner workings of timeit.Timer, |
|
|||
1978 | # but is there a better way to achieve that the code stmt has access |
|
|||
1979 | # to the shell namespace? |
|
|||
1980 |
|
||||
1981 | src = timeit.template % {'stmt': timeit.reindent(stmt, 8), |
|
|||
1982 | 'setup': "pass"} |
|
|||
1983 | # Track compilation time so it can be reported if too long |
|
|||
1984 | # Minimum time above which compilation time will be reported |
|
|||
1985 | tc_min = 0.1 |
|
|||
1986 |
|
||||
1987 | t0 = clock() |
|
|||
1988 | code = compile(src, "<magic-timeit>", "exec") |
|
|||
1989 | tc = clock()-t0 |
|
|||
1990 |
|
||||
1991 | ns = {} |
|
|||
1992 | exec code in self.shell.user_ns, ns |
|
|||
1993 | timer.inner = ns["inner"] |
|
|||
1994 |
|
||||
1995 | if number == 0: |
|
|||
1996 | # determine number so that 0.2 <= total time < 2.0 |
|
|||
1997 | number = 1 |
|
|||
1998 | for i in range(1, 10): |
|
|||
1999 | if timer.timeit(number) >= 0.2: |
|
|||
2000 | break |
|
|||
2001 | number *= 10 |
|
|||
2002 |
|
||||
2003 | best = min(timer.repeat(repeat, number)) / number |
|
|||
2004 |
|
||||
2005 | if best > 0.0 and best < 1000.0: |
|
|||
2006 | order = min(-int(math.floor(math.log10(best)) // 3), 3) |
|
|||
2007 | elif best >= 1000.0: |
|
|||
2008 | order = 0 |
|
|||
2009 | else: |
|
|||
2010 | order = 3 |
|
|||
2011 | print u"%d loops, best of %d: %.*g %s per loop" % (number, repeat, |
|
|||
2012 | precision, |
|
|||
2013 | best * scaling[order], |
|
|||
2014 | units[order]) |
|
|||
2015 | if tc > tc_min: |
|
|||
2016 | print "Compiler time: %.2f s" % tc |
|
|||
2017 |
|
||||
2018 | @skip_doctest |
|
|||
2019 | @needs_local_scope |
|
|||
2020 | def magic_time(self,parameter_s = ''): |
|
|||
2021 | """Time execution of a Python statement or expression. |
|
|||
2022 |
|
||||
2023 | The CPU and wall clock times are printed, and the value of the |
|
|||
2024 | expression (if any) is returned. Note that under Win32, system time |
|
|||
2025 | is always reported as 0, since it can not be measured. |
|
|||
2026 |
|
||||
2027 | This function provides very basic timing functionality. In Python |
|
|||
2028 | 2.3, the timeit module offers more control and sophistication, so this |
|
|||
2029 | could be rewritten to use it (patches welcome). |
|
|||
2030 |
|
||||
2031 | Examples |
|
|||
2032 | -------- |
|
|||
2033 | :: |
|
|||
2034 |
|
||||
2035 | In [1]: time 2**128 |
|
|||
2036 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
|
|||
2037 | Wall time: 0.00 |
|
|||
2038 | Out[1]: 340282366920938463463374607431768211456L |
|
|||
2039 |
|
||||
2040 | In [2]: n = 1000000 |
|
|||
2041 |
|
||||
2042 | In [3]: time sum(range(n)) |
|
|||
2043 | CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s |
|
|||
2044 | Wall time: 1.37 |
|
|||
2045 | Out[3]: 499999500000L |
|
|||
2046 |
|
||||
2047 | In [4]: time print 'hello world' |
|
|||
2048 | hello world |
|
|||
2049 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
|
|||
2050 | Wall time: 0.00 |
|
|||
2051 |
|
||||
2052 | Note that the time needed by Python to compile the given expression |
|
|||
2053 | will be reported if it is more than 0.1s. In this example, the |
|
|||
2054 | actual exponentiation is done by Python at compilation time, so while |
|
|||
2055 | the expression can take a noticeable amount of time to compute, that |
|
|||
2056 | time is purely due to the compilation: |
|
|||
2057 |
|
||||
2058 | In [5]: time 3**9999; |
|
|||
2059 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
|
|||
2060 | Wall time: 0.00 s |
|
|||
2061 |
|
||||
2062 | In [6]: time 3**999999; |
|
|||
2063 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
|
|||
2064 | Wall time: 0.00 s |
|
|||
2065 | Compiler : 0.78 s |
|
|||
2066 | """ |
|
|||
2067 |
|
||||
2068 | # fail immediately if the given expression can't be compiled |
|
|||
2069 |
|
||||
2070 | expr = self.shell.prefilter(parameter_s,False) |
|
|||
2071 |
|
||||
2072 | # Minimum time above which compilation time will be reported |
|
|||
2073 | tc_min = 0.1 |
|
|||
2074 |
|
||||
2075 | try: |
|
|||
2076 | mode = 'eval' |
|
|||
2077 | t0 = clock() |
|
|||
2078 | code = compile(expr,'<timed eval>',mode) |
|
|||
2079 | tc = clock()-t0 |
|
|||
2080 | except SyntaxError: |
|
|||
2081 | mode = 'exec' |
|
|||
2082 | t0 = clock() |
|
|||
2083 | code = compile(expr,'<timed exec>',mode) |
|
|||
2084 | tc = clock()-t0 |
|
|||
2085 | # skew measurement as little as possible |
|
|||
2086 | glob = self.shell.user_ns |
|
|||
2087 | locs = self._magic_locals |
|
|||
2088 | clk = clock2 |
|
|||
2089 | wtime = time.time |
|
|||
2090 | # time execution |
|
|||
2091 | wall_st = wtime() |
|
|||
2092 | if mode=='eval': |
|
|||
2093 | st = clk() |
|
|||
2094 | out = eval(code, glob, locs) |
|
|||
2095 | end = clk() |
|
|||
2096 | else: |
|
|||
2097 | st = clk() |
|
|||
2098 | exec code in glob, locs |
|
|||
2099 | end = clk() |
|
|||
2100 | out = None |
|
|||
2101 | wall_end = wtime() |
|
|||
2102 | # Compute actual times and report |
|
|||
2103 | wall_time = wall_end-wall_st |
|
|||
2104 | cpu_user = end[0]-st[0] |
|
|||
2105 | cpu_sys = end[1]-st[1] |
|
|||
2106 | cpu_tot = cpu_user+cpu_sys |
|
|||
2107 | print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \ |
|
|||
2108 | (cpu_user,cpu_sys,cpu_tot) |
|
|||
2109 | print "Wall time: %.2f s" % wall_time |
|
|||
2110 | if tc > tc_min: |
|
|||
2111 | print "Compiler : %.2f s" % tc |
|
|||
2112 | return out |
|
|||
2113 |
|
||||
2114 | @skip_doctest |
|
|||
2115 | def magic_macro(self,parameter_s = ''): |
|
|||
2116 | """Define a macro for future re-execution. It accepts ranges of history, |
|
|||
2117 | filenames or string objects. |
|
|||
2118 |
|
||||
2119 | Usage:\\ |
|
|||
2120 | %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ... |
|
|||
2121 |
|
||||
2122 | Options: |
|
|||
2123 |
|
||||
2124 | -r: use 'raw' input. By default, the 'processed' history is used, |
|
|||
2125 | so that magics are loaded in their transformed version to valid |
|
|||
2126 | Python. If this option is given, the raw input as typed as the |
|
|||
2127 | command line is used instead. |
|
|||
2128 |
|
||||
2129 | This will define a global variable called `name` which is a string |
|
|||
2130 | made of joining the slices and lines you specify (n1,n2,... numbers |
|
|||
2131 | above) from your input history into a single string. This variable |
|
|||
2132 | acts like an automatic function which re-executes those lines as if |
|
|||
2133 | you had typed them. You just type 'name' at the prompt and the code |
|
|||
2134 | executes. |
|
|||
2135 |
|
||||
2136 | The syntax for indicating input ranges is described in %history. |
|
|||
2137 |
|
||||
2138 | Note: as a 'hidden' feature, you can also use traditional python slice |
|
|||
2139 | notation, where N:M means numbers N through M-1. |
|
|||
2140 |
|
||||
2141 | For example, if your history contains (%hist prints it):: |
|
|||
2142 |
|
||||
2143 | 44: x=1 |
|
|||
2144 | 45: y=3 |
|
|||
2145 | 46: z=x+y |
|
|||
2146 | 47: print x |
|
|||
2147 | 48: a=5 |
|
|||
2148 | 49: print 'x',x,'y',y |
|
|||
2149 |
|
||||
2150 | you can create a macro with lines 44 through 47 (included) and line 49 |
|
|||
2151 | called my_macro with:: |
|
|||
2152 |
|
||||
2153 | In [55]: %macro my_macro 44-47 49 |
|
|||
2154 |
|
||||
2155 | Now, typing `my_macro` (without quotes) will re-execute all this code |
|
|||
2156 | in one pass. |
|
|||
2157 |
|
||||
2158 | You don't need to give the line-numbers in order, and any given line |
|
|||
2159 | number can appear multiple times. You can assemble macros with any |
|
|||
2160 | lines from your input history in any order. |
|
|||
2161 |
|
||||
2162 | The macro is a simple object which holds its value in an attribute, |
|
|||
2163 | but IPython's display system checks for macros and executes them as |
|
|||
2164 | code instead of printing them when you type their name. |
|
|||
2165 |
|
||||
2166 | You can view a macro's contents by explicitly printing it with:: |
|
|||
2167 |
|
||||
2168 | print macro_name |
|
|||
2169 |
|
||||
2170 | """ |
|
|||
2171 | opts,args = self.parse_options(parameter_s,'r',mode='list') |
|
|||
2172 | if not args: # List existing macros |
|
|||
2173 | return sorted(k for k,v in self.shell.user_ns.iteritems() if\ |
|
|||
2174 | isinstance(v, Macro)) |
|
|||
2175 | if len(args) == 1: |
|
|||
2176 | raise UsageError( |
|
|||
2177 | "%macro insufficient args; usage '%macro name n1-n2 n3-4...") |
|
|||
2178 | name, codefrom = args[0], " ".join(args[1:]) |
|
|||
2179 |
|
||||
2180 | #print 'rng',ranges # dbg |
|
|||
2181 | try: |
|
|||
2182 | lines = self.shell.find_user_code(codefrom, 'r' in opts) |
|
|||
2183 | except (ValueError, TypeError) as e: |
|
|||
2184 | print e.args[0] |
|
|||
2185 | return |
|
|||
2186 | macro = Macro(lines) |
|
|||
2187 | self.shell.define_macro(name, macro) |
|
|||
2188 | print 'Macro `%s` created. To execute, type its name (without quotes).' % name |
|
|||
2189 | print '=== Macro contents: ===' |
|
|||
2190 | print macro, |
|
|||
2191 |
|
||||
2192 | def magic_save(self,parameter_s = ''): |
|
|||
2193 | """Save a set of lines or a macro to a given filename. |
|
|||
2194 |
|
||||
2195 | Usage:\\ |
|
|||
2196 | %save [options] filename n1-n2 n3-n4 ... n5 .. n6 ... |
|
|||
2197 |
|
||||
2198 | Options: |
|
|||
2199 |
|
||||
2200 | -r: use 'raw' input. By default, the 'processed' history is used, |
|
|||
2201 | so that magics are loaded in their transformed version to valid |
|
|||
2202 | Python. If this option is given, the raw input as typed as the |
|
|||
2203 | command line is used instead. |
|
|||
2204 |
|
||||
2205 | This function uses the same syntax as %history for input ranges, |
|
|||
2206 | then saves the lines to the filename you specify. |
|
|||
2207 |
|
||||
2208 | It adds a '.py' extension to the file if you don't do so yourself, and |
|
|||
2209 | it asks for confirmation before overwriting existing files.""" |
|
|||
2210 |
|
||||
2211 | opts,args = self.parse_options(parameter_s,'r',mode='list') |
|
|||
2212 | fname, codefrom = unquote_filename(args[0]), " ".join(args[1:]) |
|
|||
2213 | if not fname.endswith('.py'): |
|
|||
2214 | fname += '.py' |
|
|||
2215 | if os.path.isfile(fname): |
|
|||
2216 | overwrite = self.shell.ask_yes_no('File `%s` exists. Overwrite (y/[N])? ' % fname, default='n') |
|
|||
2217 | if not overwrite : |
|
|||
2218 | print 'Operation cancelled.' |
|
|||
2219 | return |
|
|||
2220 | try: |
|
|||
2221 | cmds = self.shell.find_user_code(codefrom, 'r' in opts) |
|
|||
2222 | except (TypeError, ValueError) as e: |
|
|||
2223 | print e.args[0] |
|
|||
2224 | return |
|
|||
2225 | with io.open(fname,'w', encoding="utf-8") as f: |
|
|||
2226 | f.write(u"# coding: utf-8\n") |
|
|||
2227 | f.write(py3compat.cast_unicode(cmds)) |
|
|||
2228 | print 'The following commands were written to file `%s`:' % fname |
|
|||
2229 | print cmds |
|
|||
2230 |
|
||||
2231 | def magic_pastebin(self, parameter_s = ''): |
|
|||
2232 | """Upload code to Github's Gist paste bin, returning the URL. |
|
|||
2233 |
|
||||
2234 | Usage:\\ |
|
|||
2235 | %pastebin [-d "Custom description"] 1-7 |
|
|||
2236 |
|
||||
2237 | The argument can be an input history range, a filename, or the name of a |
|
|||
2238 | string or macro. |
|
|||
2239 |
|
||||
2240 | Options: |
|
|||
2241 |
|
||||
2242 | -d: Pass a custom description for the gist. The default will say |
|
|||
2243 | "Pasted from IPython". |
|
|||
2244 | """ |
|
|||
2245 | opts, args = self.parse_options(parameter_s, 'd:') |
|
|||
2246 |
|
||||
2247 | try: |
|
|||
2248 | code = self.shell.find_user_code(args) |
|
|||
2249 | except (ValueError, TypeError) as e: |
|
|||
2250 | print e.args[0] |
|
|||
2251 | return |
|
|||
2252 |
|
||||
2253 | post_data = json.dumps({ |
|
|||
2254 | "description": opts.get('d', "Pasted from IPython"), |
|
|||
2255 | "public": True, |
|
|||
2256 | "files": { |
|
|||
2257 | "file1.py": { |
|
|||
2258 | "content": code |
|
|||
2259 | } |
|
|||
2260 | } |
|
|||
2261 | }).encode('utf-8') |
|
|||
2262 |
|
||||
2263 | response = urlopen("https://api.github.com/gists", post_data) |
|
|||
2264 | response_data = json.loads(response.read().decode('utf-8')) |
|
|||
2265 | return response_data['html_url'] |
|
|||
2266 |
|
||||
2267 | def magic_loadpy(self, arg_s): |
|
|||
2268 | """Alias of `%load` |
|
|||
2269 |
|
||||
2270 | `%loadpy` has gained some flexibility and droped the requirement of a `.py` |
|
|||
2271 | extension. So it has been renamed simply into %load. You can look at |
|
|||
2272 | `%load`'s docstring for more info. |
|
|||
2273 | """ |
|
|||
2274 | self.magic_load(arg_s) |
|
|||
2275 |
|
||||
2276 | def magic_load(self, arg_s): |
|
|||
2277 | """Load code into the current frontend. |
|
|||
2278 |
|
||||
2279 | Usage:\\ |
|
|||
2280 | %load [options] source |
|
|||
2281 |
|
||||
2282 | where source can be a filename, URL, input history range or macro |
|
|||
2283 |
|
||||
2284 | Options: |
|
|||
2285 | -------- |
|
|||
2286 | -y : Don't ask confirmation for loading source above 200 000 characters. |
|
|||
2287 |
|
||||
2288 | This magic command can either take a local filename, a URL, an history |
|
|||
2289 | range (see %history) or a macro as argument, it will prompt for |
|
|||
2290 | confirmation before loading source with more than 200 000 characters, unless |
|
|||
2291 | -y flag is passed or if the frontend does not support raw_input:: |
|
|||
2292 |
|
||||
2293 | %load myscript.py |
|
|||
2294 | %load 7-27 |
|
|||
2295 | %load myMacro |
|
|||
2296 | %load http://www.example.com/myscript.py |
|
|||
2297 | """ |
|
|||
2298 | opts,args = self.parse_options(arg_s,'y') |
|
|||
2299 |
|
||||
2300 | contents = self.shell.find_user_code(args) |
|
|||
2301 | l = len(contents) |
|
|||
2302 |
|
||||
2303 | # 200 000 is ~ 2500 full 80 caracter lines |
|
|||
2304 | # so in average, more than 5000 lines |
|
|||
2305 | if l > 200000 and 'y' not in opts: |
|
|||
2306 | try: |
|
|||
2307 | ans = self.shell.ask_yes_no(("The text you're trying to load seems pretty big"\ |
|
|||
2308 | " (%d characters). Continue (y/[N]) ?" % l), default='n' ) |
|
|||
2309 | except StdinNotImplementedError: |
|
|||
2310 | #asume yes if raw input not implemented |
|
|||
2311 | ans = True |
|
|||
2312 |
|
||||
2313 | if ans is False : |
|
|||
2314 | print 'Operation cancelled.' |
|
|||
2315 | return |
|
|||
2316 |
|
||||
2317 | self.set_next_input(contents) |
|
|||
2318 |
|
||||
2319 | def _find_edit_target(self, args, opts, last_call): |
|
|||
2320 | """Utility method used by magic_edit to find what to edit.""" |
|
|||
2321 |
|
||||
2322 | def make_filename(arg): |
|
|||
2323 | "Make a filename from the given args" |
|
|||
2324 | arg = unquote_filename(arg) |
|
|||
2325 | try: |
|
|||
2326 | filename = get_py_filename(arg) |
|
|||
2327 | except IOError: |
|
|||
2328 | # If it ends with .py but doesn't already exist, assume we want |
|
|||
2329 | # a new file. |
|
|||
2330 | if arg.endswith('.py'): |
|
|||
2331 | filename = arg |
|
|||
2332 | else: |
|
|||
2333 | filename = None |
|
|||
2334 | return filename |
|
|||
2335 |
|
||||
2336 | # Set a few locals from the options for convenience: |
|
|||
2337 | opts_prev = 'p' in opts |
|
|||
2338 | opts_raw = 'r' in opts |
|
|||
2339 |
|
||||
2340 | # custom exceptions |
|
|||
2341 | class DataIsObject(Exception): pass |
|
|||
2342 |
|
||||
2343 | # Default line number value |
|
|||
2344 | lineno = opts.get('n',None) |
|
|||
2345 |
|
||||
2346 | if opts_prev: |
|
|||
2347 | args = '_%s' % last_call[0] |
|
|||
2348 | if not self.shell.user_ns.has_key(args): |
|
|||
2349 | args = last_call[1] |
|
|||
2350 |
|
||||
2351 | # use last_call to remember the state of the previous call, but don't |
|
|||
2352 | # let it be clobbered by successive '-p' calls. |
|
|||
2353 | try: |
|
|||
2354 | last_call[0] = self.shell.displayhook.prompt_count |
|
|||
2355 | if not opts_prev: |
|
|||
2356 | last_call[1] = args |
|
|||
2357 | except: |
|
|||
2358 | pass |
|
|||
2359 |
|
||||
2360 | # by default this is done with temp files, except when the given |
|
|||
2361 | # arg is a filename |
|
|||
2362 | use_temp = True |
|
|||
2363 |
|
||||
2364 | data = '' |
|
|||
2365 |
|
||||
2366 | # First, see if the arguments should be a filename. |
|
|||
2367 | filename = make_filename(args) |
|
|||
2368 | if filename: |
|
|||
2369 | use_temp = False |
|
|||
2370 | elif args: |
|
|||
2371 | # Mode where user specifies ranges of lines, like in %macro. |
|
|||
2372 | data = self.extract_input_lines(args, opts_raw) |
|
|||
2373 | if not data: |
|
|||
2374 | try: |
|
|||
2375 | # Load the parameter given as a variable. If not a string, |
|
|||
2376 | # process it as an object instead (below) |
|
|||
2377 |
|
||||
2378 | #print '*** args',args,'type',type(args) # dbg |
|
|||
2379 | data = eval(args, self.shell.user_ns) |
|
|||
2380 | if not isinstance(data, basestring): |
|
|||
2381 | raise DataIsObject |
|
|||
2382 |
|
||||
2383 | except (NameError,SyntaxError): |
|
|||
2384 | # given argument is not a variable, try as a filename |
|
|||
2385 | filename = make_filename(args) |
|
|||
2386 | if filename is None: |
|
|||
2387 | warn("Argument given (%s) can't be found as a variable " |
|
|||
2388 | "or as a filename." % args) |
|
|||
2389 | return |
|
|||
2390 | use_temp = False |
|
|||
2391 |
|
||||
2392 | except DataIsObject: |
|
|||
2393 | # macros have a special edit function |
|
|||
2394 | if isinstance(data, Macro): |
|
|||
2395 | raise MacroToEdit(data) |
|
|||
2396 |
|
||||
2397 | # For objects, try to edit the file where they are defined |
|
|||
2398 | try: |
|
|||
2399 | filename = inspect.getabsfile(data) |
|
|||
2400 | if 'fakemodule' in filename.lower() and inspect.isclass(data): |
|
|||
2401 | # class created by %edit? Try to find source |
|
|||
2402 | # by looking for method definitions instead, the |
|
|||
2403 | # __module__ in those classes is FakeModule. |
|
|||
2404 | attrs = [getattr(data, aname) for aname in dir(data)] |
|
|||
2405 | for attr in attrs: |
|
|||
2406 | if not inspect.ismethod(attr): |
|
|||
2407 | continue |
|
|||
2408 | filename = inspect.getabsfile(attr) |
|
|||
2409 | if filename and 'fakemodule' not in filename.lower(): |
|
|||
2410 | # change the attribute to be the edit target instead |
|
|||
2411 | data = attr |
|
|||
2412 | break |
|
|||
2413 |
|
||||
2414 | datafile = 1 |
|
|||
2415 | except TypeError: |
|
|||
2416 | filename = make_filename(args) |
|
|||
2417 | datafile = 1 |
|
|||
2418 | warn('Could not find file where `%s` is defined.\n' |
|
|||
2419 | 'Opening a file named `%s`' % (args,filename)) |
|
|||
2420 | # Now, make sure we can actually read the source (if it was in |
|
|||
2421 | # a temp file it's gone by now). |
|
|||
2422 | if datafile: |
|
|||
2423 | try: |
|
|||
2424 | if lineno is None: |
|
|||
2425 | lineno = inspect.getsourcelines(data)[1] |
|
|||
2426 | except IOError: |
|
|||
2427 | filename = make_filename(args) |
|
|||
2428 | if filename is None: |
|
|||
2429 | warn('The file `%s` where `%s` was defined cannot ' |
|
|||
2430 | 'be read.' % (filename,data)) |
|
|||
2431 | return |
|
|||
2432 | use_temp = False |
|
|||
2433 |
|
||||
2434 | if use_temp: |
|
|||
2435 | filename = self.shell.mktempfile(data) |
|
|||
2436 | print 'IPython will make a temporary file named:',filename |
|
|||
2437 |
|
||||
2438 | return filename, lineno, use_temp |
|
|||
2439 |
|
||||
2440 | def _edit_macro(self,mname,macro): |
|
|||
2441 | """open an editor with the macro data in a file""" |
|
|||
2442 | filename = self.shell.mktempfile(macro.value) |
|
|||
2443 | self.shell.hooks.editor(filename) |
|
|||
2444 |
|
||||
2445 | # and make a new macro object, to replace the old one |
|
|||
2446 | mfile = open(filename) |
|
|||
2447 | mvalue = mfile.read() |
|
|||
2448 | mfile.close() |
|
|||
2449 | self.shell.user_ns[mname] = Macro(mvalue) |
|
|||
2450 |
|
||||
2451 | def magic_ed(self,parameter_s=''): |
|
|||
2452 | """Alias to %edit.""" |
|
|||
2453 | return self.magic_edit(parameter_s) |
|
|||
2454 |
|
||||
2455 | @skip_doctest |
|
|||
2456 | def magic_edit(self,parameter_s='',last_call=['','']): |
|
|||
2457 | """Bring up an editor and execute the resulting code. |
|
|||
2458 |
|
||||
2459 | Usage: |
|
|||
2460 | %edit [options] [args] |
|
|||
2461 |
|
||||
2462 | %edit runs IPython's editor hook. The default version of this hook is |
|
|||
2463 | set to call the editor specified by your $EDITOR environment variable. |
|
|||
2464 | If this isn't found, it will default to vi under Linux/Unix and to |
|
|||
2465 | notepad under Windows. See the end of this docstring for how to change |
|
|||
2466 | the editor hook. |
|
|||
2467 |
|
||||
2468 | You can also set the value of this editor via the |
|
|||
2469 | ``TerminalInteractiveShell.editor`` option in your configuration file. |
|
|||
2470 | This is useful if you wish to use a different editor from your typical |
|
|||
2471 | default with IPython (and for Windows users who typically don't set |
|
|||
2472 | environment variables). |
|
|||
2473 |
|
||||
2474 | This command allows you to conveniently edit multi-line code right in |
|
|||
2475 | your IPython session. |
|
|||
2476 |
|
||||
2477 | If called without arguments, %edit opens up an empty editor with a |
|
|||
2478 | temporary file and will execute the contents of this file when you |
|
|||
2479 | close it (don't forget to save it!). |
|
|||
2480 |
|
||||
2481 |
|
||||
2482 | Options: |
|
|||
2483 |
|
||||
2484 | -n <number>: open the editor at a specified line number. By default, |
|
|||
2485 | the IPython editor hook uses the unix syntax 'editor +N filename', but |
|
|||
2486 | you can configure this by providing your own modified hook if your |
|
|||
2487 | favorite editor supports line-number specifications with a different |
|
|||
2488 | syntax. |
|
|||
2489 |
|
||||
2490 | -p: this will call the editor with the same data as the previous time |
|
|||
2491 | it was used, regardless of how long ago (in your current session) it |
|
|||
2492 | was. |
|
|||
2493 |
|
||||
2494 | -r: use 'raw' input. This option only applies to input taken from the |
|
|||
2495 | user's history. By default, the 'processed' history is used, so that |
|
|||
2496 | magics are loaded in their transformed version to valid Python. If |
|
|||
2497 | this option is given, the raw input as typed as the command line is |
|
|||
2498 | used instead. When you exit the editor, it will be executed by |
|
|||
2499 | IPython's own processor. |
|
|||
2500 |
|
||||
2501 | -x: do not execute the edited code immediately upon exit. This is |
|
|||
2502 | mainly useful if you are editing programs which need to be called with |
|
|||
2503 | command line arguments, which you can then do using %run. |
|
|||
2504 |
|
||||
2505 |
|
||||
2506 | Arguments: |
|
|||
2507 |
|
||||
2508 | If arguments are given, the following possibilities exist: |
|
|||
2509 |
|
||||
2510 | - If the argument is a filename, IPython will load that into the |
|
|||
2511 | editor. It will execute its contents with execfile() when you exit, |
|
|||
2512 | loading any code in the file into your interactive namespace. |
|
|||
2513 |
|
||||
2514 | - The arguments are ranges of input history, e.g. "7 ~1/4-6". |
|
|||
2515 | The syntax is the same as in the %history magic. |
|
|||
2516 |
|
||||
2517 | - If the argument is a string variable, its contents are loaded |
|
|||
2518 | into the editor. You can thus edit any string which contains |
|
|||
2519 | python code (including the result of previous edits). |
|
|||
2520 |
|
||||
2521 | - If the argument is the name of an object (other than a string), |
|
|||
2522 | IPython will try to locate the file where it was defined and open the |
|
|||
2523 | editor at the point where it is defined. You can use `%edit function` |
|
|||
2524 | to load an editor exactly at the point where 'function' is defined, |
|
|||
2525 | edit it and have the file be executed automatically. |
|
|||
2526 |
|
||||
2527 | - If the object is a macro (see %macro for details), this opens up your |
|
|||
2528 | specified editor with a temporary file containing the macro's data. |
|
|||
2529 | Upon exit, the macro is reloaded with the contents of the file. |
|
|||
2530 |
|
||||
2531 | Note: opening at an exact line is only supported under Unix, and some |
|
|||
2532 | editors (like kedit and gedit up to Gnome 2.8) do not understand the |
|
|||
2533 | '+NUMBER' parameter necessary for this feature. Good editors like |
|
|||
2534 | (X)Emacs, vi, jed, pico and joe all do. |
|
|||
2535 |
|
||||
2536 | After executing your code, %edit will return as output the code you |
|
|||
2537 | typed in the editor (except when it was an existing file). This way |
|
|||
2538 | you can reload the code in further invocations of %edit as a variable, |
|
|||
2539 | via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of |
|
|||
2540 | the output. |
|
|||
2541 |
|
||||
2542 | Note that %edit is also available through the alias %ed. |
|
|||
2543 |
|
||||
2544 | This is an example of creating a simple function inside the editor and |
|
|||
2545 | then modifying it. First, start up the editor:: |
|
|||
2546 |
|
||||
2547 | In [1]: ed |
|
|||
2548 | Editing... done. Executing edited code... |
|
|||
2549 | Out[1]: 'def foo():\\n print "foo() was defined in an editing |
|
|||
2550 | session"\\n' |
|
|||
2551 |
|
||||
2552 | We can then call the function foo():: |
|
|||
2553 |
|
||||
2554 | In [2]: foo() |
|
|||
2555 | foo() was defined in an editing session |
|
|||
2556 |
|
||||
2557 | Now we edit foo. IPython automatically loads the editor with the |
|
|||
2558 | (temporary) file where foo() was previously defined:: |
|
|||
2559 |
|
||||
2560 | In [3]: ed foo |
|
|||
2561 | Editing... done. Executing edited code... |
|
|||
2562 |
|
||||
2563 | And if we call foo() again we get the modified version:: |
|
|||
2564 |
|
||||
2565 | In [4]: foo() |
|
|||
2566 | foo() has now been changed! |
|
|||
2567 |
|
||||
2568 | Here is an example of how to edit a code snippet successive |
|
|||
2569 | times. First we call the editor:: |
|
|||
2570 |
|
||||
2571 | In [5]: ed |
|
|||
2572 | Editing... done. Executing edited code... |
|
|||
2573 | hello |
|
|||
2574 | Out[5]: "print 'hello'\\n" |
|
|||
2575 |
|
||||
2576 | Now we call it again with the previous output (stored in _):: |
|
|||
2577 |
|
||||
2578 | In [6]: ed _ |
|
|||
2579 | Editing... done. Executing edited code... |
|
|||
2580 | hello world |
|
|||
2581 | Out[6]: "print 'hello world'\\n" |
|
|||
2582 |
|
||||
2583 | Now we call it with the output #8 (stored in _8, also as Out[8]):: |
|
|||
2584 |
|
||||
2585 | In [7]: ed _8 |
|
|||
2586 | Editing... done. Executing edited code... |
|
|||
2587 | hello again |
|
|||
2588 | Out[7]: "print 'hello again'\\n" |
|
|||
2589 |
|
||||
2590 |
|
||||
2591 | Changing the default editor hook: |
|
|||
2592 |
|
||||
2593 | If you wish to write your own editor hook, you can put it in a |
|
|||
2594 | configuration file which you load at startup time. The default hook |
|
|||
2595 | is defined in the IPython.core.hooks module, and you can use that as a |
|
|||
2596 | starting example for further modifications. That file also has |
|
|||
2597 | general instructions on how to set a new hook for use once you've |
|
|||
2598 | defined it.""" |
|
|||
2599 | opts,args = self.parse_options(parameter_s,'prxn:') |
|
|||
2600 |
|
||||
2601 | try: |
|
|||
2602 | filename, lineno, is_temp = self._find_edit_target(args, opts, last_call) |
|
|||
2603 | except MacroToEdit as e: |
|
|||
2604 | self._edit_macro(args, e.args[0]) |
|
|||
2605 | return |
|
|||
2606 |
|
||||
2607 | # do actual editing here |
|
|||
2608 | print 'Editing...', |
|
|||
2609 | sys.stdout.flush() |
|
|||
2610 | try: |
|
|||
2611 | # Quote filenames that may have spaces in them |
|
|||
2612 | if ' ' in filename: |
|
|||
2613 | filename = "'%s'" % filename |
|
|||
2614 | self.shell.hooks.editor(filename,lineno) |
|
|||
2615 | except TryNext: |
|
|||
2616 | warn('Could not open editor') |
|
|||
2617 | return |
|
|||
2618 |
|
||||
2619 | # XXX TODO: should this be generalized for all string vars? |
|
|||
2620 | # For now, this is special-cased to blocks created by cpaste |
|
|||
2621 | if args.strip() == 'pasted_block': |
|
|||
2622 | self.shell.user_ns['pasted_block'] = file_read(filename) |
|
|||
2623 |
|
||||
2624 | if 'x' in opts: # -x prevents actual execution |
|
|||
2625 |
|
||||
2626 | else: |
|
|||
2627 | print 'done. Executing edited code...' |
|
|||
2628 | if 'r' in opts: # Untranslated IPython code |
|
|||
2629 | self.shell.run_cell(file_read(filename), |
|
|||
2630 | store_history=False) |
|
|||
2631 | else: |
|
|||
2632 | self.shell.safe_execfile(filename,self.shell.user_ns, |
|
|||
2633 | self.shell.user_ns) |
|
|||
2634 |
|
||||
2635 | if is_temp: |
|
|||
2636 | try: |
|
|||
2637 | return open(filename).read() |
|
|||
2638 | except IOError,msg: |
|
|||
2639 | if msg.filename == filename: |
|
|||
2640 | warn('File not found. Did you forget to save?') |
|
|||
2641 | return |
|
|||
2642 | else: |
|
|||
2643 | self.shell.showtraceback() |
|
|||
2644 |
|
||||
2645 | def magic_xmode(self,parameter_s = ''): |
|
|||
2646 | """Switch modes for the exception handlers. |
|
|||
2647 |
|
||||
2648 | Valid modes: Plain, Context and Verbose. |
|
|||
2649 |
|
||||
2650 | If called without arguments, acts as a toggle.""" |
|
|||
2651 |
|
||||
2652 | def xmode_switch_err(name): |
|
|||
2653 | warn('Error changing %s exception modes.\n%s' % |
|
|||
2654 | (name,sys.exc_info()[1])) |
|
|||
2655 |
|
||||
2656 | shell = self.shell |
|
|||
2657 | new_mode = parameter_s.strip().capitalize() |
|
|||
2658 | try: |
|
|||
2659 | shell.InteractiveTB.set_mode(mode=new_mode) |
|
|||
2660 | print 'Exception reporting mode:',shell.InteractiveTB.mode |
|
|||
2661 | except: |
|
|||
2662 | xmode_switch_err('user') |
|
|||
2663 |
|
||||
2664 | def magic_colors(self,parameter_s = ''): |
|
|||
2665 | """Switch color scheme for prompts, info system and exception handlers. |
|
|||
2666 |
|
||||
2667 | Currently implemented schemes: NoColor, Linux, LightBG. |
|
|||
2668 |
|
||||
2669 | Color scheme names are not case-sensitive. |
|
|||
2670 |
|
||||
2671 | Examples |
|
|||
2672 | -------- |
|
|||
2673 | To get a plain black and white terminal:: |
|
|||
2674 |
|
||||
2675 | %colors nocolor |
|
|||
2676 | """ |
|
|||
2677 |
|
||||
2678 | def color_switch_err(name): |
|
|||
2679 | warn('Error changing %s color schemes.\n%s' % |
|
|||
2680 | (name,sys.exc_info()[1])) |
|
|||
2681 |
|
||||
2682 |
|
||||
2683 | new_scheme = parameter_s.strip() |
|
|||
2684 | if not new_scheme: |
|
|||
2685 | raise UsageError( |
|
|||
2686 | "%colors: you must specify a color scheme. See '%colors?'") |
|
|||
2687 | return |
|
|||
2688 | # local shortcut |
|
|||
2689 | shell = self.shell |
|
|||
2690 |
|
||||
2691 | import IPython.utils.rlineimpl as readline |
|
|||
2692 |
|
||||
2693 | if not shell.colors_force and \ |
|
|||
2694 | not readline.have_readline and sys.platform == "win32": |
|
|||
2695 | msg = """\ |
|
|||
2696 | Proper color support under MS Windows requires the pyreadline library. |
|
|||
2697 | You can find it at: |
|
|||
2698 | http://ipython.org/pyreadline.html |
|
|||
2699 | Gary's readline needs the ctypes module, from: |
|
|||
2700 | http://starship.python.net/crew/theller/ctypes |
|
|||
2701 | (Note that ctypes is already part of Python versions 2.5 and newer). |
|
|||
2702 |
|
||||
2703 | Defaulting color scheme to 'NoColor'""" |
|
|||
2704 | new_scheme = 'NoColor' |
|
|||
2705 | warn(msg) |
|
|||
2706 |
|
||||
2707 | # readline option is 0 |
|
|||
2708 | if not shell.colors_force and not shell.has_readline: |
|
|||
2709 | new_scheme = 'NoColor' |
|
|||
2710 |
|
||||
2711 | # Set prompt colors |
|
|||
2712 | try: |
|
|||
2713 | shell.prompt_manager.color_scheme = new_scheme |
|
|||
2714 | except: |
|
|||
2715 | color_switch_err('prompt') |
|
|||
2716 | else: |
|
|||
2717 | shell.colors = \ |
|
|||
2718 | shell.prompt_manager.color_scheme_table.active_scheme_name |
|
|||
2719 | # Set exception colors |
|
|||
2720 | try: |
|
|||
2721 | shell.InteractiveTB.set_colors(scheme = new_scheme) |
|
|||
2722 | shell.SyntaxTB.set_colors(scheme = new_scheme) |
|
|||
2723 | except: |
|
|||
2724 | color_switch_err('exception') |
|
|||
2725 |
|
||||
2726 | # Set info (for 'object?') colors |
|
|||
2727 | if shell.color_info: |
|
|||
2728 | try: |
|
|||
2729 | shell.inspector.set_active_scheme(new_scheme) |
|
|||
2730 | except: |
|
|||
2731 | color_switch_err('object inspector') |
|
|||
2732 | else: |
|
|||
2733 | shell.inspector.set_active_scheme('NoColor') |
|
|||
2734 |
|
||||
2735 | def magic_pprint(self, parameter_s=''): |
|
|||
2736 | """Toggle pretty printing on/off.""" |
|
|||
2737 | ptformatter = self.shell.display_formatter.formatters['text/plain'] |
|
|||
2738 | ptformatter.pprint = bool(1 - ptformatter.pprint) |
|
|||
2739 | print 'Pretty printing has been turned', \ |
|
|||
2740 | ['OFF','ON'][ptformatter.pprint] |
|
|||
2741 |
|
||||
2742 | #...................................................................... |
|
|||
2743 | # Functions to implement unix shell-type things |
|
|||
2744 |
|
||||
2745 | @skip_doctest |
|
|||
2746 | def magic_alias(self, parameter_s = ''): |
|
|||
2747 | """Define an alias for a system command. |
|
|||
2748 |
|
||||
2749 | '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd' |
|
|||
2750 |
|
||||
2751 | Then, typing 'alias_name params' will execute the system command 'cmd |
|
|||
2752 | params' (from your underlying operating system). |
|
|||
2753 |
|
||||
2754 | Aliases have lower precedence than magic functions and Python normal |
|
|||
2755 | variables, so if 'foo' is both a Python variable and an alias, the |
|
|||
2756 | alias can not be executed until 'del foo' removes the Python variable. |
|
|||
2757 |
|
||||
2758 | You can use the %l specifier in an alias definition to represent the |
|
|||
2759 | whole line when the alias is called. For example:: |
|
|||
2760 |
|
||||
2761 | In [2]: alias bracket echo "Input in brackets: <%l>" |
|
|||
2762 | In [3]: bracket hello world |
|
|||
2763 | Input in brackets: <hello world> |
|
|||
2764 |
|
||||
2765 | You can also define aliases with parameters using %s specifiers (one |
|
|||
2766 | per parameter):: |
|
|||
2767 |
|
||||
2768 | In [1]: alias parts echo first %s second %s |
|
|||
2769 | In [2]: %parts A B |
|
|||
2770 | first A second B |
|
|||
2771 | In [3]: %parts A |
|
|||
2772 | Incorrect number of arguments: 2 expected. |
|
|||
2773 | parts is an alias to: 'echo first %s second %s' |
|
|||
2774 |
|
||||
2775 | Note that %l and %s are mutually exclusive. You can only use one or |
|
|||
2776 | the other in your aliases. |
|
|||
2777 |
|
||||
2778 | Aliases expand Python variables just like system calls using ! or !! |
|
|||
2779 | do: all expressions prefixed with '$' get expanded. For details of |
|
|||
2780 | the semantic rules, see PEP-215: |
|
|||
2781 | http://www.python.org/peps/pep-0215.html. This is the library used by |
|
|||
2782 | IPython for variable expansion. If you want to access a true shell |
|
|||
2783 | variable, an extra $ is necessary to prevent its expansion by |
|
|||
2784 | IPython:: |
|
|||
2785 |
|
||||
2786 | In [6]: alias show echo |
|
|||
2787 | In [7]: PATH='A Python string' |
|
|||
2788 | In [8]: show $PATH |
|
|||
2789 | A Python string |
|
|||
2790 | In [9]: show $$PATH |
|
|||
2791 | /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:... |
|
|||
2792 |
|
||||
2793 | You can use the alias facility to acess all of $PATH. See the %rehash |
|
|||
2794 | and %rehashx functions, which automatically create aliases for the |
|
|||
2795 | contents of your $PATH. |
|
|||
2796 |
|
||||
2797 | If called with no parameters, %alias prints the current alias table.""" |
|
|||
2798 |
|
||||
2799 | par = parameter_s.strip() |
|
|||
2800 | if not par: |
|
|||
2801 | stored = self.db.get('stored_aliases', {} ) |
|
|||
2802 | aliases = sorted(self.shell.alias_manager.aliases) |
|
|||
2803 | # for k, v in stored: |
|
|||
2804 | # atab.append(k, v[0]) |
|
|||
2805 |
|
||||
2806 | print "Total number of aliases:", len(aliases) |
|
|||
2807 | sys.stdout.flush() |
|
|||
2808 | return aliases |
|
|||
2809 |
|
||||
2810 | # Now try to define a new one |
|
|||
2811 | try: |
|
|||
2812 | alias,cmd = par.split(None, 1) |
|
|||
2813 | except: |
|
|||
2814 | print oinspect.getdoc(self.magic_alias) |
|
|||
2815 | else: |
|
|||
2816 | self.shell.alias_manager.soft_define_alias(alias, cmd) |
|
|||
2817 | # end magic_alias |
|
|||
2818 |
|
||||
2819 | def magic_unalias(self, parameter_s = ''): |
|
|||
2820 | """Remove an alias""" |
|
|||
2821 |
|
||||
2822 | aname = parameter_s.strip() |
|
|||
2823 | self.shell.alias_manager.undefine_alias(aname) |
|
|||
2824 | stored = self.db.get('stored_aliases', {} ) |
|
|||
2825 | if aname in stored: |
|
|||
2826 | print "Removing %stored alias",aname |
|
|||
2827 | del stored[aname] |
|
|||
2828 | self.db['stored_aliases'] = stored |
|
|||
2829 |
|
||||
2830 | def magic_rehashx(self, parameter_s = ''): |
|
|||
2831 | """Update the alias table with all executable files in $PATH. |
|
|||
2832 |
|
||||
2833 | This version explicitly checks that every entry in $PATH is a file |
|
|||
2834 | with execute access (os.X_OK), so it is much slower than %rehash. |
|
|||
2835 |
|
||||
2836 | Under Windows, it checks executability as a match against a |
|
|||
2837 | '|'-separated string of extensions, stored in the IPython config |
|
|||
2838 | variable win_exec_ext. This defaults to 'exe|com|bat'. |
|
|||
2839 |
|
||||
2840 | This function also resets the root module cache of module completer, |
|
|||
2841 | used on slow filesystems. |
|
|||
2842 | """ |
|
|||
2843 | from IPython.core.alias import InvalidAliasError |
|
|||
2844 |
|
||||
2845 | # for the benefit of module completer in ipy_completers.py |
|
|||
2846 | del self.shell.db['rootmodules'] |
|
|||
2847 |
|
||||
2848 | path = [os.path.abspath(os.path.expanduser(p)) for p in |
|
|||
2849 | os.environ.get('PATH','').split(os.pathsep)] |
|
|||
2850 | path = filter(os.path.isdir,path) |
|
|||
2851 |
|
||||
2852 | syscmdlist = [] |
|
|||
2853 | # Now define isexec in a cross platform manner. |
|
|||
2854 | if os.name == 'posix': |
|
|||
2855 | isexec = lambda fname:os.path.isfile(fname) and \ |
|
|||
2856 | os.access(fname,os.X_OK) |
|
|||
2857 | else: |
|
|||
2858 | try: |
|
|||
2859 | winext = os.environ['pathext'].replace(';','|').replace('.','') |
|
|||
2860 | except KeyError: |
|
|||
2861 | winext = 'exe|com|bat|py' |
|
|||
2862 | if 'py' not in winext: |
|
|||
2863 | winext += '|py' |
|
|||
2864 | execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE) |
|
|||
2865 | isexec = lambda fname:os.path.isfile(fname) and execre.match(fname) |
|
|||
2866 | savedir = os.getcwdu() |
|
|||
2867 |
|
||||
2868 | # Now walk the paths looking for executables to alias. |
|
|||
2869 | try: |
|
|||
2870 | # write the whole loop for posix/Windows so we don't have an if in |
|
|||
2871 | # the innermost part |
|
|||
2872 | if os.name == 'posix': |
|
|||
2873 | for pdir in path: |
|
|||
2874 | os.chdir(pdir) |
|
|||
2875 | for ff in os.listdir(pdir): |
|
|||
2876 | if isexec(ff): |
|
|||
2877 | try: |
|
|||
2878 | # Removes dots from the name since ipython |
|
|||
2879 | # will assume names with dots to be python. |
|
|||
2880 | self.shell.alias_manager.define_alias( |
|
|||
2881 | ff.replace('.',''), ff) |
|
|||
2882 | except InvalidAliasError: |
|
|||
2883 | pass |
|
|||
2884 | else: |
|
|||
2885 | syscmdlist.append(ff) |
|
|||
2886 | else: |
|
|||
2887 | no_alias = self.shell.alias_manager.no_alias |
|
|||
2888 | for pdir in path: |
|
|||
2889 | os.chdir(pdir) |
|
|||
2890 | for ff in os.listdir(pdir): |
|
|||
2891 | base, ext = os.path.splitext(ff) |
|
|||
2892 | if isexec(ff) and base.lower() not in no_alias: |
|
|||
2893 | if ext.lower() == '.exe': |
|
|||
2894 | ff = base |
|
|||
2895 | try: |
|
|||
2896 | # Removes dots from the name since ipython |
|
|||
2897 | # will assume names with dots to be python. |
|
|||
2898 | self.shell.alias_manager.define_alias( |
|
|||
2899 | base.lower().replace('.',''), ff) |
|
|||
2900 | except InvalidAliasError: |
|
|||
2901 | pass |
|
|||
2902 | syscmdlist.append(ff) |
|
|||
2903 | self.shell.db['syscmdlist'] = syscmdlist |
|
|||
2904 | finally: |
|
|||
2905 | os.chdir(savedir) |
|
|||
2906 |
|
||||
2907 | @skip_doctest |
|
|||
2908 | def magic_pwd(self, parameter_s = ''): |
|
|||
2909 | """Return the current working directory path. |
|
|||
2910 |
|
||||
2911 | Examples |
|
|||
2912 | -------- |
|
|||
2913 | :: |
|
|||
2914 |
|
||||
2915 | In [9]: pwd |
|
|||
2916 | Out[9]: '/home/tsuser/sprint/ipython' |
|
|||
2917 | """ |
|
|||
2918 | return os.getcwdu() |
|
|||
2919 |
|
||||
2920 | @skip_doctest |
|
|||
2921 | def magic_cd(self, parameter_s=''): |
|
|||
2922 | """Change the current working directory. |
|
|||
2923 |
|
||||
2924 | This command automatically maintains an internal list of directories |
|
|||
2925 | you visit during your IPython session, in the variable _dh. The |
|
|||
2926 | command %dhist shows this history nicely formatted. You can also |
|
|||
2927 | do 'cd -<tab>' to see directory history conveniently. |
|
|||
2928 |
|
||||
2929 | Usage: |
|
|||
2930 |
|
||||
2931 | cd 'dir': changes to directory 'dir'. |
|
|||
2932 |
|
||||
2933 | cd -: changes to the last visited directory. |
|
|||
2934 |
|
||||
2935 | cd -<n>: changes to the n-th directory in the directory history. |
|
|||
2936 |
|
||||
2937 | cd --foo: change to directory that matches 'foo' in history |
|
|||
2938 |
|
||||
2939 | cd -b <bookmark_name>: jump to a bookmark set by %bookmark |
|
|||
2940 | (note: cd <bookmark_name> is enough if there is no |
|
|||
2941 | directory <bookmark_name>, but a bookmark with the name exists.) |
|
|||
2942 | 'cd -b <tab>' allows you to tab-complete bookmark names. |
|
|||
2943 |
|
||||
2944 | Options: |
|
|||
2945 |
|
||||
2946 | -q: quiet. Do not print the working directory after the cd command is |
|
|||
2947 | executed. By default IPython's cd command does print this directory, |
|
|||
2948 | since the default prompts do not display path information. |
|
|||
2949 |
|
||||
2950 | Note that !cd doesn't work for this purpose because the shell where |
|
|||
2951 | !command runs is immediately discarded after executing 'command'. |
|
|||
2952 |
|
||||
2953 | Examples |
|
|||
2954 | -------- |
|
|||
2955 | :: |
|
|||
2956 |
|
||||
2957 | In [10]: cd parent/child |
|
|||
2958 | /home/tsuser/parent/child |
|
|||
2959 | """ |
|
|||
2960 |
|
||||
2961 | parameter_s = parameter_s.strip() |
|
|||
2962 | #bkms = self.shell.persist.get("bookmarks",{}) |
|
|||
2963 |
|
||||
2964 | oldcwd = os.getcwdu() |
|
|||
2965 | numcd = re.match(r'(-)(\d+)$',parameter_s) |
|
|||
2966 | # jump in directory history by number |
|
|||
2967 | if numcd: |
|
|||
2968 | nn = int(numcd.group(2)) |
|
|||
2969 | try: |
|
|||
2970 | ps = self.shell.user_ns['_dh'][nn] |
|
|||
2971 | except IndexError: |
|
|||
2972 | print 'The requested directory does not exist in history.' |
|
|||
2973 | return |
|
|||
2974 | else: |
|
|||
2975 | opts = {} |
|
|||
2976 | elif parameter_s.startswith('--'): |
|
|||
2977 | ps = None |
|
|||
2978 | fallback = None |
|
|||
2979 | pat = parameter_s[2:] |
|
|||
2980 | dh = self.shell.user_ns['_dh'] |
|
|||
2981 | # first search only by basename (last component) |
|
|||
2982 | for ent in reversed(dh): |
|
|||
2983 | if pat in os.path.basename(ent) and os.path.isdir(ent): |
|
|||
2984 | ps = ent |
|
|||
2985 | break |
|
|||
2986 |
|
||||
2987 | if fallback is None and pat in ent and os.path.isdir(ent): |
|
|||
2988 | fallback = ent |
|
|||
2989 |
|
||||
2990 | # if we have no last part match, pick the first full path match |
|
|||
2991 | if ps is None: |
|
|||
2992 | ps = fallback |
|
|||
2993 |
|
||||
2994 | if ps is None: |
|
|||
2995 | print "No matching entry in directory history" |
|
|||
2996 | return |
|
|||
2997 | else: |
|
|||
2998 | opts = {} |
|
|||
2999 |
|
||||
3000 |
|
||||
3001 | else: |
|
|||
3002 | #turn all non-space-escaping backslashes to slashes, |
|
|||
3003 | # for c:\windows\directory\names\ |
|
|||
3004 | parameter_s = re.sub(r'\\(?! )','/', parameter_s) |
|
|||
3005 | opts,ps = self.parse_options(parameter_s,'qb',mode='string') |
|
|||
3006 | # jump to previous |
|
|||
3007 | if ps == '-': |
|
|||
3008 | try: |
|
|||
3009 | ps = self.shell.user_ns['_dh'][-2] |
|
|||
3010 | except IndexError: |
|
|||
3011 | raise UsageError('%cd -: No previous directory to change to.') |
|
|||
3012 | # jump to bookmark if needed |
|
|||
3013 | else: |
|
|||
3014 | if not os.path.isdir(ps) or opts.has_key('b'): |
|
|||
3015 | bkms = self.db.get('bookmarks', {}) |
|
|||
3016 |
|
||||
3017 | if bkms.has_key(ps): |
|
|||
3018 | target = bkms[ps] |
|
|||
3019 | print '(bookmark:%s) -> %s' % (ps,target) |
|
|||
3020 | ps = target |
|
|||
3021 | else: |
|
|||
3022 | if opts.has_key('b'): |
|
|||
3023 | raise UsageError("Bookmark '%s' not found. " |
|
|||
3024 | "Use '%%bookmark -l' to see your bookmarks." % ps) |
|
|||
3025 |
|
||||
3026 | # strip extra quotes on Windows, because os.chdir doesn't like them |
|
|||
3027 | ps = unquote_filename(ps) |
|
|||
3028 | # at this point ps should point to the target dir |
|
|||
3029 | if ps: |
|
|||
3030 | try: |
|
|||
3031 | os.chdir(os.path.expanduser(ps)) |
|
|||
3032 | if hasattr(self.shell, 'term_title') and self.shell.term_title: |
|
|||
3033 | set_term_title('IPython: ' + abbrev_cwd()) |
|
|||
3034 | except OSError: |
|
|||
3035 | print sys.exc_info()[1] |
|
|||
3036 | else: |
|
|||
3037 | cwd = os.getcwdu() |
|
|||
3038 | dhist = self.shell.user_ns['_dh'] |
|
|||
3039 | if oldcwd != cwd: |
|
|||
3040 | dhist.append(cwd) |
|
|||
3041 | self.db['dhist'] = compress_dhist(dhist)[-100:] |
|
|||
3042 |
|
||||
3043 | else: |
|
|||
3044 | os.chdir(self.shell.home_dir) |
|
|||
3045 | if hasattr(self.shell, 'term_title') and self.shell.term_title: |
|
|||
3046 | set_term_title('IPython: ' + '~') |
|
|||
3047 | cwd = os.getcwdu() |
|
|||
3048 | dhist = self.shell.user_ns['_dh'] |
|
|||
3049 |
|
||||
3050 | if oldcwd != cwd: |
|
|||
3051 | dhist.append(cwd) |
|
|||
3052 | self.db['dhist'] = compress_dhist(dhist)[-100:] |
|
|||
3053 | if not 'q' in opts and self.shell.user_ns['_dh']: |
|
|||
3054 | print self.shell.user_ns['_dh'][-1] |
|
|||
3055 |
|
||||
3056 |
|
||||
3057 | def magic_env(self, parameter_s=''): |
|
|||
3058 | """List environment variables.""" |
|
|||
3059 |
|
||||
3060 | return dict(os.environ) |
|
|||
3061 |
|
||||
3062 | def magic_pushd(self, parameter_s=''): |
|
|||
3063 | """Place the current dir on stack and change directory. |
|
|||
3064 |
|
||||
3065 | Usage:\\ |
|
|||
3066 | %pushd ['dirname'] |
|
|||
3067 | """ |
|
|||
3068 |
|
||||
3069 | dir_s = self.shell.dir_stack |
|
|||
3070 | tgt = os.path.expanduser(unquote_filename(parameter_s)) |
|
|||
3071 | cwd = os.getcwdu().replace(self.home_dir,'~') |
|
|||
3072 | if tgt: |
|
|||
3073 | self.magic_cd(parameter_s) |
|
|||
3074 | dir_s.insert(0,cwd) |
|
|||
3075 | return self.magic_dirs() |
|
|||
3076 |
|
||||
3077 | def magic_popd(self, parameter_s=''): |
|
|||
3078 | """Change to directory popped off the top of the stack. |
|
|||
3079 | """ |
|
|||
3080 | if not self.shell.dir_stack: |
|
|||
3081 | raise UsageError("%popd on empty stack") |
|
|||
3082 | top = self.shell.dir_stack.pop(0) |
|
|||
3083 | self.magic_cd(top) |
|
|||
3084 | print "popd ->",top |
|
|||
3085 |
|
||||
3086 | def magic_dirs(self, parameter_s=''): |
|
|||
3087 | """Return the current directory stack.""" |
|
|||
3088 |
|
||||
3089 | return self.shell.dir_stack |
|
|||
3090 |
|
||||
3091 | def magic_dhist(self, parameter_s=''): |
|
|||
3092 | """Print your history of visited directories. |
|
|||
3093 |
|
||||
3094 | %dhist -> print full history\\ |
|
|||
3095 | %dhist n -> print last n entries only\\ |
|
|||
3096 | %dhist n1 n2 -> print entries between n1 and n2 (n1 not included)\\ |
|
|||
3097 |
|
||||
3098 | This history is automatically maintained by the %cd command, and |
|
|||
3099 | always available as the global list variable _dh. You can use %cd -<n> |
|
|||
3100 | to go to directory number <n>. |
|
|||
3101 |
|
||||
3102 | Note that most of time, you should view directory history by entering |
|
|||
3103 | cd -<TAB>. |
|
|||
3104 |
|
||||
3105 | """ |
|
|||
3106 |
|
||||
3107 | dh = self.shell.user_ns['_dh'] |
|
|||
3108 | if parameter_s: |
|
|||
3109 | try: |
|
|||
3110 | args = map(int,parameter_s.split()) |
|
|||
3111 | except: |
|
|||
3112 | self.arg_err(Magic.magic_dhist) |
|
|||
3113 | return |
|
|||
3114 | if len(args) == 1: |
|
|||
3115 | ini,fin = max(len(dh)-(args[0]),0),len(dh) |
|
|||
3116 | elif len(args) == 2: |
|
|||
3117 | ini,fin = args |
|
|||
3118 | else: |
|
|||
3119 | self.arg_err(Magic.magic_dhist) |
|
|||
3120 | return |
|
|||
3121 | else: |
|
|||
3122 | ini,fin = 0,len(dh) |
|
|||
3123 | nlprint(dh, |
|
|||
3124 | header = 'Directory history (kept in _dh)', |
|
|||
3125 | start=ini,stop=fin) |
|
|||
3126 |
|
||||
3127 | @skip_doctest |
|
|||
3128 | def magic_sc(self, parameter_s=''): |
|
|||
3129 | """Shell capture - execute a shell command and capture its output. |
|
|||
3130 |
|
||||
3131 | DEPRECATED. Suboptimal, retained for backwards compatibility. |
|
|||
3132 |
|
||||
3133 | You should use the form 'var = !command' instead. Example: |
|
|||
3134 |
|
||||
3135 | "%sc -l myfiles = ls ~" should now be written as |
|
|||
3136 |
|
||||
3137 | "myfiles = !ls ~" |
|
|||
3138 |
|
||||
3139 | myfiles.s, myfiles.l and myfiles.n still apply as documented |
|
|||
3140 | below. |
|
|||
3141 |
|
||||
3142 | -- |
|
|||
3143 | %sc [options] varname=command |
|
|||
3144 |
|
||||
3145 | IPython will run the given command using commands.getoutput(), and |
|
|||
3146 | will then update the user's interactive namespace with a variable |
|
|||
3147 | called varname, containing the value of the call. Your command can |
|
|||
3148 | contain shell wildcards, pipes, etc. |
|
|||
3149 |
|
||||
3150 | The '=' sign in the syntax is mandatory, and the variable name you |
|
|||
3151 | supply must follow Python's standard conventions for valid names. |
|
|||
3152 |
|
||||
3153 | (A special format without variable name exists for internal use) |
|
|||
3154 |
|
||||
3155 | Options: |
|
|||
3156 |
|
||||
3157 | -l: list output. Split the output on newlines into a list before |
|
|||
3158 | assigning it to the given variable. By default the output is stored |
|
|||
3159 | as a single string. |
|
|||
3160 |
|
||||
3161 | -v: verbose. Print the contents of the variable. |
|
|||
3162 |
|
||||
3163 | In most cases you should not need to split as a list, because the |
|
|||
3164 | returned value is a special type of string which can automatically |
|
|||
3165 | provide its contents either as a list (split on newlines) or as a |
|
|||
3166 | space-separated string. These are convenient, respectively, either |
|
|||
3167 | for sequential processing or to be passed to a shell command. |
|
|||
3168 |
|
||||
3169 | For example:: |
|
|||
3170 |
|
||||
3171 | # Capture into variable a |
|
|||
3172 | In [1]: sc a=ls *py |
|
|||
3173 |
|
||||
3174 | # a is a string with embedded newlines |
|
|||
3175 | In [2]: a |
|
|||
3176 | Out[2]: 'setup.py\\nwin32_manual_post_install.py' |
|
|||
3177 |
|
||||
3178 | # which can be seen as a list: |
|
|||
3179 | In [3]: a.l |
|
|||
3180 | Out[3]: ['setup.py', 'win32_manual_post_install.py'] |
|
|||
3181 |
|
||||
3182 | # or as a whitespace-separated string: |
|
|||
3183 | In [4]: a.s |
|
|||
3184 | Out[4]: 'setup.py win32_manual_post_install.py' |
|
|||
3185 |
|
||||
3186 | # a.s is useful to pass as a single command line: |
|
|||
3187 | In [5]: !wc -l $a.s |
|
|||
3188 | 146 setup.py |
|
|||
3189 | 130 win32_manual_post_install.py |
|
|||
3190 | 276 total |
|
|||
3191 |
|
||||
3192 | # while the list form is useful to loop over: |
|
|||
3193 | In [6]: for f in a.l: |
|
|||
3194 | ...: !wc -l $f |
|
|||
3195 | ...: |
|
|||
3196 | 146 setup.py |
|
|||
3197 | 130 win32_manual_post_install.py |
|
|||
3198 |
|
||||
3199 | Similarly, the lists returned by the -l option are also special, in |
|
|||
3200 | the sense that you can equally invoke the .s attribute on them to |
|
|||
3201 | automatically get a whitespace-separated string from their contents:: |
|
|||
3202 |
|
||||
3203 | In [7]: sc -l b=ls *py |
|
|||
3204 |
|
||||
3205 | In [8]: b |
|
|||
3206 | Out[8]: ['setup.py', 'win32_manual_post_install.py'] |
|
|||
3207 |
|
||||
3208 | In [9]: b.s |
|
|||
3209 | Out[9]: 'setup.py win32_manual_post_install.py' |
|
|||
3210 |
|
||||
3211 | In summary, both the lists and strings used for output capture have |
|
|||
3212 | the following special attributes:: |
|
|||
3213 |
|
||||
3214 | .l (or .list) : value as list. |
|
|||
3215 | .n (or .nlstr): value as newline-separated string. |
|
|||
3216 | .s (or .spstr): value as space-separated string. |
|
|||
3217 | """ |
|
|||
3218 |
|
||||
3219 | opts,args = self.parse_options(parameter_s,'lv') |
|
|||
3220 | # Try to get a variable name and command to run |
|
|||
3221 | try: |
|
|||
3222 | # the variable name must be obtained from the parse_options |
|
|||
3223 | # output, which uses shlex.split to strip options out. |
|
|||
3224 | var,_ = args.split('=',1) |
|
|||
3225 | var = var.strip() |
|
|||
3226 | # But the command has to be extracted from the original input |
|
|||
3227 | # parameter_s, not on what parse_options returns, to avoid the |
|
|||
3228 | # quote stripping which shlex.split performs on it. |
|
|||
3229 | _,cmd = parameter_s.split('=',1) |
|
|||
3230 | except ValueError: |
|
|||
3231 | var,cmd = '','' |
|
|||
3232 | # If all looks ok, proceed |
|
|||
3233 | split = 'l' in opts |
|
|||
3234 | out = self.shell.getoutput(cmd, split=split) |
|
|||
3235 | if opts.has_key('v'): |
|
|||
3236 | print '%s ==\n%s' % (var,pformat(out)) |
|
|||
3237 | if var: |
|
|||
3238 | self.shell.user_ns.update({var:out}) |
|
|||
3239 | else: |
|
|||
3240 | return out |
|
|||
3241 |
|
||||
3242 | def magic_sx(self, parameter_s=''): |
|
|||
3243 | """Shell execute - run a shell command and capture its output. |
|
|||
3244 |
|
||||
3245 | %sx command |
|
|||
3246 |
|
||||
3247 | IPython will run the given command using commands.getoutput(), and |
|
|||
3248 | return the result formatted as a list (split on '\\n'). Since the |
|
|||
3249 | output is _returned_, it will be stored in ipython's regular output |
|
|||
3250 | cache Out[N] and in the '_N' automatic variables. |
|
|||
3251 |
|
||||
3252 | Notes: |
|
|||
3253 |
|
||||
3254 | 1) If an input line begins with '!!', then %sx is automatically |
|
|||
3255 | invoked. That is, while:: |
|
|||
3256 |
|
||||
3257 | !ls |
|
|||
3258 |
|
||||
3259 | causes ipython to simply issue system('ls'), typing:: |
|
|||
3260 |
|
||||
3261 | !!ls |
|
|||
3262 |
|
||||
3263 | is a shorthand equivalent to:: |
|
|||
3264 |
|
||||
3265 | %sx ls |
|
|||
3266 |
|
||||
3267 | 2) %sx differs from %sc in that %sx automatically splits into a list, |
|
|||
3268 | like '%sc -l'. The reason for this is to make it as easy as possible |
|
|||
3269 | to process line-oriented shell output via further python commands. |
|
|||
3270 | %sc is meant to provide much finer control, but requires more |
|
|||
3271 | typing. |
|
|||
3272 |
|
||||
3273 | 3) Just like %sc -l, this is a list with special attributes: |
|
|||
3274 | :: |
|
|||
3275 |
|
||||
3276 | .l (or .list) : value as list. |
|
|||
3277 | .n (or .nlstr): value as newline-separated string. |
|
|||
3278 | .s (or .spstr): value as whitespace-separated string. |
|
|||
3279 |
|
||||
3280 | This is very useful when trying to use such lists as arguments to |
|
|||
3281 | system commands.""" |
|
|||
3282 |
|
||||
3283 | if parameter_s: |
|
|||
3284 | return self.shell.getoutput(parameter_s) |
|
|||
3285 |
|
||||
3286 |
|
||||
3287 | def magic_bookmark(self, parameter_s=''): |
|
|||
3288 | """Manage IPython's bookmark system. |
|
|||
3289 |
|
||||
3290 | %bookmark <name> - set bookmark to current dir |
|
|||
3291 | %bookmark <name> <dir> - set bookmark to <dir> |
|
|||
3292 | %bookmark -l - list all bookmarks |
|
|||
3293 | %bookmark -d <name> - remove bookmark |
|
|||
3294 | %bookmark -r - remove all bookmarks |
|
|||
3295 |
|
||||
3296 | You can later on access a bookmarked folder with:: |
|
|||
3297 |
|
||||
3298 | %cd -b <name> |
|
|||
3299 |
|
||||
3300 | or simply '%cd <name>' if there is no directory called <name> AND |
|
|||
3301 | there is such a bookmark defined. |
|
|||
3302 |
|
||||
3303 | Your bookmarks persist through IPython sessions, but they are |
|
|||
3304 | associated with each profile.""" |
|
|||
3305 |
|
||||
3306 | opts,args = self.parse_options(parameter_s,'drl',mode='list') |
|
|||
3307 | if len(args) > 2: |
|
|||
3308 | raise UsageError("%bookmark: too many arguments") |
|
|||
3309 |
|
||||
3310 | bkms = self.db.get('bookmarks',{}) |
|
|||
3311 |
|
||||
3312 | if opts.has_key('d'): |
|
|||
3313 | try: |
|
|||
3314 | todel = args[0] |
|
|||
3315 | except IndexError: |
|
|||
3316 | raise UsageError( |
|
|||
3317 | "%bookmark -d: must provide a bookmark to delete") |
|
|||
3318 | else: |
|
|||
3319 | try: |
|
|||
3320 | del bkms[todel] |
|
|||
3321 | except KeyError: |
|
|||
3322 | raise UsageError( |
|
|||
3323 | "%%bookmark -d: Can't delete bookmark '%s'" % todel) |
|
|||
3324 |
|
||||
3325 | elif opts.has_key('r'): |
|
|||
3326 | bkms = {} |
|
|||
3327 | elif opts.has_key('l'): |
|
|||
3328 | bks = bkms.keys() |
|
|||
3329 | bks.sort() |
|
|||
3330 | if bks: |
|
|||
3331 | size = max(map(len,bks)) |
|
|||
3332 | else: |
|
|||
3333 | size = 0 |
|
|||
3334 | fmt = '%-'+str(size)+'s -> %s' |
|
|||
3335 | print 'Current bookmarks:' |
|
|||
3336 | for bk in bks: |
|
|||
3337 | print fmt % (bk,bkms[bk]) |
|
|||
3338 | else: |
|
|||
3339 | if not args: |
|
|||
3340 | raise UsageError("%bookmark: You must specify the bookmark name") |
|
|||
3341 | elif len(args)==1: |
|
|||
3342 | bkms[args[0]] = os.getcwdu() |
|
|||
3343 | elif len(args)==2: |
|
|||
3344 | bkms[args[0]] = args[1] |
|
|||
3345 | self.db['bookmarks'] = bkms |
|
|||
3346 |
|
||||
3347 |
|
||||
3348 | def magic_pycat(self, parameter_s=''): |
|
|||
3349 | """Show a syntax-highlighted file through a pager. |
|
|||
3350 |
|
||||
3351 | This magic is similar to the cat utility, but it will assume the file |
|
|||
3352 | to be Python source and will show it with syntax highlighting. |
|
|||
3353 |
|
||||
3354 | This magic command can either take a local filename, an url, |
|
|||
3355 | an history range (see %history) or a macro as argument :: |
|
|||
3356 |
|
||||
3357 | %pycat myscript.py |
|
|||
3358 | %pycat 7-27 |
|
|||
3359 | %pycat myMacro |
|
|||
3360 | %pycat http://www.example.com/myscript.py |
|
|||
3361 | """ |
|
|||
3362 |
|
||||
3363 | try : |
|
|||
3364 | cont = self.shell.find_user_code(parameter_s) |
|
|||
3365 | except ValueError, IOError: |
|
|||
3366 | print "Error: no such file, variable, URL, history range or macro" |
|
|||
3367 | return |
|
|||
3368 |
|
||||
3369 | page.page(self.shell.pycolorize(cont)) |
|
|||
3370 |
|
||||
3371 | def magic_quickref(self,arg): |
|
|||
3372 | """ Show a quick reference sheet """ |
|
|||
3373 | import IPython.core.usage |
|
|||
3374 | qr = IPython.core.usage.quick_reference + self.magic_magic('-brief') |
|
|||
3375 |
|
||||
3376 | page.page(qr) |
|
|||
3377 |
|
||||
3378 | def magic_doctest_mode(self,parameter_s=''): |
|
|||
3379 | """Toggle doctest mode on and off. |
|
|||
3380 |
|
||||
3381 | This mode is intended to make IPython behave as much as possible like a |
|
|||
3382 | plain Python shell, from the perspective of how its prompts, exceptions |
|
|||
3383 | and output look. This makes it easy to copy and paste parts of a |
|
|||
3384 | session into doctests. It does so by: |
|
|||
3385 |
|
||||
3386 | - Changing the prompts to the classic ``>>>`` ones. |
|
|||
3387 | - Changing the exception reporting mode to 'Plain'. |
|
|||
3388 | - Disabling pretty-printing of output. |
|
|||
3389 |
|
||||
3390 | Note that IPython also supports the pasting of code snippets that have |
|
|||
3391 | leading '>>>' and '...' prompts in them. This means that you can paste |
|
|||
3392 | doctests from files or docstrings (even if they have leading |
|
|||
3393 | whitespace), and the code will execute correctly. You can then use |
|
|||
3394 | '%history -t' to see the translated history; this will give you the |
|
|||
3395 | input after removal of all the leading prompts and whitespace, which |
|
|||
3396 | can be pasted back into an editor. |
|
|||
3397 |
|
||||
3398 | With these features, you can switch into this mode easily whenever you |
|
|||
3399 | need to do testing and changes to doctests, without having to leave |
|
|||
3400 | your existing IPython session. |
|
|||
3401 | """ |
|
|||
3402 |
|
||||
3403 | from IPython.utils.ipstruct import Struct |
|
|||
3404 |
|
||||
3405 | # Shorthands |
|
|||
3406 | shell = self.shell |
|
|||
3407 | pm = shell.prompt_manager |
|
|||
3408 | meta = shell.meta |
|
|||
3409 | disp_formatter = self.shell.display_formatter |
|
|||
3410 | ptformatter = disp_formatter.formatters['text/plain'] |
|
|||
3411 | # dstore is a data store kept in the instance metadata bag to track any |
|
|||
3412 | # changes we make, so we can undo them later. |
|
|||
3413 | dstore = meta.setdefault('doctest_mode',Struct()) |
|
|||
3414 | save_dstore = dstore.setdefault |
|
|||
3415 |
|
||||
3416 | # save a few values we'll need to recover later |
|
|||
3417 | mode = save_dstore('mode',False) |
|
|||
3418 | save_dstore('rc_pprint',ptformatter.pprint) |
|
|||
3419 | save_dstore('xmode',shell.InteractiveTB.mode) |
|
|||
3420 | save_dstore('rc_separate_out',shell.separate_out) |
|
|||
3421 | save_dstore('rc_separate_out2',shell.separate_out2) |
|
|||
3422 | save_dstore('rc_prompts_pad_left',pm.justify) |
|
|||
3423 | save_dstore('rc_separate_in',shell.separate_in) |
|
|||
3424 | save_dstore('rc_plain_text_only',disp_formatter.plain_text_only) |
|
|||
3425 | save_dstore('prompt_templates',(pm.in_template, pm.in2_template, pm.out_template)) |
|
|||
3426 |
|
||||
3427 | if mode == False: |
|
|||
3428 | # turn on |
|
|||
3429 | pm.in_template = '>>> ' |
|
|||
3430 | pm.in2_template = '... ' |
|
|||
3431 | pm.out_template = '' |
|
|||
3432 |
|
||||
3433 | # Prompt separators like plain python |
|
|||
3434 | shell.separate_in = '' |
|
|||
3435 | shell.separate_out = '' |
|
|||
3436 | shell.separate_out2 = '' |
|
|||
3437 |
|
||||
3438 | pm.justify = False |
|
|||
3439 |
|
||||
3440 | ptformatter.pprint = False |
|
|||
3441 | disp_formatter.plain_text_only = True |
|
|||
3442 |
|
||||
3443 | shell.magic_xmode('Plain') |
|
|||
3444 | else: |
|
|||
3445 | # turn off |
|
|||
3446 | pm.in_template, pm.in2_template, pm.out_template = dstore.prompt_templates |
|
|||
3447 |
|
||||
3448 | shell.separate_in = dstore.rc_separate_in |
|
|||
3449 |
|
||||
3450 | shell.separate_out = dstore.rc_separate_out |
|
|||
3451 | shell.separate_out2 = dstore.rc_separate_out2 |
|
|||
3452 |
|
||||
3453 | pm.justify = dstore.rc_prompts_pad_left |
|
|||
3454 |
|
||||
3455 | ptformatter.pprint = dstore.rc_pprint |
|
|||
3456 | disp_formatter.plain_text_only = dstore.rc_plain_text_only |
|
|||
3457 |
|
||||
3458 | shell.magic_xmode(dstore.xmode) |
|
|||
3459 |
|
||||
3460 | # Store new mode and inform |
|
|||
3461 | dstore.mode = bool(1-int(mode)) |
|
|||
3462 | mode_label = ['OFF','ON'][dstore.mode] |
|
|||
3463 | print 'Doctest mode is:', mode_label |
|
|||
3464 |
|
||||
3465 | def magic_gui(self, parameter_s=''): |
|
|||
3466 | """Enable or disable IPython GUI event loop integration. |
|
|||
3467 |
|
||||
3468 | %gui [GUINAME] |
|
|||
3469 |
|
||||
3470 | This magic replaces IPython's threaded shells that were activated |
|
|||
3471 | using the (pylab/wthread/etc.) command line flags. GUI toolkits |
|
|||
3472 | can now be enabled at runtime and keyboard |
|
|||
3473 | interrupts should work without any problems. The following toolkits |
|
|||
3474 | are supported: wxPython, PyQt4, PyGTK, Tk and Cocoa (OSX):: |
|
|||
3475 |
|
||||
3476 | %gui wx # enable wxPython event loop integration |
|
|||
3477 | %gui qt4|qt # enable PyQt4 event loop integration |
|
|||
3478 | %gui gtk # enable PyGTK event loop integration |
|
|||
3479 | %gui gtk3 # enable Gtk3 event loop integration |
|
|||
3480 | %gui tk # enable Tk event loop integration |
|
|||
3481 | %gui OSX # enable Cocoa event loop integration |
|
|||
3482 | # (requires %matplotlib 1.1) |
|
|||
3483 | %gui # disable all event loop integration |
|
|||
3484 |
|
||||
3485 | WARNING: after any of these has been called you can simply create |
|
|||
3486 | an application object, but DO NOT start the event loop yourself, as |
|
|||
3487 | we have already handled that. |
|
|||
3488 | """ |
|
|||
3489 | opts, arg = self.parse_options(parameter_s, '') |
|
|||
3490 | if arg=='': arg = None |
|
|||
3491 | try: |
|
|||
3492 | return self.enable_gui(arg) |
|
|||
3493 | except Exception as e: |
|
|||
3494 | # print simple error message, rather than traceback if we can't |
|
|||
3495 | # hook up the GUI |
|
|||
3496 | error(str(e)) |
|
|||
3497 |
|
||||
3498 | def magic_install_ext(self, parameter_s): |
|
|||
3499 | """Download and install an extension from a URL, e.g.:: |
|
|||
3500 |
|
||||
3501 | %install_ext https://bitbucket.org/birkenfeld/ipython-physics/raw/d1310a2ab15d/physics.py |
|
|||
3502 |
|
||||
3503 | The URL should point to an importable Python module - either a .py file |
|
|||
3504 | or a .zip file. |
|
|||
3505 |
|
||||
3506 | Parameters: |
|
|||
3507 |
|
||||
3508 | -n filename : Specify a name for the file, rather than taking it from |
|
|||
3509 | the URL. |
|
|||
3510 | """ |
|
|||
3511 | opts, args = self.parse_options(parameter_s, 'n:') |
|
|||
3512 | try: |
|
|||
3513 | filename = self.extension_manager.install_extension(args, opts.get('n')) |
|
|||
3514 | except ValueError as e: |
|
|||
3515 | print e |
|
|||
3516 | return |
|
|||
3517 |
|
||||
3518 | filename = os.path.basename(filename) |
|
|||
3519 | print "Installed %s. To use it, type:" % filename |
|
|||
3520 | print " %%load_ext %s" % os.path.splitext(filename)[0] |
|
|||
3521 |
|
||||
3522 |
|
||||
3523 | def magic_load_ext(self, module_str): |
|
|||
3524 | """Load an IPython extension by its module name.""" |
|
|||
3525 | return self.extension_manager.load_extension(module_str) |
|
|||
3526 |
|
||||
3527 | def magic_unload_ext(self, module_str): |
|
|||
3528 | """Unload an IPython extension by its module name.""" |
|
|||
3529 | self.extension_manager.unload_extension(module_str) |
|
|||
3530 |
|
||||
3531 | def magic_reload_ext(self, module_str): |
|
|||
3532 | """Reload an IPython extension by its module name.""" |
|
|||
3533 | self.extension_manager.reload_extension(module_str) |
|
|||
3534 |
|
||||
3535 | def magic_install_profiles(self, s): |
|
|||
3536 | """%install_profiles has been deprecated.""" |
|
|||
3537 | print '\n'.join([ |
|
|||
3538 | "%install_profiles has been deprecated.", |
|
|||
3539 | "Use `ipython profile list` to view available profiles.", |
|
|||
3540 | "Requesting a profile with `ipython profile create <name>`", |
|
|||
3541 | "or `ipython --profile=<name>` will start with the bundled", |
|
|||
3542 | "profile of that name if it exists." |
|
|||
3543 | ]) |
|
|||
3544 |
|
||||
3545 | def magic_install_default_config(self, s): |
|
|||
3546 | """%install_default_config has been deprecated.""" |
|
|||
3547 | print '\n'.join([ |
|
|||
3548 | "%install_default_config has been deprecated.", |
|
|||
3549 | "Use `ipython profile create <name>` to initialize a profile", |
|
|||
3550 | "with the default config files.", |
|
|||
3551 | "Add `--reset` to overwrite already existing config files with defaults." |
|
|||
3552 | ]) |
|
|||
3553 |
|
||||
3554 | # Pylab support: simple wrappers that activate pylab, load gui input |
|
|||
3555 | # handling and modify slightly %run |
|
|||
3556 |
|
||||
3557 | @skip_doctest |
|
|||
3558 | def _pylab_magic_run(self, parameter_s=''): |
|
|||
3559 | Magic.magic_run(self, parameter_s, |
|
|||
3560 | runner=mpl_runner(self.shell.safe_execfile)) |
|
|||
3561 |
|
||||
3562 | _pylab_magic_run.__doc__ = magic_run.__doc__ |
|
|||
3563 |
|
||||
3564 | @skip_doctest |
|
|||
3565 | def magic_pylab(self, s): |
|
|||
3566 | """Load numpy and matplotlib to work interactively. |
|
|||
3567 |
|
||||
3568 | %pylab [GUINAME] |
|
|||
3569 |
|
||||
3570 | This function lets you activate pylab (matplotlib, numpy and |
|
|||
3571 | interactive support) at any point during an IPython session. |
|
|||
3572 |
|
||||
3573 | It will import at the top level numpy as np, pyplot as plt, matplotlib, |
|
|||
3574 | pylab and mlab, as well as all names from numpy and pylab. |
|
|||
3575 |
|
||||
3576 | If you are using the inline matplotlib backend for embedded figures, |
|
|||
3577 | you can adjust its behavior via the %config magic:: |
|
|||
3578 |
|
||||
3579 | # enable SVG figures, necessary for SVG+XHTML export in the qtconsole |
|
|||
3580 | In [1]: %config InlineBackend.figure_format = 'svg' |
|
|||
3581 |
|
||||
3582 | # change the behavior of closing all figures at the end of each |
|
|||
3583 | # execution (cell), or allowing reuse of active figures across |
|
|||
3584 | # cells: |
|
|||
3585 | In [2]: %config InlineBackend.close_figures = False |
|
|||
3586 |
|
||||
3587 | Parameters |
|
|||
3588 | ---------- |
|
|||
3589 | guiname : optional |
|
|||
3590 | One of the valid arguments to the %gui magic ('qt', 'wx', 'gtk', |
|
|||
3591 | 'osx' or 'tk'). If given, the corresponding Matplotlib backend is |
|
|||
3592 | used, otherwise matplotlib's default (which you can override in your |
|
|||
3593 | matplotlib config file) is used. |
|
|||
3594 |
|
||||
3595 | Examples |
|
|||
3596 | -------- |
|
|||
3597 | In this case, where the MPL default is TkAgg:: |
|
|||
3598 |
|
||||
3599 | In [2]: %pylab |
|
|||
3600 |
|
||||
3601 | Welcome to pylab, a matplotlib-based Python environment. |
|
|||
3602 | Backend in use: TkAgg |
|
|||
3603 | For more information, type 'help(pylab)'. |
|
|||
3604 |
|
||||
3605 | But you can explicitly request a different backend:: |
|
|||
3606 |
|
||||
3607 | In [3]: %pylab qt |
|
|||
3608 |
|
||||
3609 | Welcome to pylab, a matplotlib-based Python environment. |
|
|||
3610 | Backend in use: Qt4Agg |
|
|||
3611 | For more information, type 'help(pylab)'. |
|
|||
3612 | """ |
|
|||
3613 |
|
||||
3614 | if Application.initialized(): |
|
|||
3615 | app = Application.instance() |
|
|||
3616 | try: |
|
|||
3617 | import_all_status = app.pylab_import_all |
|
|||
3618 | except AttributeError: |
|
|||
3619 | import_all_status = True |
|
|||
3620 | else: |
|
|||
3621 | import_all_status = True |
|
|||
3622 |
|
||||
3623 | self.shell.enable_pylab(s, import_all=import_all_status) |
|
|||
3624 |
|
||||
3625 | def magic_tb(self, s): |
|
|||
3626 | """Print the last traceback with the currently active exception mode. |
|
|||
3627 |
|
||||
3628 | See %xmode for changing exception reporting modes.""" |
|
|||
3629 | self.shell.showtraceback() |
|
|||
3630 |
|
||||
3631 | @skip_doctest |
|
|||
3632 | def magic_precision(self, s=''): |
|
|||
3633 | """Set floating point precision for pretty printing. |
|
|||
3634 |
|
||||
3635 | Can set either integer precision or a format string. |
|
|||
3636 |
|
||||
3637 | If numpy has been imported and precision is an int, |
|
|||
3638 | numpy display precision will also be set, via ``numpy.set_printoptions``. |
|
|||
3639 |
|
||||
3640 | If no argument is given, defaults will be restored. |
|
|||
3641 |
|
||||
3642 | Examples |
|
|||
3643 | -------- |
|
|||
3644 | :: |
|
|||
3645 |
|
||||
3646 | In [1]: from math import pi |
|
|||
3647 |
|
||||
3648 | In [2]: %precision 3 |
|
|||
3649 | Out[2]: u'%.3f' |
|
|||
3650 |
|
||||
3651 | In [3]: pi |
|
|||
3652 | Out[3]: 3.142 |
|
|||
3653 |
|
||||
3654 | In [4]: %precision %i |
|
|||
3655 | Out[4]: u'%i' |
|
|||
3656 |
|
||||
3657 | In [5]: pi |
|
|||
3658 | Out[5]: 3 |
|
|||
3659 |
|
||||
3660 | In [6]: %precision %e |
|
|||
3661 | Out[6]: u'%e' |
|
|||
3662 |
|
||||
3663 | In [7]: pi**10 |
|
|||
3664 | Out[7]: 9.364805e+04 |
|
|||
3665 |
|
||||
3666 | In [8]: %precision |
|
|||
3667 | Out[8]: u'%r' |
|
|||
3668 |
|
||||
3669 | In [9]: pi**10 |
|
|||
3670 | Out[9]: 93648.047476082982 |
|
|||
3671 |
|
||||
3672 | """ |
|
|||
3673 |
|
||||
3674 | ptformatter = self.shell.display_formatter.formatters['text/plain'] |
|
|||
3675 | ptformatter.float_precision = s |
|
|||
3676 | return ptformatter.float_format |
|
|||
3677 |
|
||||
3678 |
|
||||
3679 | @magic_arguments.magic_arguments() |
|
|||
3680 | @magic_arguments.argument( |
|
|||
3681 | '-e', '--export', action='store_true', default=False, |
|
|||
3682 | help='Export IPython history as a notebook. The filename argument ' |
|
|||
3683 | 'is used to specify the notebook name and format. For example ' |
|
|||
3684 | 'a filename of notebook.ipynb will result in a notebook name ' |
|
|||
3685 | 'of "notebook" and a format of "xml". Likewise using a ".json" ' |
|
|||
3686 | 'or ".py" file extension will write the notebook in the json ' |
|
|||
3687 | 'or py formats.' |
|
|||
3688 | ) |
|
|||
3689 | @magic_arguments.argument( |
|
|||
3690 | '-f', '--format', |
|
|||
3691 | help='Convert an existing IPython notebook to a new format. This option ' |
|
|||
3692 | 'specifies the new format and can have the values: xml, json, py. ' |
|
|||
3693 | 'The target filename is chosen automatically based on the new ' |
|
|||
3694 | 'format. The filename argument gives the name of the source file.' |
|
|||
3695 | ) |
|
|||
3696 | @magic_arguments.argument( |
|
|||
3697 | 'filename', type=unicode, |
|
|||
3698 | help='Notebook name or filename' |
|
|||
3699 | ) |
|
|||
3700 | def magic_notebook(self, s): |
|
|||
3701 | """Export and convert IPython notebooks. |
|
|||
3702 |
|
||||
3703 | This function can export the current IPython history to a notebook file |
|
|||
3704 | or can convert an existing notebook file into a different format. For |
|
|||
3705 | example, to export the history to "foo.ipynb" do "%notebook -e foo.ipynb". |
|
|||
3706 | To export the history to "foo.py" do "%notebook -e foo.py". To convert |
|
|||
3707 | "foo.ipynb" to "foo.json" do "%notebook -f json foo.ipynb". Possible |
|
|||
3708 | formats include (json/ipynb, py). |
|
|||
3709 | """ |
|
|||
3710 | args = magic_arguments.parse_argstring(self.magic_notebook, s) |
|
|||
3711 |
|
||||
3712 | from IPython.nbformat import current |
|
|||
3713 | args.filename = unquote_filename(args.filename) |
|
|||
3714 | if args.export: |
|
|||
3715 | fname, name, format = current.parse_filename(args.filename) |
|
|||
3716 | cells = [] |
|
|||
3717 | hist = list(self.history_manager.get_range()) |
|
|||
3718 | for session, prompt_number, input in hist[:-1]: |
|
|||
3719 | cells.append(current.new_code_cell(prompt_number=prompt_number, input=input)) |
|
|||
3720 | worksheet = current.new_worksheet(cells=cells) |
|
|||
3721 | nb = current.new_notebook(name=name,worksheets=[worksheet]) |
|
|||
3722 | with io.open(fname, 'w', encoding='utf-8') as f: |
|
|||
3723 | current.write(nb, f, format); |
|
|||
3724 | elif args.format is not None: |
|
|||
3725 | old_fname, old_name, old_format = current.parse_filename(args.filename) |
|
|||
3726 | new_format = args.format |
|
|||
3727 | if new_format == u'xml': |
|
|||
3728 | raise ValueError('Notebooks cannot be written as xml.') |
|
|||
3729 | elif new_format == u'ipynb' or new_format == u'json': |
|
|||
3730 | new_fname = old_name + u'.ipynb' |
|
|||
3731 | new_format = u'json' |
|
|||
3732 | elif new_format == u'py': |
|
|||
3733 | new_fname = old_name + u'.py' |
|
|||
3734 | else: |
|
|||
3735 | raise ValueError('Invalid notebook format: %s' % new_format) |
|
|||
3736 | with io.open(old_fname, 'r', encoding='utf-8') as f: |
|
|||
3737 | nb = current.read(f, old_format) |
|
|||
3738 | with io.open(new_fname, 'w', encoding='utf-8') as f: |
|
|||
3739 | current.write(nb, f, new_format) |
|
|||
3740 |
|
||||
3741 | def magic_config(self, s): |
|
|||
3742 | """configure IPython |
|
|||
3743 |
|
||||
3744 | %config Class[.trait=value] |
|
|||
3745 |
|
||||
3746 | This magic exposes most of the IPython config system. Any |
|
|||
3747 | Configurable class should be able to be configured with the simple |
|
|||
3748 | line:: |
|
|||
3749 |
|
||||
3750 | %config Class.trait=value |
|
|||
3751 |
|
||||
3752 | Where `value` will be resolved in the user's namespace, if it is an |
|
|||
3753 | expression or variable name. |
|
|||
3754 |
|
||||
3755 | Examples |
|
|||
3756 | -------- |
|
|||
3757 |
|
||||
3758 | To see what classes are available for config, pass no arguments:: |
|
|||
3759 |
|
||||
3760 | In [1]: %config |
|
|||
3761 | Available objects for config: |
|
|||
3762 | TerminalInteractiveShell |
|
|||
3763 | HistoryManager |
|
|||
3764 | PrefilterManager |
|
|||
3765 | AliasManager |
|
|||
3766 | IPCompleter |
|
|||
3767 | PromptManager |
|
|||
3768 | DisplayFormatter |
|
|||
3769 |
|
||||
3770 | To view what is configurable on a given class, just pass the class |
|
|||
3771 | name:: |
|
|||
3772 |
|
||||
3773 | In [2]: %config IPCompleter |
|
|||
3774 | IPCompleter options |
|
|||
3775 | ----------------- |
|
|||
3776 | IPCompleter.omit__names=<Enum> |
|
|||
3777 | Current: 2 |
|
|||
3778 | Choices: (0, 1, 2) |
|
|||
3779 | Instruct the completer to omit private method names |
|
|||
3780 | Specifically, when completing on ``object.<tab>``. |
|
|||
3781 | When 2 [default]: all names that start with '_' will be excluded. |
|
|||
3782 | When 1: all 'magic' names (``__foo__``) will be excluded. |
|
|||
3783 | When 0: nothing will be excluded. |
|
|||
3784 | IPCompleter.merge_completions=<CBool> |
|
|||
3785 | Current: True |
|
|||
3786 | Whether to merge completion results into a single list |
|
|||
3787 | If False, only the completion results from the first non-empty completer |
|
|||
3788 | will be returned. |
|
|||
3789 | IPCompleter.limit_to__all__=<CBool> |
|
|||
3790 | Current: False |
|
|||
3791 | Instruct the completer to use __all__ for the completion |
|
|||
3792 | Specifically, when completing on ``object.<tab>``. |
|
|||
3793 | When True: only those names in obj.__all__ will be included. |
|
|||
3794 | When False [default]: the __all__ attribute is ignored |
|
|||
3795 | IPCompleter.greedy=<CBool> |
|
|||
3796 | Current: False |
|
|||
3797 | Activate greedy completion |
|
|||
3798 | This will enable completion on elements of lists, results of function calls, |
|
|||
3799 | etc., but can be unsafe because the code is actually evaluated on TAB. |
|
|||
3800 |
|
||||
3801 | but the real use is in setting values:: |
|
|||
3802 |
|
||||
3803 | In [3]: %config IPCompleter.greedy = True |
|
|||
3804 |
|
||||
3805 | and these values are read from the user_ns if they are variables:: |
|
|||
3806 |
|
||||
3807 | In [4]: feeling_greedy=False |
|
|||
3808 |
|
||||
3809 | In [5]: %config IPCompleter.greedy = feeling_greedy |
|
|||
3810 |
|
||||
3811 | """ |
|
|||
3812 | from IPython.config.loader import Config |
|
|||
3813 | # some IPython objects are Configurable, but do not yet have |
|
|||
3814 | # any configurable traits. Exclude them from the effects of |
|
|||
3815 | # this magic, as their presence is just noise: |
|
|||
3816 | configurables = [ c for c in self.configurables if c.__class__.class_traits(config=True) ] |
|
|||
3817 | classnames = [ c.__class__.__name__ for c in configurables ] |
|
|||
3818 |
|
||||
3819 | line = s.strip() |
|
|||
3820 | if not line: |
|
|||
3821 | # print available configurable names |
|
|||
3822 | print "Available objects for config:" |
|
|||
3823 | for name in classnames: |
|
|||
3824 | print " ", name |
|
|||
3825 | return |
|
|||
3826 | elif line in classnames: |
|
|||
3827 | # `%config TerminalInteractiveShell` will print trait info for |
|
|||
3828 | # TerminalInteractiveShell |
|
|||
3829 | c = configurables[classnames.index(line)] |
|
|||
3830 | cls = c.__class__ |
|
|||
3831 | help = cls.class_get_help(c) |
|
|||
3832 | # strip leading '--' from cl-args: |
|
|||
3833 | help = re.sub(re.compile(r'^--', re.MULTILINE), '', help) |
|
|||
3834 | print help |
|
|||
3835 | return |
|
|||
3836 | elif '=' not in line: |
|
|||
3837 | raise UsageError("Invalid config statement: %r, should be Class.trait = value" % line) |
|
|||
3838 |
|
||||
3839 |
|
||||
3840 | # otherwise, assume we are setting configurables. |
|
|||
3841 | # leave quotes on args when splitting, because we want |
|
|||
3842 | # unquoted args to eval in user_ns |
|
|||
3843 | cfg = Config() |
|
|||
3844 | exec "cfg."+line in locals(), self.user_ns |
|
|||
3845 |
|
||||
3846 | for configurable in configurables: |
|
|||
3847 | try: |
|
|||
3848 | configurable.update_config(cfg) |
|
|||
3849 | except Exception as e: |
|
|||
3850 | error(e) |
|
|||
3851 |
|
||||
3852 | # end Magic |
|
@@ -612,7 +612,7 b' class AutoMagicChecker(PrefilterChecker):' | |||||
612 | check_esc_chars. This just checks for automagic. Also, before |
|
612 | check_esc_chars. This just checks for automagic. Also, before | |
613 | triggering the magic handler, make sure that there is nothing in the |
|
613 | triggering the magic handler, make sure that there is nothing in the | |
614 | user namespace which could shadow it.""" |
|
614 | user namespace which could shadow it.""" | |
615 |
if not self.shell.automagic or not |
|
615 | if not self.shell.automagic or not self.shell.find_magic(line_info.ifun): | |
616 | return None |
|
616 | return None | |
617 |
|
617 | |||
618 | # We have a likely magic method. Make sure we should actually call it. |
|
618 | # We have a likely magic method. Make sure we should actually call it. | |
@@ -807,7 +807,7 b' class AutoHandler(PrefilterHandler):' | |||||
807 | pre = line_info.pre |
|
807 | pre = line_info.pre | |
808 | esc = line_info.esc |
|
808 | esc = line_info.esc | |
809 | continue_prompt = line_info.continue_prompt |
|
809 | continue_prompt = line_info.continue_prompt | |
810 | obj = line_info.ofind(self)['obj'] |
|
810 | obj = line_info.ofind(self.shell)['obj'] | |
811 | #print 'pre <%s> ifun <%s> rest <%s>' % (pre,ifun,the_rest) # dbg |
|
811 | #print 'pre <%s> ifun <%s> rest <%s>' % (pre,ifun,the_rest) # dbg | |
812 |
|
812 | |||
813 | # This should only be active for single-line input! |
|
813 | # This should only be active for single-line input! | |
@@ -891,7 +891,7 b' class HelpHandler(PrefilterHandler):' | |||||
891 | line = line[:-1] |
|
891 | line = line[:-1] | |
892 | if line: |
|
892 | if line: | |
893 | #print 'line:<%r>' % line # dbg |
|
893 | #print 'line:<%r>' % line # dbg | |
894 |
self.shell.magic |
|
894 | self.shell.magic('pinfo %s' % line_info.ifun) | |
895 | else: |
|
895 | else: | |
896 | self.shell.show_usage() |
|
896 | self.shell.show_usage() | |
897 | return '' # Empty string is needed here! |
|
897 | return '' # Empty string is needed here! |
@@ -44,11 +44,12 b' from IPython.utils.encoding import get_stream_enc' | |||||
44 | line_split = re.compile(""" |
|
44 | line_split = re.compile(""" | |
45 | ^(\s*) # any leading space |
|
45 | ^(\s*) # any leading space | |
46 | ([,;/%]|!!?|\?\??)? # escape character or characters |
|
46 | ([,;/%]|!!?|\?\??)? # escape character or characters | |
47 |
\s*(% |
|
47 | \s*(%{0,2}[\w\.\*]*) # function/method, possibly with leading % | |
48 | # to correctly treat things like '?%magic' |
|
48 | # to correctly treat things like '?%magic' | |
49 | (.*?$|$) # rest of line |
|
49 | (.*?$|$) # rest of line | |
50 | """, re.VERBOSE) |
|
50 | """, re.VERBOSE) | |
51 |
|
51 | |||
|
52 | ||||
52 | def split_user_input(line, pattern=None): |
|
53 | def split_user_input(line, pattern=None): | |
53 | """Split user input into initial whitespace, escape character, function part |
|
54 | """Split user input into initial whitespace, escape character, function part | |
54 | and the rest. |
|
55 | and the rest. | |
@@ -76,6 +77,7 b' def split_user_input(line, pattern=None):' | |||||
76 | #print 'pre <%s> ifun <%s> rest <%s>' % (pre,ifun.strip(),the_rest) # dbg |
|
77 | #print 'pre <%s> ifun <%s> rest <%s>' % (pre,ifun.strip(),the_rest) # dbg | |
77 | return pre, esc or '', ifun.strip(), the_rest.lstrip() |
|
78 | return pre, esc or '', ifun.strip(), the_rest.lstrip() | |
78 |
|
79 | |||
|
80 | ||||
79 | class LineInfo(object): |
|
81 | class LineInfo(object): | |
80 | """A single line of input and associated info. |
|
82 | """A single line of input and associated info. | |
81 |
|
83 | |||
@@ -116,13 +118,11 b' class LineInfo(object):' | |||||
116 | else: |
|
118 | else: | |
117 | self.pre_whitespace = self.pre |
|
119 | self.pre_whitespace = self.pre | |
118 |
|
120 | |||
119 | self._oinfo = None |
|
|||
120 |
|
||||
121 | def ofind(self, ip): |
|
121 | def ofind(self, ip): | |
122 | """Do a full, attribute-walking lookup of the ifun in the various |
|
122 | """Do a full, attribute-walking lookup of the ifun in the various | |
123 | namespaces for the given IPython InteractiveShell instance. |
|
123 | namespaces for the given IPython InteractiveShell instance. | |
124 |
|
124 | |||
125 | Return a dict with keys: found,obj,ospace,ismagic |
|
125 | Return a dict with keys: {found, obj, ospace, ismagic} | |
126 |
|
126 | |||
127 | Note: can cause state changes because of calling getattr, but should |
|
127 | Note: can cause state changes because of calling getattr, but should | |
128 | only be run if autocall is on and if the line hasn't matched any |
|
128 | only be run if autocall is on and if the line hasn't matched any | |
@@ -131,10 +131,7 b' class LineInfo(object):' | |||||
131 | Does cache the results of the call, so can be called multiple times |
|
131 | Does cache the results of the call, so can be called multiple times | |
132 | without worrying about *further* damaging state. |
|
132 | without worrying about *further* damaging state. | |
133 | """ |
|
133 | """ | |
134 | if not self._oinfo: |
|
134 | return ip._ofind(self.ifun) | |
135 | # ip.shell._ofind is actually on the Magic class! |
|
|||
136 | self._oinfo = ip.shell._ofind(self.ifun) |
|
|||
137 | return self._oinfo |
|
|||
138 |
|
135 | |||
139 | def __str__(self): |
|
136 | def __str__(self): | |
140 | return "LineInfo [%s|%s|%s|%s]" %(self.pre, self.esc, self.ifun, self.the_rest) |
|
137 | return "LineInfo [%s|%s|%s|%s]" %(self.pre, self.esc, self.ifun, self.the_rest) |
@@ -119,8 +119,8 b' class CompletionSplitterTestCase(unittest.TestCase):' | |||||
119 | self.sp = completer.CompletionSplitter() |
|
119 | self.sp = completer.CompletionSplitter() | |
120 |
|
120 | |||
121 | def test_delim_setting(self): |
|
121 | def test_delim_setting(self): | |
122 |
self.sp. |
|
122 | self.sp.delims = ' ' | |
123 |
nt.assert_equal(self.sp. |
|
123 | nt.assert_equal(self.sp.delims, ' ') | |
124 | nt.assert_equal(self.sp._delim_expr, '[\ ]') |
|
124 | nt.assert_equal(self.sp._delim_expr, '[\ ]') | |
125 |
|
125 | |||
126 | def test_spaces(self): |
|
126 | def test_spaces(self): | |
@@ -202,13 +202,18 b' def test_local_file_completions():' | |||||
202 |
|
202 | |||
203 | def test_greedy_completions(): |
|
203 | def test_greedy_completions(): | |
204 | ip = get_ipython() |
|
204 | ip = get_ipython() | |
205 |
ip.Completer.greedy |
|
205 | greedy_original = ip.Completer.greedy | |
206 | ip.ex('a=range(5)') |
|
206 | try: | |
207 | _,c = ip.complete('.',line='a[0].') |
|
207 | ip.Completer.greedy = False | |
208 | nt.assert_false('a[0].real' in c, "Shouldn't have completed on a[0]: %s"%c) |
|
208 | ip.ex('a=range(5)') | |
209 | ip.Completer.greedy = True |
|
209 | _,c = ip.complete('.',line='a[0].') | |
210 | _,c = ip.complete('.',line='a[0].') |
|
210 | nt.assert_false('a[0].real' in c, | |
211 |
|
|
211 | "Shouldn't have completed on a[0]: %s"%c) | |
|
212 | ip.Completer.greedy = True | |||
|
213 | _,c = ip.complete('.',line='a[0].') | |||
|
214 | nt.assert_true('a[0].real' in c, "Should have completed on a[0]: %s"%c) | |||
|
215 | finally: | |||
|
216 | ip.Completer.greedy = greedy_original | |||
212 |
|
217 | |||
213 |
|
218 | |||
214 | def test_omit__names(): |
|
219 | def test_omit__names(): | |
@@ -280,7 +285,59 b' def test_func_kw_completions():' | |||||
280 | ip = get_ipython() |
|
285 | ip = get_ipython() | |
281 | c = ip.Completer |
|
286 | c = ip.Completer | |
282 | ip.ex('def myfunc(a=1,b=2): return a+b') |
|
287 | ip.ex('def myfunc(a=1,b=2): return a+b') | |
283 | s, matches = c.complete(None,'myfunc(1,b') |
|
288 | s, matches = c.complete(None, 'myfunc(1,b') | |
284 |
nt.assert_ |
|
289 | nt.assert_in('b=', matches) | |
285 | s, matches = c.complete(None,'myfunc(1,b)',10)#cursor is right after b |
|
290 | # Simulate completing with cursor right after b (pos==10): | |
286 | nt.assert_true('b=' in matches) |
|
291 | s, matches = c.complete(None,'myfunc(1,b)', 10) | |
|
292 | nt.assert_in('b=', matches) | |||
|
293 | ||||
|
294 | ||||
|
295 | def test_line_magics(): | |||
|
296 | ip = get_ipython() | |||
|
297 | c = ip.Completer | |||
|
298 | s, matches = c.complete(None, 'lsmag') | |||
|
299 | nt.assert_in('%lsmagic', matches) | |||
|
300 | s, matches = c.complete(None, '%lsmag') | |||
|
301 | nt.assert_in('%lsmagic', matches) | |||
|
302 | ||||
|
303 | ||||
|
304 | def test_cell_magics(): | |||
|
305 | from IPython.core.magic import register_cell_magic | |||
|
306 | ||||
|
307 | @register_cell_magic | |||
|
308 | def _foo_cellm(line, cell): | |||
|
309 | pass | |||
|
310 | ||||
|
311 | ip = get_ipython() | |||
|
312 | c = ip.Completer | |||
|
313 | ||||
|
314 | s, matches = c.complete(None, '_foo_ce') | |||
|
315 | nt.assert_in('%%_foo_cellm', matches) | |||
|
316 | s, matches = c.complete(None, '%%_foo_ce') | |||
|
317 | nt.assert_in('%%_foo_cellm', matches) | |||
|
318 | ||||
|
319 | ||||
|
320 | def test_line_cell_magics(): | |||
|
321 | from IPython.core.magic import register_line_cell_magic | |||
|
322 | ||||
|
323 | @register_line_cell_magic | |||
|
324 | def _bar_cellm(line, cell): | |||
|
325 | pass | |||
|
326 | ||||
|
327 | ip = get_ipython() | |||
|
328 | c = ip.Completer | |||
|
329 | ||||
|
330 | # The policy here is trickier, see comments in completion code. The | |||
|
331 | # returned values depend on whether the user passes %% or not explicitly, | |||
|
332 | # and this will show a difference if the same name is both a line and cell | |||
|
333 | # magic. | |||
|
334 | s, matches = c.complete(None, '_bar_ce') | |||
|
335 | nt.assert_in('%_bar_cellm', matches) | |||
|
336 | nt.assert_in('%%_bar_cellm', matches) | |||
|
337 | s, matches = c.complete(None, '%_bar_ce') | |||
|
338 | nt.assert_in('%_bar_cellm', matches) | |||
|
339 | nt.assert_in('%%_bar_cellm', matches) | |||
|
340 | s, matches = c.complete(None, '%%_bar_ce') | |||
|
341 | nt.assert_not_in('%_bar_cellm', matches) | |||
|
342 | nt.assert_in('%%_bar_cellm', matches) | |||
|
343 |
@@ -92,7 +92,7 b' def test_history():' | |||||
92 |
|
92 | |||
93 | # Cross testing: check that magic %save can get previous session. |
|
93 | # Cross testing: check that magic %save can get previous session. | |
94 | testfilename = os.path.realpath(os.path.join(tmpdir, "test.py")) |
|
94 | testfilename = os.path.realpath(os.path.join(tmpdir, "test.py")) | |
95 |
ip.magic |
|
95 | ip.magic("save " + testfilename + " ~1/1-3") | |
96 | with py3compat.open(testfilename) as testfile: |
|
96 | with py3compat.open(testfilename) as testfile: | |
97 | nt.assert_equal(testfile.read(), |
|
97 | nt.assert_equal(testfile.read(), | |
98 | u"# coding: utf-8\n" + u"\n".join(hist)) |
|
98 | u"# coding: utf-8\n" + u"\n".join(hist)) |
@@ -455,7 +455,8 b' syntax = \\' | |||||
455 | (u'?x1', "get_ipython().magic({u}'pinfo x1')"), |
|
455 | (u'?x1', "get_ipython().magic({u}'pinfo x1')"), | |
456 | (u'??x2', "get_ipython().magic({u}'pinfo2 x2')"), |
|
456 | (u'??x2', "get_ipython().magic({u}'pinfo2 x2')"), | |
457 | (u'?a.*s', "get_ipython().magic({u}'psearch a.*s')"), |
|
457 | (u'?a.*s', "get_ipython().magic({u}'psearch a.*s')"), | |
458 | (u'?%hist', "get_ipython().magic({u}'pinfo %hist')"), |
|
458 | (u'?%hist1', "get_ipython().magic({u}'pinfo %hist1')"), | |
|
459 | (u'?%%hist2', "get_ipython().magic({u}'pinfo %%hist2')"), | |||
459 | (u'?abc = qwe', "get_ipython().magic({u}'pinfo abc')"), |
|
460 | (u'?abc = qwe', "get_ipython().magic({u}'pinfo abc')"), | |
460 | ]], |
|
461 | ]], | |
461 |
|
462 | |||
@@ -463,13 +464,20 b' syntax = \\' | |||||
463 | [(i,py3compat.u_format(o)) for i,o in \ |
|
464 | [(i,py3compat.u_format(o)) for i,o in \ | |
464 | [ (u'x3?', "get_ipython().magic({u}'pinfo x3')"), |
|
465 | [ (u'x3?', "get_ipython().magic({u}'pinfo x3')"), | |
465 | (u'x4??', "get_ipython().magic({u}'pinfo2 x4')"), |
|
466 | (u'x4??', "get_ipython().magic({u}'pinfo2 x4')"), | |
466 | (u'%hist?', "get_ipython().magic({u}'pinfo %hist')"), |
|
467 | (u'%hist1?', "get_ipython().magic({u}'pinfo %hist1')"), | |
|
468 | (u'%hist2??', "get_ipython().magic({u}'pinfo2 %hist2')"), | |||
|
469 | (u'%%hist3?', "get_ipython().magic({u}'pinfo %%hist3')"), | |||
|
470 | (u'%%hist4??', "get_ipython().magic({u}'pinfo2 %%hist4')"), | |||
467 | (u'f*?', "get_ipython().magic({u}'psearch f*')"), |
|
471 | (u'f*?', "get_ipython().magic({u}'psearch f*')"), | |
468 | (u'ax.*aspe*?', "get_ipython().magic({u}'psearch ax.*aspe*')"), |
|
472 | (u'ax.*aspe*?', "get_ipython().magic({u}'psearch ax.*aspe*')"), | |
469 |
(u'a = abc?', "get_ipython(). |
|
473 | (u'a = abc?', "get_ipython().set_next_input({u}'a = abc');" | |
470 |
|
|
474 | "get_ipython().magic({u}'pinfo abc')"), | |
471 |
(u'a = |
|
475 | (u'a = abc.qe??', "get_ipython().set_next_input({u}'a = abc.qe');" | |
472 |
|
|
476 | "get_ipython().magic({u}'pinfo2 abc.qe')"), | |
|
477 | (u'a = *.items?', "get_ipython().set_next_input({u}'a = *.items');" | |||
|
478 | "get_ipython().magic({u}'psearch *.items')"), | |||
|
479 | (u'plot(a?', "get_ipython().set_next_input({u}'plot(a');" | |||
|
480 | "get_ipython().magic({u}'pinfo a')"), | |||
473 | (u'a*2 #comment?', 'a*2 #comment?'), |
|
481 | (u'a*2 #comment?', 'a*2 #comment?'), | |
474 | ]], |
|
482 | ]], | |
475 |
|
483 | |||
@@ -616,7 +624,7 b' class IPythonInputTestCase(InputSplitterTestCase):' | |||||
616 | if raw.startswith(' '): |
|
624 | if raw.startswith(' '): | |
617 | continue |
|
625 | continue | |
618 |
|
626 | |||
619 | isp.push(raw) |
|
627 | isp.push(raw+'\n') | |
620 | out, out_raw = isp.source_raw_reset() |
|
628 | out, out_raw = isp.source_raw_reset() | |
621 | self.assertEqual(out.rstrip(), out_t, |
|
629 | self.assertEqual(out.rstrip(), out_t, | |
622 | tt.pair_fail_msg.format("inputsplitter",raw, out_t, out)) |
|
630 | tt.pair_fail_msg.format("inputsplitter",raw, out_t, out)) | |
@@ -704,3 +712,87 b" if __name__ == '__main__':" | |||||
704 | print 'Raw source was:\n', raw |
|
712 | print 'Raw source was:\n', raw | |
705 | except EOFError: |
|
713 | except EOFError: | |
706 | print 'Bye' |
|
714 | print 'Bye' | |
|
715 | ||||
|
716 | # Tests for cell magics support | |||
|
717 | ||||
|
718 | def test_last_blank(): | |||
|
719 | nt.assert_false(isp.last_blank('')) | |||
|
720 | nt.assert_false(isp.last_blank('abc')) | |||
|
721 | nt.assert_false(isp.last_blank('abc\n')) | |||
|
722 | nt.assert_false(isp.last_blank('abc\na')) | |||
|
723 | ||||
|
724 | nt.assert_true(isp.last_blank('\n')) | |||
|
725 | nt.assert_true(isp.last_blank('\n ')) | |||
|
726 | nt.assert_true(isp.last_blank('abc\n ')) | |||
|
727 | nt.assert_true(isp.last_blank('abc\n\n')) | |||
|
728 | nt.assert_true(isp.last_blank('abc\nd\n\n')) | |||
|
729 | nt.assert_true(isp.last_blank('abc\nd\ne\n\n')) | |||
|
730 | nt.assert_true(isp.last_blank('abc \n \n \n\n')) | |||
|
731 | ||||
|
732 | ||||
|
733 | def test_last_two_blanks(): | |||
|
734 | nt.assert_false(isp.last_two_blanks('')) | |||
|
735 | nt.assert_false(isp.last_two_blanks('abc')) | |||
|
736 | nt.assert_false(isp.last_two_blanks('abc\n')) | |||
|
737 | nt.assert_false(isp.last_two_blanks('abc\n\na')) | |||
|
738 | nt.assert_false(isp.last_two_blanks('abc\n \n')) | |||
|
739 | nt.assert_false(isp.last_two_blanks('abc\n\n')) | |||
|
740 | ||||
|
741 | nt.assert_true(isp.last_two_blanks('\n\n')) | |||
|
742 | nt.assert_true(isp.last_two_blanks('\n\n ')) | |||
|
743 | nt.assert_true(isp.last_two_blanks('\n \n')) | |||
|
744 | nt.assert_true(isp.last_two_blanks('abc\n\n ')) | |||
|
745 | nt.assert_true(isp.last_two_blanks('abc\n\n\n')) | |||
|
746 | nt.assert_true(isp.last_two_blanks('abc\n\n \n')) | |||
|
747 | nt.assert_true(isp.last_two_blanks('abc\n\n \n ')) | |||
|
748 | nt.assert_true(isp.last_two_blanks('abc\n\n \n \n')) | |||
|
749 | nt.assert_true(isp.last_two_blanks('abc\nd\n\n\n')) | |||
|
750 | nt.assert_true(isp.last_two_blanks('abc\nd\ne\nf\n\n\n')) | |||
|
751 | ||||
|
752 | ||||
|
753 | class CellMagicsCommon(object): | |||
|
754 | ||||
|
755 | def test_whole_cell(self): | |||
|
756 | src = "%%cellm line\nbody\n" | |||
|
757 | sp = self.sp | |||
|
758 | sp.push(src) | |||
|
759 | nt.assert_equal(sp.cell_magic_parts, ['body\n']) | |||
|
760 | out = sp.source | |||
|
761 | ref = u"get_ipython()._run_cached_cell_magic({u}'cellm', {u}'line')\n" | |||
|
762 | nt.assert_equal(out, py3compat.u_format(ref)) | |||
|
763 | ||||
|
764 | def tearDown(self): | |||
|
765 | self.sp.reset() | |||
|
766 | ||||
|
767 | ||||
|
768 | class CellModeCellMagics(CellMagicsCommon, unittest.TestCase): | |||
|
769 | sp = isp.IPythonInputSplitter(input_mode='cell') | |||
|
770 | ||||
|
771 | def test_incremental(self): | |||
|
772 | sp = self.sp | |||
|
773 | src = '%%cellm line2\n' | |||
|
774 | sp.push(src) | |||
|
775 | nt.assert_true(sp.push_accepts_more()) #1 | |||
|
776 | src += '\n' | |||
|
777 | sp.push(src) | |||
|
778 | # Note: if we ever change the logic to allow full blank lines (see | |||
|
779 | # _handle_cell_magic), then the following test should change to true | |||
|
780 | nt.assert_false(sp.push_accepts_more()) #2 | |||
|
781 | # By now, even with full blanks allowed, a second blank should signal | |||
|
782 | # the end. For now this test is only a redundancy safety, but don't | |||
|
783 | # delete it in case we change our mind and the previous one goes to | |||
|
784 | # true. | |||
|
785 | src += '\n' | |||
|
786 | sp.push(src) | |||
|
787 | nt.assert_false(sp.push_accepts_more()) #3 | |||
|
788 | ||||
|
789 | ||||
|
790 | class LineModeCellMagics(CellMagicsCommon, unittest.TestCase): | |||
|
791 | sp = isp.IPythonInputSplitter(input_mode='line') | |||
|
792 | ||||
|
793 | def test_incremental(self): | |||
|
794 | sp = self.sp | |||
|
795 | sp.push('%%cellm line2\n') | |||
|
796 | nt.assert_true(sp.push_accepts_more()) #1 | |||
|
797 | sp.push('\n') | |||
|
798 | nt.assert_false(sp.push_accepts_more()) #2 |
@@ -22,23 +22,32 b' Authors' | |||||
22 | # stdlib |
|
22 | # stdlib | |
23 | import os |
|
23 | import os | |
24 | import shutil |
|
24 | import shutil | |
|
25 | import sys | |||
25 | import tempfile |
|
26 | import tempfile | |
26 | import unittest |
|
27 | import unittest | |
27 | from os.path import join |
|
28 | from os.path import join | |
28 | import sys |
|
|||
29 | from StringIO import StringIO |
|
29 | from StringIO import StringIO | |
30 |
|
30 | |||
|
31 | # third-party | |||
|
32 | import nose.tools as nt | |||
|
33 | ||||
|
34 | # Our own | |||
31 | from IPython.testing.decorators import skipif |
|
35 | from IPython.testing.decorators import skipif | |
32 | from IPython.utils import io |
|
36 | from IPython.utils import io | |
33 |
|
37 | |||
34 | #----------------------------------------------------------------------------- |
|
38 | #----------------------------------------------------------------------------- | |
|
39 | # Globals | |||
|
40 | #----------------------------------------------------------------------------- | |||
|
41 | # This is used by every single test, no point repeating it ad nauseam | |||
|
42 | ip = get_ipython() | |||
|
43 | ||||
|
44 | #----------------------------------------------------------------------------- | |||
35 | # Tests |
|
45 | # Tests | |
36 | #----------------------------------------------------------------------------- |
|
46 | #----------------------------------------------------------------------------- | |
37 |
|
47 | |||
38 | class InteractiveShellTestCase(unittest.TestCase): |
|
48 | class InteractiveShellTestCase(unittest.TestCase): | |
39 | def test_naked_string_cells(self): |
|
49 | def test_naked_string_cells(self): | |
40 | """Test that cells with only naked strings are fully executed""" |
|
50 | """Test that cells with only naked strings are fully executed""" | |
41 | ip = get_ipython() |
|
|||
42 | # First, single-line inputs |
|
51 | # First, single-line inputs | |
43 | ip.run_cell('"a"\n') |
|
52 | ip.run_cell('"a"\n') | |
44 | self.assertEquals(ip.user_ns['_'], 'a') |
|
53 | self.assertEquals(ip.user_ns['_'], 'a') | |
@@ -49,7 +58,6 b' class InteractiveShellTestCase(unittest.TestCase):' | |||||
49 | def test_run_empty_cell(self): |
|
58 | def test_run_empty_cell(self): | |
50 | """Just make sure we don't get a horrible error with a blank |
|
59 | """Just make sure we don't get a horrible error with a blank | |
51 | cell of input. Yes, I did overlook that.""" |
|
60 | cell of input. Yes, I did overlook that.""" | |
52 | ip = get_ipython() |
|
|||
53 | old_xc = ip.execution_count |
|
61 | old_xc = ip.execution_count | |
54 | ip.run_cell('') |
|
62 | ip.run_cell('') | |
55 | self.assertEquals(ip.execution_count, old_xc) |
|
63 | self.assertEquals(ip.execution_count, old_xc) | |
@@ -57,7 +65,6 b' class InteractiveShellTestCase(unittest.TestCase):' | |||||
57 | def test_run_cell_multiline(self): |
|
65 | def test_run_cell_multiline(self): | |
58 | """Multi-block, multi-line cells must execute correctly. |
|
66 | """Multi-block, multi-line cells must execute correctly. | |
59 | """ |
|
67 | """ | |
60 | ip = get_ipython() |
|
|||
61 | src = '\n'.join(["x=1", |
|
68 | src = '\n'.join(["x=1", | |
62 | "y=2", |
|
69 | "y=2", | |
63 | "if 1:", |
|
70 | "if 1:", | |
@@ -69,7 +76,6 b' class InteractiveShellTestCase(unittest.TestCase):' | |||||
69 |
|
76 | |||
70 | def test_multiline_string_cells(self): |
|
77 | def test_multiline_string_cells(self): | |
71 | "Code sprinkled with multiline strings should execute (GH-306)" |
|
78 | "Code sprinkled with multiline strings should execute (GH-306)" | |
72 | ip = get_ipython() |
|
|||
73 | ip.run_cell('tmp=0') |
|
79 | ip.run_cell('tmp=0') | |
74 | self.assertEquals(ip.user_ns['tmp'], 0) |
|
80 | self.assertEquals(ip.user_ns['tmp'], 0) | |
75 | ip.run_cell('tmp=1;"""a\nb"""\n') |
|
81 | ip.run_cell('tmp=1;"""a\nb"""\n') | |
@@ -77,7 +83,6 b' class InteractiveShellTestCase(unittest.TestCase):' | |||||
77 |
|
83 | |||
78 | def test_dont_cache_with_semicolon(self): |
|
84 | def test_dont_cache_with_semicolon(self): | |
79 | "Ending a line with semicolon should not cache the returned object (GH-307)" |
|
85 | "Ending a line with semicolon should not cache the returned object (GH-307)" | |
80 | ip = get_ipython() |
|
|||
81 | oldlen = len(ip.user_ns['Out']) |
|
86 | oldlen = len(ip.user_ns['Out']) | |
82 | a = ip.run_cell('1;', store_history=True) |
|
87 | a = ip.run_cell('1;', store_history=True) | |
83 | newlen = len(ip.user_ns['Out']) |
|
88 | newlen = len(ip.user_ns['Out']) | |
@@ -89,7 +94,6 b' class InteractiveShellTestCase(unittest.TestCase):' | |||||
89 |
|
94 | |||
90 | def test_In_variable(self): |
|
95 | def test_In_variable(self): | |
91 | "Verify that In variable grows with user input (GH-284)" |
|
96 | "Verify that In variable grows with user input (GH-284)" | |
92 | ip = get_ipython() |
|
|||
93 | oldlen = len(ip.user_ns['In']) |
|
97 | oldlen = len(ip.user_ns['In']) | |
94 | ip.run_cell('1;', store_history=True) |
|
98 | ip.run_cell('1;', store_history=True) | |
95 | newlen = len(ip.user_ns['In']) |
|
99 | newlen = len(ip.user_ns['In']) | |
@@ -97,13 +101,11 b' class InteractiveShellTestCase(unittest.TestCase):' | |||||
97 | self.assertEquals(ip.user_ns['In'][-1],'1;') |
|
101 | self.assertEquals(ip.user_ns['In'][-1],'1;') | |
98 |
|
102 | |||
99 | def test_magic_names_in_string(self): |
|
103 | def test_magic_names_in_string(self): | |
100 | ip = get_ipython() |
|
|||
101 | ip.run_cell('a = """\n%exit\n"""') |
|
104 | ip.run_cell('a = """\n%exit\n"""') | |
102 | self.assertEquals(ip.user_ns['a'], '\n%exit\n') |
|
105 | self.assertEquals(ip.user_ns['a'], '\n%exit\n') | |
103 |
|
106 | |||
104 | def test_alias_crash(self): |
|
107 | def test_alias_crash(self): | |
105 | """Errors in prefilter can't crash IPython""" |
|
108 | """Errors in prefilter can't crash IPython""" | |
106 | ip = get_ipython() |
|
|||
107 | ip.run_cell('%alias parts echo first %s second %s') |
|
109 | ip.run_cell('%alias parts echo first %s second %s') | |
108 | # capture stderr: |
|
110 | # capture stderr: | |
109 | save_err = io.stderr |
|
111 | save_err = io.stderr | |
@@ -115,7 +117,6 b' class InteractiveShellTestCase(unittest.TestCase):' | |||||
115 |
|
117 | |||
116 | def test_trailing_newline(self): |
|
118 | def test_trailing_newline(self): | |
117 | """test that running !(command) does not raise a SyntaxError""" |
|
119 | """test that running !(command) does not raise a SyntaxError""" | |
118 | ip = get_ipython() |
|
|||
119 | ip.run_cell('!(true)\n', False) |
|
120 | ip.run_cell('!(true)\n', False) | |
120 | ip.run_cell('!(true)\n\n\n', False) |
|
121 | ip.run_cell('!(true)\n\n\n', False) | |
121 |
|
122 | |||
@@ -132,7 +133,6 b' class InteractiveShellTestCase(unittest.TestCase):' | |||||
132 |
|
133 | |||
133 | def test_future_flags(self): |
|
134 | def test_future_flags(self): | |
134 | """Check that future flags are used for parsing code (gh-777)""" |
|
135 | """Check that future flags are used for parsing code (gh-777)""" | |
135 | ip = get_ipython() |
|
|||
136 | ip.run_cell('from __future__ import print_function') |
|
136 | ip.run_cell('from __future__ import print_function') | |
137 | try: |
|
137 | try: | |
138 | ip.run_cell('prfunc_return_val = print(1,2, sep=" ")') |
|
138 | ip.run_cell('prfunc_return_val = print(1,2, sep=" ")') | |
@@ -143,7 +143,6 b' class InteractiveShellTestCase(unittest.TestCase):' | |||||
143 |
|
143 | |||
144 | def test_future_unicode(self): |
|
144 | def test_future_unicode(self): | |
145 | """Check that unicode_literals is imported from __future__ (gh #786)""" |
|
145 | """Check that unicode_literals is imported from __future__ (gh #786)""" | |
146 | ip = get_ipython() |
|
|||
147 | try: |
|
146 | try: | |
148 | ip.run_cell(u'byte_str = "a"') |
|
147 | ip.run_cell(u'byte_str = "a"') | |
149 | assert isinstance(ip.user_ns['byte_str'], str) # string literals are byte strings by default |
|
148 | assert isinstance(ip.user_ns['byte_str'], str) # string literals are byte strings by default | |
@@ -187,7 +186,6 b' class InteractiveShellTestCase(unittest.TestCase):' | |||||
187 |
|
186 | |||
188 | def test_bad_custom_tb(self): |
|
187 | def test_bad_custom_tb(self): | |
189 | """Check that InteractiveShell is protected from bad custom exception handlers""" |
|
188 | """Check that InteractiveShell is protected from bad custom exception handlers""" | |
190 | ip = get_ipython() |
|
|||
191 | from IPython.utils import io |
|
189 | from IPython.utils import io | |
192 | save_stderr = io.stderr |
|
190 | save_stderr = io.stderr | |
193 | try: |
|
191 | try: | |
@@ -203,7 +201,6 b' class InteractiveShellTestCase(unittest.TestCase):' | |||||
203 |
|
201 | |||
204 | def test_bad_custom_tb_return(self): |
|
202 | def test_bad_custom_tb_return(self): | |
205 | """Check that InteractiveShell is protected from bad return types in custom exception handlers""" |
|
203 | """Check that InteractiveShell is protected from bad return types in custom exception handlers""" | |
206 | ip = get_ipython() |
|
|||
207 | from IPython.utils import io |
|
204 | from IPython.utils import io | |
208 | save_stderr = io.stderr |
|
205 | save_stderr = io.stderr | |
209 | try: |
|
206 | try: | |
@@ -218,7 +215,6 b' class InteractiveShellTestCase(unittest.TestCase):' | |||||
218 | io.stderr = save_stderr |
|
215 | io.stderr = save_stderr | |
219 |
|
216 | |||
220 | def test_drop_by_id(self): |
|
217 | def test_drop_by_id(self): | |
221 | ip = get_ipython() |
|
|||
222 | myvars = {"a":object(), "b":object(), "c": object()} |
|
218 | myvars = {"a":object(), "b":object(), "c": object()} | |
223 | ip.push(myvars, interactive=False) |
|
219 | ip.push(myvars, interactive=False) | |
224 | for name in myvars: |
|
220 | for name in myvars: | |
@@ -233,7 +229,6 b' class InteractiveShellTestCase(unittest.TestCase):' | |||||
233 | ip.reset() |
|
229 | ip.reset() | |
234 |
|
230 | |||
235 | def test_var_expand(self): |
|
231 | def test_var_expand(self): | |
236 | ip = get_ipython() |
|
|||
237 | ip.user_ns['f'] = u'Ca\xf1o' |
|
232 | ip.user_ns['f'] = u'Ca\xf1o' | |
238 | self.assertEqual(ip.var_expand(u'echo $f'), u'echo Ca\xf1o') |
|
233 | self.assertEqual(ip.var_expand(u'echo $f'), u'echo Ca\xf1o') | |
239 | self.assertEqual(ip.var_expand(u'echo {f}'), u'echo Ca\xf1o') |
|
234 | self.assertEqual(ip.var_expand(u'echo {f}'), u'echo Ca\xf1o') | |
@@ -246,8 +241,6 b' class InteractiveShellTestCase(unittest.TestCase):' | |||||
246 |
|
241 | |||
247 | def test_bad_var_expand(self): |
|
242 | def test_bad_var_expand(self): | |
248 | """var_expand on invalid formats shouldn't raise""" |
|
243 | """var_expand on invalid formats shouldn't raise""" | |
249 | ip = get_ipython() |
|
|||
250 |
|
||||
251 | # SyntaxError |
|
244 | # SyntaxError | |
252 | self.assertEqual(ip.var_expand(u"{'a':5}"), u"{'a':5}") |
|
245 | self.assertEqual(ip.var_expand(u"{'a':5}"), u"{'a':5}") | |
253 | # NameError |
|
246 | # NameError | |
@@ -257,8 +250,6 b' class InteractiveShellTestCase(unittest.TestCase):' | |||||
257 |
|
250 | |||
258 | def test_silent_nopostexec(self): |
|
251 | def test_silent_nopostexec(self): | |
259 | """run_cell(silent=True) doesn't invoke post-exec funcs""" |
|
252 | """run_cell(silent=True) doesn't invoke post-exec funcs""" | |
260 | ip = get_ipython() |
|
|||
261 |
|
||||
262 | d = dict(called=False) |
|
253 | d = dict(called=False) | |
263 | def set_called(): |
|
254 | def set_called(): | |
264 | d['called'] = True |
|
255 | d['called'] = True | |
@@ -275,8 +266,6 b' class InteractiveShellTestCase(unittest.TestCase):' | |||||
275 |
|
266 | |||
276 | def test_silent_noadvance(self): |
|
267 | def test_silent_noadvance(self): | |
277 | """run_cell(silent=True) doesn't advance execution_count""" |
|
268 | """run_cell(silent=True) doesn't advance execution_count""" | |
278 | ip = get_ipython() |
|
|||
279 |
|
||||
280 | ec = ip.execution_count |
|
269 | ec = ip.execution_count | |
281 | # silent should force store_history=False |
|
270 | # silent should force store_history=False | |
282 | ip.run_cell("1", store_history=True, silent=True) |
|
271 | ip.run_cell("1", store_history=True, silent=True) | |
@@ -289,8 +278,6 b' class InteractiveShellTestCase(unittest.TestCase):' | |||||
289 |
|
278 | |||
290 | def test_silent_nodisplayhook(self): |
|
279 | def test_silent_nodisplayhook(self): | |
291 | """run_cell(silent=True) doesn't trigger displayhook""" |
|
280 | """run_cell(silent=True) doesn't trigger displayhook""" | |
292 | ip = get_ipython() |
|
|||
293 |
|
||||
294 | d = dict(called=False) |
|
281 | d = dict(called=False) | |
295 |
|
282 | |||
296 | trap = ip.display_trap |
|
283 | trap = ip.display_trap | |
@@ -322,6 +309,35 b' class InteractiveShellTestCase(unittest.TestCase):' | |||||
322 | In [2]: print 1,; print 2 |
|
309 | In [2]: print 1,; print 2 | |
323 | 1 2 |
|
310 | 1 2 | |
324 | """ |
|
311 | """ | |
|
312 | ||||
|
313 | def test_ofind_line_magic(self): | |||
|
314 | from IPython.core.magic import register_line_magic | |||
|
315 | ||||
|
316 | @register_line_magic | |||
|
317 | def lmagic(line): | |||
|
318 | "A line magic" | |||
|
319 | ||||
|
320 | # Get info on line magic | |||
|
321 | lfind = ip._ofind('lmagic') | |||
|
322 | info = dict(found=True, isalias=False, ismagic=True, | |||
|
323 | namespace = 'IPython internal', obj= lmagic.__wrapped__, | |||
|
324 | parent = None) | |||
|
325 | nt.assert_equal(lfind, info) | |||
|
326 | ||||
|
327 | def test_ofind_cell_magic(self): | |||
|
328 | from IPython.core.magic import register_cell_magic | |||
|
329 | ||||
|
330 | @register_cell_magic | |||
|
331 | def cmagic(line, cell): | |||
|
332 | "A cell magic" | |||
|
333 | ||||
|
334 | # Get info on cell magic | |||
|
335 | find = ip._ofind('cmagic') | |||
|
336 | info = dict(found=True, isalias=False, ismagic=True, | |||
|
337 | namespace = 'IPython internal', obj= cmagic.__wrapped__, | |||
|
338 | parent = None) | |||
|
339 | nt.assert_equal(find, info) | |||
|
340 | ||||
325 |
|
341 | |||
326 | class TestSafeExecfileNonAsciiPath(unittest.TestCase): |
|
342 | class TestSafeExecfileNonAsciiPath(unittest.TestCase): | |
327 |
|
343 | |||
@@ -335,7 +351,6 b' class TestSafeExecfileNonAsciiPath(unittest.TestCase):' | |||||
335 | os.chdir(self.TESTDIR) |
|
351 | os.chdir(self.TESTDIR) | |
336 | self.fname = u"åäötestscript.py" |
|
352 | self.fname = u"åäötestscript.py" | |
337 |
|
353 | |||
338 |
|
||||
339 | def tearDown(self): |
|
354 | def tearDown(self): | |
340 | os.chdir(self.oldpath) |
|
355 | os.chdir(self.oldpath) | |
341 | shutil.rmtree(self.BASETESTDIR) |
|
356 | shutil.rmtree(self.BASETESTDIR) | |
@@ -343,7 +358,7 b' class TestSafeExecfileNonAsciiPath(unittest.TestCase):' | |||||
343 | def test_1(self): |
|
358 | def test_1(self): | |
344 | """Test safe_execfile with non-ascii path |
|
359 | """Test safe_execfile with non-ascii path | |
345 | """ |
|
360 | """ | |
346 |
|
|
361 | ip.safe_execfile(self.fname, {}, raise_exceptions=True) | |
347 |
|
362 | |||
348 |
|
363 | |||
349 | class TestSystemRaw(unittest.TestCase): |
|
364 | class TestSystemRaw(unittest.TestCase): | |
@@ -351,7 +366,7 b' class TestSystemRaw(unittest.TestCase):' | |||||
351 | """Test system_raw with non-ascii cmd |
|
366 | """Test system_raw with non-ascii cmd | |
352 | """ |
|
367 | """ | |
353 | cmd = ur'''python -c "'åäö'" ''' |
|
368 | cmd = ur'''python -c "'åäö'" ''' | |
354 |
|
|
369 | ip.system_raw(cmd) | |
355 |
|
370 | |||
356 |
|
371 | |||
357 | def test__IPYTHON__(): |
|
372 | def test__IPYTHON__(): |
@@ -13,9 +13,16 b' import io' | |||||
13 | import os |
|
13 | import os | |
14 | import sys |
|
14 | import sys | |
15 | from StringIO import StringIO |
|
15 | from StringIO import StringIO | |
|
16 | from unittest import TestCase | |||
16 |
|
17 | |||
17 | import nose.tools as nt |
|
18 | import nose.tools as nt | |
18 |
|
19 | |||
|
20 | from IPython.core import magic | |||
|
21 | from IPython.core.magic import (Magics, magics_class, line_magic, | |||
|
22 | cell_magic, line_cell_magic, | |||
|
23 | register_line_magic, register_cell_magic, | |||
|
24 | register_line_cell_magic) | |||
|
25 | from IPython.core.magics import execution | |||
19 | from IPython.nbformat.v3.tests.nbexamples import nb0 |
|
26 | from IPython.nbformat.v3.tests.nbexamples import nb0 | |
20 | from IPython.nbformat import current |
|
27 | from IPython.nbformat import current | |
21 | from IPython.testing import decorators as dec |
|
28 | from IPython.testing import decorators as dec | |
@@ -27,6 +34,8 b' from IPython.utils.tempdir import TemporaryDirectory' | |||||
27 | # Test functions begin |
|
34 | # Test functions begin | |
28 | #----------------------------------------------------------------------------- |
|
35 | #----------------------------------------------------------------------------- | |
29 |
|
36 | |||
|
37 | @magic.magics_class | |||
|
38 | class DummyMagics(magic.Magics): pass | |||
30 |
|
39 | |||
31 | def test_rehashx(): |
|
40 | def test_rehashx(): | |
32 | # clear up everything |
|
41 | # clear up everything | |
@@ -51,7 +60,8 b' def test_magic_parse_options():' | |||||
51 | """Test that we don't mangle paths when parsing magic options.""" |
|
60 | """Test that we don't mangle paths when parsing magic options.""" | |
52 | ip = get_ipython() |
|
61 | ip = get_ipython() | |
53 | path = 'c:\\x' |
|
62 | path = 'c:\\x' | |
54 | opts = ip.parse_options('-f %s' % path,'f:')[0] |
|
63 | m = DummyMagics(ip) | |
|
64 | opts = m.parse_options('-f %s' % path,'f:')[0] | |||
55 | # argv splitting is os-dependent |
|
65 | # argv splitting is os-dependent | |
56 | if os.name == 'posix': |
|
66 | if os.name == 'posix': | |
57 | expected = 'c:x' |
|
67 | expected = 'c:x' | |
@@ -284,8 +294,9 b' def test_parse_options():' | |||||
284 | """Tests for basic options parsing in magics.""" |
|
294 | """Tests for basic options parsing in magics.""" | |
285 | # These are only the most minimal of tests, more should be added later. At |
|
295 | # These are only the most minimal of tests, more should be added later. At | |
286 | # the very least we check that basic text/unicode calls work OK. |
|
296 | # the very least we check that basic text/unicode calls work OK. | |
287 | nt.assert_equal(_ip.parse_options('foo', '')[1], 'foo') |
|
297 | m = DummyMagics(_ip) | |
288 |
nt.assert_equal( |
|
298 | nt.assert_equal(m.parse_options('foo', '')[1], 'foo') | |
|
299 | nt.assert_equal(m.parse_options(u'foo', '')[1], u'foo') | |||
289 |
|
300 | |||
290 |
|
301 | |||
291 | def test_dirops(): |
|
302 | def test_dirops(): | |
@@ -326,7 +337,7 b' def test_reset_hard():' | |||||
326 | _ip.run_cell("a") |
|
337 | _ip.run_cell("a") | |
327 |
|
338 | |||
328 | nt.assert_equal(monitor, []) |
|
339 | nt.assert_equal(monitor, []) | |
329 |
_ip.magic |
|
340 | _ip.magic("reset -f") | |
330 | nt.assert_equal(monitor, [1]) |
|
341 | nt.assert_equal(monitor, [1]) | |
331 |
|
342 | |||
332 | class TestXdel(tt.TempFileMixin): |
|
343 | class TestXdel(tt.TempFileMixin): | |
@@ -388,7 +399,7 b' def test_whos():' | |||||
388 | def doctest_precision(): |
|
399 | def doctest_precision(): | |
389 | """doctest for %precision |
|
400 | """doctest for %precision | |
390 |
|
401 | |||
391 |
In [1]: f = get_ipython(). |
|
402 | In [1]: f = get_ipython().display_formatter.formatters['text/plain'] | |
392 |
|
403 | |||
393 | In [2]: %precision 5 |
|
404 | In [2]: %precision 5 | |
394 | Out[2]: {u}'%.5f' |
|
405 | Out[2]: {u}'%.5f' | |
@@ -422,7 +433,8 b' def test_timeit_arguments():' | |||||
422 | "Test valid timeit arguments, should not cause SyntaxError (GH #1269)" |
|
433 | "Test valid timeit arguments, should not cause SyntaxError (GH #1269)" | |
423 | _ip.magic("timeit ('#')") |
|
434 | _ip.magic("timeit ('#')") | |
424 |
|
435 | |||
425 | @dec.skipif(_ip.magic_prun == _ip.profile_missing_notice) |
|
436 | ||
|
437 | @dec.skipif(execution.profile is None) | |||
426 | def test_prun_quotes(): |
|
438 | def test_prun_quotes(): | |
427 | "Test that prun does not clobber string escapes (GH #1302)" |
|
439 | "Test that prun does not clobber string escapes (GH #1302)" | |
428 | _ip.magic("prun -q x = '\t'") |
|
440 | _ip.magic("prun -q x = '\t'") | |
@@ -478,3 +490,58 b' def test_notebook_reformat_json():' | |||||
478 | def test_env(): |
|
490 | def test_env(): | |
479 | env = _ip.magic("env") |
|
491 | env = _ip.magic("env") | |
480 | assert isinstance(env, dict), type(env) |
|
492 | assert isinstance(env, dict), type(env) | |
|
493 | ||||
|
494 | ||||
|
495 | class CellMagicTestCase(TestCase): | |||
|
496 | ||||
|
497 | def check_ident(self, magic): | |||
|
498 | # Manually called, we get the result | |||
|
499 | out = _ip.run_cell_magic(magic, 'a', 'b') | |||
|
500 | nt.assert_equals(out, ('a','b')) | |||
|
501 | # Via run_cell, it goes into the user's namespace via displayhook | |||
|
502 | _ip.run_cell('%%' + magic +' c\nd') | |||
|
503 | nt.assert_equals(_ip.user_ns['_'], ('c','d')) | |||
|
504 | ||||
|
505 | def test_cell_magic_func_deco(self): | |||
|
506 | "Cell magic using simple decorator" | |||
|
507 | @register_cell_magic | |||
|
508 | def cellm(line, cell): | |||
|
509 | return line, cell | |||
|
510 | ||||
|
511 | self.check_ident('cellm') | |||
|
512 | ||||
|
513 | def test_cell_magic_reg(self): | |||
|
514 | "Cell magic manually registered" | |||
|
515 | def cellm(line, cell): | |||
|
516 | return line, cell | |||
|
517 | ||||
|
518 | _ip.register_magic_function(cellm, 'cell', 'cellm2') | |||
|
519 | self.check_ident('cellm2') | |||
|
520 | ||||
|
521 | def test_cell_magic_class(self): | |||
|
522 | "Cell magics declared via a class" | |||
|
523 | @magics_class | |||
|
524 | class MyMagics(Magics): | |||
|
525 | ||||
|
526 | @cell_magic | |||
|
527 | def cellm3(self, line, cell): | |||
|
528 | return line, cell | |||
|
529 | ||||
|
530 | _ip.register_magics(MyMagics) | |||
|
531 | self.check_ident('cellm3') | |||
|
532 | ||||
|
533 | def test_cell_magic_class2(self): | |||
|
534 | "Cell magics declared via a class, #2" | |||
|
535 | @magics_class | |||
|
536 | class MyMagics2(Magics): | |||
|
537 | ||||
|
538 | @cell_magic('cellm4') | |||
|
539 | def cellm33(self, line, cell): | |||
|
540 | return line, cell | |||
|
541 | ||||
|
542 | _ip.register_magics(MyMagics2) | |||
|
543 | self.check_ident('cellm4') | |||
|
544 | # Check that nothing is registered as 'cellm33' | |||
|
545 | c33 = _ip.find_cell_magic('cellm33') | |||
|
546 | nt.assert_equals(c33, None) | |||
|
547 |
@@ -20,6 +20,10 b' import nose.tools as nt' | |||||
20 |
|
20 | |||
21 | # Our own imports |
|
21 | # Our own imports | |
22 | from .. import oinspect |
|
22 | from .. import oinspect | |
|
23 | from IPython.core.magic import (Magics, magics_class, line_magic, | |||
|
24 | cell_magic, line_cell_magic, | |||
|
25 | register_line_magic, register_cell_magic, | |||
|
26 | register_line_cell_magic) | |||
23 | from IPython.utils import py3compat |
|
27 | from IPython.utils import py3compat | |
24 |
|
28 | |||
25 | #----------------------------------------------------------------------------- |
|
29 | #----------------------------------------------------------------------------- | |
@@ -27,6 +31,7 b' from IPython.utils import py3compat' | |||||
27 | #----------------------------------------------------------------------------- |
|
31 | #----------------------------------------------------------------------------- | |
28 |
|
32 | |||
29 | inspector = oinspect.Inspector() |
|
33 | inspector = oinspect.Inspector() | |
|
34 | ip = get_ipython() | |||
30 |
|
35 | |||
31 | #----------------------------------------------------------------------------- |
|
36 | #----------------------------------------------------------------------------- | |
32 | # Local utilities |
|
37 | # Local utilities | |
@@ -46,17 +51,50 b' class Call(object):' | |||||
46 | def method(self, x, z=2): |
|
51 | def method(self, x, z=2): | |
47 | """Some method's docstring""" |
|
52 | """Some method's docstring""" | |
48 |
|
53 | |||
|
54 | ||||
49 | class OldStyle: |
|
55 | class OldStyle: | |
50 | """An old-style class for testing.""" |
|
56 | """An old-style class for testing.""" | |
51 | pass |
|
57 | pass | |
52 |
|
58 | |||
|
59 | ||||
53 | def f(x, y=2, *a, **kw): |
|
60 | def f(x, y=2, *a, **kw): | |
54 | """A simple function.""" |
|
61 | """A simple function.""" | |
55 |
|
62 | |||
|
63 | ||||
56 | def g(y, z=3, *a, **kw): |
|
64 | def g(y, z=3, *a, **kw): | |
57 | pass # no docstring |
|
65 | pass # no docstring | |
58 |
|
66 | |||
59 |
|
67 | |||
|
68 | @register_line_magic | |||
|
69 | def lmagic(line): | |||
|
70 | "A line magic" | |||
|
71 | ||||
|
72 | ||||
|
73 | @register_cell_magic | |||
|
74 | def cmagic(line, cell): | |||
|
75 | "A cell magic" | |||
|
76 | ||||
|
77 | ||||
|
78 | @register_line_cell_magic | |||
|
79 | def lcmagic(line, cell=None): | |||
|
80 | "A line/cell magic" | |||
|
81 | ||||
|
82 | ||||
|
83 | @magics_class | |||
|
84 | class SimpleMagics(Magics): | |||
|
85 | @line_magic | |||
|
86 | def Clmagic(self, cline): | |||
|
87 | "A class-based line magic" | |||
|
88 | ||||
|
89 | @cell_magic | |||
|
90 | def Ccmagic(self, cline, ccell): | |||
|
91 | "A class-based cell magic" | |||
|
92 | ||||
|
93 | @line_cell_magic | |||
|
94 | def Clcmagic(self, cline, ccell=None): | |||
|
95 | "A class-based line/cell magic" | |||
|
96 | ||||
|
97 | ||||
60 | def check_calltip(obj, name, call, docstring): |
|
98 | def check_calltip(obj, name, call, docstring): | |
61 | """Generic check pattern all calltip tests will use""" |
|
99 | """Generic check pattern all calltip tests will use""" | |
62 | info = inspector.info(obj, name) |
|
100 | info = inspector.info(obj, name) | |
@@ -93,6 +131,31 b' def test_calltip_function2():' | |||||
93 | def test_calltip_builtin(): |
|
131 | def test_calltip_builtin(): | |
94 | check_calltip(sum, 'sum', None, sum.__doc__) |
|
132 | check_calltip(sum, 'sum', None, sum.__doc__) | |
95 |
|
133 | |||
|
134 | ||||
|
135 | def test_calltip_line_magic(): | |||
|
136 | check_calltip(lmagic, 'lmagic', 'lmagic(line)', "A line magic") | |||
|
137 | ||||
|
138 | ||||
|
139 | def test_calltip_cell_magic(): | |||
|
140 | check_calltip(cmagic, 'cmagic', 'cmagic(line, cell)', "A cell magic") | |||
|
141 | ||||
|
142 | ||||
|
143 | def test_calltip_line_magic(): | |||
|
144 | check_calltip(lcmagic, 'lcmagic', 'lcmagic(line, cell=None)', | |||
|
145 | "A line/cell magic") | |||
|
146 | ||||
|
147 | ||||
|
148 | def test_class_magics(): | |||
|
149 | cm = SimpleMagics(ip) | |||
|
150 | ip.register_magics(cm) | |||
|
151 | check_calltip(cm.Clmagic, 'Clmagic', 'Clmagic(cline)', | |||
|
152 | "A class-based line magic") | |||
|
153 | check_calltip(cm.Ccmagic, 'Ccmagic', 'Ccmagic(cline, ccell)', | |||
|
154 | "A class-based cell magic") | |||
|
155 | check_calltip(cm.Clcmagic, 'Clcmagic', 'Clcmagic(cline, ccell=None)', | |||
|
156 | "A class-based line/cell magic") | |||
|
157 | ||||
|
158 | ||||
96 | def test_info(): |
|
159 | def test_info(): | |
97 | "Check that Inspector.info fills out various fields as expected." |
|
160 | "Check that Inspector.info fills out various fields as expected." | |
98 | i = inspector.info(Call, oname='Call') |
|
161 | i = inspector.info(Call, oname='Call') |
@@ -79,7 +79,7 b' def test_issue_114():' | |||||
79 | msp = ip.prefilter_manager.multi_line_specials |
|
79 | msp = ip.prefilter_manager.multi_line_specials | |
80 | ip.prefilter_manager.multi_line_specials = False |
|
80 | ip.prefilter_manager.multi_line_specials = False | |
81 | try: |
|
81 | try: | |
82 | for mgk in ip.lsmagic(): |
|
82 | for mgk in ip.magics_manager.lsmagic()['line']: | |
83 | raw = template % mgk |
|
83 | raw = template % mgk | |
84 | yield nt.assert_equals(ip.prefilter(raw), raw) |
|
84 | yield nt.assert_equals(ip.prefilter(raw), raw) | |
85 | finally: |
|
85 | finally: |
@@ -20,7 +20,10 b' tests = [' | |||||
20 | (' ;ls', (' ', ';', 'ls', '')), |
|
20 | (' ;ls', (' ', ';', 'ls', '')), | |
21 | ('f.g(x)', ('', '', 'f.g', '(x)')), |
|
21 | ('f.g(x)', ('', '', 'f.g', '(x)')), | |
22 | ('f.g (x)', ('', '', 'f.g', '(x)')), |
|
22 | ('f.g (x)', ('', '', 'f.g', '(x)')), | |
23 | ('?%hist', ('', '?', '%hist', '')), |
|
23 | ('?%hist1', ('', '?', '%hist1', '')), | |
|
24 | ('?%%hist2', ('', '?', '%%hist2', '')), | |||
|
25 | ('??%hist3', ('', '??', '%hist3', '')), | |||
|
26 | ('??%%hist4', ('', '??', '%%hist4', '')), | |||
24 | ('?x*', ('', '?', 'x*', '')), |
|
27 | ('?x*', ('', '?', 'x*', '')), | |
25 | ] |
|
28 | ] | |
26 | if py3compat.PY3: |
|
29 | if py3compat.PY3: |
@@ -258,8 +258,9 b' obj?, obj?? : Get help, or more help for object (also works as' | |||||
258 | ?foo.*abc* : List names in 'foo' containing 'abc' in them. |
|
258 | ?foo.*abc* : List names in 'foo' containing 'abc' in them. | |
259 | %magic : Information about IPython's 'magic' % functions. |
|
259 | %magic : Information about IPython's 'magic' % functions. | |
260 |
|
260 | |||
261 |
Magic functions are prefixed by %, and typically take their arguments |
|
261 | Magic functions are prefixed by % or %%, and typically take their arguments | |
262 | parentheses, quotes or even commas for convenience. |
|
262 | without parentheses, quotes or even commas for convenience. Line magics take a | |
|
263 | single % and cell magics are prefixed with two %%. | |||
263 |
|
264 | |||
264 | Example magic function calls: |
|
265 | Example magic function calls: | |
265 |
|
266 | |||
@@ -268,6 +269,10 b" alias d ls -F : Works if 'alias' not a python name" | |||||
268 | alist = %alias : Get list of aliases to 'alist' |
|
269 | alist = %alias : Get list of aliases to 'alist' | |
269 | cd /usr/share : Obvious. cd -<tab> to choose from visited dirs. |
|
270 | cd /usr/share : Obvious. cd -<tab> to choose from visited dirs. | |
270 | %cd?? : See help AND source for magic %cd |
|
271 | %cd?? : See help AND source for magic %cd | |
|
272 | %timeit x=10 : time the 'x=10' statement with high precision. | |||
|
273 | %%timeit x=2**100 | |||
|
274 | x**100 : time 'x*100' with a setup of 'x=2**100'; setup code is not | |||
|
275 | counted. This is an example of a cell magic. | |||
271 |
|
276 | |||
272 | System commands: |
|
277 | System commands: | |
273 |
|
278 |
@@ -1,6 +1,7 b'' | |||||
1 | """ |
|
1 | """IPython extension to reload modules before executing user code. | |
2 | ``autoreload`` is an IPython extension that reloads modules |
|
2 | ||
3 | automatically before executing the line of code typed. |
|
3 | ``autoreload`` reloads modules automatically before entering the execution of | |
|
4 | code typed at the IPython prompt. | |||
4 |
|
5 | |||
5 | This makes for example the following workflow possible: |
|
6 | This makes for example the following workflow possible: | |
6 |
|
7 | |||
@@ -20,8 +21,8 b' This makes for example the following workflow possible:' | |||||
20 | In [6]: some_function() |
|
21 | In [6]: some_function() | |
21 | Out[6]: 43 |
|
22 | Out[6]: 43 | |
22 |
|
23 | |||
23 | The module was reloaded without reloading it explicitly, and the |
|
24 | The module was reloaded without reloading it explicitly, and the object | |
24 |
|
|
25 | imported with ``from foo import ...`` was also updated. | |
25 |
|
26 | |||
26 | Usage |
|
27 | Usage | |
27 | ===== |
|
28 | ===== | |
@@ -84,26 +85,39 b' Some of the known remaining caveats are:' | |||||
84 | - Functions that are removed (eg. via monkey-patching) from a module |
|
85 | - Functions that are removed (eg. via monkey-patching) from a module | |
85 | before it is reloaded are not upgraded. |
|
86 | before it is reloaded are not upgraded. | |
86 |
|
87 | |||
87 | - C extension modules cannot be reloaded, and so cannot be |
|
88 | - C extension modules cannot be reloaded, and so cannot be autoreloaded. | |
88 | autoreloaded. |
|
|||
89 |
|
||||
90 | """ |
|
89 | """ | |
91 |
|
90 | |||
92 | skip_doctest = True |
|
91 | skip_doctest = True | |
93 |
|
92 | |||
94 | # Pauli Virtanen <pav@iki.fi>, 2008. |
|
93 | #----------------------------------------------------------------------------- | |
95 | # Thomas Heller, 2000. |
|
94 | # Copyright (C) 2000 Thomas Heller | |
|
95 | # Copyright (C) 2008 Pauli Virtanen <pav@iki.fi> | |||
|
96 | # Copyright (C) 2012 The IPython Development Team | |||
|
97 | # | |||
|
98 | # Distributed under the terms of the BSD License. The full license is in | |||
|
99 | # the file COPYING, distributed as part of this software. | |||
|
100 | #----------------------------------------------------------------------------- | |||
96 | # |
|
101 | # | |
97 | # This IPython module is written by Pauli Virtanen, based on the autoreload |
|
102 | # This IPython module is written by Pauli Virtanen, based on the autoreload | |
98 | # code by Thomas Heller. |
|
103 | # code by Thomas Heller. | |
99 |
|
104 | |||
100 |
#----------------------------------------------------------------------------- |
|
105 | #----------------------------------------------------------------------------- | |
101 | # Autoreload functionality |
|
106 | # Imports | |
102 |
#----------------------------------------------------------------------------- |
|
107 | #----------------------------------------------------------------------------- | |
103 |
|
108 | import atexit | ||
104 | import time, os, threading, sys, types, imp, inspect, traceback, atexit |
|
109 | import imp | |
|
110 | import inspect | |||
|
111 | import os | |||
|
112 | import sys | |||
|
113 | import threading | |||
|
114 | import time | |||
|
115 | import traceback | |||
|
116 | import types | |||
105 | import weakref |
|
117 | import weakref | |
|
118 | ||||
106 | try: |
|
119 | try: | |
|
120 | # Reload is not defined by default in Python3. | |||
107 | reload |
|
121 | reload | |
108 | except NameError: |
|
122 | except NameError: | |
109 | from imp import reload |
|
123 | from imp import reload | |
@@ -111,14 +125,20 b' except NameError:' | |||||
111 | from IPython.utils import pyfile |
|
125 | from IPython.utils import pyfile | |
112 | from IPython.utils.py3compat import PY3 |
|
126 | from IPython.utils.py3compat import PY3 | |
113 |
|
127 | |||
|
128 | #------------------------------------------------------------------------------ | |||
|
129 | # Autoreload functionality | |||
|
130 | #------------------------------------------------------------------------------ | |||
|
131 | ||||
114 | def _get_compiled_ext(): |
|
132 | def _get_compiled_ext(): | |
115 | """Official way to get the extension of compiled files (.pyc or .pyo)""" |
|
133 | """Official way to get the extension of compiled files (.pyc or .pyo)""" | |
116 | for ext, mode, typ in imp.get_suffixes(): |
|
134 | for ext, mode, typ in imp.get_suffixes(): | |
117 | if typ == imp.PY_COMPILED: |
|
135 | if typ == imp.PY_COMPILED: | |
118 | return ext |
|
136 | return ext | |
119 |
|
137 | |||
|
138 | ||||
120 | PY_COMPILED_EXT = _get_compiled_ext() |
|
139 | PY_COMPILED_EXT = _get_compiled_ext() | |
121 |
|
140 | |||
|
141 | ||||
122 | class ModuleReloader(object): |
|
142 | class ModuleReloader(object): | |
123 | enabled = False |
|
143 | enabled = False | |
124 | """Whether this reloader is enabled""" |
|
144 | """Whether this reloader is enabled""" | |
@@ -239,6 +259,7 b' else:' | |||||
239 | func_attrs = ['func_code', 'func_defaults', 'func_doc', |
|
259 | func_attrs = ['func_code', 'func_defaults', 'func_doc', | |
240 | 'func_closure', 'func_globals', 'func_dict'] |
|
260 | 'func_closure', 'func_globals', 'func_dict'] | |
241 |
|
261 | |||
|
262 | ||||
242 | def update_function(old, new): |
|
263 | def update_function(old, new): | |
243 | """Upgrade the code object of a function""" |
|
264 | """Upgrade the code object of a function""" | |
244 | for name in func_attrs: |
|
265 | for name in func_attrs: | |
@@ -247,6 +268,7 b' def update_function(old, new):' | |||||
247 | except (AttributeError, TypeError): |
|
268 | except (AttributeError, TypeError): | |
248 | pass |
|
269 | pass | |
249 |
|
270 | |||
|
271 | ||||
250 | def update_class(old, new): |
|
272 | def update_class(old, new): | |
251 | """Replace stuff in the __dict__ of a class, and upgrade |
|
273 | """Replace stuff in the __dict__ of a class, and upgrade | |
252 | method code objects""" |
|
274 | method code objects""" | |
@@ -270,15 +292,18 b' def update_class(old, new):' | |||||
270 | except (AttributeError, TypeError): |
|
292 | except (AttributeError, TypeError): | |
271 | pass # skip non-writable attributes |
|
293 | pass # skip non-writable attributes | |
272 |
|
294 | |||
|
295 | ||||
273 | def update_property(old, new): |
|
296 | def update_property(old, new): | |
274 | """Replace get/set/del functions of a property""" |
|
297 | """Replace get/set/del functions of a property""" | |
275 | update_generic(old.fdel, new.fdel) |
|
298 | update_generic(old.fdel, new.fdel) | |
276 | update_generic(old.fget, new.fget) |
|
299 | update_generic(old.fget, new.fget) | |
277 | update_generic(old.fset, new.fset) |
|
300 | update_generic(old.fset, new.fset) | |
278 |
|
301 | |||
|
302 | ||||
279 | def isinstance2(a, b, typ): |
|
303 | def isinstance2(a, b, typ): | |
280 | return isinstance(a, typ) and isinstance(b, typ) |
|
304 | return isinstance(a, typ) and isinstance(b, typ) | |
281 |
|
305 | |||
|
306 | ||||
282 | UPDATE_RULES = [ |
|
307 | UPDATE_RULES = [ | |
283 | (lambda a, b: isinstance2(a, b, type), |
|
308 | (lambda a, b: isinstance2(a, b, type), | |
284 | update_class), |
|
309 | update_class), | |
@@ -288,6 +313,7 b' UPDATE_RULES = [' | |||||
288 | update_property), |
|
313 | update_property), | |
289 | ] |
|
314 | ] | |
290 |
|
315 | |||
|
316 | ||||
291 | if PY3: |
|
317 | if PY3: | |
292 | UPDATE_RULES.extend([(lambda a, b: isinstance2(a, b, types.MethodType), |
|
318 | UPDATE_RULES.extend([(lambda a, b: isinstance2(a, b, types.MethodType), | |
293 | lambda a, b: update_function(a.__func__, b.__func__)), |
|
319 | lambda a, b: update_function(a.__func__, b.__func__)), | |
@@ -307,12 +333,14 b' def update_generic(a, b):' | |||||
307 | return True |
|
333 | return True | |
308 | return False |
|
334 | return False | |
309 |
|
335 | |||
|
336 | ||||
310 | class StrongRef(object): |
|
337 | class StrongRef(object): | |
311 | def __init__(self, obj): |
|
338 | def __init__(self, obj): | |
312 | self.obj = obj |
|
339 | self.obj = obj | |
313 | def __call__(self): |
|
340 | def __call__(self): | |
314 | return self.obj |
|
341 | return self.obj | |
315 |
|
342 | |||
|
343 | ||||
316 | def superreload(module, reload=reload, old_objects={}): |
|
344 | def superreload(module, reload=reload, old_objects={}): | |
317 | """Enhanced version of the builtin reload function. |
|
345 | """Enhanced version of the builtin reload function. | |
318 |
|
346 | |||
@@ -377,16 +405,19 b' def superreload(module, reload=reload, old_objects={}):' | |||||
377 | # IPython connectivity |
|
405 | # IPython connectivity | |
378 | #------------------------------------------------------------------------------ |
|
406 | #------------------------------------------------------------------------------ | |
379 |
|
407 | |||
380 | from IPython.core.plugin import Plugin |
|
|||
381 | from IPython.core.hooks import TryNext |
|
408 | from IPython.core.hooks import TryNext | |
|
409 | from IPython.core.magic import Magics, magics_class, line_magic | |||
|
410 | from IPython.core.plugin import Plugin | |||
382 |
|
411 | |||
383 | class AutoreloadInterface(object): |
|
412 | @magics_class | |
|
413 | class AutoreloadMagics(Magics): | |||
384 | def __init__(self, *a, **kw): |
|
414 | def __init__(self, *a, **kw): | |
385 |
super(Autoreload |
|
415 | super(AutoreloadMagics, self).__init__(*a, **kw) | |
386 | self._reloader = ModuleReloader() |
|
416 | self._reloader = ModuleReloader() | |
387 | self._reloader.check_all = False |
|
417 | self._reloader.check_all = False | |
388 |
|
418 | |||
389 | def magic_autoreload(self, ipself, parameter_s=''): |
|
419 | @line_magic | |
|
420 | def autoreload(self, parameter_s=''): | |||
390 | r"""%autoreload => Reload modules automatically |
|
421 | r"""%autoreload => Reload modules automatically | |
391 |
|
422 | |||
392 | %autoreload |
|
423 | %autoreload | |
@@ -441,7 +472,8 b' class AutoreloadInterface(object):' | |||||
441 | self._reloader.check_all = True |
|
472 | self._reloader.check_all = True | |
442 | self._reloader.enabled = True |
|
473 | self._reloader.enabled = True | |
443 |
|
474 | |||
444 | def magic_aimport(self, ipself, parameter_s='', stream=None): |
|
475 | @line_magic | |
|
476 | def aimport(self, parameter_s='', stream=None): | |||
445 | """%aimport => Import modules for automatic reloading. |
|
477 | """%aimport => Import modules for automatic reloading. | |
446 |
|
478 | |||
447 | %aimport |
|
479 | %aimport | |
@@ -452,9 +484,7 b' class AutoreloadInterface(object):' | |||||
452 |
|
484 | |||
453 | %aimport -foo |
|
485 | %aimport -foo | |
454 | Mark module 'foo' to not be autoreloaded for %autoreload 1 |
|
486 | Mark module 'foo' to not be autoreloaded for %autoreload 1 | |
455 |
|
||||
456 | """ |
|
487 | """ | |
457 |
|
||||
458 | modname = parameter_s |
|
488 | modname = parameter_s | |
459 | if not modname: |
|
489 | if not modname: | |
460 | to_reload = self._reloader.modules.keys() |
|
490 | to_reload = self._reloader.modules.keys() | |
@@ -475,9 +505,9 b' class AutoreloadInterface(object):' | |||||
475 | top_module, top_name = self._reloader.aimport_module(modname) |
|
505 | top_module, top_name = self._reloader.aimport_module(modname) | |
476 |
|
506 | |||
477 | # Inject module to user namespace |
|
507 | # Inject module to user namespace | |
478 |
|
|
508 | self.shell.push({top_name: top_module}) | |
479 |
|
509 | |||
480 |
def pre_run_code_hook(self, ip |
|
510 | def pre_run_code_hook(self, ip): | |
481 | if not self._reloader.enabled: |
|
511 | if not self._reloader.enabled: | |
482 | raise TryNext |
|
512 | raise TryNext | |
483 | try: |
|
513 | try: | |
@@ -485,16 +515,18 b' class AutoreloadInterface(object):' | |||||
485 | except: |
|
515 | except: | |
486 | pass |
|
516 | pass | |
487 |
|
517 | |||
488 | class AutoreloadPlugin(AutoreloadInterface, Plugin): |
|
518 | ||
|
519 | class AutoreloadPlugin(Plugin): | |||
489 | def __init__(self, shell=None, config=None): |
|
520 | def __init__(self, shell=None, config=None): | |
490 | super(AutoreloadPlugin, self).__init__(shell=shell, config=config) |
|
521 | super(AutoreloadPlugin, self).__init__(shell=shell, config=config) | |
|
522 | self.auto_magics = AutoreloadMagics(shell) | |||
|
523 | shell.register_magics(self.auto_magics) | |||
|
524 | shell.set_hook('pre_run_code_hook', self.auto_magics.pre_run_code_hook) | |||
491 |
|
525 | |||
492 | self.shell.define_magic('autoreload', self.magic_autoreload) |
|
|||
493 | self.shell.define_magic('aimport', self.magic_aimport) |
|
|||
494 | self.shell.set_hook('pre_run_code_hook', self.pre_run_code_hook) |
|
|||
495 |
|
526 | |||
496 | _loaded = False |
|
527 | _loaded = False | |
497 |
|
528 | |||
|
529 | ||||
498 | def load_ipython_extension(ip): |
|
530 | def load_ipython_extension(ip): | |
499 | """Load the extension in IPython.""" |
|
531 | """Load the extension in IPython.""" | |
500 | global _loaded |
|
532 | global _loaded |
@@ -24,7 +24,7 b' Usage' | |||||
24 | """ |
|
24 | """ | |
25 |
|
25 | |||
26 | #----------------------------------------------------------------------------- |
|
26 | #----------------------------------------------------------------------------- | |
27 |
# Copyright (C) 2008 |
|
27 | # Copyright (C) 2008 The IPython Development Team | |
28 | # |
|
28 | # | |
29 | # Distributed under the terms of the BSD License. The full license is in |
|
29 | # Distributed under the terms of the BSD License. The full license is in | |
30 | # the file COPYING, distributed as part of this software. |
|
30 | # the file COPYING, distributed as part of this software. | |
@@ -37,41 +37,31 b' Usage' | |||||
37 | import ast |
|
37 | import ast | |
38 | import re |
|
38 | import re | |
39 |
|
39 | |||
40 |
from IPython.core. |
|
40 | from IPython.core.magic import Magics, magics_class, line_magic | |
41 | from IPython.utils.traitlets import Bool, Any, Instance |
|
|||
42 | from IPython.testing.skipdoctest import skip_doctest |
|
41 | from IPython.testing.skipdoctest import skip_doctest | |
43 |
|
42 | |||
44 | #----------------------------------------------------------------------------- |
|
43 | #----------------------------------------------------------------------------- | |
45 | # Definitions of magic functions for use with IPython |
|
44 | # Definitions of magic functions for use with IPython | |
46 | #----------------------------------------------------------------------------- |
|
45 | #----------------------------------------------------------------------------- | |
47 |
|
46 | |||
48 |
|
||||
49 | NO_ACTIVE_VIEW = """ |
|
47 | NO_ACTIVE_VIEW = """ | |
50 | Use activate() on a DirectView object to activate it for magics. |
|
48 | Use activate() on a DirectView object to activate it for magics. | |
51 | """ |
|
49 | """ | |
52 |
|
50 | |||
53 |
|
51 | |||
54 | class ParalleMagic(Plugin): |
|
52 | @magics_class | |
55 | """A component to manage the %result, %px and %autopx magics.""" |
|
53 | class ParallelMagics(Magics): | |
56 |
|
54 | """A set of magics useful when controlling a parallel IPython cluster. | ||
57 | active_view = Instance('IPython.parallel.client.view.DirectView') |
|
55 | """ | |
58 | verbose = Bool(False, config=True) |
|
|||
59 | shell = Instance('IPython.core.interactiveshell.InteractiveShellABC') |
|
|||
60 |
|
56 | |||
61 |
def __init__(self, shell |
|
57 | def __init__(self, shell): | |
62 |
super(ParalleMagic, self).__init__(shell |
|
58 | super(ParallelMagics, self).__init__(shell) | |
63 | self._define_magics() |
|
|||
64 | # A flag showing if autopx is activated or not |
|
59 | # A flag showing if autopx is activated or not | |
65 | self.autopx = False |
|
60 | self.autopx = False | |
66 |
|
61 | |||
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 |
|
62 | @skip_doctest | |
74 | def magic_result(self, ipself, parameter_s=''): |
|
63 | @line_magic | |
|
64 | def result(self, parameter_s=''): | |||
75 | """Print the result of command i on all engines.. |
|
65 | """Print the result of command i on all engines.. | |
76 |
|
66 | |||
77 | To use this a :class:`DirectView` instance must be created |
|
67 | To use this a :class:`DirectView` instance must be created | |
@@ -103,7 +93,8 b' class ParalleMagic(Plugin):' | |||||
103 | return result |
|
93 | return result | |
104 |
|
94 | |||
105 | @skip_doctest |
|
95 | @skip_doctest | |
106 | def magic_px(self, ipself, parameter_s=''): |
|
96 | @line_magic | |
|
97 | def px(self, parameter_s=''): | |||
107 | """Executes the given python command in parallel. |
|
98 | """Executes the given python command in parallel. | |
108 |
|
99 | |||
109 | To use this a :class:`DirectView` instance must be created |
|
100 | To use this a :class:`DirectView` instance must be created | |
@@ -129,7 +120,8 b' class ParalleMagic(Plugin):' | |||||
129 | self._maybe_display_output(result) |
|
120 | self._maybe_display_output(result) | |
130 |
|
121 | |||
131 | @skip_doctest |
|
122 | @skip_doctest | |
132 | def magic_autopx(self, ipself, parameter_s=''): |
|
123 | @line_magic | |
|
124 | def autopx(self, parameter_s=''): | |||
133 | """Toggles auto parallel mode. |
|
125 | """Toggles auto parallel mode. | |
134 |
|
126 | |||
135 | To use this a :class:`DirectView` instance must be created |
|
127 | To use this a :class:`DirectView` instance must be created | |
@@ -237,8 +229,9 b' class ParalleMagic(Plugin):' | |||||
237 | cell_name = ipself.compile.cache(cell, ipself.execution_count) |
|
229 | cell_name = ipself.compile.cache(cell, ipself.execution_count) | |
238 |
|
230 | |||
239 | try: |
|
231 | try: | |
240 |
|
|
232 | ast.parse(cell, filename=cell_name) | |
241 |
except (OverflowError, SyntaxError, ValueError, TypeError, |
|
233 | except (OverflowError, SyntaxError, ValueError, TypeError, | |
|
234 | MemoryError): | |||
242 | # Case 1 |
|
235 | # Case 1 | |
243 | ipself.showsyntaxerror() |
|
236 | ipself.showsyntaxerror() | |
244 | ipself.execution_count += 1 |
|
237 | ipself.execution_count += 1 | |
@@ -282,8 +275,9 b' class ParalleMagic(Plugin):' | |||||
282 | """ |
|
275 | """ | |
283 | ipself = self.shell |
|
276 | ipself = self.shell | |
284 | # check code object for the autopx magic |
|
277 | # check code object for the autopx magic | |
285 |
if 'get_ipython' in code_obj.co_names and 'magic' in code_obj.co_names |
|
278 | if 'get_ipython' in code_obj.co_names and 'magic' in code_obj.co_names \ | |
286 |
|
|
279 | and any( [ isinstance(c, basestring) and 'autopx' in c | |
|
280 | for c in code_obj.co_consts ]): | |||
287 | self._disable_autopx() |
|
281 | self._disable_autopx() | |
288 | return False |
|
282 | return False | |
289 | else: |
|
283 | else: | |
@@ -305,12 +299,11 b' class ParalleMagic(Plugin):' | |||||
305 |
|
299 | |||
306 |
|
300 | |||
307 | __doc__ = __doc__.replace('@AUTOPX_DOC@', |
|
301 | __doc__ = __doc__.replace('@AUTOPX_DOC@', | |
308 |
" " + ParalleMagic. |
|
302 | " " + ParallelMagics.autopx.__doc__) | |
309 | __doc__ = __doc__.replace('@PX_DOC@', |
|
303 | __doc__ = __doc__.replace('@PX_DOC@', | |
310 |
" " + ParalleMagic. |
|
304 | " " + ParallelMagics.px.__doc__) | |
311 | __doc__ = __doc__.replace('@RESULT_DOC@', |
|
305 | __doc__ = __doc__.replace('@RESULT_DOC@', | |
312 |
" " + ParalleMagic. |
|
306 | " " + ParallelMagics.result.__doc__) | |
313 |
|
||||
314 |
|
307 | |||
315 | _loaded = False |
|
308 | _loaded = False | |
316 |
|
309 | |||
@@ -319,7 +312,5 b' def load_ipython_extension(ip):' | |||||
319 | """Load the extension in IPython.""" |
|
312 | """Load the extension in IPython.""" | |
320 | global _loaded |
|
313 | global _loaded | |
321 | if not _loaded: |
|
314 | if not _loaded: | |
322 | plugin = ParalleMagic(shell=ip, config=ip.config) |
|
315 | ip.register_magics(ParallelMagics) | |
323 | ip.plugin_manager.register_plugin('parallelmagic', plugin) |
|
|||
324 | _loaded = True |
|
316 | _loaded = True | |
325 |
|
@@ -8,17 +8,33 b' To automatically restore stored variables at startup, add this to your' | |||||
8 | :file:`ipython_config.py` file:: |
|
8 | :file:`ipython_config.py` file:: | |
9 |
|
9 | |||
10 | c.StoreMagic.autorestore = True |
|
10 | c.StoreMagic.autorestore = True | |
11 |
|
||||
12 | """ |
|
11 | """ | |
13 |
|
12 | #----------------------------------------------------------------------------- | ||
14 | from IPython.core.error import TryNext, UsageError |
|
13 | # Copyright (c) 2012, The IPython Development Team. | |
|
14 | # | |||
|
15 | # Distributed under the terms of the Modified BSD License. | |||
|
16 | # | |||
|
17 | # The full license is in the file COPYING.txt, distributed with this software. | |||
|
18 | #----------------------------------------------------------------------------- | |||
|
19 | ||||
|
20 | #----------------------------------------------------------------------------- | |||
|
21 | # Imports | |||
|
22 | #----------------------------------------------------------------------------- | |||
|
23 | ||||
|
24 | # Stdlib | |||
|
25 | import inspect, os, sys, textwrap | |||
|
26 | ||||
|
27 | # Our own | |||
|
28 | from IPython.core.error import UsageError | |||
|
29 | from IPython.core.fakemodule import FakeModule | |||
|
30 | from IPython.core.magic import Magics, magics_class, line_magic | |||
15 | from IPython.core.plugin import Plugin |
|
31 | from IPython.core.plugin import Plugin | |
16 | from IPython.testing.skipdoctest import skip_doctest |
|
32 | from IPython.testing.skipdoctest import skip_doctest | |
17 | from IPython.utils import pickleshare |
|
|||
18 | from IPython.utils.traitlets import Bool, Instance |
|
33 | from IPython.utils.traitlets import Bool, Instance | |
19 |
|
34 | |||
20 | import inspect,pickle,os,sys,textwrap |
|
35 | #----------------------------------------------------------------------------- | |
21 | from IPython.core.fakemodule import FakeModule |
|
36 | # Functions and classes | |
|
37 | #----------------------------------------------------------------------------- | |||
22 |
|
38 | |||
23 | def restore_aliases(ip): |
|
39 | def restore_aliases(ip): | |
24 | staliases = ip.db.get('stored_aliases', {}) |
|
40 | staliases = ip.db.get('stored_aliases', {}) | |
@@ -37,7 +53,7 b' def refresh_variables(ip):' | |||||
37 | obj = db[key] |
|
53 | obj = db[key] | |
38 | except KeyError: |
|
54 | except KeyError: | |
39 | print "Unable to restore variable '%s', ignoring (use %%store -d to forget!)" % justkey |
|
55 | print "Unable to restore variable '%s', ignoring (use %%store -d to forget!)" % justkey | |
40 | print "The error was:",sys.exc_info()[0] |
|
56 | print "The error was:", sys.exc_info()[0] | |
41 | else: |
|
57 | else: | |
42 | #print "restored",justkey,"=",obj #dbg |
|
58 | #print "restored",justkey,"=",obj #dbg | |
43 | ip.user_ns[justkey] = obj |
|
59 | ip.user_ns[justkey] = obj | |
@@ -46,141 +62,153 b' def refresh_variables(ip):' | |||||
46 | def restore_dhist(ip): |
|
62 | def restore_dhist(ip): | |
47 | ip.user_ns['_dh'] = ip.db.get('dhist',[]) |
|
63 | ip.user_ns['_dh'] = ip.db.get('dhist',[]) | |
48 |
|
64 | |||
|
65 | ||||
49 | def restore_data(ip): |
|
66 | def restore_data(ip): | |
50 | refresh_variables(ip) |
|
67 | refresh_variables(ip) | |
51 | restore_aliases(ip) |
|
68 | restore_aliases(ip) | |
52 | restore_dhist(ip) |
|
69 | restore_dhist(ip) | |
53 |
|
70 | |||
54 | @skip_doctest |
|
71 | ||
55 | def magic_store(self, parameter_s=''): |
|
72 | @magics_class | |
|
73 | class StoreMagics(Magics): | |||
56 | """Lightweight persistence for python variables. |
|
74 | """Lightweight persistence for python variables. | |
57 |
|
75 | |||
58 | Example:: |
|
76 | Provides the %store magic.""" | |
59 |
|
77 | |||
60 | In [1]: l = ['hello',10,'world'] |
|
78 | @skip_doctest | |
61 | In [2]: %store l |
|
79 | @line_magic | |
62 | In [3]: exit |
|
80 | def store(self, parameter_s=''): | |
|
81 | """Lightweight persistence for python variables. | |||
63 |
|
82 | |||
64 | (IPython session is closed and started again...) |
|
83 | Example:: | |
65 |
|
84 | |||
66 | ville@badger:~$ ipython |
|
85 | In [1]: l = ['hello',10,'world'] | |
67 |
|
|
86 | In [2]: %store l | |
68 | Out[1]: ['hello', 10, 'world'] |
|
87 | In [3]: exit | |
69 |
|
88 | |||
70 | Usage: |
|
89 | (IPython session is closed and started again...) | |
71 |
|
90 | |||
72 | * ``%store`` - Show list of all variables and their current values |
|
91 | ville@badger:~$ ipython | |
73 | * ``%store spam`` - Store the *current* value of the variable spam to disk |
|
92 | In [1]: l | |
74 | * ``%store -d spam`` - Remove the variable and its value from storage |
|
93 | Out[1]: ['hello', 10, 'world'] | |
75 | * ``%store -z`` - Remove all variables from storage |
|
|||
76 | * ``%store -r`` - Refresh all variables from store (delete current vals) |
|
|||
77 | * ``%store foo >a.txt`` - Store value of foo to new file a.txt |
|
|||
78 | * ``%store foo >>a.txt`` - Append value of foo to file a.txt |
|
|||
79 |
|
94 | |||
80 | It should be noted that if you change the value of a variable, you |
|
95 | Usage: | |
81 | need to %store it again if you want to persist the new value. |
|
|||
82 |
|
96 | |||
83 | Note also that the variables will need to be pickleable; most basic |
|
97 | * ``%store`` - Show list of all variables and their current | |
84 | python types can be safely %store'd. |
|
98 | values | |
|
99 | * ``%store spam`` - Store the *current* value of the variable spam | |||
|
100 | to disk | |||
|
101 | * ``%store -d spam`` - Remove the variable and its value from storage | |||
|
102 | * ``%store -z`` - Remove all variables from storage | |||
|
103 | * ``%store -r`` - Refresh all variables from store (delete | |||
|
104 | current vals) | |||
|
105 | * ``%store foo >a.txt`` - Store value of foo to new file a.txt | |||
|
106 | * ``%store foo >>a.txt`` - Append value of foo to file a.txt | |||
85 |
|
107 | |||
86 | Also aliases can be %store'd across sessions. |
|
108 | It should be noted that if you change the value of a variable, you | |
87 | """ |
|
109 | need to %store it again if you want to persist the new value. | |
88 |
|
|
110 | ||
89 | opts,argsl = self.parse_options(parameter_s,'drz',mode='string') |
|
111 | Note also that the variables will need to be pickleable; most basic | |
90 | args = argsl.split(None,1) |
|
112 | python types can be safely %store'd. | |
91 | ip = self.shell |
|
|||
92 | db = ip.db |
|
|||
93 | # delete |
|
|||
94 | if opts.has_key('d'): |
|
|||
95 | try: |
|
|||
96 | todel = args[0] |
|
|||
97 | except IndexError: |
|
|||
98 | raise UsageError('You must provide the variable to forget') |
|
|||
99 | else: |
|
|||
100 | try: |
|
|||
101 | del db['autorestore/' + todel] |
|
|||
102 | except: |
|
|||
103 | raise UsageError("Can't delete variable '%s'" % todel) |
|
|||
104 | # reset |
|
|||
105 | elif opts.has_key('z'): |
|
|||
106 | for k in db.keys('autorestore/*'): |
|
|||
107 | del db[k] |
|
|||
108 |
|
||||
109 | elif opts.has_key('r'): |
|
|||
110 | refresh_variables(ip) |
|
|||
111 |
|
||||
112 |
|
||||
113 | # run without arguments -> list variables & values |
|
|||
114 | elif not args: |
|
|||
115 | vars = self.db.keys('autorestore/*') |
|
|||
116 | vars.sort() |
|
|||
117 | if vars: |
|
|||
118 | size = max(map(len,vars)) |
|
|||
119 | else: |
|
|||
120 | size = 0 |
|
|||
121 |
|
||||
122 | print 'Stored variables and their in-db values:' |
|
|||
123 | fmt = '%-'+str(size)+'s -> %s' |
|
|||
124 | get = db.get |
|
|||
125 | for var in vars: |
|
|||
126 | justkey = os.path.basename(var) |
|
|||
127 | # print 30 first characters from every var |
|
|||
128 | print fmt % (justkey,repr(get(var,'<unavailable>'))[:50]) |
|
|||
129 |
|
||||
130 | # default action - store the variable |
|
|||
131 | else: |
|
|||
132 | # %store foo >file.txt or >>file.txt |
|
|||
133 | if len(args) > 1 and args[1].startswith('>'): |
|
|||
134 | fnam = os.path.expanduser(args[1].lstrip('>').lstrip()) |
|
|||
135 | if args[1].startswith('>>'): |
|
|||
136 | fil = open(fnam,'a') |
|
|||
137 | else: |
|
|||
138 | fil = open(fnam,'w') |
|
|||
139 | obj = ip.ev(args[0]) |
|
|||
140 | print "Writing '%s' (%s) to file '%s'." % (args[0], |
|
|||
141 | obj.__class__.__name__, fnam) |
|
|||
142 |
|
|
113 | ||
|
114 | Also aliases can be %store'd across sessions. | |||
|
115 | """ | |||
143 |
|
116 | |||
144 | if not isinstance (obj,basestring): |
|
117 | opts,argsl = self.parse_options(parameter_s,'drz',mode='string') | |
145 | from pprint import pprint |
|
118 | args = argsl.split(None,1) | |
146 | pprint(obj,fil) |
|
119 | ip = self.shell | |
|
120 | db = ip.db | |||
|
121 | # delete | |||
|
122 | if opts.has_key('d'): | |||
|
123 | try: | |||
|
124 | todel = args[0] | |||
|
125 | except IndexError: | |||
|
126 | raise UsageError('You must provide the variable to forget') | |||
147 | else: |
|
127 | else: | |
148 |
|
|
128 | try: | |
149 | if not obj.endswith('\n'): |
|
129 | del db['autorestore/' + todel] | |
150 | fil.write('\n') |
|
130 | except: | |
151 |
|
131 | raise UsageError("Can't delete variable '%s'" % todel) | ||
152 | fil.close() |
|
132 | # reset | |
153 | return |
|
133 | elif opts.has_key('z'): | |
154 |
|
134 | for k in db.keys('autorestore/*'): | ||
155 | # %store foo |
|
135 | del db[k] | |
156 | try: |
|
136 | ||
157 | obj = ip.user_ns[args[0]] |
|
137 | elif opts.has_key('r'): | |
158 | except KeyError: |
|
138 | refresh_variables(ip) | |
159 | # it might be an alias |
|
139 | ||
160 | # This needs to be refactored to use the new AliasManager stuff. |
|
140 | ||
161 | if args[0] in self.alias_manager: |
|
141 | # run without arguments -> list variables & values | |
162 | name = args[0] |
|
142 | elif not args: | |
163 | nargs, cmd = self.alias_manager.alias_table[ name ] |
|
143 | vars = self.db.keys('autorestore/*') | |
164 | staliases = db.get('stored_aliases',{}) |
|
144 | vars.sort() | |
165 | staliases[ name ] = cmd |
|
145 | if vars: | |
166 | db['stored_aliases'] = staliases |
|
146 | size = max(map(len, vars)) | |
167 | print "Alias stored: %s (%s)" % (name, cmd) |
|
|||
168 | return |
|
|||
169 | else: |
|
147 | else: | |
170 | raise UsageError("Unknown variable '%s'" % args[0]) |
|
148 | size = 0 | |
171 |
|
149 | |||
|
150 | print 'Stored variables and their in-db values:' | |||
|
151 | fmt = '%-'+str(size)+'s -> %s' | |||
|
152 | get = db.get | |||
|
153 | for var in vars: | |||
|
154 | justkey = os.path.basename(var) | |||
|
155 | # print 30 first characters from every var | |||
|
156 | print fmt % (justkey, repr(get(var, '<unavailable>'))[:50]) | |||
|
157 | ||||
|
158 | # default action - store the variable | |||
172 | else: |
|
159 | else: | |
173 | if isinstance(inspect.getmodule(obj), FakeModule): |
|
160 | # %store foo >file.txt or >>file.txt | |
174 | print textwrap.dedent("""\ |
|
161 | if len(args) > 1 and args[1].startswith('>'): | |
175 | Warning:%s is %s |
|
162 | fnam = os.path.expanduser(args[1].lstrip('>').lstrip()) | |
176 | Proper storage of interactively declared classes (or instances |
|
163 | if args[1].startswith('>>'): | |
177 | of those classes) is not possible! Only instances |
|
164 | fil = open(fnam, 'a') | |
178 | of classes in real modules on file system can be %%store'd. |
|
165 | else: | |
179 | """ % (args[0], obj) ) |
|
166 | fil = open(fnam, 'w') | |
|
167 | obj = ip.ev(args[0]) | |||
|
168 | print "Writing '%s' (%s) to file '%s'." % (args[0], | |||
|
169 | obj.__class__.__name__, fnam) | |||
|
170 | ||||
|
171 | ||||
|
172 | if not isinstance (obj, basestring): | |||
|
173 | from pprint import pprint | |||
|
174 | pprint(obj, fil) | |||
|
175 | else: | |||
|
176 | fil.write(obj) | |||
|
177 | if not obj.endswith('\n'): | |||
|
178 | fil.write('\n') | |||
|
179 | ||||
|
180 | fil.close() | |||
180 | return |
|
181 | return | |
181 | #pickled = pickle.dumps(obj) |
|
182 | ||
182 | self.db[ 'autorestore/' + args[0] ] = obj |
|
183 | # %store foo | |
183 | print "Stored '%s' (%s)" % (args[0], obj.__class__.__name__) |
|
184 | try: | |
|
185 | obj = ip.user_ns[args[0]] | |||
|
186 | except KeyError: | |||
|
187 | # it might be an alias | |||
|
188 | # This needs to be refactored to use the new AliasManager stuff. | |||
|
189 | if args[0] in self.alias_manager: | |||
|
190 | name = args[0] | |||
|
191 | nargs, cmd = self.alias_manager.alias_table[ name ] | |||
|
192 | staliases = db.get('stored_aliases',{}) | |||
|
193 | staliases[ name ] = cmd | |||
|
194 | db['stored_aliases'] = staliases | |||
|
195 | print "Alias stored: %s (%s)" % (name, cmd) | |||
|
196 | return | |||
|
197 | else: | |||
|
198 | raise UsageError("Unknown variable '%s'" % args[0]) | |||
|
199 | ||||
|
200 | else: | |||
|
201 | if isinstance(inspect.getmodule(obj), FakeModule): | |||
|
202 | print textwrap.dedent("""\ | |||
|
203 | Warning:%s is %s | |||
|
204 | Proper storage of interactively declared classes (or instances | |||
|
205 | of those classes) is not possible! Only instances | |||
|
206 | of classes in real modules on file system can be %%store'd. | |||
|
207 | """ % (args[0], obj) ) | |||
|
208 | return | |||
|
209 | #pickled = pickle.dumps(obj) | |||
|
210 | self.db[ 'autorestore/' + args[0] ] = obj | |||
|
211 | print "Stored '%s' (%s)" % (args[0], obj.__class__.__name__) | |||
184 |
|
212 | |||
185 |
|
213 | |||
186 | class StoreMagic(Plugin): |
|
214 | class StoreMagic(Plugin): | |
@@ -189,11 +217,12 b' class StoreMagic(Plugin):' | |||||
189 |
|
217 | |||
190 | def __init__(self, shell, config): |
|
218 | def __init__(self, shell, config): | |
191 | super(StoreMagic, self).__init__(shell=shell, config=config) |
|
219 | super(StoreMagic, self).__init__(shell=shell, config=config) | |
192 |
shell. |
|
220 | shell.register_magics(StoreMagics) | |
193 |
|
221 | |||
194 | if self.autorestore: |
|
222 | if self.autorestore: | |
195 | restore_data(shell) |
|
223 | restore_data(shell) | |
196 |
|
224 | |||
|
225 | ||||
197 | _loaded = False |
|
226 | _loaded = False | |
198 |
|
227 | |||
199 | def load_ipython_extension(ip): |
|
228 | def load_ipython_extension(ip): |
@@ -31,7 +31,7 b' except ImportError:' | |||||
31 | pass |
|
31 | pass | |
32 |
|
32 | |||
33 | #----------------------------------------------------------------------------- |
|
33 | #----------------------------------------------------------------------------- | |
34 |
# Definitions of |
|
34 | # Definitions of special display functions for use with IPython | |
35 | #----------------------------------------------------------------------------- |
|
35 | #----------------------------------------------------------------------------- | |
36 |
|
36 | |||
37 | def print_basic_unicode(o, p, cycle): |
|
37 | def print_basic_unicode(o, p, cycle): |
@@ -1,3 +1,17 b'' | |||||
|
1 | """Tests for autoreload extension. | |||
|
2 | """ | |||
|
3 | #----------------------------------------------------------------------------- | |||
|
4 | # Copyright (c) 2012 IPython Development Team. | |||
|
5 | # | |||
|
6 | # Distributed under the terms of the Modified BSD License. | |||
|
7 | # | |||
|
8 | # The full license is in the file COPYING.txt, distributed with this software. | |||
|
9 | #----------------------------------------------------------------------------- | |||
|
10 | ||||
|
11 | #----------------------------------------------------------------------------- | |||
|
12 | # Imports | |||
|
13 | #----------------------------------------------------------------------------- | |||
|
14 | ||||
1 | import os |
|
15 | import os | |
2 | import sys |
|
16 | import sys | |
3 | import tempfile |
|
17 | import tempfile | |
@@ -9,21 +23,25 b' from StringIO import StringIO' | |||||
9 | import nose.tools as nt |
|
23 | import nose.tools as nt | |
10 | import IPython.testing.tools as tt |
|
24 | import IPython.testing.tools as tt | |
11 |
|
25 | |||
12 |
from IPython.extensions.autoreload import Autoreload |
|
26 | from IPython.extensions.autoreload import AutoreloadPlugin | |
13 | from IPython.core.hooks import TryNext |
|
27 | from IPython.core.hooks import TryNext | |
14 |
|
28 | |||
15 | #----------------------------------------------------------------------------- |
|
29 | #----------------------------------------------------------------------------- | |
16 | # Test fixture |
|
30 | # Test fixture | |
17 | #----------------------------------------------------------------------------- |
|
31 | #----------------------------------------------------------------------------- | |
18 |
|
32 | |||
|
33 | noop = lambda *a, **kw: None | |||
|
34 | ||||
19 | class FakeShell(object): |
|
35 | class FakeShell(object): | |
20 | def __init__(self): |
|
36 | def __init__(self): | |
21 | self.ns = {} |
|
37 | self.ns = {} | |
22 |
self.reloader = Autoreload |
|
38 | self.reloader = AutoreloadPlugin(shell=self) | |
|
39 | ||||
|
40 | register_magics = set_hook = noop | |||
23 |
|
41 | |||
24 | def run_code(self, code): |
|
42 | def run_code(self, code): | |
25 | try: |
|
43 | try: | |
26 | self.reloader.pre_run_code_hook(self) |
|
44 | self.reloader.auto_magics.pre_run_code_hook(self) | |
27 | except TryNext: |
|
45 | except TryNext: | |
28 | pass |
|
46 | pass | |
29 | exec code in self.ns |
|
47 | exec code in self.ns | |
@@ -32,10 +50,10 b' class FakeShell(object):' | |||||
32 | self.ns.update(items) |
|
50 | self.ns.update(items) | |
33 |
|
51 | |||
34 | def magic_autoreload(self, parameter): |
|
52 | def magic_autoreload(self, parameter): | |
35 |
self.reloader. |
|
53 | self.reloader.auto_magics.autoreload(parameter) | |
36 |
|
54 | |||
37 | def magic_aimport(self, parameter, stream=None): |
|
55 | def magic_aimport(self, parameter, stream=None): | |
38 |
self.reloader. |
|
56 | self.reloader.auto_magics.aimport(parameter, stream=stream) | |
39 |
|
57 | |||
40 |
|
58 | |||
41 | class Fixture(object): |
|
59 | class Fixture(object): | |
@@ -81,7 +99,6 b' class Fixture(object):' | |||||
81 | future, and without changing the timestamp of the .pyc file |
|
99 | future, and without changing the timestamp of the .pyc file | |
82 | (because that is stored in the file). The only reliable way |
|
100 | (because that is stored in the file). The only reliable way | |
83 | to achieve this seems to be to sleep. |
|
101 | to achieve this seems to be to sleep. | |
84 |
|
||||
85 | """ |
|
102 | """ | |
86 |
|
103 | |||
87 | # Sleep one second + eps |
|
104 | # Sleep one second + eps | |
@@ -160,7 +177,7 b" class Bar: # old-style class: weakref doesn't work for it on Python < 2.7" | |||||
160 | self.shell.magic_aimport("", stream=stream) |
|
177 | self.shell.magic_aimport("", stream=stream) | |
161 | nt.assert_true("Modules to reload:\nall-except-skipped" in |
|
178 | nt.assert_true("Modules to reload:\nall-except-skipped" in | |
162 | stream.getvalue()) |
|
179 | stream.getvalue()) | |
163 |
nt.assert_ |
|
180 | nt.assert_in(mod_name, self.shell.ns) | |
164 |
|
181 | |||
165 | mod = sys.modules[mod_name] |
|
182 | mod = sys.modules[mod_name] | |
166 |
|
183 |
@@ -1,37 +1,43 b'' | |||||
1 | ########################## LICENCE ############################### |
|
1 | ########################## LICENCE ############################### | |
2 | ## |
|
2 | ||
3 |
# |
|
3 | # Copyright (c) 2005-2012, Michele Simionato | |
4 |
# |
|
4 | # All rights reserved. | |
5 | ## |
|
5 | ||
6 | ## Redistributions of source code must retain the above copyright |
|
6 | # Redistribution and use in source and binary forms, with or without | |
7 | ## notice, this list of conditions and the following disclaimer. |
|
7 | # modification, are permitted provided that the following conditions are | |
8 | ## Redistributions in bytecode form must reproduce the above copyright |
|
8 | # met: | |
9 | ## notice, this list of conditions and the following disclaimer in |
|
9 | ||
10 | ## the documentation and/or other materials provided with the |
|
10 | # Redistributions of source code must retain the above copyright | |
11 | ## distribution. |
|
11 | # notice, this list of conditions and the following disclaimer. | |
12 |
|
12 | # Redistributions in bytecode form must reproduce the above copyright | ||
13 | ## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
13 | # notice, this list of conditions and the following disclaimer in | |
14 | ## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
14 | # the documentation and/or other materials provided with the | |
15 | ## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
15 | # distribution. | |
16 | ## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
16 | ||
17 | ## HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
|
17 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
18 | ## INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|
18 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
19 | ## BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
|
19 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
20 | ## OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
|
20 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
21 | ## ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR |
|
21 | # HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
22 | ## TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
|
22 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
23 | ## USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
|
23 | # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS | |
24 | ## DAMAGE. |
|
24 | # OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
|
25 | # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR | |||
|
26 | # TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE | |||
|
27 | # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH | |||
|
28 | # DAMAGE. | |||
25 |
|
29 | |||
26 | """ |
|
30 | """ | |
27 | Decorator module, see http://pypi.python.org/pypi/decorator |
|
31 | Decorator module, see http://pypi.python.org/pypi/decorator | |
28 | for the documentation. |
|
32 | for the documentation. | |
29 | """ |
|
33 | """ | |
30 |
|
34 | |||
31 | __all__ = ["decorator", "FunctionMaker", "partial", |
|
35 | __version__ = '3.3.3' | |
32 | "deprecated", "getinfo", "new_wrapper"] |
|
36 | ||
|
37 | __all__ = ["decorator", "FunctionMaker", "partial"] | |||
|
38 | ||||
|
39 | import sys, re, inspect | |||
33 |
|
40 | |||
34 | import os, sys, re, inspect, string, warnings |
|
|||
35 | try: |
|
41 | try: | |
36 | from functools import partial |
|
42 | from functools import partial | |
37 | except ImportError: # for Python version < 2.5 |
|
43 | except ImportError: # for Python version < 2.5 | |
@@ -46,6 +52,22 b' except ImportError: # for Python version < 2.5' | |||||
46 | kw.update(otherkw) |
|
52 | kw.update(otherkw) | |
47 | return self.func(*(self.args + otherargs), **kw) |
|
53 | return self.func(*(self.args + otherargs), **kw) | |
48 |
|
54 | |||
|
55 | if sys.version >= '3': | |||
|
56 | from inspect import getfullargspec | |||
|
57 | else: | |||
|
58 | class getfullargspec(object): | |||
|
59 | "A quick and dirty replacement for getfullargspec for Python 2.X" | |||
|
60 | def __init__(self, f): | |||
|
61 | self.args, self.varargs, self.varkw, self.defaults = \ | |||
|
62 | inspect.getargspec(f) | |||
|
63 | self.kwonlyargs = [] | |||
|
64 | self.kwonlydefaults = None | |||
|
65 | def __iter__(self): | |||
|
66 | yield self.args | |||
|
67 | yield self.varargs | |||
|
68 | yield self.varkw | |||
|
69 | yield self.defaults | |||
|
70 | ||||
49 | DEF = re.compile('\s*def\s*([_\w][_\w\d]*)\s*\(') |
|
71 | DEF = re.compile('\s*def\s*([_\w][_\w\d]*)\s*\(') | |
50 |
|
72 | |||
51 | # basic functionality |
|
73 | # basic functionality | |
@@ -57,6 +79,7 b' class FunctionMaker(object):' | |||||
57 | """ |
|
79 | """ | |
58 | def __init__(self, func=None, name=None, signature=None, |
|
80 | def __init__(self, func=None, name=None, signature=None, | |
59 | defaults=None, doc=None, module=None, funcdict=None): |
|
81 | defaults=None, doc=None, module=None, funcdict=None): | |
|
82 | self.shortsignature = signature | |||
60 | if func: |
|
83 | if func: | |
61 | # func can be a class or a callable, but not an instance method |
|
84 | # func can be a class or a callable, but not an instance method | |
62 | self.name = func.__name__ |
|
85 | self.name = func.__name__ | |
@@ -65,13 +88,31 b' class FunctionMaker(object):' | |||||
65 | self.doc = func.__doc__ |
|
88 | self.doc = func.__doc__ | |
66 | self.module = func.__module__ |
|
89 | self.module = func.__module__ | |
67 | if inspect.isfunction(func): |
|
90 | if inspect.isfunction(func): | |
68 |
argspec = |
|
91 | argspec = getfullargspec(func) | |
69 | self.args, self.varargs, self.keywords, self.defaults = argspec |
|
92 | self.annotations = getattr(func, '__annotations__', {}) | |
|
93 | for a in ('args', 'varargs', 'varkw', 'defaults', 'kwonlyargs', | |||
|
94 | 'kwonlydefaults'): | |||
|
95 | setattr(self, a, getattr(argspec, a)) | |||
70 | for i, arg in enumerate(self.args): |
|
96 | for i, arg in enumerate(self.args): | |
71 | setattr(self, 'arg%d' % i, arg) |
|
97 | setattr(self, 'arg%d' % i, arg) | |
72 | self.signature = inspect.formatargspec( |
|
98 | if sys.version < '3': # easy way | |
73 | formatvalue=lambda val: "", *argspec)[1:-1] |
|
99 | self.shortsignature = self.signature = \ | |
|
100 | inspect.formatargspec( | |||
|
101 | formatvalue=lambda val: "", *argspec)[1:-1] | |||
|
102 | else: # Python 3 way | |||
|
103 | self.signature = self.shortsignature = ', '.join(self.args) | |||
|
104 | if self.varargs: | |||
|
105 | self.signature += ', *' + self.varargs | |||
|
106 | self.shortsignature += ', *' + self.varargs | |||
|
107 | if self.kwonlyargs: | |||
|
108 | for a in self.kwonlyargs: | |||
|
109 | self.signature += ', %s=None' % a | |||
|
110 | self.shortsignature += ', %s=%s' % (a, a) | |||
|
111 | if self.varkw: | |||
|
112 | self.signature += ', **' + self.varkw | |||
|
113 | self.shortsignature += ', **' + self.varkw | |||
74 | self.dict = func.__dict__.copy() |
|
114 | self.dict = func.__dict__.copy() | |
|
115 | # func=None happens when decorating a caller | |||
75 | if name: |
|
116 | if name: | |
76 | self.name = name |
|
117 | self.name = name | |
77 | if signature is not None: |
|
118 | if signature is not None: | |
@@ -95,6 +136,8 b' class FunctionMaker(object):' | |||||
95 | func.__doc__ = getattr(self, 'doc', None) |
|
136 | func.__doc__ = getattr(self, 'doc', None) | |
96 | func.__dict__ = getattr(self, 'dict', {}) |
|
137 | func.__dict__ = getattr(self, 'dict', {}) | |
97 | func.func_defaults = getattr(self, 'defaults', ()) |
|
138 | func.func_defaults = getattr(self, 'defaults', ()) | |
|
139 | func.__kwdefaults__ = getattr(self, 'kwonlydefaults', None) | |||
|
140 | func.__annotations__ = getattr(self, 'annotations', None) | |||
98 | callermodule = sys._getframe(3).f_globals.get('__name__', '?') |
|
141 | callermodule = sys._getframe(3).f_globals.get('__name__', '?') | |
99 | func.__module__ = getattr(self, 'module', callermodule) |
|
142 | func.__module__ = getattr(self, 'module', callermodule) | |
100 | func.__dict__.update(kw) |
|
143 | func.__dict__.update(kw) | |
@@ -107,15 +150,16 b' class FunctionMaker(object):' | |||||
107 | if mo is None: |
|
150 | if mo is None: | |
108 | raise SyntaxError('not a valid function template\n%s' % src) |
|
151 | raise SyntaxError('not a valid function template\n%s' % src) | |
109 | name = mo.group(1) # extract the function name |
|
152 | name = mo.group(1) # extract the function name | |
110 |
|
|
153 | names = set([name] + [arg.strip(' *') for arg in | |
111 |
|
|
154 | self.shortsignature.split(',')]) | |
112 |
for n |
|
155 | for n in names: | |
113 |
if n in |
|
156 | if n in ('_func_', '_call_'): | |
114 | raise NameError('%s is overridden in\n%s' % (n, src)) |
|
157 | raise NameError('%s is overridden in\n%s' % (n, src)) | |
115 | if not src.endswith('\n'): # add a newline just for safety |
|
158 | if not src.endswith('\n'): # add a newline just for safety | |
116 | src += '\n' |
|
159 | src += '\n' # this is needed in old versions of Python | |
117 | try: |
|
160 | try: | |
118 | code = compile(src, '<string>', 'single') |
|
161 | code = compile(src, '<string>', 'single') | |
|
162 | # print >> sys.stderr, 'Compiling %s' % src | |||
119 | exec code in evaldict |
|
163 | exec code in evaldict | |
120 | except: |
|
164 | except: | |
121 | print >> sys.stderr, 'Error in generated code:' |
|
165 | print >> sys.stderr, 'Error in generated code:' | |
@@ -129,7 +173,7 b' class FunctionMaker(object):' | |||||
129 |
|
173 | |||
130 | @classmethod |
|
174 | @classmethod | |
131 | def create(cls, obj, body, evaldict, defaults=None, |
|
175 | def create(cls, obj, body, evaldict, defaults=None, | |
132 | doc=None, module=None, addsource=True,**attrs): |
|
176 | doc=None, module=None, addsource=True, **attrs): | |
133 | """ |
|
177 | """ | |
134 | Create a function from the strings name, signature and body. |
|
178 | Create a function from the strings name, signature and body. | |
135 | evaldict is the evaluation dictionary. If addsource is true an attribute |
|
179 | evaldict is the evaluation dictionary. If addsource is true an attribute | |
@@ -144,9 +188,9 b' class FunctionMaker(object):' | |||||
144 | name = None |
|
188 | name = None | |
145 | signature = None |
|
189 | signature = None | |
146 | func = obj |
|
190 | func = obj | |
147 |
f |
|
191 | self = cls(func, name, signature, defaults, doc, module) | |
148 | ibody = '\n'.join(' ' + line for line in body.splitlines()) |
|
192 | ibody = '\n'.join(' ' + line for line in body.splitlines()) | |
149 |
return f |
|
193 | return self.make('def %(name)s(%(signature)s):\n' + ibody, | |
150 | evaldict, addsource, **attrs) |
|
194 | evaldict, addsource, **attrs) | |
151 |
|
195 | |||
152 | def decorator(caller, func=None): |
|
196 | def decorator(caller, func=None): | |
@@ -155,100 +199,22 b' def decorator(caller, func=None):' | |||||
155 | decorator(caller, func) decorates a function using a caller. |
|
199 | decorator(caller, func) decorates a function using a caller. | |
156 | """ |
|
200 | """ | |
157 | if func is not None: # returns a decorated function |
|
201 | if func is not None: # returns a decorated function | |
|
202 | evaldict = func.func_globals.copy() | |||
|
203 | evaldict['_call_'] = caller | |||
|
204 | evaldict['_func_'] = func | |||
158 | return FunctionMaker.create( |
|
205 | return FunctionMaker.create( | |
159 | func, "return _call_(_func_, %(signature)s)", |
|
206 | func, "return _call_(_func_, %(shortsignature)s)", | |
160 | dict(_call_=caller, _func_=func), undecorated=func) |
|
207 | evaldict, undecorated=func, __wrapped__=func) | |
161 | else: # returns a decorator |
|
208 | else: # returns a decorator | |
162 | if isinstance(caller, partial): |
|
209 | if isinstance(caller, partial): | |
163 | return partial(decorator, caller) |
|
210 | return partial(decorator, caller) | |
164 | # otherwise assume caller is a function |
|
211 | # otherwise assume caller is a function | |
165 | f = inspect.getargspec(caller)[0][0] # first arg |
|
212 | first = inspect.getargspec(caller)[0][0] # first arg | |
|
213 | evaldict = caller.func_globals.copy() | |||
|
214 | evaldict['_call_'] = caller | |||
|
215 | evaldict['decorator'] = decorator | |||
166 | return FunctionMaker.create( |
|
216 | return FunctionMaker.create( | |
167 | '%s(%s)' % (caller.__name__, f), |
|
217 | '%s(%s)' % (caller.__name__, first), | |
168 | 'return decorator(_call_, %s)' % f, |
|
218 | 'return decorator(_call_, %s)' % first, | |
169 | dict(_call_=caller, decorator=decorator), undecorated=caller, |
|
219 | evaldict, undecorated=caller, __wrapped__=caller, | |
170 | doc=caller.__doc__, module=caller.__module__) |
|
220 | doc=caller.__doc__, module=caller.__module__) | |
171 |
|
||||
172 | ###################### deprecated functionality ######################### |
|
|||
173 |
|
||||
174 | @decorator |
|
|||
175 | def deprecated(func, *args, **kw): |
|
|||
176 | "A decorator for deprecated functions" |
|
|||
177 | warnings.warn( |
|
|||
178 | ('Calling the deprecated function %r\n' |
|
|||
179 | 'Downgrade to decorator 2.3 if you want to use this functionality') |
|
|||
180 | % func.__name__, DeprecationWarning, stacklevel=3) |
|
|||
181 | return func(*args, **kw) |
|
|||
182 |
|
||||
183 | @deprecated |
|
|||
184 | def getinfo(func): |
|
|||
185 | """ |
|
|||
186 | Returns an info dictionary containing: |
|
|||
187 | - name (the name of the function : str) |
|
|||
188 | - argnames (the names of the arguments : list) |
|
|||
189 | - defaults (the values of the default arguments : tuple) |
|
|||
190 | - signature (the signature : str) |
|
|||
191 | - doc (the docstring : str) |
|
|||
192 | - module (the module name : str) |
|
|||
193 | - dict (the function __dict__ : str) |
|
|||
194 |
|
||||
195 | >>> def f(self, x=1, y=2, *args, **kw): pass |
|
|||
196 |
|
||||
197 | >>> info = getinfo(f) |
|
|||
198 |
|
||||
199 | >>> info["name"] |
|
|||
200 | 'f' |
|
|||
201 | >>> info["argnames"] |
|
|||
202 | ['self', 'x', 'y', 'args', 'kw'] |
|
|||
203 |
|
||||
204 | >>> info["defaults"] |
|
|||
205 | (1, 2) |
|
|||
206 |
|
||||
207 | >>> info["signature"] |
|
|||
208 | 'self, x, y, *args, **kw' |
|
|||
209 | """ |
|
|||
210 | assert inspect.ismethod(func) or inspect.isfunction(func) |
|
|||
211 | regargs, varargs, varkwargs, defaults = inspect.getargspec(func) |
|
|||
212 | argnames = list(regargs) |
|
|||
213 | if varargs: |
|
|||
214 | argnames.append(varargs) |
|
|||
215 | if varkwargs: |
|
|||
216 | argnames.append(varkwargs) |
|
|||
217 | signature = inspect.formatargspec(regargs, varargs, varkwargs, defaults, |
|
|||
218 | formatvalue=lambda value: "")[1:-1] |
|
|||
219 | return dict(name=func.__name__, argnames=argnames, signature=signature, |
|
|||
220 | defaults = func.func_defaults, doc=func.__doc__, |
|
|||
221 | module=func.__module__, dict=func.__dict__, |
|
|||
222 | globals=func.func_globals, closure=func.func_closure) |
|
|||
223 |
|
||||
224 | @deprecated |
|
|||
225 | def update_wrapper(wrapper, model, infodict=None): |
|
|||
226 | "A replacement for functools.update_wrapper" |
|
|||
227 | infodict = infodict or getinfo(model) |
|
|||
228 | wrapper.__name__ = infodict['name'] |
|
|||
229 | wrapper.__doc__ = infodict['doc'] |
|
|||
230 | wrapper.__module__ = infodict['module'] |
|
|||
231 | wrapper.__dict__.update(infodict['dict']) |
|
|||
232 | wrapper.func_defaults = infodict['defaults'] |
|
|||
233 | wrapper.undecorated = model |
|
|||
234 | return wrapper |
|
|||
235 |
|
||||
236 | @deprecated |
|
|||
237 | def new_wrapper(wrapper, model): |
|
|||
238 | """ |
|
|||
239 | An improvement over functools.update_wrapper. The wrapper is a generic |
|
|||
240 | callable object. It works by generating a copy of the wrapper with the |
|
|||
241 | right signature and by updating the copy, not the original. |
|
|||
242 | Moreovoer, 'model' can be a dictionary with keys 'name', 'doc', 'module', |
|
|||
243 | 'dict', 'defaults'. |
|
|||
244 | """ |
|
|||
245 | if isinstance(model, dict): |
|
|||
246 | infodict = model |
|
|||
247 | else: # assume model is a function |
|
|||
248 | infodict = getinfo(model) |
|
|||
249 | assert not '_wrapper_' in infodict["argnames"], ( |
|
|||
250 | '"_wrapper_" is a reserved argument name!') |
|
|||
251 | src = "lambda %(signature)s: _wrapper_(%(signature)s)" % infodict |
|
|||
252 | funcopy = eval(src, dict(_wrapper_=wrapper)) |
|
|||
253 | return update_wrapper(funcopy, model, infodict) |
|
|||
254 |
|
@@ -210,17 +210,23 b' def main():' | |||||
210 |
|
210 | |||
211 | print "\n".join(expand(sys.argv[1:])), |
|
211 | print "\n".join(expand(sys.argv[1:])), | |
212 |
|
212 | |||
213 | def mglob_f(self, arg): |
|
213 | ||
|
214 | def mglob(self, arg): | |||
214 | from IPython.utils.text import SList |
|
215 | from IPython.utils.text import SList | |
215 | if arg.strip(): |
|
216 | if arg.strip(): | |
216 | return SList(expand(arg)) |
|
217 | return SList(expand(arg)) | |
217 | print "Please specify pattern!" |
|
218 | print "Please specify pattern!" | |
218 | print globsyntax |
|
219 | print globsyntax | |
219 |
|
220 | |||
|
221 | ||||
|
222 | mglob.__doc__ = globsyntax | |||
|
223 | ||||
|
224 | ||||
220 | def init_ipython(ip): |
|
225 | def init_ipython(ip): | |
221 | """ register %mglob for IPython """ |
|
226 | """ register %mglob for IPython """ | |
222 | mglob_f.__doc__ = globsyntax |
|
227 | ||
223 |
ip. |
|
228 | ip.function_as_magic(mglob) | |
|
229 | ||||
224 |
|
230 | |||
225 | # test() |
|
231 | # test() | |
226 | if __name__ == "__main__": |
|
232 | if __name__ == "__main__": |
@@ -23,16 +23,19 b' Notes' | |||||
23 | #----------------------------------------------------------------------------- |
|
23 | #----------------------------------------------------------------------------- | |
24 |
|
24 | |||
25 | from __future__ import with_statement |
|
25 | from __future__ import with_statement | |
26 | import __main__ |
|
|||
27 |
|
26 | |||
28 | import sys |
|
27 | import sys | |
|
28 | import warnings | |||
|
29 | ||||
|
30 | # We need to use nested to support python 2.6, once we move to >=2.7, we can | |||
|
31 | # use the with keyword's new builtin support for nested managers | |||
29 | try: |
|
32 | try: | |
30 | from contextlib import nested |
|
33 | from contextlib import nested | |
31 | except: |
|
34 | except: | |
32 | from IPython.utils.nested_context import nested |
|
35 | from IPython.utils.nested_context import nested | |
33 | import warnings |
|
|||
34 |
|
36 | |||
35 | from IPython.core import ultratb |
|
37 | from IPython.core import ultratb | |
|
38 | from IPython.core.magic import Magics, magics_class, line_magic | |||
36 | from IPython.frontend.terminal.interactiveshell import TerminalInteractiveShell |
|
39 | from IPython.frontend.terminal.interactiveshell import TerminalInteractiveShell | |
37 | from IPython.frontend.terminal.ipapp import load_default_config |
|
40 | from IPython.frontend.terminal.ipapp import load_default_config | |
38 |
|
41 | |||
@@ -45,21 +48,27 b' from IPython.utils.io import ask_yes_no' | |||||
45 | #----------------------------------------------------------------------------- |
|
48 | #----------------------------------------------------------------------------- | |
46 |
|
49 | |||
47 | # This is an additional magic that is exposed in embedded shells. |
|
50 | # This is an additional magic that is exposed in embedded shells. | |
48 | def kill_embedded(self,parameter_s=''): |
|
51 | @magics_class | |
49 | """%kill_embedded : deactivate for good the current embedded IPython. |
|
52 | class EmbeddedMagics(Magics): | |
50 |
|
53 | |||
51 | This function (after asking for confirmation) sets an internal flag so that |
|
54 | @line_magic | |
52 | an embedded IPython will never activate again. This is useful to |
|
55 | def kill_embedded(self, parameter_s=''): | |
53 | permanently disable a shell that is being called inside a loop: once you've |
|
56 | """%kill_embedded : deactivate for good the current embedded IPython. | |
54 | figured out what you needed from it, you may then kill it and the program |
|
57 | ||
55 | will then continue to run without the interactive shell interfering again. |
|
58 | This function (after asking for confirmation) sets an internal flag so | |
56 | """ |
|
59 | that an embedded IPython will never activate again. This is useful to | |
|
60 | permanently disable a shell that is being called inside a loop: once | |||
|
61 | you've figured out what you needed from it, you may then kill it and | |||
|
62 | the program will then continue to run without the interactive shell | |||
|
63 | interfering again. | |||
|
64 | """ | |||
57 |
|
65 | |||
58 | kill = ask_yes_no("Are you sure you want to kill this embedded instance " |
|
66 | kill = ask_yes_no("Are you sure you want to kill this embedded instance " | |
59 | "(y/n)? [y/N] ",'n') |
|
67 | "(y/n)? [y/N] ",'n') | |
60 | if kill: |
|
68 | if kill: | |
61 | self.embedded_active = False |
|
69 | self.shell.embedded_active = False | |
62 |
print "This embedded IPython will not reactivate anymore |
|
70 | print ("This embedded IPython will not reactivate anymore " | |
|
71 | "once you exit.") | |||
63 |
|
72 | |||
64 |
|
73 | |||
65 | class InteractiveShellEmbed(TerminalInteractiveShell): |
|
74 | class InteractiveShellEmbed(TerminalInteractiveShell): | |
@@ -89,7 +98,6 b' class InteractiveShellEmbed(TerminalInteractiveShell):' | |||||
89 | ) |
|
98 | ) | |
90 |
|
99 | |||
91 | self.exit_msg = exit_msg |
|
100 | self.exit_msg = exit_msg | |
92 | self.define_magic("kill_embedded", kill_embedded) |
|
|||
93 |
|
101 | |||
94 | # don't use the ipython crash handler so that user exceptions aren't |
|
102 | # don't use the ipython crash handler so that user exceptions aren't | |
95 | # trapped |
|
103 | # trapped | |
@@ -100,6 +108,10 b' class InteractiveShellEmbed(TerminalInteractiveShell):' | |||||
100 | def init_sys_modules(self): |
|
108 | def init_sys_modules(self): | |
101 | pass |
|
109 | pass | |
102 |
|
110 | |||
|
111 | def init_magics(self): | |||
|
112 | super(InteractiveShellEmbed, self).init_magics() | |||
|
113 | self.register_magics(EmbeddedMagics) | |||
|
114 | ||||
103 | def __call__(self, header='', local_ns=None, module=None, dummy=None, |
|
115 | def __call__(self, header='', local_ns=None, module=None, dummy=None, | |
104 | stack_depth=1, global_ns=None): |
|
116 | stack_depth=1, global_ns=None): | |
105 | """Activate the interactive interpreter. |
|
117 | """Activate the interactive interpreter. |
@@ -14,13 +14,14 b'' | |||||
14 | # Imports |
|
14 | # Imports | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 |
|
16 | |||
17 | import __builtin__ |
|
|||
18 | import bdb |
|
17 | import bdb | |
19 | import os |
|
18 | import os | |
20 | import re |
|
19 | import re | |
21 | import sys |
|
20 | import sys | |
22 | import textwrap |
|
21 | import textwrap | |
23 |
|
22 | |||
|
23 | # We need to use nested to support python 2.6, once we move to >=2.7, we can | |||
|
24 | # use the with keyword's new builtin support for nested managers | |||
24 | try: |
|
25 | try: | |
25 | from contextlib import nested |
|
26 | from contextlib import nested | |
26 | except: |
|
27 | except: | |
@@ -29,7 +30,7 b' except:' | |||||
29 | from IPython.core.error import TryNext, UsageError |
|
30 | from IPython.core.error import TryNext, UsageError | |
30 | from IPython.core.usage import interactive_usage, default_banner |
|
31 | from IPython.core.usage import interactive_usage, default_banner | |
31 | from IPython.core.interactiveshell import InteractiveShell, InteractiveShellABC |
|
32 | from IPython.core.interactiveshell import InteractiveShell, InteractiveShellABC | |
32 | from IPython.core.pylabtools import pylab_activate |
|
33 | from IPython.core.magic import Magics, magics_class, line_magic | |
33 | from IPython.testing.skipdoctest import skip_doctest |
|
34 | from IPython.testing.skipdoctest import skip_doctest | |
34 | from IPython.utils.encoding import get_stream_enc |
|
35 | from IPython.utils.encoding import get_stream_enc | |
35 | from IPython.utils import py3compat |
|
36 | from IPython.utils import py3compat | |
@@ -120,6 +121,138 b" def rerun_pasted(shell, name='pasted_block'):" | |||||
120 | shell.run_cell(b) |
|
121 | shell.run_cell(b) | |
121 |
|
122 | |||
122 |
|
123 | |||
|
124 | #------------------------------------------------------------------------ | |||
|
125 | # Terminal-specific magics | |||
|
126 | #------------------------------------------------------------------------ | |||
|
127 | ||||
|
128 | @magics_class | |||
|
129 | class TerminalMagics(Magics): | |||
|
130 | ||||
|
131 | @line_magic | |||
|
132 | def autoindent(self, parameter_s = ''): | |||
|
133 | """Toggle autoindent on/off (if available).""" | |||
|
134 | ||||
|
135 | self.shell.set_autoindent() | |||
|
136 | print "Automatic indentation is:",['OFF','ON'][self.shell.autoindent] | |||
|
137 | ||||
|
138 | @skip_doctest | |||
|
139 | @line_magic | |||
|
140 | def cpaste(self, parameter_s=''): | |||
|
141 | """Paste & execute a pre-formatted code block from clipboard. | |||
|
142 | ||||
|
143 | You must terminate the block with '--' (two minus-signs) or Ctrl-D | |||
|
144 | alone on the line. You can also provide your own sentinel with '%paste | |||
|
145 | -s %%' ('%%' is the new sentinel for this operation) | |||
|
146 | ||||
|
147 | The block is dedented prior to execution to enable execution of method | |||
|
148 | definitions. '>' and '+' characters at the beginning of a line are | |||
|
149 | ignored, to allow pasting directly from e-mails, diff files and | |||
|
150 | doctests (the '...' continuation prompt is also stripped). The | |||
|
151 | executed block is also assigned to variable named 'pasted_block' for | |||
|
152 | later editing with '%edit pasted_block'. | |||
|
153 | ||||
|
154 | You can also pass a variable name as an argument, e.g. '%cpaste foo'. | |||
|
155 | This assigns the pasted block to variable 'foo' as string, without | |||
|
156 | dedenting or executing it (preceding >>> and + is still stripped) | |||
|
157 | ||||
|
158 | '%cpaste -r' re-executes the block previously entered by cpaste. | |||
|
159 | ||||
|
160 | Do not be alarmed by garbled output on Windows (it's a readline bug). | |||
|
161 | Just press enter and type -- (and press enter again) and the block | |||
|
162 | will be what was just pasted. | |||
|
163 | ||||
|
164 | IPython statements (magics, shell escapes) are not supported (yet). | |||
|
165 | ||||
|
166 | See also | |||
|
167 | -------- | |||
|
168 | paste: automatically pull code from clipboard. | |||
|
169 | ||||
|
170 | Examples | |||
|
171 | -------- | |||
|
172 | :: | |||
|
173 | ||||
|
174 | In [8]: %cpaste | |||
|
175 | Pasting code; enter '--' alone on the line to stop. | |||
|
176 | :>>> a = ["world!", "Hello"] | |||
|
177 | :>>> print " ".join(sorted(a)) | |||
|
178 | :-- | |||
|
179 | Hello world! | |||
|
180 | """ | |||
|
181 | ||||
|
182 | opts, name = self.parse_options(parameter_s, 'rs:', mode='string') | |||
|
183 | if 'r' in opts: | |||
|
184 | rerun_pasted(self.shell) | |||
|
185 | return | |||
|
186 | ||||
|
187 | sentinel = opts.get('s', '--') | |||
|
188 | block = strip_email_quotes(get_pasted_lines(sentinel)) | |||
|
189 | store_or_execute(self.shell, block, name) | |||
|
190 | ||||
|
191 | @line_magic | |||
|
192 | def paste(self, parameter_s=''): | |||
|
193 | """Paste & execute a pre-formatted code block from clipboard. | |||
|
194 | ||||
|
195 | The text is pulled directly from the clipboard without user | |||
|
196 | intervention and printed back on the screen before execution (unless | |||
|
197 | the -q flag is given to force quiet mode). | |||
|
198 | ||||
|
199 | The block is dedented prior to execution to enable execution of method | |||
|
200 | definitions. '>' and '+' characters at the beginning of a line are | |||
|
201 | ignored, to allow pasting directly from e-mails, diff files and | |||
|
202 | doctests (the '...' continuation prompt is also stripped). The | |||
|
203 | executed block is also assigned to variable named 'pasted_block' for | |||
|
204 | later editing with '%edit pasted_block'. | |||
|
205 | ||||
|
206 | You can also pass a variable name as an argument, e.g. '%paste foo'. | |||
|
207 | This assigns the pasted block to variable 'foo' as string, without | |||
|
208 | dedenting or executing it (preceding >>> and + is still stripped) | |||
|
209 | ||||
|
210 | Options | |||
|
211 | ------- | |||
|
212 | ||||
|
213 | -r: re-executes the block previously entered by cpaste. | |||
|
214 | ||||
|
215 | -q: quiet mode: do not echo the pasted text back to the terminal. | |||
|
216 | ||||
|
217 | IPython statements (magics, shell escapes) are not supported (yet). | |||
|
218 | ||||
|
219 | See also | |||
|
220 | -------- | |||
|
221 | cpaste: manually paste code into terminal until you mark its end. | |||
|
222 | """ | |||
|
223 | opts, name = self.parse_options(parameter_s, 'rq', mode='string') | |||
|
224 | if 'r' in opts: | |||
|
225 | rerun_pasted(self.shell) | |||
|
226 | return | |||
|
227 | try: | |||
|
228 | text = self.shell.hooks.clipboard_get() | |||
|
229 | block = strip_email_quotes(text.splitlines()) | |||
|
230 | except TryNext as clipboard_exc: | |||
|
231 | message = getattr(clipboard_exc, 'args') | |||
|
232 | if message: | |||
|
233 | error(message[0]) | |||
|
234 | else: | |||
|
235 | error('Could not get text from the clipboard.') | |||
|
236 | return | |||
|
237 | ||||
|
238 | # By default, echo back to terminal unless quiet mode is requested | |||
|
239 | if 'q' not in opts: | |||
|
240 | write = self.shell.write | |||
|
241 | write(self.shell.pycolorize(block)) | |||
|
242 | if not block.endswith('\n'): | |||
|
243 | write('\n') | |||
|
244 | write("## -- End pasted text --\n") | |||
|
245 | ||||
|
246 | store_or_execute(self.shell, block, name) | |||
|
247 | ||||
|
248 | # Class-level: add a '%cls' magic only on Windows | |||
|
249 | if sys.platform == 'win32': | |||
|
250 | @line_magic | |||
|
251 | def cls(self, s): | |||
|
252 | """Clear screen. | |||
|
253 | """ | |||
|
254 | os.system("cls") | |||
|
255 | ||||
123 | #----------------------------------------------------------------------------- |
|
256 | #----------------------------------------------------------------------------- | |
124 | # Main class |
|
257 | # Main class | |
125 | #----------------------------------------------------------------------------- |
|
258 | #----------------------------------------------------------------------------- | |
@@ -531,134 +664,13 b' class TerminalInteractiveShell(InteractiveShell):' | |||||
531 | else: |
|
664 | else: | |
532 | self.ask_exit() |
|
665 | self.ask_exit() | |
533 |
|
666 | |||
534 | #------------------------------------------------------------------------ |
|
667 | #------------------------------------------------------------------------- | |
535 | # Magic overrides |
|
668 | # Things related to magics | |
536 | #------------------------------------------------------------------------ |
|
669 | #------------------------------------------------------------------------- | |
537 | # Once the base class stops inheriting from magic, this code needs to be |
|
|||
538 | # moved into a separate machinery as well. For now, at least isolate here |
|
|||
539 | # the magics which this class needs to implement differently from the base |
|
|||
540 | # class, or that are unique to it. |
|
|||
541 |
|
||||
542 | def magic_autoindent(self, parameter_s = ''): |
|
|||
543 | """Toggle autoindent on/off (if available).""" |
|
|||
544 |
|
||||
545 | self.shell.set_autoindent() |
|
|||
546 | print "Automatic indentation is:",['OFF','ON'][self.shell.autoindent] |
|
|||
547 |
|
||||
548 | @skip_doctest |
|
|||
549 | def magic_cpaste(self, parameter_s=''): |
|
|||
550 | """Paste & execute a pre-formatted code block from clipboard. |
|
|||
551 |
|
||||
552 | You must terminate the block with '--' (two minus-signs) or Ctrl-D |
|
|||
553 | alone on the line. You can also provide your own sentinel with '%paste |
|
|||
554 | -s %%' ('%%' is the new sentinel for this operation) |
|
|||
555 |
|
||||
556 | The block is dedented prior to execution to enable execution of method |
|
|||
557 | definitions. '>' and '+' characters at the beginning of a line are |
|
|||
558 | ignored, to allow pasting directly from e-mails, diff files and |
|
|||
559 | doctests (the '...' continuation prompt is also stripped). The |
|
|||
560 | executed block is also assigned to variable named 'pasted_block' for |
|
|||
561 | later editing with '%edit pasted_block'. |
|
|||
562 |
|
||||
563 | You can also pass a variable name as an argument, e.g. '%cpaste foo'. |
|
|||
564 | This assigns the pasted block to variable 'foo' as string, without |
|
|||
565 | dedenting or executing it (preceding >>> and + is still stripped) |
|
|||
566 |
|
||||
567 | '%cpaste -r' re-executes the block previously entered by cpaste. |
|
|||
568 |
|
||||
569 | Do not be alarmed by garbled output on Windows (it's a readline bug). |
|
|||
570 | Just press enter and type -- (and press enter again) and the block |
|
|||
571 | will be what was just pasted. |
|
|||
572 |
|
||||
573 | IPython statements (magics, shell escapes) are not supported (yet). |
|
|||
574 |
|
||||
575 | See also |
|
|||
576 | -------- |
|
|||
577 | paste: automatically pull code from clipboard. |
|
|||
578 |
|
||||
579 | Examples |
|
|||
580 | -------- |
|
|||
581 | :: |
|
|||
582 |
|
||||
583 | In [8]: %cpaste |
|
|||
584 | Pasting code; enter '--' alone on the line to stop. |
|
|||
585 | :>>> a = ["world!", "Hello"] |
|
|||
586 | :>>> print " ".join(sorted(a)) |
|
|||
587 | :-- |
|
|||
588 | Hello world! |
|
|||
589 | """ |
|
|||
590 |
|
||||
591 | opts, name = self.parse_options(parameter_s, 'rs:', mode='string') |
|
|||
592 | if 'r' in opts: |
|
|||
593 | rerun_pasted(self.shell) |
|
|||
594 | return |
|
|||
595 |
|
||||
596 | sentinel = opts.get('s', '--') |
|
|||
597 | block = strip_email_quotes(get_pasted_lines(sentinel)) |
|
|||
598 | store_or_execute(self.shell, block, name) |
|
|||
599 |
|
||||
600 | def magic_paste(self, parameter_s=''): |
|
|||
601 | """Paste & execute a pre-formatted code block from clipboard. |
|
|||
602 |
|
||||
603 | The text is pulled directly from the clipboard without user |
|
|||
604 | intervention and printed back on the screen before execution (unless |
|
|||
605 | the -q flag is given to force quiet mode). |
|
|||
606 |
|
||||
607 | The block is dedented prior to execution to enable execution of method |
|
|||
608 | definitions. '>' and '+' characters at the beginning of a line are |
|
|||
609 | ignored, to allow pasting directly from e-mails, diff files and |
|
|||
610 | doctests (the '...' continuation prompt is also stripped). The |
|
|||
611 | executed block is also assigned to variable named 'pasted_block' for |
|
|||
612 | later editing with '%edit pasted_block'. |
|
|||
613 |
|
||||
614 | You can also pass a variable name as an argument, e.g. '%paste foo'. |
|
|||
615 | This assigns the pasted block to variable 'foo' as string, without |
|
|||
616 | dedenting or executing it (preceding >>> and + is still stripped) |
|
|||
617 |
|
||||
618 | Options |
|
|||
619 | ------- |
|
|||
620 |
|
||||
621 | -r: re-executes the block previously entered by cpaste. |
|
|||
622 |
|
||||
623 | -q: quiet mode: do not echo the pasted text back to the terminal. |
|
|||
624 |
|
||||
625 | IPython statements (magics, shell escapes) are not supported (yet). |
|
|||
626 |
|
||||
627 | See also |
|
|||
628 | -------- |
|
|||
629 | cpaste: manually paste code into terminal until you mark its end. |
|
|||
630 | """ |
|
|||
631 | opts, name = self.parse_options(parameter_s, 'rq', mode='string') |
|
|||
632 | if 'r' in opts: |
|
|||
633 | rerun_pasted(self.shell) |
|
|||
634 | return |
|
|||
635 | try: |
|
|||
636 | text = self.shell.hooks.clipboard_get() |
|
|||
637 | block = strip_email_quotes(text.splitlines()) |
|
|||
638 | except TryNext as clipboard_exc: |
|
|||
639 | message = getattr(clipboard_exc, 'args') |
|
|||
640 | if message: |
|
|||
641 | error(message[0]) |
|
|||
642 | else: |
|
|||
643 | error('Could not get text from the clipboard.') |
|
|||
644 | return |
|
|||
645 |
|
||||
646 | # By default, echo back to terminal unless quiet mode is requested |
|
|||
647 | if 'q' not in opts: |
|
|||
648 | write = self.shell.write |
|
|||
649 | write(self.shell.pycolorize(block)) |
|
|||
650 | if not block.endswith('\n'): |
|
|||
651 | write('\n') |
|
|||
652 | write("## -- End pasted text --\n") |
|
|||
653 |
|
||||
654 | store_or_execute(self.shell, block, name) |
|
|||
655 |
|
670 | |||
656 | # Class-level: add a '%cls' magic only on Windows |
|
671 | def init_magics(self): | |
657 | if sys.platform == 'win32': |
|
672 | super(TerminalInteractiveShell, self).init_magics() | |
658 | def magic_cls(self, s): |
|
673 | self.register_magics(TerminalMagics) | |
659 | """Clear screen. |
|
|||
660 | """ |
|
|||
661 | os.system("cls") |
|
|||
662 |
|
674 | |||
663 | def showindentationerror(self): |
|
675 | def showindentationerror(self): | |
664 | super(TerminalInteractiveShell, self).showindentationerror() |
|
676 | super(TerminalInteractiveShell, self).showindentationerror() |
@@ -813,10 +813,10 b' class DirectView(View):' | |||||
813 | except NameError: |
|
813 | except NameError: | |
814 | print "The IPython parallel magics (%result, %px, %autopx) only work within IPython." |
|
814 | print "The IPython parallel magics (%result, %px, %autopx) only work within IPython." | |
815 | else: |
|
815 | else: | |
816 |
pmagic = ip. |
|
816 | pmagic = ip.magics_manager.registry.get('ParallelMagics') | |
817 | if pmagic is None: |
|
817 | if pmagic is None: | |
818 |
ip.magic |
|
818 | ip.magic('load_ext parallelmagic') | |
819 |
pmagic = ip. |
|
819 | pmagic = ip.magics_manager.registry.get('ParallelMagics') | |
820 |
|
820 | |||
821 | pmagic.active_view = self |
|
821 | pmagic.active_view = self | |
822 |
|
822 |
@@ -374,21 +374,21 b' class TestView(ClusterTestCase, ParametricTestCase):' | |||||
374 | v.activate() |
|
374 | v.activate() | |
375 | v.block=True |
|
375 | v.block=True | |
376 |
|
376 | |||
377 |
ip.magic |
|
377 | ip.magic('px a=5') | |
378 | self.assertEquals(v['a'], 5) |
|
378 | self.assertEquals(v['a'], 5) | |
379 |
ip.magic |
|
379 | ip.magic('px a=10') | |
380 | self.assertEquals(v['a'], 10) |
|
380 | self.assertEquals(v['a'], 10) | |
381 | sio = StringIO() |
|
381 | sio = StringIO() | |
382 | savestdout = sys.stdout |
|
382 | savestdout = sys.stdout | |
383 | sys.stdout = sio |
|
383 | sys.stdout = sio | |
384 | # just 'print a' worst ~99% of the time, but this ensures that |
|
384 | # just 'print a' worst ~99% of the time, but this ensures that | |
385 | # the stdout message has arrived when the result is finished: |
|
385 | # the stdout message has arrived when the result is finished: | |
386 |
ip.magic |
|
386 | ip.magic('px import sys,time;print (a); sys.stdout.flush();time.sleep(0.2)') | |
387 | sys.stdout = savestdout |
|
387 | sys.stdout = savestdout | |
388 | buf = sio.getvalue() |
|
388 | buf = sio.getvalue() | |
389 | self.assertTrue('[stdout:' in buf, buf) |
|
389 | self.assertTrue('[stdout:' in buf, buf) | |
390 | self.assertTrue(buf.rstrip().endswith('10')) |
|
390 | self.assertTrue(buf.rstrip().endswith('10')) | |
391 |
self.assertRaisesRemote(ZeroDivisionError, ip.magic |
|
391 | self.assertRaisesRemote(ZeroDivisionError, ip.magic, 'px 1/0') | |
392 |
|
392 | |||
393 | def test_magic_px_nonblocking(self): |
|
393 | def test_magic_px_nonblocking(self): | |
394 | ip = get_ipython() |
|
394 | ip = get_ipython() | |
@@ -396,18 +396,18 b' class TestView(ClusterTestCase, ParametricTestCase):' | |||||
396 | v.activate() |
|
396 | v.activate() | |
397 | v.block=False |
|
397 | v.block=False | |
398 |
|
398 | |||
399 |
ip.magic |
|
399 | ip.magic('px a=5') | |
400 | self.assertEquals(v['a'], 5) |
|
400 | self.assertEquals(v['a'], 5) | |
401 |
ip.magic |
|
401 | ip.magic('px a=10') | |
402 | self.assertEquals(v['a'], 10) |
|
402 | self.assertEquals(v['a'], 10) | |
403 | sio = StringIO() |
|
403 | sio = StringIO() | |
404 | savestdout = sys.stdout |
|
404 | savestdout = sys.stdout | |
405 | sys.stdout = sio |
|
405 | sys.stdout = sio | |
406 |
ip.magic |
|
406 | ip.magic('px print a') | |
407 | sys.stdout = savestdout |
|
407 | sys.stdout = savestdout | |
408 | buf = sio.getvalue() |
|
408 | buf = sio.getvalue() | |
409 | self.assertFalse('[stdout:%i]'%v.targets in buf) |
|
409 | self.assertFalse('[stdout:%i]'%v.targets in buf) | |
410 |
ip.magic |
|
410 | ip.magic('px 1/0') | |
411 | ar = v.get_result(-1) |
|
411 | ar = v.get_result(-1) | |
412 | self.assertRaisesRemote(ZeroDivisionError, ar.get) |
|
412 | self.assertRaisesRemote(ZeroDivisionError, ar.get) | |
413 |
|
413 | |||
@@ -420,12 +420,12 b' class TestView(ClusterTestCase, ParametricTestCase):' | |||||
420 | sio = StringIO() |
|
420 | sio = StringIO() | |
421 | savestdout = sys.stdout |
|
421 | savestdout = sys.stdout | |
422 | sys.stdout = sio |
|
422 | sys.stdout = sio | |
423 |
ip.magic |
|
423 | ip.magic('autopx') | |
424 | ip.run_cell('\n'.join(('a=5','b=10','c=0'))) |
|
424 | ip.run_cell('\n'.join(('a=5','b=10','c=0'))) | |
425 | ip.run_cell('b*=2') |
|
425 | ip.run_cell('b*=2') | |
426 | ip.run_cell('print (b)') |
|
426 | ip.run_cell('print (b)') | |
427 | ip.run_cell("b/c") |
|
427 | ip.run_cell("b/c") | |
428 |
ip.magic |
|
428 | ip.magic('autopx') | |
429 | sys.stdout = savestdout |
|
429 | sys.stdout = savestdout | |
430 | output = sio.getvalue().strip() |
|
430 | output = sio.getvalue().strip() | |
431 | self.assertTrue(output.startswith('%autopx enabled')) |
|
431 | self.assertTrue(output.startswith('%autopx enabled')) | |
@@ -445,13 +445,13 b' class TestView(ClusterTestCase, ParametricTestCase):' | |||||
445 | sio = StringIO() |
|
445 | sio = StringIO() | |
446 | savestdout = sys.stdout |
|
446 | savestdout = sys.stdout | |
447 | sys.stdout = sio |
|
447 | sys.stdout = sio | |
448 |
ip.magic |
|
448 | ip.magic('autopx') | |
449 | ip.run_cell('\n'.join(('a=5','b=10','c=0'))) |
|
449 | ip.run_cell('\n'.join(('a=5','b=10','c=0'))) | |
450 | ip.run_cell('print (b)') |
|
450 | ip.run_cell('print (b)') | |
451 | ip.run_cell('import time; time.sleep(0.1)') |
|
451 | ip.run_cell('import time; time.sleep(0.1)') | |
452 | ip.run_cell("b/c") |
|
452 | ip.run_cell("b/c") | |
453 | ip.run_cell('b*=2') |
|
453 | ip.run_cell('b*=2') | |
454 |
ip.magic |
|
454 | ip.magic('autopx') | |
455 | sys.stdout = savestdout |
|
455 | sys.stdout = savestdout | |
456 | output = sio.getvalue().strip() |
|
456 | output = sio.getvalue().strip() | |
457 | self.assertTrue(output.startswith('%autopx enabled')) |
|
457 | self.assertTrue(output.startswith('%autopx enabled')) | |
@@ -472,10 +472,10 b' class TestView(ClusterTestCase, ParametricTestCase):' | |||||
472 | v['a'] = 111 |
|
472 | v['a'] = 111 | |
473 | ra = v['a'] |
|
473 | ra = v['a'] | |
474 |
|
474 | |||
475 |
ar = ip.magic |
|
475 | ar = ip.magic('result') | |
476 | self.assertEquals(ar.msg_ids, [v.history[-1]]) |
|
476 | self.assertEquals(ar.msg_ids, [v.history[-1]]) | |
477 | self.assertEquals(ar.get(), 111) |
|
477 | self.assertEquals(ar.get(), 111) | |
478 |
ar = ip.magic |
|
478 | ar = ip.magic('result -2') | |
479 | self.assertEquals(ar.msg_ids, [v.history[-2]]) |
|
479 | self.assertEquals(ar.msg_ids, [v.history[-2]]) | |
480 |
|
480 | |||
481 | def test_unicode_execute(self): |
|
481 | def test_unicode_execute(self): |
@@ -25,7 +25,6 b' import nose.tools as nt' | |||||
25 | from IPython.zmq.blockingkernelmanager import BlockingKernelManager |
|
25 | from IPython.zmq.blockingkernelmanager import BlockingKernelManager | |
26 | from IPython.utils import path, py3compat |
|
26 | from IPython.utils import path, py3compat | |
27 |
|
27 | |||
28 |
|
||||
29 | #------------------------------------------------------------------------------- |
|
28 | #------------------------------------------------------------------------------- | |
30 | # Tests |
|
29 | # Tests | |
31 | #------------------------------------------------------------------------------- |
|
30 | #------------------------------------------------------------------------------- | |
@@ -38,6 +37,8 b' def setup():' | |||||
38 |
|
37 | |||
39 | IPYTHONDIR = tempfile.mkdtemp() |
|
38 | IPYTHONDIR = tempfile.mkdtemp() | |
40 | env = dict(IPYTHONDIR=IPYTHONDIR) |
|
39 | env = dict(IPYTHONDIR=IPYTHONDIR) | |
|
40 | if 'PYTHONPATH' in os.environ: | |||
|
41 | env['PYTHONPATH'] = os.environ['PYTHONPATH'] | |||
41 | save_get_ipython_dir = path.get_ipython_dir |
|
42 | save_get_ipython_dir = path.get_ipython_dir | |
42 | path.get_ipython_dir = lambda : IPYTHONDIR |
|
43 | path.get_ipython_dir = lambda : IPYTHONDIR | |
43 |
|
44 |
@@ -33,7 +33,7 b' from IPython.core import page, pylabtools' | |||||
33 | from IPython.core.autocall import ZMQExitAutocall |
|
33 | from IPython.core.autocall import ZMQExitAutocall | |
34 | from IPython.core.displaypub import DisplayPublisher |
|
34 | from IPython.core.displaypub import DisplayPublisher | |
35 | from IPython.core.macro import Macro |
|
35 | from IPython.core.macro import Macro | |
36 | from IPython.core.magic import MacroToEdit |
|
36 | from IPython.core.magics import MacroToEdit | |
37 | from IPython.core.payloadpage import install_payload_page |
|
37 | from IPython.core.payloadpage import install_payload_page | |
38 | from IPython.lib.kernel import ( |
|
38 | from IPython.lib.kernel import ( | |
39 | get_connection_file, get_connection_info, connect_qtconsole |
|
39 | get_connection_file, get_connection_info, connect_qtconsole | |
@@ -49,7 +49,6 b' from IPython.zmq.displayhook import ZMQShellDisplayHook, _encode_binary' | |||||
49 | from IPython.zmq.session import extract_header |
|
49 | from IPython.zmq.session import extract_header | |
50 | from session import Session |
|
50 | from session import Session | |
51 |
|
51 | |||
52 |
|
||||
53 | #----------------------------------------------------------------------------- |
|
52 | #----------------------------------------------------------------------------- | |
54 | # Functions and classes |
|
53 | # Functions and classes | |
55 | #----------------------------------------------------------------------------- |
|
54 | #----------------------------------------------------------------------------- |
@@ -100,17 +100,45 b' IPython itself, plus a lot of system-type features. They are all' | |||||
100 | prefixed with a % character, but parameters are given without |
|
100 | prefixed with a % character, but parameters are given without | |
101 | parentheses or quotes. |
|
101 | parentheses or quotes. | |
102 |
|
102 | |||
103 | Example: typing ``%cd mydir`` changes your working directory to 'mydir', if it |
|
103 | Lines that begin with ``%%`` signal a *cell magic*: they take as arguments not | |
104 | exists. |
|
104 | only the rest of the current line, but all lines below them as well, in the | |
105 |
|
105 | current execution block. Cell magics can in fact make arbitrary modifications | ||
106 | If you have 'automagic' enabled (as it by default), you don't need |
|
106 | to the input they receive, which need not even be valid Python code at all. | |
107 | to type in the % explicitly. IPython will scan its internal list of |
|
107 | They receive the whole block as a single string. | |
108 | magic functions and call one if it exists. With automagic on you can |
|
108 | ||
109 | then just type ``cd mydir`` to go to directory 'mydir'. The automagic |
|
109 | As a line magic example, the ``%cd`` magic works just like the OS command of | |
110 | system has the lowest possible precedence in name searches, so defining |
|
110 | the same name:: | |
111 | an identifier with the same name as an existing magic function will |
|
111 | ||
112 | shadow it for automagic use. You can still access the shadowed magic |
|
112 | In [8]: %cd | |
113 | function by explicitly using the % character at the beginning of the line. |
|
113 | /home/fperez | |
|
114 | ||||
|
115 | The following uses the builtin ``timeit`` in cell mode:: | |||
|
116 | ||||
|
117 | In [10]: %%timeit x = range(10000) | |||
|
118 | ...: min(x) | |||
|
119 | ...: max(x) | |||
|
120 | ...: | |||
|
121 | 1000 loops, best of 3: 438 us per loop | |||
|
122 | ||||
|
123 | In this case, ``x = range(10000)`` is called as the line argument, and the | |||
|
124 | block with ``min(x)`` and ``max(x)`` is called as the cell body. The | |||
|
125 | ``timeit`` magic receives both. | |||
|
126 | ||||
|
127 | If you have 'automagic' enabled (as it by default), you don't need to type in | |||
|
128 | the single ``%`` explicitly for line magics; IPython will scan its internal | |||
|
129 | list of magic functions and call one if it exists. With automagic on you can | |||
|
130 | then just type ``cd mydir`` to go to directory 'mydir':: | |||
|
131 | ||||
|
132 | In [9]: cd mydir | |||
|
133 | /home/fperez/mydir | |||
|
134 | ||||
|
135 | Note that cell magics *always* require an explicit ``%%`` prefix, automagic | |||
|
136 | calling only works for line magics. | |||
|
137 | ||||
|
138 | The automagic system has the lowest possible precedence in name searches, so | |||
|
139 | defining an identifier with the same name as an existing magic function will | |||
|
140 | shadow it for automagic use. You can still access the shadowed magic function | |||
|
141 | by explicitly using the ``%`` character at the beginning of the line. | |||
114 |
|
142 | |||
115 | An example (with automagic on) should clarify all this: |
|
143 | An example (with automagic on) should clarify all this: | |
116 |
|
144 | |||
@@ -137,24 +165,141 b' An example (with automagic on) should clarify all this:' | |||||
137 |
|
165 | |||
138 | /home/fperez/ipython |
|
166 | /home/fperez/ipython | |
139 |
|
167 | |||
140 | You can define your own magic functions to extend the system. The |
|
168 | Defining your own magics | |
141 | following example defines a new magic command, %impall: |
|
169 | ~~~~~~~~~~~~~~~~~~~~~~~~ | |
|
170 | ||||
|
171 | There are two main ways to define your own magic functions: from standalone | |||
|
172 | functions and by inheriting from a base class provided by IPython: | |||
|
173 | :class:`IPython.core.magic.Magics`. Below we show code you can place in a file | |||
|
174 | that you load from your configuration, such as any file in the ``startup`` | |||
|
175 | subdirectory of your default IPython profile. | |||
|
176 | ||||
|
177 | First, let us see the simplest case. The following shows how to create a line | |||
|
178 | magic, a cell one and one that works in both modes, using just plain functions: | |||
142 |
|
179 | |||
143 | .. sourcecode:: python |
|
180 | .. sourcecode:: python | |
144 |
|
181 | |||
|
182 | from IPython.core.magic import (register_line_magic, register_cell_magic, | |||
|
183 | register_line_cell_magic) | |||
|
184 | ||||
|
185 | @register_line_magic | |||
|
186 | def lmagic(line): | |||
|
187 | "my line magic" | |||
|
188 | return line | |||
|
189 | ||||
|
190 | @register_cell_magic | |||
|
191 | def cmagic(line, cell): | |||
|
192 | "my cell magic" | |||
|
193 | return line, cell | |||
|
194 | ||||
|
195 | @register_line_cell_magic | |||
|
196 | def lcmagic(line, cell=None): | |||
|
197 | "Magic that works both as %lcmagic and as %%lcmagic" | |||
|
198 | if cell is None: | |||
|
199 | print "Called as line magic" | |||
|
200 | return line | |||
|
201 | else: | |||
|
202 | print "Called as cell magic" | |||
|
203 | return line, cell | |||
|
204 | ||||
|
205 | # We delete these to avoid name conflicts for automagic to work | |||
|
206 | del lmagic, lcmagic | |||
|
207 | ||||
|
208 | ||||
|
209 | You can also create magics of all three kinds by inheriting from the | |||
|
210 | :class:`IPython.core.magic.Magics` class. This lets you create magics that can | |||
|
211 | potentially hold state in between calls, and that have full access to the main | |||
|
212 | IPython object: | |||
|
213 | ||||
|
214 | .. sourcecode:: python | |||
|
215 | ||||
|
216 | # This code can be put in any Python module, it does not require IPython | |||
|
217 | # itself to be running already. It only creates the magics subclass but | |||
|
218 | # doesn't instantiate it yet. | |||
|
219 | from IPython.core.magic import (Magics, magics_class, line_magic, | |||
|
220 | cell_magic, line_cell_magic) | |||
|
221 | ||||
|
222 | # The class MUST call this class decorator at creation time | |||
|
223 | @magics_class | |||
|
224 | class MyMagics(Magics): | |||
|
225 | ||||
|
226 | @line_magic | |||
|
227 | def lmagic(self, line): | |||
|
228 | "my line magic" | |||
|
229 | print "Full access to the main IPython object:", self.shell | |||
|
230 | print "Variables in the user namespace:", self.user_ns.keys() | |||
|
231 | return line | |||
|
232 | ||||
|
233 | @cell_magic | |||
|
234 | def cmagic(self, line, cell): | |||
|
235 | "my cell magic" | |||
|
236 | return line, cell | |||
|
237 | ||||
|
238 | @line_cell_magic | |||
|
239 | def lcmagic(self, line, cell=None): | |||
|
240 | "Magic that works both as %lcmagic and as %%lcmagic" | |||
|
241 | if cell is None: | |||
|
242 | print "Called as line magic" | |||
|
243 | return line | |||
|
244 | else: | |||
|
245 | print "Called as cell magic" | |||
|
246 | return line, cell | |||
|
247 | ||||
|
248 | ||||
|
249 | # In order to actually use these magics, you must register them with a | |||
|
250 | # running IPython. This code must be placed in a file that is loaded once | |||
|
251 | # IPython is up and running: | |||
145 | ip = get_ipython() |
|
252 | ip = get_ipython() | |
|
253 | # You can register the class itself without instantiating it. IPython will | |||
|
254 | # call the default constructor on it. | |||
|
255 | ip.register_magics(MyMagics) | |||
146 |
|
256 | |||
147 | def doimp(self, arg): |
|
257 | If you want to create a class with a different constructor that holds | |
148 | ip = self.api |
|
258 | additional state, then you should always call the parent constructor and | |
149 | ip.ex("import %s; reload(%s); from %s import *" % (arg,arg,arg) ) |
|
259 | instantiate the class yourself before registration: | |
150 |
|
260 | |||
151 | ip.define_magic('impall', doimp) |
|
261 | .. sourcecode:: python | |
|
262 | ||||
|
263 | @magics_class | |||
|
264 | class StatefulMagics(Magics): | |||
|
265 | "Magics that hold additional state" | |||
|
266 | ||||
|
267 | def __init__(self, shell, data): | |||
|
268 | # You must call the parent constructor | |||
|
269 | super(StatefulMagics, self).__init__(shell) | |||
|
270 | self.data = data | |||
|
271 | ||||
|
272 | # etc... | |||
|
273 | ||||
|
274 | # This class must then be registered with a manually created instance, | |||
|
275 | # since its constructor has different arguments from the default: | |||
|
276 | ip = get_ipython() | |||
|
277 | magics = StatefulMagics(ip, some_data) | |||
|
278 | ip.register_magics(magics) | |||
|
279 | ||||
|
280 | ||||
|
281 | In earlier versions, IPython had an API for the creation of line magics (cell | |||
|
282 | magics did not exist at the time) that required you to create functions with a | |||
|
283 | method-looking signature and to manually pass both the function and the name. | |||
|
284 | While this API is no longer recommended, it remains indefinitely supported for | |||
|
285 | backwards compatibility purposes. With the old API, you'd create a magic as | |||
|
286 | follows: | |||
|
287 | ||||
|
288 | .. sourcecode:: python | |||
|
289 | ||||
|
290 | def func(self, line): | |||
|
291 | print "Line magic called with line:", line | |||
|
292 | print "IPython object:", self.shell | |||
|
293 | ||||
|
294 | ip = get_ipython() | |||
|
295 | # Declare this function as the magic %mycommand | |||
|
296 | ip.define_magic('mycommand', func) | |||
152 |
|
297 | |||
153 | Type ``%magic`` for more information, including a list of all available magic |
|
298 | Type ``%magic`` for more information, including a list of all available magic | |
154 | functions at any time and their docstrings. You can also type |
|
299 | functions at any time and their docstrings. You can also type | |
155 |
``%magic_function_name?`` (see :ref:`below <dynamic_object_info>` for |
|
300 | ``%magic_function_name?`` (see :ref:`below <dynamic_object_info>` for | |
156 |
the '?' system) to get information about any particular magic |
|
301 | information on the '?' system) to get information about any particular magic | |
157 | interested in. |
|
302 | function you are interested in. | |
158 |
|
303 | |||
159 | The API documentation for the :mod:`IPython.core.magic` module contains the full |
|
304 | The API documentation for the :mod:`IPython.core.magic` module contains the full | |
160 | docstrings of all currently available magic commands. |
|
305 | docstrings of all currently available magic commands. |
@@ -35,20 +35,42 b' Magic functions' | |||||
35 | =============== |
|
35 | =============== | |
36 |
|
36 | |||
37 | IPython has a set of predefined 'magic functions' that you can call with a |
|
37 | IPython has a set of predefined 'magic functions' that you can call with a | |
38 |
command line style syntax. |
|
38 | command line style syntax. There are two kinds of magics, line-oriented and | |
|
39 | cell-oriented. Line magics are prefixed with the ``%`` character and work much | |||
|
40 | like OS command-line calls: they get as an argument the rest of the line, where | |||
|
41 | arguments are passed without parentheses or quotes. Cell magics are prefixed | |||
|
42 | with a double ``%%``, and they are functions that get as an argument not only | |||
|
43 | the rest of the line, but also the lines below it in a separate argument. | |||
|
44 | ||||
|
45 | The following examples show how to call the builtin ``timeit`` magic, both in | |||
|
46 | line and cell mode:: | |||
|
47 | ||||
|
48 | In [1]: %timeit range(1000) | |||
|
49 | 100000 loops, best of 3: 7.76 us per loop | |||
|
50 | ||||
|
51 | In [2]: %%timeit x = range(10000) | |||
|
52 | ...: max(x) | |||
|
53 | ...: | |||
|
54 | 1000 loops, best of 3: 223 us per loop | |||
|
55 | ||||
|
56 | The builtin magics include: | |||
39 |
|
57 | |||
40 | - Functions that work with code: ``%run``, ``%edit``, ``%save``, ``%macro``, |
|
58 | - Functions that work with code: ``%run``, ``%edit``, ``%save``, ``%macro``, | |
41 | ``%recall``, etc. |
|
59 | ``%recall``, etc. | |
42 |
- Functions which affect the shell: ``%colors``, ``%xmode``, ``%autoindent``, |
|
60 | - Functions which affect the shell: ``%colors``, ``%xmode``, ``%autoindent``, | |
|
61 | etc. | |||
43 | - Other functions such as ``%reset``, ``%timeit`` or ``%paste``. |
|
62 | - Other functions such as ``%reset``, ``%timeit`` or ``%paste``. | |
44 |
|
63 | |||
45 |
You can always call the |
|
64 | You can always call them using the % prefix, and if you're calling a line magic | |
46 | by itself, you can omit even that:: |
|
65 | on a line by itself, you can omit even that (cell magics must always have the | |
|
66 | ``%%`` prefix):: | |||
47 |
|
67 | |||
48 | run thescript.py |
|
68 | run thescript.py | |
49 |
|
69 | |||
50 | For more details on any magic function, call ``%somemagic?`` to read its |
|
70 | A more detailed explanation of the magic system can be obtained by calling | |
51 |
|
|
71 | ``%magic``, and for more details on any magic function, call ``%somemagic?`` to | |
|
72 | read its docstring. To see all the available magic functions, call | |||
|
73 | ``%lsmagic``. | |||
52 |
|
74 | |||
53 | Running and Editing |
|
75 | Running and Editing | |
54 | ------------------- |
|
76 | ------------------- |
General Comments 0
You need to be logged in to leave comments.
Login now