##// END OF EJS Templates
Merge pull request #4297 from abhinav-upadhyay/unreachable-code...
Min RK -
r12827:7c6ccedb merge
parent child Browse files
Show More
@@ -1,647 +1,646 b''
1 """Implementation of basic magic functions.
1 """Implementation of basic magic functions.
2 """
2 """
3 #-----------------------------------------------------------------------------
3 #-----------------------------------------------------------------------------
4 # Copyright (c) 2012 The IPython Development Team.
4 # Copyright (c) 2012 The IPython Development Team.
5 #
5 #
6 # Distributed under the terms of the Modified BSD License.
6 # Distributed under the terms of the Modified BSD License.
7 #
7 #
8 # The full license is in the file COPYING.txt, distributed with this software.
8 # The full license is in the file COPYING.txt, distributed with this software.
9 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
10
10
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 # Imports
12 # Imports
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 from __future__ import print_function
14 from __future__ import print_function
15
15
16 # Stdlib
16 # Stdlib
17 import io
17 import io
18 import json
18 import json
19 import sys
19 import sys
20 from pprint import pformat
20 from pprint import pformat
21
21
22 # Our own packages
22 # Our own packages
23 from IPython.core import magic_arguments, page
23 from IPython.core import magic_arguments, page
24 from IPython.core.error import UsageError
24 from IPython.core.error import UsageError
25 from IPython.core.magic import Magics, magics_class, line_magic, magic_escapes
25 from IPython.core.magic import Magics, magics_class, line_magic, magic_escapes
26 from IPython.utils.text import format_screen, dedent, indent
26 from IPython.utils.text import format_screen, dedent, indent
27 from IPython.testing.skipdoctest import skip_doctest
27 from IPython.testing.skipdoctest import skip_doctest
28 from IPython.utils.ipstruct import Struct
28 from IPython.utils.ipstruct import Struct
29 from IPython.utils.path import unquote_filename
29 from IPython.utils.path import unquote_filename
30 from IPython.utils.warn import warn, error
30 from IPython.utils.warn import warn, error
31
31
32 #-----------------------------------------------------------------------------
32 #-----------------------------------------------------------------------------
33 # Magics class implementation
33 # Magics class implementation
34 #-----------------------------------------------------------------------------
34 #-----------------------------------------------------------------------------
35
35
36 class MagicsDisplay(object):
36 class MagicsDisplay(object):
37 def __init__(self, magics_manager):
37 def __init__(self, magics_manager):
38 self.magics_manager = magics_manager
38 self.magics_manager = magics_manager
39
39
40 def _lsmagic(self):
40 def _lsmagic(self):
41 """The main implementation of the %lsmagic"""
41 """The main implementation of the %lsmagic"""
42 mesc = magic_escapes['line']
42 mesc = magic_escapes['line']
43 cesc = magic_escapes['cell']
43 cesc = magic_escapes['cell']
44 mman = self.magics_manager
44 mman = self.magics_manager
45 magics = mman.lsmagic()
45 magics = mman.lsmagic()
46 out = ['Available line magics:',
46 out = ['Available line magics:',
47 mesc + (' '+mesc).join(sorted(magics['line'])),
47 mesc + (' '+mesc).join(sorted(magics['line'])),
48 '',
48 '',
49 'Available cell magics:',
49 'Available cell magics:',
50 cesc + (' '+cesc).join(sorted(magics['cell'])),
50 cesc + (' '+cesc).join(sorted(magics['cell'])),
51 '',
51 '',
52 mman.auto_status()]
52 mman.auto_status()]
53 return '\n'.join(out)
53 return '\n'.join(out)
54
54
55 def _repr_pretty_(self, p, cycle):
55 def _repr_pretty_(self, p, cycle):
56 p.text(self._lsmagic())
56 p.text(self._lsmagic())
57
57
58 def __str__(self):
58 def __str__(self):
59 return self._lsmagic()
59 return self._lsmagic()
60
60
61 def _jsonable(self):
61 def _jsonable(self):
62 """turn magics dict into jsonable dict of the same structure
62 """turn magics dict into jsonable dict of the same structure
63
63
64 replaces object instances with their class names as strings
64 replaces object instances with their class names as strings
65 """
65 """
66 magic_dict = {}
66 magic_dict = {}
67 mman = self.magics_manager
67 mman = self.magics_manager
68 magics = mman.lsmagic()
68 magics = mman.lsmagic()
69 for key, subdict in magics.items():
69 for key, subdict in magics.items():
70 d = {}
70 d = {}
71 magic_dict[key] = d
71 magic_dict[key] = d
72 for name, obj in subdict.items():
72 for name, obj in subdict.items():
73 try:
73 try:
74 classname = obj.im_class.__name__
74 classname = obj.im_class.__name__
75 except AttributeError:
75 except AttributeError:
76 classname = 'Other'
76 classname = 'Other'
77
77
78 d[name] = classname
78 d[name] = classname
79 return magic_dict
79 return magic_dict
80
80
81 def _repr_json_(self):
81 def _repr_json_(self):
82 return json.dumps(self._jsonable())
82 return json.dumps(self._jsonable())
83
83
84
84
85 @magics_class
85 @magics_class
86 class BasicMagics(Magics):
86 class BasicMagics(Magics):
87 """Magics that provide central IPython functionality.
87 """Magics that provide central IPython functionality.
88
88
89 These are various magics that don't fit into specific categories but that
89 These are various magics that don't fit into specific categories but that
90 are all part of the base 'IPython experience'."""
90 are all part of the base 'IPython experience'."""
91
91
92 @magic_arguments.magic_arguments()
92 @magic_arguments.magic_arguments()
93 @magic_arguments.argument(
93 @magic_arguments.argument(
94 '-l', '--line', action='store_true',
94 '-l', '--line', action='store_true',
95 help="""Create a line magic alias."""
95 help="""Create a line magic alias."""
96 )
96 )
97 @magic_arguments.argument(
97 @magic_arguments.argument(
98 '-c', '--cell', action='store_true',
98 '-c', '--cell', action='store_true',
99 help="""Create a cell magic alias."""
99 help="""Create a cell magic alias."""
100 )
100 )
101 @magic_arguments.argument(
101 @magic_arguments.argument(
102 'name',
102 'name',
103 help="""Name of the magic to be created."""
103 help="""Name of the magic to be created."""
104 )
104 )
105 @magic_arguments.argument(
105 @magic_arguments.argument(
106 'target',
106 'target',
107 help="""Name of the existing line or cell magic."""
107 help="""Name of the existing line or cell magic."""
108 )
108 )
109 @line_magic
109 @line_magic
110 def alias_magic(self, line=''):
110 def alias_magic(self, line=''):
111 """Create an alias for an existing line or cell magic.
111 """Create an alias for an existing line or cell magic.
112
112
113 Examples
113 Examples
114 --------
114 --------
115 ::
115 ::
116 In [1]: %alias_magic t timeit
116 In [1]: %alias_magic t timeit
117 Created `%t` as an alias for `%timeit`.
117 Created `%t` as an alias for `%timeit`.
118 Created `%%t` as an alias for `%%timeit`.
118 Created `%%t` as an alias for `%%timeit`.
119
119
120 In [2]: %t -n1 pass
120 In [2]: %t -n1 pass
121 1 loops, best of 3: 954 ns per loop
121 1 loops, best of 3: 954 ns per loop
122
122
123 In [3]: %%t -n1
123 In [3]: %%t -n1
124 ...: pass
124 ...: pass
125 ...:
125 ...:
126 1 loops, best of 3: 954 ns per loop
126 1 loops, best of 3: 954 ns per loop
127
127
128 In [4]: %alias_magic --cell whereami pwd
128 In [4]: %alias_magic --cell whereami pwd
129 UsageError: Cell magic function `%%pwd` not found.
129 UsageError: Cell magic function `%%pwd` not found.
130 In [5]: %alias_magic --line whereami pwd
130 In [5]: %alias_magic --line whereami pwd
131 Created `%whereami` as an alias for `%pwd`.
131 Created `%whereami` as an alias for `%pwd`.
132
132
133 In [6]: %whereami
133 In [6]: %whereami
134 Out[6]: u'/home/testuser'
134 Out[6]: u'/home/testuser'
135 """
135 """
136 args = magic_arguments.parse_argstring(self.alias_magic, line)
136 args = magic_arguments.parse_argstring(self.alias_magic, line)
137 shell = self.shell
137 shell = self.shell
138 mman = self.shell.magics_manager
138 mman = self.shell.magics_manager
139 escs = ''.join(magic_escapes.values())
139 escs = ''.join(magic_escapes.values())
140
140
141 target = args.target.lstrip(escs)
141 target = args.target.lstrip(escs)
142 name = args.name.lstrip(escs)
142 name = args.name.lstrip(escs)
143
143
144 # Find the requested magics.
144 # Find the requested magics.
145 m_line = shell.find_magic(target, 'line')
145 m_line = shell.find_magic(target, 'line')
146 m_cell = shell.find_magic(target, 'cell')
146 m_cell = shell.find_magic(target, 'cell')
147 if args.line and m_line is None:
147 if args.line and m_line is None:
148 raise UsageError('Line magic function `%s%s` not found.' %
148 raise UsageError('Line magic function `%s%s` not found.' %
149 (magic_escapes['line'], target))
149 (magic_escapes['line'], target))
150 if args.cell and m_cell is None:
150 if args.cell and m_cell is None:
151 raise UsageError('Cell magic function `%s%s` not found.' %
151 raise UsageError('Cell magic function `%s%s` not found.' %
152 (magic_escapes['cell'], target))
152 (magic_escapes['cell'], target))
153
153
154 # If --line and --cell are not specified, default to the ones
154 # If --line and --cell are not specified, default to the ones
155 # that are available.
155 # that are available.
156 if not args.line and not args.cell:
156 if not args.line and not args.cell:
157 if not m_line and not m_cell:
157 if not m_line and not m_cell:
158 raise UsageError(
158 raise UsageError(
159 'No line or cell magic with name `%s` found.' % target
159 'No line or cell magic with name `%s` found.' % target
160 )
160 )
161 args.line = bool(m_line)
161 args.line = bool(m_line)
162 args.cell = bool(m_cell)
162 args.cell = bool(m_cell)
163
163
164 if args.line:
164 if args.line:
165 mman.register_alias(name, target, 'line')
165 mman.register_alias(name, target, 'line')
166 print('Created `%s%s` as an alias for `%s%s`.' % (
166 print('Created `%s%s` as an alias for `%s%s`.' % (
167 magic_escapes['line'], name,
167 magic_escapes['line'], name,
168 magic_escapes['line'], target))
168 magic_escapes['line'], target))
169
169
170 if args.cell:
170 if args.cell:
171 mman.register_alias(name, target, 'cell')
171 mman.register_alias(name, target, 'cell')
172 print('Created `%s%s` as an alias for `%s%s`.' % (
172 print('Created `%s%s` as an alias for `%s%s`.' % (
173 magic_escapes['cell'], name,
173 magic_escapes['cell'], name,
174 magic_escapes['cell'], target))
174 magic_escapes['cell'], target))
175
175
176 @line_magic
176 @line_magic
177 def lsmagic(self, parameter_s=''):
177 def lsmagic(self, parameter_s=''):
178 """List currently available magic functions."""
178 """List currently available magic functions."""
179 return MagicsDisplay(self.shell.magics_manager)
179 return MagicsDisplay(self.shell.magics_manager)
180
180
181 def _magic_docs(self, brief=False, rest=False):
181 def _magic_docs(self, brief=False, rest=False):
182 """Return docstrings from magic functions."""
182 """Return docstrings from magic functions."""
183 mman = self.shell.magics_manager
183 mman = self.shell.magics_manager
184 docs = mman.lsmagic_docs(brief, missing='No documentation')
184 docs = mman.lsmagic_docs(brief, missing='No documentation')
185
185
186 if rest:
186 if rest:
187 format_string = '**%s%s**::\n\n%s\n\n'
187 format_string = '**%s%s**::\n\n%s\n\n'
188 else:
188 else:
189 format_string = '%s%s:\n%s\n'
189 format_string = '%s%s:\n%s\n'
190
190
191 return ''.join(
191 return ''.join(
192 [format_string % (magic_escapes['line'], fname,
192 [format_string % (magic_escapes['line'], fname,
193 indent(dedent(fndoc)))
193 indent(dedent(fndoc)))
194 for fname, fndoc in sorted(docs['line'].items())]
194 for fname, fndoc in sorted(docs['line'].items())]
195 +
195 +
196 [format_string % (magic_escapes['cell'], fname,
196 [format_string % (magic_escapes['cell'], fname,
197 indent(dedent(fndoc)))
197 indent(dedent(fndoc)))
198 for fname, fndoc in sorted(docs['cell'].items())]
198 for fname, fndoc in sorted(docs['cell'].items())]
199 )
199 )
200
200
201 @line_magic
201 @line_magic
202 def magic(self, parameter_s=''):
202 def magic(self, parameter_s=''):
203 """Print information about the magic function system.
203 """Print information about the magic function system.
204
204
205 Supported formats: -latex, -brief, -rest
205 Supported formats: -latex, -brief, -rest
206 """
206 """
207
207
208 mode = ''
208 mode = ''
209 try:
209 try:
210 mode = parameter_s.split()[0][1:]
210 mode = parameter_s.split()[0][1:]
211 if mode == 'rest':
211 if mode == 'rest':
212 rest_docs = []
212 rest_docs = []
213 except IndexError:
213 except IndexError:
214 pass
214 pass
215
215
216 brief = (mode == 'brief')
216 brief = (mode == 'brief')
217 rest = (mode == 'rest')
217 rest = (mode == 'rest')
218 magic_docs = self._magic_docs(brief, rest)
218 magic_docs = self._magic_docs(brief, rest)
219
219
220 if mode == 'latex':
220 if mode == 'latex':
221 print(self.format_latex(magic_docs))
221 print(self.format_latex(magic_docs))
222 return
222 return
223 else:
223 else:
224 magic_docs = format_screen(magic_docs)
224 magic_docs = format_screen(magic_docs)
225
225
226 out = ["""
226 out = ["""
227 IPython's 'magic' functions
227 IPython's 'magic' functions
228 ===========================
228 ===========================
229
229
230 The magic function system provides a series of functions which allow you to
230 The magic function system provides a series of functions which allow you to
231 control the behavior of IPython itself, plus a lot of system-type
231 control the behavior of IPython itself, plus a lot of system-type
232 features. There are two kinds of magics, line-oriented and cell-oriented.
232 features. There are two kinds of magics, line-oriented and cell-oriented.
233
233
234 Line magics are prefixed with the % character and work much like OS
234 Line magics are prefixed with the % character and work much like OS
235 command-line calls: they get as an argument the rest of the line, where
235 command-line calls: they get as an argument the rest of the line, where
236 arguments are passed without parentheses or quotes. For example, this will
236 arguments are passed without parentheses or quotes. For example, this will
237 time the given statement::
237 time the given statement::
238
238
239 %timeit range(1000)
239 %timeit range(1000)
240
240
241 Cell magics are prefixed with a double %%, and they are functions that get as
241 Cell magics are prefixed with a double %%, and they are functions that get as
242 an argument not only the rest of the line, but also the lines below it in a
242 an argument not only the rest of the line, but also the lines below it in a
243 separate argument. These magics are called with two arguments: the rest of the
243 separate argument. These magics are called with two arguments: the rest of the
244 call line and the body of the cell, consisting of the lines below the first.
244 call line and the body of the cell, consisting of the lines below the first.
245 For example::
245 For example::
246
246
247 %%timeit x = numpy.random.randn((100, 100))
247 %%timeit x = numpy.random.randn((100, 100))
248 numpy.linalg.svd(x)
248 numpy.linalg.svd(x)
249
249
250 will time the execution of the numpy svd routine, running the assignment of x
250 will time the execution of the numpy svd routine, running the assignment of x
251 as part of the setup phase, which is not timed.
251 as part of the setup phase, which is not timed.
252
252
253 In a line-oriented client (the terminal or Qt console IPython), starting a new
253 In a line-oriented client (the terminal or Qt console IPython), starting a new
254 input with %% will automatically enter cell mode, and IPython will continue
254 input with %% will automatically enter cell mode, and IPython will continue
255 reading input until a blank line is given. In the notebook, simply type the
255 reading input until a blank line is given. In the notebook, simply type the
256 whole cell as one entity, but keep in mind that the %% escape can only be at
256 whole cell as one entity, but keep in mind that the %% escape can only be at
257 the very start of the cell.
257 the very start of the cell.
258
258
259 NOTE: If you have 'automagic' enabled (via the command line option or with the
259 NOTE: If you have 'automagic' enabled (via the command line option or with the
260 %automagic function), you don't need to type in the % explicitly for line
260 %automagic function), you don't need to type in the % explicitly for line
261 magics; cell magics always require an explicit '%%' escape. By default,
261 magics; cell magics always require an explicit '%%' escape. By default,
262 IPython ships with automagic on, so you should only rarely need the % escape.
262 IPython ships with automagic on, so you should only rarely need the % escape.
263
263
264 Example: typing '%cd mydir' (without the quotes) changes you working directory
264 Example: typing '%cd mydir' (without the quotes) changes you working directory
265 to 'mydir', if it exists.
265 to 'mydir', if it exists.
266
266
267 For a list of the available magic functions, use %lsmagic. For a description
267 For a list of the available magic functions, use %lsmagic. For a description
268 of any of them, type %magic_name?, e.g. '%cd?'.
268 of any of them, type %magic_name?, e.g. '%cd?'.
269
269
270 Currently the magic system has the following functions:""",
270 Currently the magic system has the following functions:""",
271 magic_docs,
271 magic_docs,
272 "Summary of magic functions (from %slsmagic):" % magic_escapes['line'],
272 "Summary of magic functions (from %slsmagic):" % magic_escapes['line'],
273 str(self.lsmagic()),
273 str(self.lsmagic()),
274 ]
274 ]
275 page.page('\n'.join(out))
275 page.page('\n'.join(out))
276
276
277
277
278 @line_magic
278 @line_magic
279 def page(self, parameter_s=''):
279 def page(self, parameter_s=''):
280 """Pretty print the object and display it through a pager.
280 """Pretty print the object and display it through a pager.
281
281
282 %page [options] OBJECT
282 %page [options] OBJECT
283
283
284 If no object is given, use _ (last output).
284 If no object is given, use _ (last output).
285
285
286 Options:
286 Options:
287
287
288 -r: page str(object), don't pretty-print it."""
288 -r: page str(object), don't pretty-print it."""
289
289
290 # After a function contributed by Olivier Aubert, slightly modified.
290 # After a function contributed by Olivier Aubert, slightly modified.
291
291
292 # Process options/args
292 # Process options/args
293 opts, args = self.parse_options(parameter_s, 'r')
293 opts, args = self.parse_options(parameter_s, 'r')
294 raw = 'r' in opts
294 raw = 'r' in opts
295
295
296 oname = args and args or '_'
296 oname = args and args or '_'
297 info = self.shell._ofind(oname)
297 info = self.shell._ofind(oname)
298 if info['found']:
298 if info['found']:
299 txt = (raw and str or pformat)( info['obj'] )
299 txt = (raw and str or pformat)( info['obj'] )
300 page.page(txt)
300 page.page(txt)
301 else:
301 else:
302 print('Object `%s` not found' % oname)
302 print('Object `%s` not found' % oname)
303
303
304 @line_magic
304 @line_magic
305 def profile(self, parameter_s=''):
305 def profile(self, parameter_s=''):
306 """Print your currently active IPython profile."""
306 """Print your currently active IPython profile."""
307 from IPython.core.application import BaseIPythonApplication
307 from IPython.core.application import BaseIPythonApplication
308 if BaseIPythonApplication.initialized():
308 if BaseIPythonApplication.initialized():
309 print(BaseIPythonApplication.instance().profile)
309 print(BaseIPythonApplication.instance().profile)
310 else:
310 else:
311 error("profile is an application-level value, but you don't appear to be in an IPython application")
311 error("profile is an application-level value, but you don't appear to be in an IPython application")
312
312
313 @line_magic
313 @line_magic
314 def pprint(self, parameter_s=''):
314 def pprint(self, parameter_s=''):
315 """Toggle pretty printing on/off."""
315 """Toggle pretty printing on/off."""
316 ptformatter = self.shell.display_formatter.formatters['text/plain']
316 ptformatter = self.shell.display_formatter.formatters['text/plain']
317 ptformatter.pprint = bool(1 - ptformatter.pprint)
317 ptformatter.pprint = bool(1 - ptformatter.pprint)
318 print('Pretty printing has been turned',
318 print('Pretty printing has been turned',
319 ['OFF','ON'][ptformatter.pprint])
319 ['OFF','ON'][ptformatter.pprint])
320
320
321 @line_magic
321 @line_magic
322 def colors(self, parameter_s=''):
322 def colors(self, parameter_s=''):
323 """Switch color scheme for prompts, info system and exception handlers.
323 """Switch color scheme for prompts, info system and exception handlers.
324
324
325 Currently implemented schemes: NoColor, Linux, LightBG.
325 Currently implemented schemes: NoColor, Linux, LightBG.
326
326
327 Color scheme names are not case-sensitive.
327 Color scheme names are not case-sensitive.
328
328
329 Examples
329 Examples
330 --------
330 --------
331 To get a plain black and white terminal::
331 To get a plain black and white terminal::
332
332
333 %colors nocolor
333 %colors nocolor
334 """
334 """
335 def color_switch_err(name):
335 def color_switch_err(name):
336 warn('Error changing %s color schemes.\n%s' %
336 warn('Error changing %s color schemes.\n%s' %
337 (name, sys.exc_info()[1]))
337 (name, sys.exc_info()[1]))
338
338
339
339
340 new_scheme = parameter_s.strip()
340 new_scheme = parameter_s.strip()
341 if not new_scheme:
341 if not new_scheme:
342 raise UsageError(
342 raise UsageError(
343 "%colors: you must specify a color scheme. See '%colors?'")
343 "%colors: you must specify a color scheme. See '%colors?'")
344 return
345 # local shortcut
344 # local shortcut
346 shell = self.shell
345 shell = self.shell
347
346
348 import IPython.utils.rlineimpl as readline
347 import IPython.utils.rlineimpl as readline
349
348
350 if not shell.colors_force and \
349 if not shell.colors_force and \
351 not readline.have_readline and \
350 not readline.have_readline and \
352 (sys.platform == "win32" or sys.platform == "cli"):
351 (sys.platform == "win32" or sys.platform == "cli"):
353 msg = """\
352 msg = """\
354 Proper color support under MS Windows requires the pyreadline library.
353 Proper color support under MS Windows requires the pyreadline library.
355 You can find it at:
354 You can find it at:
356 http://ipython.org/pyreadline.html
355 http://ipython.org/pyreadline.html
357 Gary's readline needs the ctypes module, from:
356 Gary's readline needs the ctypes module, from:
358 http://starship.python.net/crew/theller/ctypes
357 http://starship.python.net/crew/theller/ctypes
359 (Note that ctypes is already part of Python versions 2.5 and newer).
358 (Note that ctypes is already part of Python versions 2.5 and newer).
360
359
361 Defaulting color scheme to 'NoColor'"""
360 Defaulting color scheme to 'NoColor'"""
362 new_scheme = 'NoColor'
361 new_scheme = 'NoColor'
363 warn(msg)
362 warn(msg)
364
363
365 # readline option is 0
364 # readline option is 0
366 if not shell.colors_force and not shell.has_readline:
365 if not shell.colors_force and not shell.has_readline:
367 new_scheme = 'NoColor'
366 new_scheme = 'NoColor'
368
367
369 # Set prompt colors
368 # Set prompt colors
370 try:
369 try:
371 shell.prompt_manager.color_scheme = new_scheme
370 shell.prompt_manager.color_scheme = new_scheme
372 except:
371 except:
373 color_switch_err('prompt')
372 color_switch_err('prompt')
374 else:
373 else:
375 shell.colors = \
374 shell.colors = \
376 shell.prompt_manager.color_scheme_table.active_scheme_name
375 shell.prompt_manager.color_scheme_table.active_scheme_name
377 # Set exception colors
376 # Set exception colors
378 try:
377 try:
379 shell.InteractiveTB.set_colors(scheme = new_scheme)
378 shell.InteractiveTB.set_colors(scheme = new_scheme)
380 shell.SyntaxTB.set_colors(scheme = new_scheme)
379 shell.SyntaxTB.set_colors(scheme = new_scheme)
381 except:
380 except:
382 color_switch_err('exception')
381 color_switch_err('exception')
383
382
384 # Set info (for 'object?') colors
383 # Set info (for 'object?') colors
385 if shell.color_info:
384 if shell.color_info:
386 try:
385 try:
387 shell.inspector.set_active_scheme(new_scheme)
386 shell.inspector.set_active_scheme(new_scheme)
388 except:
387 except:
389 color_switch_err('object inspector')
388 color_switch_err('object inspector')
390 else:
389 else:
391 shell.inspector.set_active_scheme('NoColor')
390 shell.inspector.set_active_scheme('NoColor')
392
391
393 @line_magic
392 @line_magic
394 def xmode(self, parameter_s=''):
393 def xmode(self, parameter_s=''):
395 """Switch modes for the exception handlers.
394 """Switch modes for the exception handlers.
396
395
397 Valid modes: Plain, Context and Verbose.
396 Valid modes: Plain, Context and Verbose.
398
397
399 If called without arguments, acts as a toggle."""
398 If called without arguments, acts as a toggle."""
400
399
401 def xmode_switch_err(name):
400 def xmode_switch_err(name):
402 warn('Error changing %s exception modes.\n%s' %
401 warn('Error changing %s exception modes.\n%s' %
403 (name,sys.exc_info()[1]))
402 (name,sys.exc_info()[1]))
404
403
405 shell = self.shell
404 shell = self.shell
406 new_mode = parameter_s.strip().capitalize()
405 new_mode = parameter_s.strip().capitalize()
407 try:
406 try:
408 shell.InteractiveTB.set_mode(mode=new_mode)
407 shell.InteractiveTB.set_mode(mode=new_mode)
409 print('Exception reporting mode:',shell.InteractiveTB.mode)
408 print('Exception reporting mode:',shell.InteractiveTB.mode)
410 except:
409 except:
411 xmode_switch_err('user')
410 xmode_switch_err('user')
412
411
413 @line_magic
412 @line_magic
414 def quickref(self,arg):
413 def quickref(self,arg):
415 """ Show a quick reference sheet """
414 """ Show a quick reference sheet """
416 from IPython.core.usage import quick_reference
415 from IPython.core.usage import quick_reference
417 qr = quick_reference + self._magic_docs(brief=True)
416 qr = quick_reference + self._magic_docs(brief=True)
418 page.page(qr)
417 page.page(qr)
419
418
420 @line_magic
419 @line_magic
421 def doctest_mode(self, parameter_s=''):
420 def doctest_mode(self, parameter_s=''):
422 """Toggle doctest mode on and off.
421 """Toggle doctest mode on and off.
423
422
424 This mode is intended to make IPython behave as much as possible like a
423 This mode is intended to make IPython behave as much as possible like a
425 plain Python shell, from the perspective of how its prompts, exceptions
424 plain Python shell, from the perspective of how its prompts, exceptions
426 and output look. This makes it easy to copy and paste parts of a
425 and output look. This makes it easy to copy and paste parts of a
427 session into doctests. It does so by:
426 session into doctests. It does so by:
428
427
429 - Changing the prompts to the classic ``>>>`` ones.
428 - Changing the prompts to the classic ``>>>`` ones.
430 - Changing the exception reporting mode to 'Plain'.
429 - Changing the exception reporting mode to 'Plain'.
431 - Disabling pretty-printing of output.
430 - Disabling pretty-printing of output.
432
431
433 Note that IPython also supports the pasting of code snippets that have
432 Note that IPython also supports the pasting of code snippets that have
434 leading '>>>' and '...' prompts in them. This means that you can paste
433 leading '>>>' and '...' prompts in them. This means that you can paste
435 doctests from files or docstrings (even if they have leading
434 doctests from files or docstrings (even if they have leading
436 whitespace), and the code will execute correctly. You can then use
435 whitespace), and the code will execute correctly. You can then use
437 '%history -t' to see the translated history; this will give you the
436 '%history -t' to see the translated history; this will give you the
438 input after removal of all the leading prompts and whitespace, which
437 input after removal of all the leading prompts and whitespace, which
439 can be pasted back into an editor.
438 can be pasted back into an editor.
440
439
441 With these features, you can switch into this mode easily whenever you
440 With these features, you can switch into this mode easily whenever you
442 need to do testing and changes to doctests, without having to leave
441 need to do testing and changes to doctests, without having to leave
443 your existing IPython session.
442 your existing IPython session.
444 """
443 """
445
444
446 # Shorthands
445 # Shorthands
447 shell = self.shell
446 shell = self.shell
448 pm = shell.prompt_manager
447 pm = shell.prompt_manager
449 meta = shell.meta
448 meta = shell.meta
450 disp_formatter = self.shell.display_formatter
449 disp_formatter = self.shell.display_formatter
451 ptformatter = disp_formatter.formatters['text/plain']
450 ptformatter = disp_formatter.formatters['text/plain']
452 # dstore is a data store kept in the instance metadata bag to track any
451 # dstore is a data store kept in the instance metadata bag to track any
453 # changes we make, so we can undo them later.
452 # changes we make, so we can undo them later.
454 dstore = meta.setdefault('doctest_mode',Struct())
453 dstore = meta.setdefault('doctest_mode',Struct())
455 save_dstore = dstore.setdefault
454 save_dstore = dstore.setdefault
456
455
457 # save a few values we'll need to recover later
456 # save a few values we'll need to recover later
458 mode = save_dstore('mode',False)
457 mode = save_dstore('mode',False)
459 save_dstore('rc_pprint',ptformatter.pprint)
458 save_dstore('rc_pprint',ptformatter.pprint)
460 save_dstore('xmode',shell.InteractiveTB.mode)
459 save_dstore('xmode',shell.InteractiveTB.mode)
461 save_dstore('rc_separate_out',shell.separate_out)
460 save_dstore('rc_separate_out',shell.separate_out)
462 save_dstore('rc_separate_out2',shell.separate_out2)
461 save_dstore('rc_separate_out2',shell.separate_out2)
463 save_dstore('rc_prompts_pad_left',pm.justify)
462 save_dstore('rc_prompts_pad_left',pm.justify)
464 save_dstore('rc_separate_in',shell.separate_in)
463 save_dstore('rc_separate_in',shell.separate_in)
465 save_dstore('rc_active_types',disp_formatter.active_types)
464 save_dstore('rc_active_types',disp_formatter.active_types)
466 save_dstore('prompt_templates',(pm.in_template, pm.in2_template, pm.out_template))
465 save_dstore('prompt_templates',(pm.in_template, pm.in2_template, pm.out_template))
467
466
468 if mode == False:
467 if mode == False:
469 # turn on
468 # turn on
470 pm.in_template = '>>> '
469 pm.in_template = '>>> '
471 pm.in2_template = '... '
470 pm.in2_template = '... '
472 pm.out_template = ''
471 pm.out_template = ''
473
472
474 # Prompt separators like plain python
473 # Prompt separators like plain python
475 shell.separate_in = ''
474 shell.separate_in = ''
476 shell.separate_out = ''
475 shell.separate_out = ''
477 shell.separate_out2 = ''
476 shell.separate_out2 = ''
478
477
479 pm.justify = False
478 pm.justify = False
480
479
481 ptformatter.pprint = False
480 ptformatter.pprint = False
482 disp_formatter.active_types = ['text/plain']
481 disp_formatter.active_types = ['text/plain']
483
482
484 shell.magic('xmode Plain')
483 shell.magic('xmode Plain')
485 else:
484 else:
486 # turn off
485 # turn off
487 pm.in_template, pm.in2_template, pm.out_template = dstore.prompt_templates
486 pm.in_template, pm.in2_template, pm.out_template = dstore.prompt_templates
488
487
489 shell.separate_in = dstore.rc_separate_in
488 shell.separate_in = dstore.rc_separate_in
490
489
491 shell.separate_out = dstore.rc_separate_out
490 shell.separate_out = dstore.rc_separate_out
492 shell.separate_out2 = dstore.rc_separate_out2
491 shell.separate_out2 = dstore.rc_separate_out2
493
492
494 pm.justify = dstore.rc_prompts_pad_left
493 pm.justify = dstore.rc_prompts_pad_left
495
494
496 ptformatter.pprint = dstore.rc_pprint
495 ptformatter.pprint = dstore.rc_pprint
497 disp_formatter.active_types = dstore.rc_active_types
496 disp_formatter.active_types = dstore.rc_active_types
498
497
499 shell.magic('xmode ' + dstore.xmode)
498 shell.magic('xmode ' + dstore.xmode)
500
499
501 # Store new mode and inform
500 # Store new mode and inform
502 dstore.mode = bool(1-int(mode))
501 dstore.mode = bool(1-int(mode))
503 mode_label = ['OFF','ON'][dstore.mode]
502 mode_label = ['OFF','ON'][dstore.mode]
504 print('Doctest mode is:', mode_label)
503 print('Doctest mode is:', mode_label)
505
504
506 @line_magic
505 @line_magic
507 def gui(self, parameter_s=''):
506 def gui(self, parameter_s=''):
508 """Enable or disable IPython GUI event loop integration.
507 """Enable or disable IPython GUI event loop integration.
509
508
510 %gui [GUINAME]
509 %gui [GUINAME]
511
510
512 This magic replaces IPython's threaded shells that were activated
511 This magic replaces IPython's threaded shells that were activated
513 using the (pylab/wthread/etc.) command line flags. GUI toolkits
512 using the (pylab/wthread/etc.) command line flags. GUI toolkits
514 can now be enabled at runtime and keyboard
513 can now be enabled at runtime and keyboard
515 interrupts should work without any problems. The following toolkits
514 interrupts should work without any problems. The following toolkits
516 are supported: wxPython, PyQt4, PyGTK, Tk and Cocoa (OSX)::
515 are supported: wxPython, PyQt4, PyGTK, Tk and Cocoa (OSX)::
517
516
518 %gui wx # enable wxPython event loop integration
517 %gui wx # enable wxPython event loop integration
519 %gui qt4|qt # enable PyQt4 event loop integration
518 %gui qt4|qt # enable PyQt4 event loop integration
520 %gui gtk # enable PyGTK event loop integration
519 %gui gtk # enable PyGTK event loop integration
521 %gui gtk3 # enable Gtk3 event loop integration
520 %gui gtk3 # enable Gtk3 event loop integration
522 %gui tk # enable Tk event loop integration
521 %gui tk # enable Tk event loop integration
523 %gui osx # enable Cocoa event loop integration
522 %gui osx # enable Cocoa event loop integration
524 # (requires %matplotlib 1.1)
523 # (requires %matplotlib 1.1)
525 %gui # disable all event loop integration
524 %gui # disable all event loop integration
526
525
527 WARNING: after any of these has been called you can simply create
526 WARNING: after any of these has been called you can simply create
528 an application object, but DO NOT start the event loop yourself, as
527 an application object, but DO NOT start the event loop yourself, as
529 we have already handled that.
528 we have already handled that.
530 """
529 """
531 opts, arg = self.parse_options(parameter_s, '')
530 opts, arg = self.parse_options(parameter_s, '')
532 if arg=='': arg = None
531 if arg=='': arg = None
533 try:
532 try:
534 return self.shell.enable_gui(arg)
533 return self.shell.enable_gui(arg)
535 except Exception as e:
534 except Exception as e:
536 # print simple error message, rather than traceback if we can't
535 # print simple error message, rather than traceback if we can't
537 # hook up the GUI
536 # hook up the GUI
538 error(str(e))
537 error(str(e))
539
538
540 @skip_doctest
539 @skip_doctest
541 @line_magic
540 @line_magic
542 def precision(self, s=''):
541 def precision(self, s=''):
543 """Set floating point precision for pretty printing.
542 """Set floating point precision for pretty printing.
544
543
545 Can set either integer precision or a format string.
544 Can set either integer precision or a format string.
546
545
547 If numpy has been imported and precision is an int,
546 If numpy has been imported and precision is an int,
548 numpy display precision will also be set, via ``numpy.set_printoptions``.
547 numpy display precision will also be set, via ``numpy.set_printoptions``.
549
548
550 If no argument is given, defaults will be restored.
549 If no argument is given, defaults will be restored.
551
550
552 Examples
551 Examples
553 --------
552 --------
554 ::
553 ::
555
554
556 In [1]: from math import pi
555 In [1]: from math import pi
557
556
558 In [2]: %precision 3
557 In [2]: %precision 3
559 Out[2]: u'%.3f'
558 Out[2]: u'%.3f'
560
559
561 In [3]: pi
560 In [3]: pi
562 Out[3]: 3.142
561 Out[3]: 3.142
563
562
564 In [4]: %precision %i
563 In [4]: %precision %i
565 Out[4]: u'%i'
564 Out[4]: u'%i'
566
565
567 In [5]: pi
566 In [5]: pi
568 Out[5]: 3
567 Out[5]: 3
569
568
570 In [6]: %precision %e
569 In [6]: %precision %e
571 Out[6]: u'%e'
570 Out[6]: u'%e'
572
571
573 In [7]: pi**10
572 In [7]: pi**10
574 Out[7]: 9.364805e+04
573 Out[7]: 9.364805e+04
575
574
576 In [8]: %precision
575 In [8]: %precision
577 Out[8]: u'%r'
576 Out[8]: u'%r'
578
577
579 In [9]: pi**10
578 In [9]: pi**10
580 Out[9]: 93648.047476082982
579 Out[9]: 93648.047476082982
581 """
580 """
582 ptformatter = self.shell.display_formatter.formatters['text/plain']
581 ptformatter = self.shell.display_formatter.formatters['text/plain']
583 ptformatter.float_precision = s
582 ptformatter.float_precision = s
584 return ptformatter.float_format
583 return ptformatter.float_format
585
584
586 @magic_arguments.magic_arguments()
585 @magic_arguments.magic_arguments()
587 @magic_arguments.argument(
586 @magic_arguments.argument(
588 '-e', '--export', action='store_true', default=False,
587 '-e', '--export', action='store_true', default=False,
589 help='Export IPython history as a notebook. The filename argument '
588 help='Export IPython history as a notebook. The filename argument '
590 'is used to specify the notebook name and format. For example '
589 'is used to specify the notebook name and format. For example '
591 'a filename of notebook.ipynb will result in a notebook name '
590 'a filename of notebook.ipynb will result in a notebook name '
592 'of "notebook" and a format of "json". Likewise using a ".py" '
591 'of "notebook" and a format of "json". Likewise using a ".py" '
593 'file extension will write the notebook as a Python script'
592 'file extension will write the notebook as a Python script'
594 )
593 )
595 @magic_arguments.argument(
594 @magic_arguments.argument(
596 '-f', '--format',
595 '-f', '--format',
597 help='Convert an existing IPython notebook to a new format. This option '
596 help='Convert an existing IPython notebook to a new format. This option '
598 'specifies the new format and can have the values: json, py. '
597 'specifies the new format and can have the values: json, py. '
599 'The target filename is chosen automatically based on the new '
598 'The target filename is chosen automatically based on the new '
600 'format. The filename argument gives the name of the source file.'
599 'format. The filename argument gives the name of the source file.'
601 )
600 )
602 @magic_arguments.argument(
601 @magic_arguments.argument(
603 'filename', type=unicode,
602 'filename', type=unicode,
604 help='Notebook name or filename'
603 help='Notebook name or filename'
605 )
604 )
606 @line_magic
605 @line_magic
607 def notebook(self, s):
606 def notebook(self, s):
608 """Export and convert IPython notebooks.
607 """Export and convert IPython notebooks.
609
608
610 This function can export the current IPython history to a notebook file
609 This function can export the current IPython history to a notebook file
611 or can convert an existing notebook file into a different format. For
610 or can convert an existing notebook file into a different format. For
612 example, to export the history to "foo.ipynb" do "%notebook -e foo.ipynb".
611 example, to export the history to "foo.ipynb" do "%notebook -e foo.ipynb".
613 To export the history to "foo.py" do "%notebook -e foo.py". To convert
612 To export the history to "foo.py" do "%notebook -e foo.py". To convert
614 "foo.ipynb" to "foo.json" do "%notebook -f json foo.ipynb". Possible
613 "foo.ipynb" to "foo.json" do "%notebook -f json foo.ipynb". Possible
615 formats include (json/ipynb, py).
614 formats include (json/ipynb, py).
616 """
615 """
617 args = magic_arguments.parse_argstring(self.notebook, s)
616 args = magic_arguments.parse_argstring(self.notebook, s)
618
617
619 from IPython.nbformat import current
618 from IPython.nbformat import current
620 args.filename = unquote_filename(args.filename)
619 args.filename = unquote_filename(args.filename)
621 if args.export:
620 if args.export:
622 fname, name, format = current.parse_filename(args.filename)
621 fname, name, format = current.parse_filename(args.filename)
623 cells = []
622 cells = []
624 hist = list(self.shell.history_manager.get_range())
623 hist = list(self.shell.history_manager.get_range())
625 for session, prompt_number, input in hist[:-1]:
624 for session, prompt_number, input in hist[:-1]:
626 cells.append(current.new_code_cell(prompt_number=prompt_number,
625 cells.append(current.new_code_cell(prompt_number=prompt_number,
627 input=input))
626 input=input))
628 worksheet = current.new_worksheet(cells=cells)
627 worksheet = current.new_worksheet(cells=cells)
629 nb = current.new_notebook(name=name,worksheets=[worksheet])
628 nb = current.new_notebook(name=name,worksheets=[worksheet])
630 with io.open(fname, 'w', encoding='utf-8') as f:
629 with io.open(fname, 'w', encoding='utf-8') as f:
631 current.write(nb, f, format);
630 current.write(nb, f, format);
632 elif args.format is not None:
631 elif args.format is not None:
633 old_fname, old_name, old_format = current.parse_filename(args.filename)
632 old_fname, old_name, old_format = current.parse_filename(args.filename)
634 new_format = args.format
633 new_format = args.format
635 if new_format == u'xml':
634 if new_format == u'xml':
636 raise ValueError('Notebooks cannot be written as xml.')
635 raise ValueError('Notebooks cannot be written as xml.')
637 elif new_format == u'ipynb' or new_format == u'json':
636 elif new_format == u'ipynb' or new_format == u'json':
638 new_fname = old_name + u'.ipynb'
637 new_fname = old_name + u'.ipynb'
639 new_format = u'json'
638 new_format = u'json'
640 elif new_format == u'py':
639 elif new_format == u'py':
641 new_fname = old_name + u'.py'
640 new_fname = old_name + u'.py'
642 else:
641 else:
643 raise ValueError('Invalid notebook format: %s' % new_format)
642 raise ValueError('Invalid notebook format: %s' % new_format)
644 with io.open(old_fname, 'r', encoding='utf-8') as f:
643 with io.open(old_fname, 'r', encoding='utf-8') as f:
645 nb = current.read(f, old_format)
644 nb = current.read(f, old_format)
646 with io.open(new_fname, 'w', encoding='utf-8') as f:
645 with io.open(new_fname, 'w', encoding='utf-8') as f:
647 current.write(nb, f, new_format)
646 current.write(nb, f, new_format)
General Comments 0
You need to be logged in to leave comments. Login now