##// END OF EJS Templates
Tweaks to RST formatting.
Thomas Kluyver -
Show More
@@ -1,588 +1,587 b''
1 1 """ History related magics and functionality """
2 2 #-----------------------------------------------------------------------------
3 3 # Copyright (C) 2010 The IPython Development Team.
4 4 #
5 5 # Distributed under the terms of the BSD License.
6 6 #
7 7 # The full license is in the file COPYING.txt, distributed with this software.
8 8 #-----------------------------------------------------------------------------
9 9
10 10 #-----------------------------------------------------------------------------
11 11 # Imports
12 12 #-----------------------------------------------------------------------------
13 13 from __future__ import print_function
14 14
15 15 # Stdlib imports
16 16 import atexit
17 17 import fnmatch
18 18 import json
19 19 import os
20 20 import sys
21 21 import threading
22 22 import time
23 23
24 24 # Our own packages
25 25 import IPython.utils.io
26 26
27 27 from IPython.utils.pickleshare import PickleShareDB
28 28 from IPython.utils.io import ask_yes_no
29 29 from IPython.utils.warn import warn
30 30
31 31 #-----------------------------------------------------------------------------
32 32 # Classes and functions
33 33 #-----------------------------------------------------------------------------
34 34
35 35 class HistoryManager(object):
36 36 """A class to organize all history-related functionality in one place.
37 37 """
38 38 # Public interface
39 39
40 40 # An instance of the IPython shell we are attached to
41 41 shell = None
42 42 # A list to hold processed history
43 43 input_hist_parsed = None
44 44 # A list to hold raw history (as typed by user)
45 45 input_hist_raw = None
46 46 # A list of directories visited during session
47 47 dir_hist = None
48 48 # A dict of output history, keyed with ints from the shell's execution count
49 49 output_hist = None
50 50 # String with path to the history file
51 51 hist_file = None
52 52 # PickleShareDB instance holding the raw data for the shadow history
53 53 shadow_db = None
54 54 # ShadowHist instance with the actual shadow history
55 55 shadow_hist = None
56 56
57 57 # Private interface
58 58 # Variables used to store the three last inputs from the user. On each new
59 59 # history update, we populate the user's namespace with these, shifted as
60 60 # necessary.
61 61 _i00, _i, _ii, _iii = '','','',''
62 62
63 63 # A set with all forms of the exit command, so that we don't store them in
64 64 # the history (it's annoying to rewind the first entry and land on an exit
65 65 # call).
66 66 _exit_commands = None
67 67
68 68 def __init__(self, shell):
69 69 """Create a new history manager associated with a shell instance.
70 70 """
71 71 # We need a pointer back to the shell for various tasks.
72 72 self.shell = shell
73 73
74 74 # List of input with multi-line handling.
75 75 self.input_hist_parsed = []
76 76 # This one will hold the 'raw' input history, without any
77 77 # pre-processing. This will allow users to retrieve the input just as
78 78 # it was exactly typed in by the user, with %hist -r.
79 79 self.input_hist_raw = []
80 80
81 81 # list of visited directories
82 82 try:
83 83 self.dir_hist = [os.getcwd()]
84 84 except OSError:
85 85 self.dir_hist = []
86 86
87 87 # dict of output history
88 88 self.output_hist = {}
89 89
90 90 # Now the history file
91 91 if shell.profile:
92 92 histfname = 'history-%s' % shell.profile
93 93 else:
94 94 histfname = 'history'
95 95 self.hist_file = os.path.join(shell.ipython_dir, histfname + '.json')
96 96
97 97 # Objects related to shadow history management
98 98 self._init_shadow_hist()
99 99
100 100 self._i00, self._i, self._ii, self._iii = '','','',''
101 101
102 102 self._exit_commands = set(['Quit', 'quit', 'Exit', 'exit', '%Quit',
103 103 '%quit', '%Exit', '%exit'])
104 104
105 105 # Object is fully initialized, we can now call methods on it.
106 106
107 107 # Fill the history zero entry, user counter starts at 1
108 108 self.store_inputs('\n', '\n')
109 109
110 110 # Create and start the autosaver.
111 111 self.autosave_flag = threading.Event()
112 112 self.autosave_timer = HistorySaveThread(self.autosave_flag, 60)
113 113 self.autosave_timer.start()
114 114 # Register the autosave handler to be triggered as a post execute
115 115 # callback.
116 116 self.shell.register_post_execute(self.autosave_if_due)
117 117
118 118 def _init_shadow_hist(self):
119 119 try:
120 120 self.shadow_db = PickleShareDB(os.path.join(
121 121 self.shell.ipython_dir, 'db'))
122 122 except UnicodeDecodeError:
123 123 print("Your ipython_dir can't be decoded to unicode!")
124 124 print("Please set HOME environment variable to something that")
125 125 print(r"only has ASCII characters, e.g. c:\home")
126 126 print("Now it is", self.ipython_dir)
127 127 sys.exit()
128 128 self.shadow_hist = ShadowHist(self.shadow_db, self.shell)
129 129
130 130 def populate_readline_history(self):
131 131 """Populate the readline history from the raw history.
132 132
133 133 We only store one copy of the raw history, which is persisted to a json
134 134 file on disk. The readline history is repopulated from the contents of
135 135 this file."""
136 136
137 137 try:
138 138 self.shell.readline.clear_history()
139 139 except AttributeError:
140 140 pass
141 141 else:
142 142 for h in self.input_hist_raw:
143 143 if not h.isspace():
144 144 for line in h.splitlines():
145 145 self.shell.readline.add_history(line)
146 146
147 147 def save_history(self):
148 148 """Save input history to a file (via readline library)."""
149 149 hist = dict(raw=self.input_hist_raw, #[-self.shell.history_length:],
150 150 parsed=self.input_hist_parsed) #[-self.shell.history_length:])
151 151 with open(self.hist_file,'wt') as hfile:
152 152 json.dump(hist, hfile,
153 153 sort_keys=True, indent=4)
154 154
155 155 def autosave_if_due(self):
156 156 """Check if the autosave event is set; if so, save history. We do it
157 157 this way so that the save takes place in the main thread."""
158 158 if self.autosave_flag.is_set():
159 159 self.save_history()
160 160 self.autosave_flag.clear()
161 161
162 162 def reload_history(self):
163 163 """Reload the input history from disk file."""
164 164
165 165 with open(self.hist_file,'rt') as hfile:
166 166 try:
167 167 hist = json.load(hfile)
168 168 except ValueError: # Ignore it if JSON is corrupt.
169 169 return
170 170 self.input_hist_parsed = hist['parsed']
171 171 self.input_hist_raw = hist['raw']
172 172 if self.shell.has_readline:
173 173 self.populate_readline_history()
174 174
175 175 def get_history(self, index=None, raw=False, output=True):
176 176 """Get the history list.
177 177
178 178 Get the input and output history.
179 179
180 180 Parameters
181 181 ----------
182 182 index : n or (n1, n2) or None
183 183 If n, then the last entries. If a tuple, then all in
184 184 range(n1, n2). If None, then all entries. Raises IndexError if
185 185 the format of index is incorrect.
186 186 raw : bool
187 187 If True, return the raw input.
188 188 output : bool
189 189 If True, then return the output as well.
190 190
191 191 Returns
192 192 -------
193 193 If output is True, then return a dict of tuples, keyed by the prompt
194 194 numbers and with values of (input, output). If output is False, then
195 195 a dict, keyed by the prompt number with the values of input. Raises
196 196 IndexError if no history is found.
197 197 """
198 198 if raw:
199 199 input_hist = self.input_hist_raw
200 200 else:
201 201 input_hist = self.input_hist_parsed
202 202 if output:
203 203 output_hist = self.output_hist
204 204 n = len(input_hist)
205 205 if index is None:
206 206 start=0; stop=n
207 207 elif isinstance(index, int):
208 208 start=n-index; stop=n
209 209 elif isinstance(index, tuple) and len(index) == 2:
210 210 start=index[0]; stop=index[1]
211 211 else:
212 212 raise IndexError('Not a valid index for the input history: %r'
213 213 % index)
214 214 hist = {}
215 215 for i in range(start, stop):
216 216 if output:
217 217 hist[i] = (input_hist[i], output_hist.get(i))
218 218 else:
219 219 hist[i] = input_hist[i]
220 220 if not hist:
221 221 raise IndexError('No history for range of indices: %r' % index)
222 222 return hist
223 223
224 224 def store_inputs(self, source, source_raw=None):
225 225 """Store source and raw input in history and create input cache
226 226 variables _i*.
227 227
228 228 Parameters
229 229 ----------
230 230 source : str
231 231 Python input.
232 232
233 233 source_raw : str, optional
234 234 If given, this is the raw input without any IPython transformations
235 235 applied to it. If not given, ``source`` is used.
236 236 """
237 237 if source_raw is None:
238 238 source_raw = source
239 239
240 240 # do not store exit/quit commands
241 241 if source_raw.strip() in self._exit_commands:
242 242 return
243 243
244 244 self.input_hist_parsed.append(source.rstrip())
245 245 self.input_hist_raw.append(source_raw.rstrip())
246 246 self.shadow_hist.add(source)
247 247
248 248 # update the auto _i variables
249 249 self._iii = self._ii
250 250 self._ii = self._i
251 251 self._i = self._i00
252 252 self._i00 = source_raw
253 253
254 254 # hackish access to user namespace to create _i1,_i2... dynamically
255 255 new_i = '_i%s' % self.shell.execution_count
256 256 to_main = {'_i': self._i,
257 257 '_ii': self._ii,
258 258 '_iii': self._iii,
259 259 new_i : self._i00 }
260 260 self.shell.user_ns.update(to_main)
261 261
262 262 def sync_inputs(self):
263 263 """Ensure raw and translated histories have same length."""
264 264 if len(self.input_hist_parsed) != len (self.input_hist_raw):
265 265 self.input_hist_raw[:] = self.input_hist_parsed
266 266
267 267 def reset(self):
268 268 """Clear all histories managed by this object."""
269 269 self.input_hist_parsed[:] = []
270 270 self.input_hist_raw[:] = []
271 271 self.output_hist.clear()
272 272 # The directory history can't be completely empty
273 273 self.dir_hist[:] = [os.getcwd()]
274 274
275 275 class HistorySaveThread(threading.Thread):
276 276 """This thread makes IPython save history periodically.
277 277
278 278 Without this class, IPython would only save the history on a clean exit.
279 279 This saves the history periodically (the current default is once per
280 280 minute), so that it is not lost in the event of a crash.
281 281
282 282 The implementation sets an event to indicate that history should be saved.
283 283 The actual save is carried out after executing a user command, to avoid
284 284 thread issues.
285 285 """
286 286 daemon = True
287 287
288 288 def __init__(self, autosave_flag, time_interval=60):
289 289 threading.Thread.__init__(self)
290 290 self.time_interval = time_interval
291 291 self.autosave_flag = autosave_flag
292 292 self.exit_now = threading.Event()
293 293 # Ensure the thread is stopped tidily when exiting normally
294 294 atexit.register(self.stop)
295 295
296 296 def run(self):
297 297 while True:
298 298 self.exit_now.wait(self.time_interval)
299 299 if self.exit_now.is_set():
300 300 break
301 301 self.autosave_flag.set()
302 302
303 303 def stop(self):
304 304 """Safely and quickly stop the autosave timer thread."""
305 305 self.exit_now.set()
306 306 self.join()
307 307
308 308 def magic_history(self, parameter_s = ''):
309 309 """Print input history (_i<n> variables), with most recent last.
310 310
311 311 %history -> print at most 40 inputs (some may be multi-line)\\
312 312 %history n -> print at most n inputs\\
313 313 %history n1 n2 -> print inputs between n1 and n2 (n2 not included)\\
314 314
315 315 By default, input history is printed without line numbers so it can be
316 316 directly pasted into an editor.
317 317
318 318 With -n, each input's number <n> is shown, and is accessible as the
319 319 automatically generated variable _i<n> as well as In[<n>]. Multi-line
320 320 statements are printed starting at a new line for easy copy/paste.
321 321
322 322 Options:
323 323
324 324 -n: print line numbers for each input.
325 325 This feature is only available if numbered prompts are in use.
326 326
327 327 -o: also print outputs for each input.
328 328
329 329 -p: print classic '>>>' python prompts before each input. This is useful
330 330 for making documentation, and in conjunction with -o, for producing
331 331 doctest-ready output.
332 332
333 333 -r: (default) print the 'raw' history, i.e. the actual commands you typed.
334 334
335 335 -t: print the 'translated' history, as IPython understands it. IPython
336 336 filters your input and converts it all into valid Python source before
337 337 executing it (things like magics or aliases are turned into function
338 338 calls, for example). With this option, you'll see the native history
339 339 instead of the user-entered version: '%cd /' will be seen as
340 340 'get_ipython().magic("%cd /")' instead of '%cd /'.
341 341
342 342 -g: treat the arg as a pattern to grep for in (full) history.
343 343 This includes the "shadow history" (almost all commands ever written).
344 344 Use '%hist -g' to show full shadow history (may be very long).
345 345 In shadow history, every index nuwber starts with 0.
346 346
347 347 -f FILENAME: instead of printing the output to the screen, redirect it to
348 348 the given file. The file is always overwritten, though IPython asks for
349 349 confirmation first if it already exists.
350 350
351 Example
352 -------
353
351 Examples
352 --------
354 353 ::
355 354
356 355 In [6]: %hist -n 4 6
357 356 4:a = 12
358 357 5:print a**2
359 358
360 359 """
361 360
362 361 if not self.shell.displayhook.do_full_cache:
363 362 print('This feature is only available if numbered prompts are in use.')
364 363 return
365 364 opts,args = self.parse_options(parameter_s,'gnoptsrf:',mode='list')
366 365
367 366 # Check if output to specific file was requested.
368 367 try:
369 368 outfname = opts['f']
370 369 except KeyError:
371 370 outfile = IPython.utils.io.Term.cout # default
372 371 # We don't want to close stdout at the end!
373 372 close_at_end = False
374 373 else:
375 374 if os.path.exists(outfname):
376 375 if not ask_yes_no("File %r exists. Overwrite?" % outfname):
377 376 print('Aborting.')
378 377 return
379 378
380 379 outfile = open(outfname,'w')
381 380 close_at_end = True
382 381
383 382 if 't' in opts:
384 383 input_hist = self.shell.history_manager.input_hist_parsed
385 384 elif 'r' in opts:
386 385 input_hist = self.shell.history_manager.input_hist_raw
387 386 else:
388 387 # Raw history is the default
389 388 input_hist = self.shell.history_manager.input_hist_raw
390 389
391 390 default_length = 40
392 391 pattern = None
393 392 if 'g' in opts:
394 393 init = 1
395 394 final = len(input_hist)
396 395 parts = parameter_s.split(None, 1)
397 396 if len(parts) == 1:
398 397 parts += '*'
399 398 head, pattern = parts
400 399 pattern = "*" + pattern + "*"
401 400 elif len(args) == 0:
402 401 final = len(input_hist)-1
403 402 init = max(1,final-default_length)
404 403 elif len(args) == 1:
405 404 final = len(input_hist)
406 405 init = max(1, final-int(args[0]))
407 406 elif len(args) == 2:
408 407 init, final = map(int, args)
409 408 else:
410 409 warn('%hist takes 0, 1 or 2 arguments separated by spaces.')
411 410 print(self.magic_hist.__doc__, file=IPython.utils.io.Term.cout)
412 411 return
413 412
414 413 width = len(str(final))
415 414 line_sep = ['','\n']
416 415 print_nums = 'n' in opts
417 416 print_outputs = 'o' in opts
418 417 pyprompts = 'p' in opts
419 418
420 419 found = False
421 420 if pattern is not None:
422 421 sh = self.shell.history_manager.shadowhist.all()
423 422 for idx, s in sh:
424 423 if fnmatch.fnmatch(s, pattern):
425 424 print("0%d: %s" %(idx, s.expandtabs(4)), file=outfile)
426 425 found = True
427 426
428 427 if found:
429 428 print("===", file=outfile)
430 429 print("shadow history ends, fetch by %rep <number> (must start with 0)",
431 430 file=outfile)
432 431 print("=== start of normal history ===", file=outfile)
433 432
434 433 for in_num in range(init, final):
435 434 # Print user history with tabs expanded to 4 spaces. The GUI clients
436 435 # use hard tabs for easier usability in auto-indented code, but we want
437 436 # to produce PEP-8 compliant history for safe pasting into an editor.
438 437 inline = input_hist[in_num].expandtabs(4).rstrip()+'\n'
439 438
440 439 if pattern is not None and not fnmatch.fnmatch(inline, pattern):
441 440 continue
442 441
443 442 multiline = int(inline.count('\n') > 1)
444 443 if print_nums:
445 444 print('%s:%s' % (str(in_num).ljust(width), line_sep[multiline]),
446 445 file=outfile)
447 446 if pyprompts:
448 447 print('>>>', file=outfile)
449 448 if multiline:
450 449 lines = inline.splitlines()
451 450 print('\n... '.join(lines), file=outfile)
452 451 print('... ', file=outfile)
453 452 else:
454 453 print(inline, end='', file=outfile)
455 454 else:
456 455 print(inline, end='', file=outfile)
457 456 if print_outputs:
458 457 output = self.shell.history_manager.output_hist.get(in_num)
459 458 if output is not None:
460 459 print(repr(output), file=outfile)
461 460
462 461 if close_at_end:
463 462 outfile.close()
464 463
465 464
466 465 def magic_hist(self, parameter_s=''):
467 466 """Alternate name for %history."""
468 467 return self.magic_history(parameter_s)
469 468
470 469
471 470 def rep_f(self, arg):
472 471 r""" Repeat a command, or get command to input line for editing
473 472
474 473 - %rep (no arguments):
475 474
476 475 Place a string version of last computation result (stored in the special '_'
477 476 variable) to the next input prompt. Allows you to create elaborate command
478 477 lines without using copy-paste::
479 478
480 479 $ l = ["hei", "vaan"]
481 480 $ "".join(l)
482 481 ==> heivaan
483 482 $ %rep
484 483 $ heivaan_ <== cursor blinking
485 484
486 485 %rep 45
487 486
488 487 Place history line 45 to next input prompt. Use %hist to find out the
489 488 number.
490 489
491 490 %rep 1-4 6-7 3
492 491
493 492 Repeat the specified lines immediately. Input slice syntax is the same as
494 493 in %macro and %save.
495 494
496 495 %rep foo
497 496
498 497 Place the most recent line that has the substring "foo" to next input.
499 498 (e.g. 'svn ci -m foobar').
500 499 """
501 500
502 501 opts,args = self.parse_options(arg,'',mode='list')
503 502 if not args:
504 503 self.set_next_input(str(self.shell.user_ns["_"]))
505 504 return
506 505
507 506 if len(args) == 1 and not '-' in args[0]:
508 507 arg = args[0]
509 508 if len(arg) > 1 and arg.startswith('0'):
510 509 # get from shadow hist
511 510 num = int(arg[1:])
512 511 line = self.shell.shadowhist.get(num)
513 512 self.set_next_input(str(line))
514 513 return
515 514 try:
516 515 num = int(args[0])
517 516 self.set_next_input(str(self.shell.input_hist_raw[num]).rstrip())
518 517 return
519 518 except ValueError:
520 519 pass
521 520
522 521 for h in reversed(self.shell.input_hist_raw):
523 522 if 'rep' in h:
524 523 continue
525 524 if fnmatch.fnmatch(h,'*' + arg + '*'):
526 525 self.set_next_input(str(h).rstrip())
527 526 return
528 527
529 528 try:
530 529 lines = self.extract_input_slices(args, True)
531 530 print("lines", lines)
532 531 self.run_cell(lines)
533 532 except ValueError:
534 533 print("Not found in recent history:", args)
535 534
536 535
537 536 _sentinel = object()
538 537
539 538 class ShadowHist(object):
540 539 def __init__(self, db, shell):
541 540 # cmd => idx mapping
542 541 self.curidx = 0
543 542 self.db = db
544 543 self.disabled = False
545 544 self.shell = shell
546 545
547 546 def inc_idx(self):
548 547 idx = self.db.get('shadowhist_idx', 1)
549 548 self.db['shadowhist_idx'] = idx + 1
550 549 return idx
551 550
552 551 def add(self, ent):
553 552 if self.disabled:
554 553 return
555 554 try:
556 555 old = self.db.hget('shadowhist', ent, _sentinel)
557 556 if old is not _sentinel:
558 557 return
559 558 newidx = self.inc_idx()
560 559 #print("new", newidx) # dbg
561 560 self.db.hset('shadowhist',ent, newidx)
562 561 except:
563 562 self.shell.showtraceback()
564 563 print("WARNING: disabling shadow history")
565 564 self.disabled = True
566 565
567 566 def all(self):
568 567 d = self.db.hdict('shadowhist')
569 568 items = [(i,s) for (s,i) in d.iteritems()]
570 569 items.sort()
571 570 return items
572 571
573 572 def get(self, idx):
574 573 all = self.all()
575 574
576 575 for k, v in all:
577 576 if k == idx:
578 577 return v
579 578
580 579
581 580 def init_ipython(ip):
582 581 ip.define_magic("rep",rep_f)
583 582 ip.define_magic("hist",magic_hist)
584 583 ip.define_magic("history",magic_history)
585 584
586 585 # XXX - ipy_completers are in quarantine, need to be updated to new apis
587 586 #import ipy_completers
588 587 #ipy_completers.quick_completer('%hist' ,'-g -t -r -n')
@@ -1,3390 +1,3388 b''
1 1 # encoding: utf-8
2 2 """Magic functions for InteractiveShell.
3 3 """
4 4
5 5 #-----------------------------------------------------------------------------
6 6 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
7 7 # Copyright (C) 2001-2007 Fernando Perez <fperez@colorado.edu>
8 8 # Copyright (C) 2008-2009 The IPython Development Team
9 9
10 10 # Distributed under the terms of the BSD License. The full license is in
11 11 # the file COPYING, distributed as part of this software.
12 12 #-----------------------------------------------------------------------------
13 13
14 14 #-----------------------------------------------------------------------------
15 15 # Imports
16 16 #-----------------------------------------------------------------------------
17 17
18 18 import __builtin__
19 19 import __future__
20 20 import bdb
21 21 import inspect
22 22 import os
23 23 import sys
24 24 import shutil
25 25 import re
26 26 import time
27 27 import textwrap
28 28 import types
29 29 from cStringIO import StringIO
30 30 from getopt import getopt,GetoptError
31 31 from pprint import pformat
32 32
33 33 # cProfile was added in Python2.5
34 34 try:
35 35 import cProfile as profile
36 36 import pstats
37 37 except ImportError:
38 38 # profile isn't bundled by default in Debian for license reasons
39 39 try:
40 40 import profile,pstats
41 41 except ImportError:
42 42 profile = pstats = None
43 43
44 44 import IPython
45 45 from IPython.core import debugger, oinspect
46 46 from IPython.core.error import TryNext
47 47 from IPython.core.error import UsageError
48 48 from IPython.core.fakemodule import FakeModule
49 49 from IPython.core.macro import Macro
50 50 from IPython.core import page
51 51 from IPython.core.prefilter import ESC_MAGIC
52 52 from IPython.lib.pylabtools import mpl_runner
53 53 from IPython.external.Itpl import itpl, printpl
54 54 from IPython.testing import decorators as testdec
55 55 from IPython.utils.io import file_read, nlprint
56 56 import IPython.utils.io
57 57 from IPython.utils.path import get_py_filename
58 58 from IPython.utils.process import arg_split, abbrev_cwd
59 59 from IPython.utils.terminal import set_term_title
60 60 from IPython.utils.text import LSString, SList, StringTypes, format_screen
61 61 from IPython.utils.timing import clock, clock2
62 62 from IPython.utils.warn import warn, error
63 63 from IPython.utils.ipstruct import Struct
64 64 import IPython.utils.generics
65 65
66 66 #-----------------------------------------------------------------------------
67 67 # Utility functions
68 68 #-----------------------------------------------------------------------------
69 69
70 70 def on_off(tag):
71 71 """Return an ON/OFF string for a 1/0 input. Simple utility function."""
72 72 return ['OFF','ON'][tag]
73 73
74 74 class Bunch: pass
75 75
76 76 def compress_dhist(dh):
77 77 head, tail = dh[:-10], dh[-10:]
78 78
79 79 newhead = []
80 80 done = set()
81 81 for h in head:
82 82 if h in done:
83 83 continue
84 84 newhead.append(h)
85 85 done.add(h)
86 86
87 87 return newhead + tail
88 88
89 89
90 90 #***************************************************************************
91 91 # Main class implementing Magic functionality
92 92
93 93 # XXX - for some odd reason, if Magic is made a new-style class, we get errors
94 94 # on construction of the main InteractiveShell object. Something odd is going
95 95 # on with super() calls, Configurable and the MRO... For now leave it as-is, but
96 96 # eventually this needs to be clarified.
97 97 # BG: This is because InteractiveShell inherits from this, but is itself a
98 98 # Configurable. This messes up the MRO in some way. The fix is that we need to
99 99 # make Magic a configurable that InteractiveShell does not subclass.
100 100
101 101 class Magic:
102 102 """Magic functions for InteractiveShell.
103 103
104 104 Shell functions which can be reached as %function_name. All magic
105 105 functions should accept a string, which they can parse for their own
106 106 needs. This can make some functions easier to type, eg `%cd ../`
107 107 vs. `%cd("../")`
108 108
109 109 ALL definitions MUST begin with the prefix magic_. The user won't need it
110 110 at the command line, but it is is needed in the definition. """
111 111
112 112 # class globals
113 113 auto_status = ['Automagic is OFF, % prefix IS needed for magic functions.',
114 114 'Automagic is ON, % prefix NOT needed for magic functions.']
115 115
116 116 #......................................................................
117 117 # some utility functions
118 118
119 119 def __init__(self,shell):
120 120
121 121 self.options_table = {}
122 122 if profile is None:
123 123 self.magic_prun = self.profile_missing_notice
124 124 self.shell = shell
125 125
126 126 # namespace for holding state we may need
127 127 self._magic_state = Bunch()
128 128
129 129 def profile_missing_notice(self, *args, **kwargs):
130 130 error("""\
131 131 The profile module could not be found. It has been removed from the standard
132 132 python packages because of its non-free license. To use profiling, install the
133 133 python-profiler package from non-free.""")
134 134
135 135 def default_option(self,fn,optstr):
136 136 """Make an entry in the options_table for fn, with value optstr"""
137 137
138 138 if fn not in self.lsmagic():
139 139 error("%s is not a magic function" % fn)
140 140 self.options_table[fn] = optstr
141 141
142 142 def lsmagic(self):
143 143 """Return a list of currently available magic functions.
144 144
145 145 Gives a list of the bare names after mangling (['ls','cd', ...], not
146 146 ['magic_ls','magic_cd',...]"""
147 147
148 148 # FIXME. This needs a cleanup, in the way the magics list is built.
149 149
150 150 # magics in class definition
151 151 class_magic = lambda fn: fn.startswith('magic_') and \
152 152 callable(Magic.__dict__[fn])
153 153 # in instance namespace (run-time user additions)
154 154 inst_magic = lambda fn: fn.startswith('magic_') and \
155 155 callable(self.__dict__[fn])
156 156 # and bound magics by user (so they can access self):
157 157 inst_bound_magic = lambda fn: fn.startswith('magic_') and \
158 158 callable(self.__class__.__dict__[fn])
159 159 magics = filter(class_magic,Magic.__dict__.keys()) + \
160 160 filter(inst_magic,self.__dict__.keys()) + \
161 161 filter(inst_bound_magic,self.__class__.__dict__.keys())
162 162 out = []
163 163 for fn in set(magics):
164 164 out.append(fn.replace('magic_','',1))
165 165 out.sort()
166 166 return out
167 167
168 168 def extract_input_slices(self,slices,raw=False):
169 169 """Return as a string a set of input history slices.
170 170
171 171 Inputs:
172 172
173 173 - slices: the set of slices is given as a list of strings (like
174 174 ['1','4:8','9'], since this function is for use by magic functions
175 175 which get their arguments as strings.
176 176
177 177 Optional inputs:
178 178
179 179 - raw(False): by default, the processed input is used. If this is
180 180 true, the raw input history is used instead.
181 181
182 182 Note that slices can be called with two notations:
183 183
184 184 N:M -> standard python form, means including items N...(M-1).
185 185
186 186 N-M -> include items N..M (closed endpoint)."""
187 187
188 188 if raw:
189 189 hist = self.shell.history_manager.input_hist_raw
190 190 else:
191 191 hist = self.shell.history_manager.input_hist_parsed
192 192
193 193 cmds = []
194 194 for chunk in slices:
195 195 if ':' in chunk:
196 196 ini,fin = map(int,chunk.split(':'))
197 197 elif '-' in chunk:
198 198 ini,fin = map(int,chunk.split('-'))
199 199 fin += 1
200 200 else:
201 201 ini = int(chunk)
202 202 fin = ini+1
203 203 cmds.append(''.join(hist[ini:fin]))
204 204 return cmds
205 205
206 206 def arg_err(self,func):
207 207 """Print docstring if incorrect arguments were passed"""
208 208 print 'Error in arguments:'
209 209 print oinspect.getdoc(func)
210 210
211 211 def format_latex(self,strng):
212 212 """Format a string for latex inclusion."""
213 213
214 214 # Characters that need to be escaped for latex:
215 215 escape_re = re.compile(r'(%|_|\$|#|&)',re.MULTILINE)
216 216 # Magic command names as headers:
217 217 cmd_name_re = re.compile(r'^(%s.*?):' % ESC_MAGIC,
218 218 re.MULTILINE)
219 219 # Magic commands
220 220 cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % ESC_MAGIC,
221 221 re.MULTILINE)
222 222 # Paragraph continue
223 223 par_re = re.compile(r'\\$',re.MULTILINE)
224 224
225 225 # The "\n" symbol
226 226 newline_re = re.compile(r'\\n')
227 227
228 228 # Now build the string for output:
229 229 #strng = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',strng)
230 230 strng = cmd_name_re.sub(r'\n\\bigskip\n\\texttt{\\textbf{ \1}}:',
231 231 strng)
232 232 strng = cmd_re.sub(r'\\texttt{\g<cmd>}',strng)
233 233 strng = par_re.sub(r'\\\\',strng)
234 234 strng = escape_re.sub(r'\\\1',strng)
235 235 strng = newline_re.sub(r'\\textbackslash{}n',strng)
236 236 return strng
237 237
238 238 def parse_options(self,arg_str,opt_str,*long_opts,**kw):
239 239 """Parse options passed to an argument string.
240 240
241 241 The interface is similar to that of getopt(), but it returns back a
242 242 Struct with the options as keys and the stripped argument string still
243 243 as a string.
244 244
245 245 arg_str is quoted as a true sys.argv vector by using shlex.split.
246 246 This allows us to easily expand variables, glob files, quote
247 247 arguments, etc.
248 248
249 249 Options:
250 250 -mode: default 'string'. If given as 'list', the argument string is
251 251 returned as a list (split on whitespace) instead of a string.
252 252
253 253 -list_all: put all option values in lists. Normally only options
254 254 appearing more than once are put in a list.
255 255
256 256 -posix (True): whether to split the input line in POSIX mode or not,
257 257 as per the conventions outlined in the shlex module from the
258 258 standard library."""
259 259
260 260 # inject default options at the beginning of the input line
261 261 caller = sys._getframe(1).f_code.co_name.replace('magic_','')
262 262 arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str)
263 263
264 264 mode = kw.get('mode','string')
265 265 if mode not in ['string','list']:
266 266 raise ValueError,'incorrect mode given: %s' % mode
267 267 # Get options
268 268 list_all = kw.get('list_all',0)
269 269 posix = kw.get('posix', os.name == 'posix')
270 270
271 271 # Check if we have more than one argument to warrant extra processing:
272 272 odict = {} # Dictionary with options
273 273 args = arg_str.split()
274 274 if len(args) >= 1:
275 275 # If the list of inputs only has 0 or 1 thing in it, there's no
276 276 # need to look for options
277 277 argv = arg_split(arg_str,posix)
278 278 # Do regular option processing
279 279 try:
280 280 opts,args = getopt(argv,opt_str,*long_opts)
281 281 except GetoptError,e:
282 282 raise UsageError('%s ( allowed: "%s" %s)' % (e.msg,opt_str,
283 283 " ".join(long_opts)))
284 284 for o,a in opts:
285 285 if o.startswith('--'):
286 286 o = o[2:]
287 287 else:
288 288 o = o[1:]
289 289 try:
290 290 odict[o].append(a)
291 291 except AttributeError:
292 292 odict[o] = [odict[o],a]
293 293 except KeyError:
294 294 if list_all:
295 295 odict[o] = [a]
296 296 else:
297 297 odict[o] = a
298 298
299 299 # Prepare opts,args for return
300 300 opts = Struct(odict)
301 301 if mode == 'string':
302 302 args = ' '.join(args)
303 303
304 304 return opts,args
305 305
306 306 #......................................................................
307 307 # And now the actual magic functions
308 308
309 309 # Functions for IPython shell work (vars,funcs, config, etc)
310 310 def magic_lsmagic(self, parameter_s = ''):
311 311 """List currently available magic functions."""
312 312 mesc = ESC_MAGIC
313 313 print 'Available magic functions:\n'+mesc+\
314 314 (' '+mesc).join(self.lsmagic())
315 315 print '\n' + Magic.auto_status[self.shell.automagic]
316 316 return None
317 317
318 318 def magic_magic(self, parameter_s = ''):
319 319 """Print information about the magic function system.
320 320
321 321 Supported formats: -latex, -brief, -rest
322 322 """
323 323
324 324 mode = ''
325 325 try:
326 326 if parameter_s.split()[0] == '-latex':
327 327 mode = 'latex'
328 328 if parameter_s.split()[0] == '-brief':
329 329 mode = 'brief'
330 330 if parameter_s.split()[0] == '-rest':
331 331 mode = 'rest'
332 332 rest_docs = []
333 333 except:
334 334 pass
335 335
336 336 magic_docs = []
337 337 for fname in self.lsmagic():
338 338 mname = 'magic_' + fname
339 339 for space in (Magic,self,self.__class__):
340 340 try:
341 341 fn = space.__dict__[mname]
342 342 except KeyError:
343 343 pass
344 344 else:
345 345 break
346 346 if mode == 'brief':
347 347 # only first line
348 348 if fn.__doc__:
349 349 fndoc = fn.__doc__.split('\n',1)[0]
350 350 else:
351 351 fndoc = 'No documentation'
352 352 else:
353 353 if fn.__doc__:
354 354 fndoc = fn.__doc__.rstrip()
355 355 else:
356 356 fndoc = 'No documentation'
357 357
358 358
359 359 if mode == 'rest':
360 360 rest_docs.append('**%s%s**::\n\n\t%s\n\n' %(ESC_MAGIC,
361 361 fname,fndoc))
362 362
363 363 else:
364 364 magic_docs.append('%s%s:\n\t%s\n' %(ESC_MAGIC,
365 365 fname,fndoc))
366 366
367 367 magic_docs = ''.join(magic_docs)
368 368
369 369 if mode == 'rest':
370 370 return "".join(rest_docs)
371 371
372 372 if mode == 'latex':
373 373 print self.format_latex(magic_docs)
374 374 return
375 375 else:
376 376 magic_docs = format_screen(magic_docs)
377 377 if mode == 'brief':
378 378 return magic_docs
379 379
380 380 outmsg = """
381 381 IPython's 'magic' functions
382 382 ===========================
383 383
384 384 The magic function system provides a series of functions which allow you to
385 385 control the behavior of IPython itself, plus a lot of system-type
386 386 features. All these functions are prefixed with a % character, but parameters
387 387 are given without parentheses or quotes.
388 388
389 389 NOTE: If you have 'automagic' enabled (via the command line option or with the
390 390 %automagic function), you don't need to type in the % explicitly. By default,
391 391 IPython ships with automagic on, so you should only rarely need the % escape.
392 392
393 393 Example: typing '%cd mydir' (without the quotes) changes you working directory
394 394 to 'mydir', if it exists.
395 395
396 396 You can define your own magic functions to extend the system. See the supplied
397 397 ipythonrc and example-magic.py files for details (in your ipython
398 398 configuration directory, typically $HOME/.ipython/).
399 399
400 400 You can also define your own aliased names for magic functions. In your
401 401 ipythonrc file, placing a line like:
402 402
403 403 execute __IPYTHON__.magic_pf = __IPYTHON__.magic_profile
404 404
405 405 will define %pf as a new name for %profile.
406 406
407 407 You can also call magics in code using the magic() function, which IPython
408 408 automatically adds to the builtin namespace. Type 'magic?' for details.
409 409
410 410 For a list of the available magic functions, use %lsmagic. For a description
411 411 of any of them, type %magic_name?, e.g. '%cd?'.
412 412
413 413 Currently the magic system has the following functions:\n"""
414 414
415 415 mesc = ESC_MAGIC
416 416 outmsg = ("%s\n%s\n\nSummary of magic functions (from %slsmagic):"
417 417 "\n\n%s%s\n\n%s" % (outmsg,
418 418 magic_docs,mesc,mesc,
419 419 (' '+mesc).join(self.lsmagic()),
420 420 Magic.auto_status[self.shell.automagic] ) )
421 421 page.page(outmsg)
422 422
423 423 def magic_automagic(self, parameter_s = ''):
424 424 """Make magic functions callable without having to type the initial %.
425 425
426 426 Without argumentsl toggles on/off (when off, you must call it as
427 427 %automagic, of course). With arguments it sets the value, and you can
428 428 use any of (case insensitive):
429 429
430 430 - on,1,True: to activate
431 431
432 432 - off,0,False: to deactivate.
433 433
434 434 Note that magic functions have lowest priority, so if there's a
435 435 variable whose name collides with that of a magic fn, automagic won't
436 436 work for that function (you get the variable instead). However, if you
437 437 delete the variable (del var), the previously shadowed magic function
438 438 becomes visible to automagic again."""
439 439
440 440 arg = parameter_s.lower()
441 441 if parameter_s in ('on','1','true'):
442 442 self.shell.automagic = True
443 443 elif parameter_s in ('off','0','false'):
444 444 self.shell.automagic = False
445 445 else:
446 446 self.shell.automagic = not self.shell.automagic
447 447 print '\n' + Magic.auto_status[self.shell.automagic]
448 448
449 449 @testdec.skip_doctest
450 450 def magic_autocall(self, parameter_s = ''):
451 451 """Make functions callable without having to type parentheses.
452 452
453 453 Usage:
454 454
455 455 %autocall [mode]
456 456
457 457 The mode can be one of: 0->Off, 1->Smart, 2->Full. If not given, the
458 458 value is toggled on and off (remembering the previous state).
459 459
460 460 In more detail, these values mean:
461 461
462 462 0 -> fully disabled
463 463
464 464 1 -> active, but do not apply if there are no arguments on the line.
465 465
466 466 In this mode, you get:
467 467
468 468 In [1]: callable
469 469 Out[1]: <built-in function callable>
470 470
471 471 In [2]: callable 'hello'
472 472 ------> callable('hello')
473 473 Out[2]: False
474 474
475 475 2 -> Active always. Even if no arguments are present, the callable
476 476 object is called:
477 477
478 478 In [2]: float
479 479 ------> float()
480 480 Out[2]: 0.0
481 481
482 482 Note that even with autocall off, you can still use '/' at the start of
483 483 a line to treat the first argument on the command line as a function
484 484 and add parentheses to it:
485 485
486 486 In [8]: /str 43
487 487 ------> str(43)
488 488 Out[8]: '43'
489 489
490 490 # all-random (note for auto-testing)
491 491 """
492 492
493 493 if parameter_s:
494 494 arg = int(parameter_s)
495 495 else:
496 496 arg = 'toggle'
497 497
498 498 if not arg in (0,1,2,'toggle'):
499 499 error('Valid modes: (0->Off, 1->Smart, 2->Full')
500 500 return
501 501
502 502 if arg in (0,1,2):
503 503 self.shell.autocall = arg
504 504 else: # toggle
505 505 if self.shell.autocall:
506 506 self._magic_state.autocall_save = self.shell.autocall
507 507 self.shell.autocall = 0
508 508 else:
509 509 try:
510 510 self.shell.autocall = self._magic_state.autocall_save
511 511 except AttributeError:
512 512 self.shell.autocall = self._magic_state.autocall_save = 1
513 513
514 514 print "Automatic calling is:",['OFF','Smart','Full'][self.shell.autocall]
515 515
516 516
517 517 def magic_page(self, parameter_s=''):
518 518 """Pretty print the object and display it through a pager.
519 519
520 520 %page [options] OBJECT
521 521
522 522 If no object is given, use _ (last output).
523 523
524 524 Options:
525 525
526 526 -r: page str(object), don't pretty-print it."""
527 527
528 528 # After a function contributed by Olivier Aubert, slightly modified.
529 529
530 530 # Process options/args
531 531 opts,args = self.parse_options(parameter_s,'r')
532 532 raw = 'r' in opts
533 533
534 534 oname = args and args or '_'
535 535 info = self._ofind(oname)
536 536 if info['found']:
537 537 txt = (raw and str or pformat)( info['obj'] )
538 538 page.page(txt)
539 539 else:
540 540 print 'Object `%s` not found' % oname
541 541
542 542 def magic_profile(self, parameter_s=''):
543 543 """Print your currently active IPython profile."""
544 544 if self.shell.profile:
545 545 printpl('Current IPython profile: $self.shell.profile.')
546 546 else:
547 547 print 'No profile active.'
548 548
549 549 def magic_pinfo(self, parameter_s='', namespaces=None):
550 550 """Provide detailed information about an object.
551 551
552 552 '%pinfo object' is just a synonym for object? or ?object."""
553 553
554 554 #print 'pinfo par: <%s>' % parameter_s # dbg
555 555
556 556
557 557 # detail_level: 0 -> obj? , 1 -> obj??
558 558 detail_level = 0
559 559 # We need to detect if we got called as 'pinfo pinfo foo', which can
560 560 # happen if the user types 'pinfo foo?' at the cmd line.
561 561 pinfo,qmark1,oname,qmark2 = \
562 562 re.match('(pinfo )?(\?*)(.*?)(\??$)',parameter_s).groups()
563 563 if pinfo or qmark1 or qmark2:
564 564 detail_level = 1
565 565 if "*" in oname:
566 566 self.magic_psearch(oname)
567 567 else:
568 568 self.shell._inspect('pinfo', oname, detail_level=detail_level,
569 569 namespaces=namespaces)
570 570
571 571 def magic_pinfo2(self, parameter_s='', namespaces=None):
572 572 """Provide extra detailed information about an object.
573 573
574 574 '%pinfo2 object' is just a synonym for object?? or ??object."""
575 575 self.shell._inspect('pinfo', parameter_s, detail_level=1,
576 576 namespaces=namespaces)
577 577
578 578 def magic_pdef(self, parameter_s='', namespaces=None):
579 579 """Print the definition header for any callable object.
580 580
581 581 If the object is a class, print the constructor information."""
582 582 self._inspect('pdef',parameter_s, namespaces)
583 583
584 584 def magic_pdoc(self, parameter_s='', namespaces=None):
585 585 """Print the docstring for an object.
586 586
587 587 If the given object is a class, it will print both the class and the
588 588 constructor docstrings."""
589 589 self._inspect('pdoc',parameter_s, namespaces)
590 590
591 591 def magic_psource(self, parameter_s='', namespaces=None):
592 592 """Print (or run through pager) the source code for an object."""
593 593 self._inspect('psource',parameter_s, namespaces)
594 594
595 595 def magic_pfile(self, parameter_s=''):
596 596 """Print (or run through pager) the file where an object is defined.
597 597
598 598 The file opens at the line where the object definition begins. IPython
599 599 will honor the environment variable PAGER if set, and otherwise will
600 600 do its best to print the file in a convenient form.
601 601
602 602 If the given argument is not an object currently defined, IPython will
603 603 try to interpret it as a filename (automatically adding a .py extension
604 604 if needed). You can thus use %pfile as a syntax highlighting code
605 605 viewer."""
606 606
607 607 # first interpret argument as an object name
608 608 out = self._inspect('pfile',parameter_s)
609 609 # if not, try the input as a filename
610 610 if out == 'not found':
611 611 try:
612 612 filename = get_py_filename(parameter_s)
613 613 except IOError,msg:
614 614 print msg
615 615 return
616 616 page.page(self.shell.inspector.format(file(filename).read()))
617 617
618 618 def magic_psearch(self, parameter_s=''):
619 619 """Search for object in namespaces by wildcard.
620 620
621 621 %psearch [options] PATTERN [OBJECT TYPE]
622 622
623 623 Note: ? can be used as a synonym for %psearch, at the beginning or at
624 624 the end: both a*? and ?a* are equivalent to '%psearch a*'. Still, the
625 625 rest of the command line must be unchanged (options come first), so
626 626 for example the following forms are equivalent
627 627
628 628 %psearch -i a* function
629 629 -i a* function?
630 630 ?-i a* function
631 631
632 632 Arguments:
633 633
634 634 PATTERN
635 635
636 636 where PATTERN is a string containing * as a wildcard similar to its
637 637 use in a shell. The pattern is matched in all namespaces on the
638 638 search path. By default objects starting with a single _ are not
639 639 matched, many IPython generated objects have a single
640 640 underscore. The default is case insensitive matching. Matching is
641 641 also done on the attributes of objects and not only on the objects
642 642 in a module.
643 643
644 644 [OBJECT TYPE]
645 645
646 646 Is the name of a python type from the types module. The name is
647 647 given in lowercase without the ending type, ex. StringType is
648 648 written string. By adding a type here only objects matching the
649 649 given type are matched. Using all here makes the pattern match all
650 650 types (this is the default).
651 651
652 652 Options:
653 653
654 654 -a: makes the pattern match even objects whose names start with a
655 655 single underscore. These names are normally ommitted from the
656 656 search.
657 657
658 658 -i/-c: make the pattern case insensitive/sensitive. If neither of
659 659 these options is given, the default is read from your ipythonrc
660 660 file. The option name which sets this value is
661 661 'wildcards_case_sensitive'. If this option is not specified in your
662 662 ipythonrc file, IPython's internal default is to do a case sensitive
663 663 search.
664 664
665 665 -e/-s NAMESPACE: exclude/search a given namespace. The pattern you
666 666 specifiy can be searched in any of the following namespaces:
667 667 'builtin', 'user', 'user_global','internal', 'alias', where
668 668 'builtin' and 'user' are the search defaults. Note that you should
669 669 not use quotes when specifying namespaces.
670 670
671 671 'Builtin' contains the python module builtin, 'user' contains all
672 672 user data, 'alias' only contain the shell aliases and no python
673 673 objects, 'internal' contains objects used by IPython. The
674 674 'user_global' namespace is only used by embedded IPython instances,
675 675 and it contains module-level globals. You can add namespaces to the
676 676 search with -s or exclude them with -e (these options can be given
677 677 more than once).
678 678
679 679 Examples:
680 680
681 681 %psearch a* -> objects beginning with an a
682 682 %psearch -e builtin a* -> objects NOT in the builtin space starting in a
683 683 %psearch a* function -> all functions beginning with an a
684 684 %psearch re.e* -> objects beginning with an e in module re
685 685 %psearch r*.e* -> objects that start with e in modules starting in r
686 686 %psearch r*.* string -> all strings in modules beginning with r
687 687
688 688 Case sensitve search:
689 689
690 690 %psearch -c a* list all object beginning with lower case a
691 691
692 692 Show objects beginning with a single _:
693 693
694 694 %psearch -a _* list objects beginning with a single underscore"""
695 695 try:
696 696 parameter_s = parameter_s.encode('ascii')
697 697 except UnicodeEncodeError:
698 698 print 'Python identifiers can only contain ascii characters.'
699 699 return
700 700
701 701 # default namespaces to be searched
702 702 def_search = ['user','builtin']
703 703
704 704 # Process options/args
705 705 opts,args = self.parse_options(parameter_s,'cias:e:',list_all=True)
706 706 opt = opts.get
707 707 shell = self.shell
708 708 psearch = shell.inspector.psearch
709 709
710 710 # select case options
711 711 if opts.has_key('i'):
712 712 ignore_case = True
713 713 elif opts.has_key('c'):
714 714 ignore_case = False
715 715 else:
716 716 ignore_case = not shell.wildcards_case_sensitive
717 717
718 718 # Build list of namespaces to search from user options
719 719 def_search.extend(opt('s',[]))
720 720 ns_exclude = ns_exclude=opt('e',[])
721 721 ns_search = [nm for nm in def_search if nm not in ns_exclude]
722 722
723 723 # Call the actual search
724 724 try:
725 725 psearch(args,shell.ns_table,ns_search,
726 726 show_all=opt('a'),ignore_case=ignore_case)
727 727 except:
728 728 shell.showtraceback()
729 729
730 730 def magic_who_ls(self, parameter_s=''):
731 731 """Return a sorted list of all interactive variables.
732 732
733 733 If arguments are given, only variables of types matching these
734 734 arguments are returned."""
735 735
736 736 user_ns = self.shell.user_ns
737 737 internal_ns = self.shell.internal_ns
738 738 user_ns_hidden = self.shell.user_ns_hidden
739 739 out = [ i for i in user_ns
740 740 if not i.startswith('_') \
741 741 and not (i in internal_ns or i in user_ns_hidden) ]
742 742
743 743 typelist = parameter_s.split()
744 744 if typelist:
745 745 typeset = set(typelist)
746 746 out = [i for i in out if type(i).__name__ in typeset]
747 747
748 748 out.sort()
749 749 return out
750 750
751 751 def magic_who(self, parameter_s=''):
752 752 """Print all interactive variables, with some minimal formatting.
753 753
754 754 If any arguments are given, only variables whose type matches one of
755 755 these are printed. For example:
756 756
757 757 %who function str
758 758
759 759 will only list functions and strings, excluding all other types of
760 760 variables. To find the proper type names, simply use type(var) at a
761 761 command line to see how python prints type names. For example:
762 762
763 763 In [1]: type('hello')\\
764 764 Out[1]: <type 'str'>
765 765
766 766 indicates that the type name for strings is 'str'.
767 767
768 768 %who always excludes executed names loaded through your configuration
769 769 file and things which are internal to IPython.
770 770
771 771 This is deliberate, as typically you may load many modules and the
772 772 purpose of %who is to show you only what you've manually defined."""
773 773
774 774 varlist = self.magic_who_ls(parameter_s)
775 775 if not varlist:
776 776 if parameter_s:
777 777 print 'No variables match your requested type.'
778 778 else:
779 779 print 'Interactive namespace is empty.'
780 780 return
781 781
782 782 # if we have variables, move on...
783 783 count = 0
784 784 for i in varlist:
785 785 print i+'\t',
786 786 count += 1
787 787 if count > 8:
788 788 count = 0
789 789 print
790 790 print
791 791
792 792 def magic_whos(self, parameter_s=''):
793 793 """Like %who, but gives some extra information about each variable.
794 794
795 795 The same type filtering of %who can be applied here.
796 796
797 797 For all variables, the type is printed. Additionally it prints:
798 798
799 799 - For {},[],(): their length.
800 800
801 801 - For numpy and Numeric arrays, a summary with shape, number of
802 802 elements, typecode and size in memory.
803 803
804 804 - Everything else: a string representation, snipping their middle if
805 805 too long."""
806 806
807 807 varnames = self.magic_who_ls(parameter_s)
808 808 if not varnames:
809 809 if parameter_s:
810 810 print 'No variables match your requested type.'
811 811 else:
812 812 print 'Interactive namespace is empty.'
813 813 return
814 814
815 815 # if we have variables, move on...
816 816
817 817 # for these types, show len() instead of data:
818 818 seq_types = [types.DictType,types.ListType,types.TupleType]
819 819
820 820 # for numpy/Numeric arrays, display summary info
821 821 try:
822 822 import numpy
823 823 except ImportError:
824 824 ndarray_type = None
825 825 else:
826 826 ndarray_type = numpy.ndarray.__name__
827 827 try:
828 828 import Numeric
829 829 except ImportError:
830 830 array_type = None
831 831 else:
832 832 array_type = Numeric.ArrayType.__name__
833 833
834 834 # Find all variable names and types so we can figure out column sizes
835 835 def get_vars(i):
836 836 return self.shell.user_ns[i]
837 837
838 838 # some types are well known and can be shorter
839 839 abbrevs = {'IPython.core.macro.Macro' : 'Macro'}
840 840 def type_name(v):
841 841 tn = type(v).__name__
842 842 return abbrevs.get(tn,tn)
843 843
844 844 varlist = map(get_vars,varnames)
845 845
846 846 typelist = []
847 847 for vv in varlist:
848 848 tt = type_name(vv)
849 849
850 850 if tt=='instance':
851 851 typelist.append( abbrevs.get(str(vv.__class__),
852 852 str(vv.__class__)))
853 853 else:
854 854 typelist.append(tt)
855 855
856 856 # column labels and # of spaces as separator
857 857 varlabel = 'Variable'
858 858 typelabel = 'Type'
859 859 datalabel = 'Data/Info'
860 860 colsep = 3
861 861 # variable format strings
862 862 vformat = "$vname.ljust(varwidth)$vtype.ljust(typewidth)"
863 863 vfmt_short = '$vstr[:25]<...>$vstr[-25:]'
864 864 aformat = "%s: %s elems, type `%s`, %s bytes"
865 865 # find the size of the columns to format the output nicely
866 866 varwidth = max(max(map(len,varnames)), len(varlabel)) + colsep
867 867 typewidth = max(max(map(len,typelist)), len(typelabel)) + colsep
868 868 # table header
869 869 print varlabel.ljust(varwidth) + typelabel.ljust(typewidth) + \
870 870 ' '+datalabel+'\n' + '-'*(varwidth+typewidth+len(datalabel)+1)
871 871 # and the table itself
872 872 kb = 1024
873 873 Mb = 1048576 # kb**2
874 874 for vname,var,vtype in zip(varnames,varlist,typelist):
875 875 print itpl(vformat),
876 876 if vtype in seq_types:
877 877 print len(var)
878 878 elif vtype in [array_type,ndarray_type]:
879 879 vshape = str(var.shape).replace(',','').replace(' ','x')[1:-1]
880 880 if vtype==ndarray_type:
881 881 # numpy
882 882 vsize = var.size
883 883 vbytes = vsize*var.itemsize
884 884 vdtype = var.dtype
885 885 else:
886 886 # Numeric
887 887 vsize = Numeric.size(var)
888 888 vbytes = vsize*var.itemsize()
889 889 vdtype = var.typecode()
890 890
891 891 if vbytes < 100000:
892 892 print aformat % (vshape,vsize,vdtype,vbytes)
893 893 else:
894 894 print aformat % (vshape,vsize,vdtype,vbytes),
895 895 if vbytes < Mb:
896 896 print '(%s kb)' % (vbytes/kb,)
897 897 else:
898 898 print '(%s Mb)' % (vbytes/Mb,)
899 899 else:
900 900 try:
901 901 vstr = str(var)
902 902 except UnicodeEncodeError:
903 903 vstr = unicode(var).encode(sys.getdefaultencoding(),
904 904 'backslashreplace')
905 905 vstr = vstr.replace('\n','\\n')
906 906 if len(vstr) < 50:
907 907 print vstr
908 908 else:
909 909 printpl(vfmt_short)
910 910
911 911 def magic_reset(self, parameter_s=''):
912 912 """Resets the namespace by removing all names defined by the user.
913 913
914 914 Input/Output history are left around in case you need them.
915 915
916 916 Parameters
917 917 ----------
918 918 -y : force reset without asking for confirmation.
919 919
920 920 Examples
921 921 --------
922 922 In [6]: a = 1
923 923
924 924 In [7]: a
925 925 Out[7]: 1
926 926
927 927 In [8]: 'a' in _ip.user_ns
928 928 Out[8]: True
929 929
930 930 In [9]: %reset -f
931 931
932 932 In [10]: 'a' in _ip.user_ns
933 933 Out[10]: False
934 934 """
935 935
936 936 if parameter_s == '-f':
937 937 ans = True
938 938 else:
939 939 ans = self.shell.ask_yes_no(
940 940 "Once deleted, variables cannot be recovered. Proceed (y/[n])? ")
941 941 if not ans:
942 942 print 'Nothing done.'
943 943 return
944 944 user_ns = self.shell.user_ns
945 945 for i in self.magic_who_ls():
946 946 del(user_ns[i])
947 947
948 948 # Also flush the private list of module references kept for script
949 949 # execution protection
950 950 self.shell.clear_main_mod_cache()
951 951
952 952 def magic_reset_selective(self, parameter_s=''):
953 953 """Resets the namespace by removing names defined by the user.
954 954
955 955 Input/Output history are left around in case you need them.
956 956
957 957 %reset_selective [-f] regex
958 958
959 959 No action is taken if regex is not included
960 960
961 961 Options
962 962 -f : force reset without asking for confirmation.
963 963
964 964 Examples
965 965 --------
966 966
967 967 We first fully reset the namespace so your output looks identical to
968 968 this example for pedagogical reasons; in practice you do not need a
969 969 full reset.
970 970
971 971 In [1]: %reset -f
972 972
973 973 Now, with a clean namespace we can make a few variables and use
974 974 %reset_selective to only delete names that match our regexp:
975 975
976 976 In [2]: a=1; b=2; c=3; b1m=4; b2m=5; b3m=6; b4m=7; b2s=8
977 977
978 978 In [3]: who_ls
979 979 Out[3]: ['a', 'b', 'b1m', 'b2m', 'b2s', 'b3m', 'b4m', 'c']
980 980
981 981 In [4]: %reset_selective -f b[2-3]m
982 982
983 983 In [5]: who_ls
984 984 Out[5]: ['a', 'b', 'b1m', 'b2s', 'b4m', 'c']
985 985
986 986 In [6]: %reset_selective -f d
987 987
988 988 In [7]: who_ls
989 989 Out[7]: ['a', 'b', 'b1m', 'b2s', 'b4m', 'c']
990 990
991 991 In [8]: %reset_selective -f c
992 992
993 993 In [9]: who_ls
994 994 Out[9]: ['a', 'b', 'b1m', 'b2s', 'b4m']
995 995
996 996 In [10]: %reset_selective -f b
997 997
998 998 In [11]: who_ls
999 999 Out[11]: ['a']
1000 1000 """
1001 1001
1002 1002 opts, regex = self.parse_options(parameter_s,'f')
1003 1003
1004 1004 if opts.has_key('f'):
1005 1005 ans = True
1006 1006 else:
1007 1007 ans = self.shell.ask_yes_no(
1008 1008 "Once deleted, variables cannot be recovered. Proceed (y/[n])? ")
1009 1009 if not ans:
1010 1010 print 'Nothing done.'
1011 1011 return
1012 1012 user_ns = self.shell.user_ns
1013 1013 if not regex:
1014 1014 print 'No regex pattern specified. Nothing done.'
1015 1015 return
1016 1016 else:
1017 1017 try:
1018 1018 m = re.compile(regex)
1019 1019 except TypeError:
1020 1020 raise TypeError('regex must be a string or compiled pattern')
1021 1021 for i in self.magic_who_ls():
1022 1022 if m.search(i):
1023 1023 del(user_ns[i])
1024 1024
1025 1025 def magic_logstart(self,parameter_s=''):
1026 1026 """Start logging anywhere in a session.
1027 1027
1028 1028 %logstart [-o|-r|-t] [log_name [log_mode]]
1029 1029
1030 1030 If no name is given, it defaults to a file named 'ipython_log.py' in your
1031 1031 current directory, in 'rotate' mode (see below).
1032 1032
1033 1033 '%logstart name' saves to file 'name' in 'backup' mode. It saves your
1034 1034 history up to that point and then continues logging.
1035 1035
1036 1036 %logstart takes a second optional parameter: logging mode. This can be one
1037 1037 of (note that the modes are given unquoted):\\
1038 1038 append: well, that says it.\\
1039 1039 backup: rename (if exists) to name~ and start name.\\
1040 1040 global: single logfile in your home dir, appended to.\\
1041 1041 over : overwrite existing log.\\
1042 1042 rotate: create rotating logs name.1~, name.2~, etc.
1043 1043
1044 1044 Options:
1045 1045
1046 1046 -o: log also IPython's output. In this mode, all commands which
1047 1047 generate an Out[NN] prompt are recorded to the logfile, right after
1048 1048 their corresponding input line. The output lines are always
1049 1049 prepended with a '#[Out]# ' marker, so that the log remains valid
1050 1050 Python code.
1051 1051
1052 1052 Since this marker is always the same, filtering only the output from
1053 1053 a log is very easy, using for example a simple awk call:
1054 1054
1055 1055 awk -F'#\\[Out\\]# ' '{if($2) {print $2}}' ipython_log.py
1056 1056
1057 1057 -r: log 'raw' input. Normally, IPython's logs contain the processed
1058 1058 input, so that user lines are logged in their final form, converted
1059 1059 into valid Python. For example, %Exit is logged as
1060 1060 '_ip.magic("Exit"). If the -r flag is given, all input is logged
1061 1061 exactly as typed, with no transformations applied.
1062 1062
1063 1063 -t: put timestamps before each input line logged (these are put in
1064 1064 comments)."""
1065 1065
1066 1066 opts,par = self.parse_options(parameter_s,'ort')
1067 1067 log_output = 'o' in opts
1068 1068 log_raw_input = 'r' in opts
1069 1069 timestamp = 't' in opts
1070 1070
1071 1071 logger = self.shell.logger
1072 1072
1073 1073 # if no args are given, the defaults set in the logger constructor by
1074 1074 # ipytohn remain valid
1075 1075 if par:
1076 1076 try:
1077 1077 logfname,logmode = par.split()
1078 1078 except:
1079 1079 logfname = par
1080 1080 logmode = 'backup'
1081 1081 else:
1082 1082 logfname = logger.logfname
1083 1083 logmode = logger.logmode
1084 1084 # put logfname into rc struct as if it had been called on the command
1085 1085 # line, so it ends up saved in the log header Save it in case we need
1086 1086 # to restore it...
1087 1087 old_logfile = self.shell.logfile
1088 1088 if logfname:
1089 1089 logfname = os.path.expanduser(logfname)
1090 1090 self.shell.logfile = logfname
1091 1091
1092 1092 loghead = '# IPython log file\n\n'
1093 1093 try:
1094 1094 started = logger.logstart(logfname,loghead,logmode,
1095 1095 log_output,timestamp,log_raw_input)
1096 1096 except:
1097 1097 self.shell.logfile = old_logfile
1098 1098 warn("Couldn't start log: %s" % sys.exc_info()[1])
1099 1099 else:
1100 1100 # log input history up to this point, optionally interleaving
1101 1101 # output if requested
1102 1102
1103 1103 if timestamp:
1104 1104 # disable timestamping for the previous history, since we've
1105 1105 # lost those already (no time machine here).
1106 1106 logger.timestamp = False
1107 1107
1108 1108 if log_raw_input:
1109 1109 input_hist = self.shell.history_manager.input_hist_raw
1110 1110 else:
1111 1111 input_hist = self.shell.history_manager.input_hist_parsed
1112 1112
1113 1113 if log_output:
1114 1114 log_write = logger.log_write
1115 1115 output_hist = self.shell.history_manager.output_hist
1116 1116 for n in range(1,len(input_hist)-1):
1117 1117 log_write(input_hist[n].rstrip())
1118 1118 if n in output_hist:
1119 1119 log_write(repr(output_hist[n]),'output')
1120 1120 else:
1121 1121 logger.log_write(''.join(input_hist[1:]))
1122 1122 if timestamp:
1123 1123 # re-enable timestamping
1124 1124 logger.timestamp = True
1125 1125
1126 1126 print ('Activating auto-logging. '
1127 1127 'Current session state plus future input saved.')
1128 1128 logger.logstate()
1129 1129
1130 1130 def magic_logstop(self,parameter_s=''):
1131 1131 """Fully stop logging and close log file.
1132 1132
1133 1133 In order to start logging again, a new %logstart call needs to be made,
1134 1134 possibly (though not necessarily) with a new filename, mode and other
1135 1135 options."""
1136 1136 self.logger.logstop()
1137 1137
1138 1138 def magic_logoff(self,parameter_s=''):
1139 1139 """Temporarily stop logging.
1140 1140
1141 1141 You must have previously started logging."""
1142 1142 self.shell.logger.switch_log(0)
1143 1143
1144 1144 def magic_logon(self,parameter_s=''):
1145 1145 """Restart logging.
1146 1146
1147 1147 This function is for restarting logging which you've temporarily
1148 1148 stopped with %logoff. For starting logging for the first time, you
1149 1149 must use the %logstart function, which allows you to specify an
1150 1150 optional log filename."""
1151 1151
1152 1152 self.shell.logger.switch_log(1)
1153 1153
1154 1154 def magic_logstate(self,parameter_s=''):
1155 1155 """Print the status of the logging system."""
1156 1156
1157 1157 self.shell.logger.logstate()
1158 1158
1159 1159 def magic_pdb(self, parameter_s=''):
1160 1160 """Control the automatic calling of the pdb interactive debugger.
1161 1161
1162 1162 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
1163 1163 argument it works as a toggle.
1164 1164
1165 1165 When an exception is triggered, IPython can optionally call the
1166 1166 interactive pdb debugger after the traceback printout. %pdb toggles
1167 1167 this feature on and off.
1168 1168
1169 1169 The initial state of this feature is set in your ipythonrc
1170 1170 configuration file (the variable is called 'pdb').
1171 1171
1172 1172 If you want to just activate the debugger AFTER an exception has fired,
1173 1173 without having to type '%pdb on' and rerunning your code, you can use
1174 1174 the %debug magic."""
1175 1175
1176 1176 par = parameter_s.strip().lower()
1177 1177
1178 1178 if par:
1179 1179 try:
1180 1180 new_pdb = {'off':0,'0':0,'on':1,'1':1}[par]
1181 1181 except KeyError:
1182 1182 print ('Incorrect argument. Use on/1, off/0, '
1183 1183 'or nothing for a toggle.')
1184 1184 return
1185 1185 else:
1186 1186 # toggle
1187 1187 new_pdb = not self.shell.call_pdb
1188 1188
1189 1189 # set on the shell
1190 1190 self.shell.call_pdb = new_pdb
1191 1191 print 'Automatic pdb calling has been turned',on_off(new_pdb)
1192 1192
1193 1193 def magic_debug(self, parameter_s=''):
1194 1194 """Activate the interactive debugger in post-mortem mode.
1195 1195
1196 1196 If an exception has just occurred, this lets you inspect its stack
1197 1197 frames interactively. Note that this will always work only on the last
1198 1198 traceback that occurred, so you must call this quickly after an
1199 1199 exception that you wish to inspect has fired, because if another one
1200 1200 occurs, it clobbers the previous one.
1201 1201
1202 1202 If you want IPython to automatically do this on every exception, see
1203 1203 the %pdb magic for more details.
1204 1204 """
1205 1205 self.shell.debugger(force=True)
1206 1206
1207 1207 @testdec.skip_doctest
1208 1208 def magic_prun(self, parameter_s ='',user_mode=1,
1209 1209 opts=None,arg_lst=None,prog_ns=None):
1210 1210
1211 1211 """Run a statement through the python code profiler.
1212 1212
1213 1213 Usage:
1214 1214 %prun [options] statement
1215 1215
1216 1216 The given statement (which doesn't require quote marks) is run via the
1217 1217 python profiler in a manner similar to the profile.run() function.
1218 1218 Namespaces are internally managed to work correctly; profile.run
1219 1219 cannot be used in IPython because it makes certain assumptions about
1220 1220 namespaces which do not hold under IPython.
1221 1221
1222 1222 Options:
1223 1223
1224 1224 -l <limit>: you can place restrictions on what or how much of the
1225 1225 profile gets printed. The limit value can be:
1226 1226
1227 1227 * A string: only information for function names containing this string
1228 1228 is printed.
1229 1229
1230 1230 * An integer: only these many lines are printed.
1231 1231
1232 1232 * A float (between 0 and 1): this fraction of the report is printed
1233 1233 (for example, use a limit of 0.4 to see the topmost 40% only).
1234 1234
1235 1235 You can combine several limits with repeated use of the option. For
1236 1236 example, '-l __init__ -l 5' will print only the topmost 5 lines of
1237 1237 information about class constructors.
1238 1238
1239 1239 -r: return the pstats.Stats object generated by the profiling. This
1240 1240 object has all the information about the profile in it, and you can
1241 1241 later use it for further analysis or in other functions.
1242 1242
1243 1243 -s <key>: sort profile by given key. You can provide more than one key
1244 1244 by using the option several times: '-s key1 -s key2 -s key3...'. The
1245 1245 default sorting key is 'time'.
1246 1246
1247 1247 The following is copied verbatim from the profile documentation
1248 1248 referenced below:
1249 1249
1250 1250 When more than one key is provided, additional keys are used as
1251 1251 secondary criteria when the there is equality in all keys selected
1252 1252 before them.
1253 1253
1254 1254 Abbreviations can be used for any key names, as long as the
1255 1255 abbreviation is unambiguous. The following are the keys currently
1256 1256 defined:
1257 1257
1258 1258 Valid Arg Meaning
1259 1259 "calls" call count
1260 1260 "cumulative" cumulative time
1261 1261 "file" file name
1262 1262 "module" file name
1263 1263 "pcalls" primitive call count
1264 1264 "line" line number
1265 1265 "name" function name
1266 1266 "nfl" name/file/line
1267 1267 "stdname" standard name
1268 1268 "time" internal time
1269 1269
1270 1270 Note that all sorts on statistics are in descending order (placing
1271 1271 most time consuming items first), where as name, file, and line number
1272 1272 searches are in ascending order (i.e., alphabetical). The subtle
1273 1273 distinction between "nfl" and "stdname" is that the standard name is a
1274 1274 sort of the name as printed, which means that the embedded line
1275 1275 numbers get compared in an odd way. For example, lines 3, 20, and 40
1276 1276 would (if the file names were the same) appear in the string order
1277 1277 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
1278 1278 line numbers. In fact, sort_stats("nfl") is the same as
1279 1279 sort_stats("name", "file", "line").
1280 1280
1281 1281 -T <filename>: save profile results as shown on screen to a text
1282 1282 file. The profile is still shown on screen.
1283 1283
1284 1284 -D <filename>: save (via dump_stats) profile statistics to given
1285 1285 filename. This data is in a format understod by the pstats module, and
1286 1286 is generated by a call to the dump_stats() method of profile
1287 1287 objects. The profile is still shown on screen.
1288 1288
1289 1289 If you want to run complete programs under the profiler's control, use
1290 1290 '%run -p [prof_opts] filename.py [args to program]' where prof_opts
1291 1291 contains profiler specific options as described here.
1292 1292
1293 1293 You can read the complete documentation for the profile module with::
1294 1294
1295 1295 In [1]: import profile; profile.help()
1296 1296 """
1297 1297
1298 1298 opts_def = Struct(D=[''],l=[],s=['time'],T=[''])
1299 1299 # protect user quote marks
1300 1300 parameter_s = parameter_s.replace('"',r'\"').replace("'",r"\'")
1301 1301
1302 1302 if user_mode: # regular user call
1303 1303 opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:',
1304 1304 list_all=1)
1305 1305 namespace = self.shell.user_ns
1306 1306 else: # called to run a program by %run -p
1307 1307 try:
1308 1308 filename = get_py_filename(arg_lst[0])
1309 1309 except IOError,msg:
1310 1310 error(msg)
1311 1311 return
1312 1312
1313 1313 arg_str = 'execfile(filename,prog_ns)'
1314 1314 namespace = locals()
1315 1315
1316 1316 opts.merge(opts_def)
1317 1317
1318 1318 prof = profile.Profile()
1319 1319 try:
1320 1320 prof = prof.runctx(arg_str,namespace,namespace)
1321 1321 sys_exit = ''
1322 1322 except SystemExit:
1323 1323 sys_exit = """*** SystemExit exception caught in code being profiled."""
1324 1324
1325 1325 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
1326 1326
1327 1327 lims = opts.l
1328 1328 if lims:
1329 1329 lims = [] # rebuild lims with ints/floats/strings
1330 1330 for lim in opts.l:
1331 1331 try:
1332 1332 lims.append(int(lim))
1333 1333 except ValueError:
1334 1334 try:
1335 1335 lims.append(float(lim))
1336 1336 except ValueError:
1337 1337 lims.append(lim)
1338 1338
1339 1339 # Trap output.
1340 1340 stdout_trap = StringIO()
1341 1341
1342 1342 if hasattr(stats,'stream'):
1343 1343 # In newer versions of python, the stats object has a 'stream'
1344 1344 # attribute to write into.
1345 1345 stats.stream = stdout_trap
1346 1346 stats.print_stats(*lims)
1347 1347 else:
1348 1348 # For older versions, we manually redirect stdout during printing
1349 1349 sys_stdout = sys.stdout
1350 1350 try:
1351 1351 sys.stdout = stdout_trap
1352 1352 stats.print_stats(*lims)
1353 1353 finally:
1354 1354 sys.stdout = sys_stdout
1355 1355
1356 1356 output = stdout_trap.getvalue()
1357 1357 output = output.rstrip()
1358 1358
1359 1359 page.page(output)
1360 1360 print sys_exit,
1361 1361
1362 1362 dump_file = opts.D[0]
1363 1363 text_file = opts.T[0]
1364 1364 if dump_file:
1365 1365 prof.dump_stats(dump_file)
1366 1366 print '\n*** Profile stats marshalled to file',\
1367 1367 `dump_file`+'.',sys_exit
1368 1368 if text_file:
1369 1369 pfile = file(text_file,'w')
1370 1370 pfile.write(output)
1371 1371 pfile.close()
1372 1372 print '\n*** Profile printout saved to text file',\
1373 1373 `text_file`+'.',sys_exit
1374 1374
1375 1375 if opts.has_key('r'):
1376 1376 return stats
1377 1377 else:
1378 1378 return None
1379 1379
1380 1380 @testdec.skip_doctest
1381 1381 def magic_run(self, parameter_s ='',runner=None,
1382 1382 file_finder=get_py_filename):
1383 1383 """Run the named file inside IPython as a program.
1384 1384
1385 1385 Usage:\\
1386 1386 %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args]
1387 1387
1388 1388 Parameters after the filename are passed as command-line arguments to
1389 1389 the program (put in sys.argv). Then, control returns to IPython's
1390 1390 prompt.
1391 1391
1392 1392 This is similar to running at a system prompt:\\
1393 1393 $ python file args\\
1394 1394 but with the advantage of giving you IPython's tracebacks, and of
1395 1395 loading all variables into your interactive namespace for further use
1396 1396 (unless -p is used, see below).
1397 1397
1398 1398 The file is executed in a namespace initially consisting only of
1399 1399 __name__=='__main__' and sys.argv constructed as indicated. It thus
1400 1400 sees its environment as if it were being run as a stand-alone program
1401 1401 (except for sharing global objects such as previously imported
1402 1402 modules). But after execution, the IPython interactive namespace gets
1403 1403 updated with all variables defined in the program (except for __name__
1404 1404 and sys.argv). This allows for very convenient loading of code for
1405 1405 interactive work, while giving each program a 'clean sheet' to run in.
1406 1406
1407 1407 Options:
1408 1408
1409 1409 -n: __name__ is NOT set to '__main__', but to the running file's name
1410 1410 without extension (as python does under import). This allows running
1411 1411 scripts and reloading the definitions in them without calling code
1412 1412 protected by an ' if __name__ == "__main__" ' clause.
1413 1413
1414 1414 -i: run the file in IPython's namespace instead of an empty one. This
1415 1415 is useful if you are experimenting with code written in a text editor
1416 1416 which depends on variables defined interactively.
1417 1417
1418 1418 -e: ignore sys.exit() calls or SystemExit exceptions in the script
1419 1419 being run. This is particularly useful if IPython is being used to
1420 1420 run unittests, which always exit with a sys.exit() call. In such
1421 1421 cases you are interested in the output of the test results, not in
1422 1422 seeing a traceback of the unittest module.
1423 1423
1424 1424 -t: print timing information at the end of the run. IPython will give
1425 1425 you an estimated CPU time consumption for your script, which under
1426 1426 Unix uses the resource module to avoid the wraparound problems of
1427 1427 time.clock(). Under Unix, an estimate of time spent on system tasks
1428 1428 is also given (for Windows platforms this is reported as 0.0).
1429 1429
1430 1430 If -t is given, an additional -N<N> option can be given, where <N>
1431 1431 must be an integer indicating how many times you want the script to
1432 1432 run. The final timing report will include total and per run results.
1433 1433
1434 1434 For example (testing the script uniq_stable.py):
1435 1435
1436 1436 In [1]: run -t uniq_stable
1437 1437
1438 1438 IPython CPU timings (estimated):\\
1439 1439 User : 0.19597 s.\\
1440 1440 System: 0.0 s.\\
1441 1441
1442 1442 In [2]: run -t -N5 uniq_stable
1443 1443
1444 1444 IPython CPU timings (estimated):\\
1445 1445 Total runs performed: 5\\
1446 1446 Times : Total Per run\\
1447 1447 User : 0.910862 s, 0.1821724 s.\\
1448 1448 System: 0.0 s, 0.0 s.
1449 1449
1450 1450 -d: run your program under the control of pdb, the Python debugger.
1451 1451 This allows you to execute your program step by step, watch variables,
1452 1452 etc. Internally, what IPython does is similar to calling:
1453 1453
1454 1454 pdb.run('execfile("YOURFILENAME")')
1455 1455
1456 1456 with a breakpoint set on line 1 of your file. You can change the line
1457 1457 number for this automatic breakpoint to be <N> by using the -bN option
1458 1458 (where N must be an integer). For example:
1459 1459
1460 1460 %run -d -b40 myscript
1461 1461
1462 1462 will set the first breakpoint at line 40 in myscript.py. Note that
1463 1463 the first breakpoint must be set on a line which actually does
1464 1464 something (not a comment or docstring) for it to stop execution.
1465 1465
1466 1466 When the pdb debugger starts, you will see a (Pdb) prompt. You must
1467 1467 first enter 'c' (without qoutes) to start execution up to the first
1468 1468 breakpoint.
1469 1469
1470 1470 Entering 'help' gives information about the use of the debugger. You
1471 1471 can easily see pdb's full documentation with "import pdb;pdb.help()"
1472 1472 at a prompt.
1473 1473
1474 1474 -p: run program under the control of the Python profiler module (which
1475 1475 prints a detailed report of execution times, function calls, etc).
1476 1476
1477 1477 You can pass other options after -p which affect the behavior of the
1478 1478 profiler itself. See the docs for %prun for details.
1479 1479
1480 1480 In this mode, the program's variables do NOT propagate back to the
1481 1481 IPython interactive namespace (because they remain in the namespace
1482 1482 where the profiler executes them).
1483 1483
1484 1484 Internally this triggers a call to %prun, see its documentation for
1485 1485 details on the options available specifically for profiling.
1486 1486
1487 1487 There is one special usage for which the text above doesn't apply:
1488 1488 if the filename ends with .ipy, the file is run as ipython script,
1489 1489 just as if the commands were written on IPython prompt.
1490 1490 """
1491 1491
1492 1492 # get arguments and set sys.argv for program to be run.
1493 1493 opts,arg_lst = self.parse_options(parameter_s,'nidtN:b:pD:l:rs:T:e',
1494 1494 mode='list',list_all=1)
1495 1495
1496 1496 try:
1497 1497 filename = file_finder(arg_lst[0])
1498 1498 except IndexError:
1499 1499 warn('you must provide at least a filename.')
1500 1500 print '\n%run:\n',oinspect.getdoc(self.magic_run)
1501 1501 return
1502 1502 except IOError,msg:
1503 1503 error(msg)
1504 1504 return
1505 1505
1506 1506 if filename.lower().endswith('.ipy'):
1507 1507 self.shell.safe_execfile_ipy(filename)
1508 1508 return
1509 1509
1510 1510 # Control the response to exit() calls made by the script being run
1511 1511 exit_ignore = opts.has_key('e')
1512 1512
1513 1513 # Make sure that the running script gets a proper sys.argv as if it
1514 1514 # were run from a system shell.
1515 1515 save_argv = sys.argv # save it for later restoring
1516 1516 sys.argv = [filename]+ arg_lst[1:] # put in the proper filename
1517 1517
1518 1518 if opts.has_key('i'):
1519 1519 # Run in user's interactive namespace
1520 1520 prog_ns = self.shell.user_ns
1521 1521 __name__save = self.shell.user_ns['__name__']
1522 1522 prog_ns['__name__'] = '__main__'
1523 1523 main_mod = self.shell.new_main_mod(prog_ns)
1524 1524 else:
1525 1525 # Run in a fresh, empty namespace
1526 1526 if opts.has_key('n'):
1527 1527 name = os.path.splitext(os.path.basename(filename))[0]
1528 1528 else:
1529 1529 name = '__main__'
1530 1530
1531 1531 main_mod = self.shell.new_main_mod()
1532 1532 prog_ns = main_mod.__dict__
1533 1533 prog_ns['__name__'] = name
1534 1534
1535 1535 # Since '%run foo' emulates 'python foo.py' at the cmd line, we must
1536 1536 # set the __file__ global in the script's namespace
1537 1537 prog_ns['__file__'] = filename
1538 1538
1539 1539 # pickle fix. See interactiveshell for an explanation. But we need to make sure
1540 1540 # that, if we overwrite __main__, we replace it at the end
1541 1541 main_mod_name = prog_ns['__name__']
1542 1542
1543 1543 if main_mod_name == '__main__':
1544 1544 restore_main = sys.modules['__main__']
1545 1545 else:
1546 1546 restore_main = False
1547 1547
1548 1548 # This needs to be undone at the end to prevent holding references to
1549 1549 # every single object ever created.
1550 1550 sys.modules[main_mod_name] = main_mod
1551 1551
1552 1552 stats = None
1553 1553 try:
1554 1554 self.shell.save_history()
1555 1555
1556 1556 if opts.has_key('p'):
1557 1557 stats = self.magic_prun('',0,opts,arg_lst,prog_ns)
1558 1558 else:
1559 1559 if opts.has_key('d'):
1560 1560 deb = debugger.Pdb(self.shell.colors)
1561 1561 # reset Breakpoint state, which is moronically kept
1562 1562 # in a class
1563 1563 bdb.Breakpoint.next = 1
1564 1564 bdb.Breakpoint.bplist = {}
1565 1565 bdb.Breakpoint.bpbynumber = [None]
1566 1566 # Set an initial breakpoint to stop execution
1567 1567 maxtries = 10
1568 1568 bp = int(opts.get('b',[1])[0])
1569 1569 checkline = deb.checkline(filename,bp)
1570 1570 if not checkline:
1571 1571 for bp in range(bp+1,bp+maxtries+1):
1572 1572 if deb.checkline(filename,bp):
1573 1573 break
1574 1574 else:
1575 1575 msg = ("\nI failed to find a valid line to set "
1576 1576 "a breakpoint\n"
1577 1577 "after trying up to line: %s.\n"
1578 1578 "Please set a valid breakpoint manually "
1579 1579 "with the -b option." % bp)
1580 1580 error(msg)
1581 1581 return
1582 1582 # if we find a good linenumber, set the breakpoint
1583 1583 deb.do_break('%s:%s' % (filename,bp))
1584 1584 # Start file run
1585 1585 print "NOTE: Enter 'c' at the",
1586 1586 print "%s prompt to start your script." % deb.prompt
1587 1587 try:
1588 1588 deb.run('execfile("%s")' % filename,prog_ns)
1589 1589
1590 1590 except:
1591 1591 etype, value, tb = sys.exc_info()
1592 1592 # Skip three frames in the traceback: the %run one,
1593 1593 # one inside bdb.py, and the command-line typed by the
1594 1594 # user (run by exec in pdb itself).
1595 1595 self.shell.InteractiveTB(etype,value,tb,tb_offset=3)
1596 1596 else:
1597 1597 if runner is None:
1598 1598 runner = self.shell.safe_execfile
1599 1599 if opts.has_key('t'):
1600 1600 # timed execution
1601 1601 try:
1602 1602 nruns = int(opts['N'][0])
1603 1603 if nruns < 1:
1604 1604 error('Number of runs must be >=1')
1605 1605 return
1606 1606 except (KeyError):
1607 1607 nruns = 1
1608 1608 if nruns == 1:
1609 1609 t0 = clock2()
1610 1610 runner(filename,prog_ns,prog_ns,
1611 1611 exit_ignore=exit_ignore)
1612 1612 t1 = clock2()
1613 1613 t_usr = t1[0]-t0[0]
1614 1614 t_sys = t1[1]-t0[1]
1615 1615 print "\nIPython CPU timings (estimated):"
1616 1616 print " User : %10s s." % t_usr
1617 1617 print " System: %10s s." % t_sys
1618 1618 else:
1619 1619 runs = range(nruns)
1620 1620 t0 = clock2()
1621 1621 for nr in runs:
1622 1622 runner(filename,prog_ns,prog_ns,
1623 1623 exit_ignore=exit_ignore)
1624 1624 t1 = clock2()
1625 1625 t_usr = t1[0]-t0[0]
1626 1626 t_sys = t1[1]-t0[1]
1627 1627 print "\nIPython CPU timings (estimated):"
1628 1628 print "Total runs performed:",nruns
1629 1629 print " Times : %10s %10s" % ('Total','Per run')
1630 1630 print " User : %10s s, %10s s." % (t_usr,t_usr/nruns)
1631 1631 print " System: %10s s, %10s s." % (t_sys,t_sys/nruns)
1632 1632
1633 1633 else:
1634 1634 # regular execution
1635 1635 runner(filename,prog_ns,prog_ns,exit_ignore=exit_ignore)
1636 1636
1637 1637 if opts.has_key('i'):
1638 1638 self.shell.user_ns['__name__'] = __name__save
1639 1639 else:
1640 1640 # The shell MUST hold a reference to prog_ns so after %run
1641 1641 # exits, the python deletion mechanism doesn't zero it out
1642 1642 # (leaving dangling references).
1643 1643 self.shell.cache_main_mod(prog_ns,filename)
1644 1644 # update IPython interactive namespace
1645 1645
1646 1646 # Some forms of read errors on the file may mean the
1647 1647 # __name__ key was never set; using pop we don't have to
1648 1648 # worry about a possible KeyError.
1649 1649 prog_ns.pop('__name__', None)
1650 1650
1651 1651 self.shell.user_ns.update(prog_ns)
1652 1652 finally:
1653 1653 # It's a bit of a mystery why, but __builtins__ can change from
1654 1654 # being a module to becoming a dict missing some key data after
1655 1655 # %run. As best I can see, this is NOT something IPython is doing
1656 1656 # at all, and similar problems have been reported before:
1657 1657 # http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-10/0188.html
1658 1658 # Since this seems to be done by the interpreter itself, the best
1659 1659 # we can do is to at least restore __builtins__ for the user on
1660 1660 # exit.
1661 1661 self.shell.user_ns['__builtins__'] = __builtin__
1662 1662
1663 1663 # Ensure key global structures are restored
1664 1664 sys.argv = save_argv
1665 1665 if restore_main:
1666 1666 sys.modules['__main__'] = restore_main
1667 1667 else:
1668 1668 # Remove from sys.modules the reference to main_mod we'd
1669 1669 # added. Otherwise it will trap references to objects
1670 1670 # contained therein.
1671 1671 del sys.modules[main_mod_name]
1672 1672
1673 1673 self.shell.reload_history()
1674 1674
1675 1675 return stats
1676 1676
1677 1677 @testdec.skip_doctest
1678 1678 def magic_timeit(self, parameter_s =''):
1679 1679 """Time execution of a Python statement or expression
1680 1680
1681 1681 Usage:\\
1682 1682 %timeit [-n<N> -r<R> [-t|-c]] statement
1683 1683
1684 1684 Time execution of a Python statement or expression using the timeit
1685 1685 module.
1686 1686
1687 1687 Options:
1688 1688 -n<N>: execute the given statement <N> times in a loop. If this value
1689 1689 is not given, a fitting value is chosen.
1690 1690
1691 1691 -r<R>: repeat the loop iteration <R> times and take the best result.
1692 1692 Default: 3
1693 1693
1694 1694 -t: use time.time to measure the time, which is the default on Unix.
1695 1695 This function measures wall time.
1696 1696
1697 1697 -c: use time.clock to measure the time, which is the default on
1698 1698 Windows and measures wall time. On Unix, resource.getrusage is used
1699 1699 instead and returns the CPU user time.
1700 1700
1701 1701 -p<P>: use a precision of <P> digits to display the timing result.
1702 1702 Default: 3
1703 1703
1704 1704
1705 1705 Examples:
1706 1706
1707 1707 In [1]: %timeit pass
1708 1708 10000000 loops, best of 3: 53.3 ns per loop
1709 1709
1710 1710 In [2]: u = None
1711 1711
1712 1712 In [3]: %timeit u is None
1713 1713 10000000 loops, best of 3: 184 ns per loop
1714 1714
1715 1715 In [4]: %timeit -r 4 u == None
1716 1716 1000000 loops, best of 4: 242 ns per loop
1717 1717
1718 1718 In [5]: import time
1719 1719
1720 1720 In [6]: %timeit -n1 time.sleep(2)
1721 1721 1 loops, best of 3: 2 s per loop
1722 1722
1723 1723
1724 1724 The times reported by %timeit will be slightly higher than those
1725 1725 reported by the timeit.py script when variables are accessed. This is
1726 1726 due to the fact that %timeit executes the statement in the namespace
1727 1727 of the shell, compared with timeit.py, which uses a single setup
1728 1728 statement to import function or create variables. Generally, the bias
1729 1729 does not matter as long as results from timeit.py are not mixed with
1730 1730 those from %timeit."""
1731 1731
1732 1732 import timeit
1733 1733 import math
1734 1734
1735 1735 # XXX: Unfortunately the unicode 'micro' symbol can cause problems in
1736 1736 # certain terminals. Until we figure out a robust way of
1737 1737 # auto-detecting if the terminal can deal with it, use plain 'us' for
1738 1738 # microseconds. I am really NOT happy about disabling the proper
1739 1739 # 'micro' prefix, but crashing is worse... If anyone knows what the
1740 1740 # right solution for this is, I'm all ears...
1741 1741 #
1742 1742 # Note: using
1743 1743 #
1744 1744 # s = u'\xb5'
1745 1745 # s.encode(sys.getdefaultencoding())
1746 1746 #
1747 1747 # is not sufficient, as I've seen terminals where that fails but
1748 1748 # print s
1749 1749 #
1750 1750 # succeeds
1751 1751 #
1752 1752 # See bug: https://bugs.launchpad.net/ipython/+bug/348466
1753 1753
1754 1754 #units = [u"s", u"ms",u'\xb5',"ns"]
1755 1755 units = [u"s", u"ms",u'us',"ns"]
1756 1756
1757 1757 scaling = [1, 1e3, 1e6, 1e9]
1758 1758
1759 1759 opts, stmt = self.parse_options(parameter_s,'n:r:tcp:',
1760 1760 posix=False)
1761 1761 if stmt == "":
1762 1762 return
1763 1763 timefunc = timeit.default_timer
1764 1764 number = int(getattr(opts, "n", 0))
1765 1765 repeat = int(getattr(opts, "r", timeit.default_repeat))
1766 1766 precision = int(getattr(opts, "p", 3))
1767 1767 if hasattr(opts, "t"):
1768 1768 timefunc = time.time
1769 1769 if hasattr(opts, "c"):
1770 1770 timefunc = clock
1771 1771
1772 1772 timer = timeit.Timer(timer=timefunc)
1773 1773 # this code has tight coupling to the inner workings of timeit.Timer,
1774 1774 # but is there a better way to achieve that the code stmt has access
1775 1775 # to the shell namespace?
1776 1776
1777 1777 src = timeit.template % {'stmt': timeit.reindent(stmt, 8),
1778 1778 'setup': "pass"}
1779 1779 # Track compilation time so it can be reported if too long
1780 1780 # Minimum time above which compilation time will be reported
1781 1781 tc_min = 0.1
1782 1782
1783 1783 t0 = clock()
1784 1784 code = compile(src, "<magic-timeit>", "exec")
1785 1785 tc = clock()-t0
1786 1786
1787 1787 ns = {}
1788 1788 exec code in self.shell.user_ns, ns
1789 1789 timer.inner = ns["inner"]
1790 1790
1791 1791 if number == 0:
1792 1792 # determine number so that 0.2 <= total time < 2.0
1793 1793 number = 1
1794 1794 for i in range(1, 10):
1795 1795 if timer.timeit(number) >= 0.2:
1796 1796 break
1797 1797 number *= 10
1798 1798
1799 1799 best = min(timer.repeat(repeat, number)) / number
1800 1800
1801 1801 if best > 0.0 and best < 1000.0:
1802 1802 order = min(-int(math.floor(math.log10(best)) // 3), 3)
1803 1803 elif best >= 1000.0:
1804 1804 order = 0
1805 1805 else:
1806 1806 order = 3
1807 1807 print u"%d loops, best of %d: %.*g %s per loop" % (number, repeat,
1808 1808 precision,
1809 1809 best * scaling[order],
1810 1810 units[order])
1811 1811 if tc > tc_min:
1812 1812 print "Compiler time: %.2f s" % tc
1813 1813
1814 1814 @testdec.skip_doctest
1815 1815 def magic_time(self,parameter_s = ''):
1816 1816 """Time execution of a Python statement or expression.
1817 1817
1818 1818 The CPU and wall clock times are printed, and the value of the
1819 1819 expression (if any) is returned. Note that under Win32, system time
1820 1820 is always reported as 0, since it can not be measured.
1821 1821
1822 1822 This function provides very basic timing functionality. In Python
1823 1823 2.3, the timeit module offers more control and sophistication, so this
1824 1824 could be rewritten to use it (patches welcome).
1825 1825
1826 1826 Some examples:
1827 1827
1828 1828 In [1]: time 2**128
1829 1829 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1830 1830 Wall time: 0.00
1831 1831 Out[1]: 340282366920938463463374607431768211456L
1832 1832
1833 1833 In [2]: n = 1000000
1834 1834
1835 1835 In [3]: time sum(range(n))
1836 1836 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
1837 1837 Wall time: 1.37
1838 1838 Out[3]: 499999500000L
1839 1839
1840 1840 In [4]: time print 'hello world'
1841 1841 hello world
1842 1842 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1843 1843 Wall time: 0.00
1844 1844
1845 1845 Note that the time needed by Python to compile the given expression
1846 1846 will be reported if it is more than 0.1s. In this example, the
1847 1847 actual exponentiation is done by Python at compilation time, so while
1848 1848 the expression can take a noticeable amount of time to compute, that
1849 1849 time is purely due to the compilation:
1850 1850
1851 1851 In [5]: time 3**9999;
1852 1852 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1853 1853 Wall time: 0.00 s
1854 1854
1855 1855 In [6]: time 3**999999;
1856 1856 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1857 1857 Wall time: 0.00 s
1858 1858 Compiler : 0.78 s
1859 1859 """
1860 1860
1861 1861 # fail immediately if the given expression can't be compiled
1862 1862
1863 1863 expr = self.shell.prefilter(parameter_s,False)
1864 1864
1865 1865 # Minimum time above which compilation time will be reported
1866 1866 tc_min = 0.1
1867 1867
1868 1868 try:
1869 1869 mode = 'eval'
1870 1870 t0 = clock()
1871 1871 code = compile(expr,'<timed eval>',mode)
1872 1872 tc = clock()-t0
1873 1873 except SyntaxError:
1874 1874 mode = 'exec'
1875 1875 t0 = clock()
1876 1876 code = compile(expr,'<timed exec>',mode)
1877 1877 tc = clock()-t0
1878 1878 # skew measurement as little as possible
1879 1879 glob = self.shell.user_ns
1880 1880 clk = clock2
1881 1881 wtime = time.time
1882 1882 # time execution
1883 1883 wall_st = wtime()
1884 1884 if mode=='eval':
1885 1885 st = clk()
1886 1886 out = eval(code,glob)
1887 1887 end = clk()
1888 1888 else:
1889 1889 st = clk()
1890 1890 exec code in glob
1891 1891 end = clk()
1892 1892 out = None
1893 1893 wall_end = wtime()
1894 1894 # Compute actual times and report
1895 1895 wall_time = wall_end-wall_st
1896 1896 cpu_user = end[0]-st[0]
1897 1897 cpu_sys = end[1]-st[1]
1898 1898 cpu_tot = cpu_user+cpu_sys
1899 1899 print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \
1900 1900 (cpu_user,cpu_sys,cpu_tot)
1901 1901 print "Wall time: %.2f s" % wall_time
1902 1902 if tc > tc_min:
1903 1903 print "Compiler : %.2f s" % tc
1904 1904 return out
1905 1905
1906 1906 @testdec.skip_doctest
1907 1907 def magic_macro(self,parameter_s = ''):
1908 1908 """Define a set of input lines as a macro for future re-execution.
1909 1909
1910 1910 Usage:\\
1911 1911 %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ...
1912 1912
1913 1913 Options:
1914 1914
1915 1915 -r: use 'raw' input. By default, the 'processed' history is used,
1916 1916 so that magics are loaded in their transformed version to valid
1917 1917 Python. If this option is given, the raw input as typed as the
1918 1918 command line is used instead.
1919 1919
1920 1920 This will define a global variable called `name` which is a string
1921 1921 made of joining the slices and lines you specify (n1,n2,... numbers
1922 1922 above) from your input history into a single string. This variable
1923 1923 acts like an automatic function which re-executes those lines as if
1924 1924 you had typed them. You just type 'name' at the prompt and the code
1925 1925 executes.
1926 1926
1927 1927 The notation for indicating number ranges is: n1-n2 means 'use line
1928 1928 numbers n1,...n2' (the endpoint is included). That is, '5-7' means
1929 1929 using the lines numbered 5,6 and 7.
1930 1930
1931 1931 Note: as a 'hidden' feature, you can also use traditional python slice
1932 1932 notation, where N:M means numbers N through M-1.
1933 1933
1934 1934 For example, if your history contains (%hist prints it):
1935 1935
1936 1936 44: x=1
1937 1937 45: y=3
1938 1938 46: z=x+y
1939 1939 47: print x
1940 1940 48: a=5
1941 1941 49: print 'x',x,'y',y
1942 1942
1943 1943 you can create a macro with lines 44 through 47 (included) and line 49
1944 1944 called my_macro with:
1945 1945
1946 1946 In [55]: %macro my_macro 44-47 49
1947 1947
1948 1948 Now, typing `my_macro` (without quotes) will re-execute all this code
1949 1949 in one pass.
1950 1950
1951 1951 You don't need to give the line-numbers in order, and any given line
1952 1952 number can appear multiple times. You can assemble macros with any
1953 1953 lines from your input history in any order.
1954 1954
1955 1955 The macro is a simple object which holds its value in an attribute,
1956 1956 but IPython's display system checks for macros and executes them as
1957 1957 code instead of printing them when you type their name.
1958 1958
1959 1959 You can view a macro's contents by explicitly printing it with:
1960 1960
1961 1961 'print macro_name'.
1962 1962
1963 1963 For one-off cases which DON'T contain magic function calls in them you
1964 1964 can obtain similar results by explicitly executing slices from your
1965 1965 input history with:
1966 1966
1967 1967 In [60]: exec In[44:48]+In[49]"""
1968 1968
1969 1969 opts,args = self.parse_options(parameter_s,'r',mode='list')
1970 1970 if not args:
1971 1971 macs = [k for k,v in self.shell.user_ns.items() if isinstance(v, Macro)]
1972 1972 macs.sort()
1973 1973 return macs
1974 1974 if len(args) == 1:
1975 1975 raise UsageError(
1976 1976 "%macro insufficient args; usage '%macro name n1-n2 n3-4...")
1977 1977 name,ranges = args[0], args[1:]
1978 1978
1979 1979 #print 'rng',ranges # dbg
1980 1980 lines = self.extract_input_slices(ranges,opts.has_key('r'))
1981 1981 macro = Macro(lines)
1982 1982 self.shell.define_macro(name, macro)
1983 1983 print 'Macro `%s` created. To execute, type its name (without quotes).' % name
1984 1984 print 'Macro contents:'
1985 1985 print macro,
1986 1986
1987 1987 def magic_save(self,parameter_s = ''):
1988 1988 """Save a set of lines to a given filename.
1989 1989
1990 1990 Usage:\\
1991 1991 %save [options] filename n1-n2 n3-n4 ... n5 .. n6 ...
1992 1992
1993 1993 Options:
1994 1994
1995 1995 -r: use 'raw' input. By default, the 'processed' history is used,
1996 1996 so that magics are loaded in their transformed version to valid
1997 1997 Python. If this option is given, the raw input as typed as the
1998 1998 command line is used instead.
1999 1999
2000 2000 This function uses the same syntax as %macro for line extraction, but
2001 2001 instead of creating a macro it saves the resulting string to the
2002 2002 filename you specify.
2003 2003
2004 2004 It adds a '.py' extension to the file if you don't do so yourself, and
2005 2005 it asks for confirmation before overwriting existing files."""
2006 2006
2007 2007 opts,args = self.parse_options(parameter_s,'r',mode='list')
2008 2008 fname,ranges = args[0], args[1:]
2009 2009 if not fname.endswith('.py'):
2010 2010 fname += '.py'
2011 2011 if os.path.isfile(fname):
2012 2012 ans = raw_input('File `%s` exists. Overwrite (y/[N])? ' % fname)
2013 2013 if ans.lower() not in ['y','yes']:
2014 2014 print 'Operation cancelled.'
2015 2015 return
2016 2016 cmds = ''.join(self.extract_input_slices(ranges,opts.has_key('r')))
2017 2017 f = file(fname,'w')
2018 2018 f.write(cmds)
2019 2019 f.close()
2020 2020 print 'The following commands were written to file `%s`:' % fname
2021 2021 print cmds
2022 2022
2023 2023 def _edit_macro(self,mname,macro):
2024 2024 """open an editor with the macro data in a file"""
2025 2025 filename = self.shell.mktempfile(macro.value)
2026 2026 self.shell.hooks.editor(filename)
2027 2027
2028 2028 # and make a new macro object, to replace the old one
2029 2029 mfile = open(filename)
2030 2030 mvalue = mfile.read()
2031 2031 mfile.close()
2032 2032 self.shell.user_ns[mname] = Macro(mvalue)
2033 2033
2034 2034 def magic_ed(self,parameter_s=''):
2035 2035 """Alias to %edit."""
2036 2036 return self.magic_edit(parameter_s)
2037 2037
2038 2038 @testdec.skip_doctest
2039 2039 def magic_edit(self,parameter_s='',last_call=['','']):
2040 2040 """Bring up an editor and execute the resulting code.
2041 2041
2042 2042 Usage:
2043 2043 %edit [options] [args]
2044 2044
2045 2045 %edit runs IPython's editor hook. The default version of this hook is
2046 2046 set to call the __IPYTHON__.rc.editor command. This is read from your
2047 2047 environment variable $EDITOR. If this isn't found, it will default to
2048 2048 vi under Linux/Unix and to notepad under Windows. See the end of this
2049 2049 docstring for how to change the editor hook.
2050 2050
2051 2051 You can also set the value of this editor via the command line option
2052 2052 '-editor' or in your ipythonrc file. This is useful if you wish to use
2053 2053 specifically for IPython an editor different from your typical default
2054 2054 (and for Windows users who typically don't set environment variables).
2055 2055
2056 2056 This command allows you to conveniently edit multi-line code right in
2057 2057 your IPython session.
2058 2058
2059 2059 If called without arguments, %edit opens up an empty editor with a
2060 2060 temporary file and will execute the contents of this file when you
2061 2061 close it (don't forget to save it!).
2062 2062
2063 2063
2064 2064 Options:
2065 2065
2066 2066 -n <number>: open the editor at a specified line number. By default,
2067 2067 the IPython editor hook uses the unix syntax 'editor +N filename', but
2068 2068 you can configure this by providing your own modified hook if your
2069 2069 favorite editor supports line-number specifications with a different
2070 2070 syntax.
2071 2071
2072 2072 -p: this will call the editor with the same data as the previous time
2073 2073 it was used, regardless of how long ago (in your current session) it
2074 2074 was.
2075 2075
2076 2076 -r: use 'raw' input. This option only applies to input taken from the
2077 2077 user's history. By default, the 'processed' history is used, so that
2078 2078 magics are loaded in their transformed version to valid Python. If
2079 2079 this option is given, the raw input as typed as the command line is
2080 2080 used instead. When you exit the editor, it will be executed by
2081 2081 IPython's own processor.
2082 2082
2083 2083 -x: do not execute the edited code immediately upon exit. This is
2084 2084 mainly useful if you are editing programs which need to be called with
2085 2085 command line arguments, which you can then do using %run.
2086 2086
2087 2087
2088 2088 Arguments:
2089 2089
2090 2090 If arguments are given, the following possibilites exist:
2091 2091
2092 2092 - The arguments are numbers or pairs of colon-separated numbers (like
2093 2093 1 4:8 9). These are interpreted as lines of previous input to be
2094 2094 loaded into the editor. The syntax is the same of the %macro command.
2095 2095
2096 2096 - If the argument doesn't start with a number, it is evaluated as a
2097 2097 variable and its contents loaded into the editor. You can thus edit
2098 2098 any string which contains python code (including the result of
2099 2099 previous edits).
2100 2100
2101 2101 - If the argument is the name of an object (other than a string),
2102 2102 IPython will try to locate the file where it was defined and open the
2103 2103 editor at the point where it is defined. You can use `%edit function`
2104 2104 to load an editor exactly at the point where 'function' is defined,
2105 2105 edit it and have the file be executed automatically.
2106 2106
2107 2107 If the object is a macro (see %macro for details), this opens up your
2108 2108 specified editor with a temporary file containing the macro's data.
2109 2109 Upon exit, the macro is reloaded with the contents of the file.
2110 2110
2111 2111 Note: opening at an exact line is only supported under Unix, and some
2112 2112 editors (like kedit and gedit up to Gnome 2.8) do not understand the
2113 2113 '+NUMBER' parameter necessary for this feature. Good editors like
2114 2114 (X)Emacs, vi, jed, pico and joe all do.
2115 2115
2116 2116 - If the argument is not found as a variable, IPython will look for a
2117 2117 file with that name (adding .py if necessary) and load it into the
2118 2118 editor. It will execute its contents with execfile() when you exit,
2119 2119 loading any code in the file into your interactive namespace.
2120 2120
2121 2121 After executing your code, %edit will return as output the code you
2122 2122 typed in the editor (except when it was an existing file). This way
2123 2123 you can reload the code in further invocations of %edit as a variable,
2124 2124 via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of
2125 2125 the output.
2126 2126
2127 2127 Note that %edit is also available through the alias %ed.
2128 2128
2129 2129 This is an example of creating a simple function inside the editor and
2130 2130 then modifying it. First, start up the editor:
2131 2131
2132 2132 In [1]: ed
2133 2133 Editing... done. Executing edited code...
2134 2134 Out[1]: 'def foo():n print "foo() was defined in an editing session"n'
2135 2135
2136 2136 We can then call the function foo():
2137 2137
2138 2138 In [2]: foo()
2139 2139 foo() was defined in an editing session
2140 2140
2141 2141 Now we edit foo. IPython automatically loads the editor with the
2142 2142 (temporary) file where foo() was previously defined:
2143 2143
2144 2144 In [3]: ed foo
2145 2145 Editing... done. Executing edited code...
2146 2146
2147 2147 And if we call foo() again we get the modified version:
2148 2148
2149 2149 In [4]: foo()
2150 2150 foo() has now been changed!
2151 2151
2152 2152 Here is an example of how to edit a code snippet successive
2153 2153 times. First we call the editor:
2154 2154
2155 2155 In [5]: ed
2156 2156 Editing... done. Executing edited code...
2157 2157 hello
2158 2158 Out[5]: "print 'hello'n"
2159 2159
2160 2160 Now we call it again with the previous output (stored in _):
2161 2161
2162 2162 In [6]: ed _
2163 2163 Editing... done. Executing edited code...
2164 2164 hello world
2165 2165 Out[6]: "print 'hello world'n"
2166 2166
2167 2167 Now we call it with the output #8 (stored in _8, also as Out[8]):
2168 2168
2169 2169 In [7]: ed _8
2170 2170 Editing... done. Executing edited code...
2171 2171 hello again
2172 2172 Out[7]: "print 'hello again'n"
2173 2173
2174 2174
2175 2175 Changing the default editor hook:
2176 2176
2177 2177 If you wish to write your own editor hook, you can put it in a
2178 2178 configuration file which you load at startup time. The default hook
2179 2179 is defined in the IPython.core.hooks module, and you can use that as a
2180 2180 starting example for further modifications. That file also has
2181 2181 general instructions on how to set a new hook for use once you've
2182 2182 defined it."""
2183 2183
2184 2184 # FIXME: This function has become a convoluted mess. It needs a
2185 2185 # ground-up rewrite with clean, simple logic.
2186 2186
2187 2187 def make_filename(arg):
2188 2188 "Make a filename from the given args"
2189 2189 try:
2190 2190 filename = get_py_filename(arg)
2191 2191 except IOError:
2192 2192 if args.endswith('.py'):
2193 2193 filename = arg
2194 2194 else:
2195 2195 filename = None
2196 2196 return filename
2197 2197
2198 2198 # custom exceptions
2199 2199 class DataIsObject(Exception): pass
2200 2200
2201 2201 opts,args = self.parse_options(parameter_s,'prxn:')
2202 2202 # Set a few locals from the options for convenience:
2203 2203 opts_p = opts.has_key('p')
2204 2204 opts_r = opts.has_key('r')
2205 2205
2206 2206 # Default line number value
2207 2207 lineno = opts.get('n',None)
2208 2208
2209 2209 if opts_p:
2210 2210 args = '_%s' % last_call[0]
2211 2211 if not self.shell.user_ns.has_key(args):
2212 2212 args = last_call[1]
2213 2213
2214 2214 # use last_call to remember the state of the previous call, but don't
2215 2215 # let it be clobbered by successive '-p' calls.
2216 2216 try:
2217 2217 last_call[0] = self.shell.displayhook.prompt_count
2218 2218 if not opts_p:
2219 2219 last_call[1] = parameter_s
2220 2220 except:
2221 2221 pass
2222 2222
2223 2223 # by default this is done with temp files, except when the given
2224 2224 # arg is a filename
2225 2225 use_temp = 1
2226 2226
2227 2227 if re.match(r'\d',args):
2228 2228 # Mode where user specifies ranges of lines, like in %macro.
2229 2229 # This means that you can't edit files whose names begin with
2230 2230 # numbers this way. Tough.
2231 2231 ranges = args.split()
2232 2232 data = ''.join(self.extract_input_slices(ranges,opts_r))
2233 2233 elif args.endswith('.py'):
2234 2234 filename = make_filename(args)
2235 2235 data = ''
2236 2236 use_temp = 0
2237 2237 elif args:
2238 2238 try:
2239 2239 # Load the parameter given as a variable. If not a string,
2240 2240 # process it as an object instead (below)
2241 2241
2242 2242 #print '*** args',args,'type',type(args) # dbg
2243 2243 data = eval(args,self.shell.user_ns)
2244 2244 if not type(data) in StringTypes:
2245 2245 raise DataIsObject
2246 2246
2247 2247 except (NameError,SyntaxError):
2248 2248 # given argument is not a variable, try as a filename
2249 2249 filename = make_filename(args)
2250 2250 if filename is None:
2251 2251 warn("Argument given (%s) can't be found as a variable "
2252 2252 "or as a filename." % args)
2253 2253 return
2254 2254
2255 2255 data = ''
2256 2256 use_temp = 0
2257 2257 except DataIsObject:
2258 2258
2259 2259 # macros have a special edit function
2260 2260 if isinstance(data,Macro):
2261 2261 self._edit_macro(args,data)
2262 2262 return
2263 2263
2264 2264 # For objects, try to edit the file where they are defined
2265 2265 try:
2266 2266 filename = inspect.getabsfile(data)
2267 2267 if 'fakemodule' in filename.lower() and inspect.isclass(data):
2268 2268 # class created by %edit? Try to find source
2269 2269 # by looking for method definitions instead, the
2270 2270 # __module__ in those classes is FakeModule.
2271 2271 attrs = [getattr(data, aname) for aname in dir(data)]
2272 2272 for attr in attrs:
2273 2273 if not inspect.ismethod(attr):
2274 2274 continue
2275 2275 filename = inspect.getabsfile(attr)
2276 2276 if filename and 'fakemodule' not in filename.lower():
2277 2277 # change the attribute to be the edit target instead
2278 2278 data = attr
2279 2279 break
2280 2280
2281 2281 datafile = 1
2282 2282 except TypeError:
2283 2283 filename = make_filename(args)
2284 2284 datafile = 1
2285 2285 warn('Could not find file where `%s` is defined.\n'
2286 2286 'Opening a file named `%s`' % (args,filename))
2287 2287 # Now, make sure we can actually read the source (if it was in
2288 2288 # a temp file it's gone by now).
2289 2289 if datafile:
2290 2290 try:
2291 2291 if lineno is None:
2292 2292 lineno = inspect.getsourcelines(data)[1]
2293 2293 except IOError:
2294 2294 filename = make_filename(args)
2295 2295 if filename is None:
2296 2296 warn('The file `%s` where `%s` was defined cannot '
2297 2297 'be read.' % (filename,data))
2298 2298 return
2299 2299 use_temp = 0
2300 2300 else:
2301 2301 data = ''
2302 2302
2303 2303 if use_temp:
2304 2304 filename = self.shell.mktempfile(data)
2305 2305 print 'IPython will make a temporary file named:',filename
2306 2306
2307 2307 # do actual editing here
2308 2308 print 'Editing...',
2309 2309 sys.stdout.flush()
2310 2310 try:
2311 2311 # Quote filenames that may have spaces in them
2312 2312 if ' ' in filename:
2313 2313 filename = "%s" % filename
2314 2314 self.shell.hooks.editor(filename,lineno)
2315 2315 except TryNext:
2316 2316 warn('Could not open editor')
2317 2317 return
2318 2318
2319 2319 # XXX TODO: should this be generalized for all string vars?
2320 2320 # For now, this is special-cased to blocks created by cpaste
2321 2321 if args.strip() == 'pasted_block':
2322 2322 self.shell.user_ns['pasted_block'] = file_read(filename)
2323 2323
2324 2324 if opts.has_key('x'): # -x prevents actual execution
2325 2325 print
2326 2326 else:
2327 2327 print 'done. Executing edited code...'
2328 2328 if opts_r:
2329 2329 self.shell.run_cell(file_read(filename))
2330 2330 else:
2331 2331 self.shell.safe_execfile(filename,self.shell.user_ns,
2332 2332 self.shell.user_ns)
2333 2333
2334 2334
2335 2335 if use_temp:
2336 2336 try:
2337 2337 return open(filename).read()
2338 2338 except IOError,msg:
2339 2339 if msg.filename == filename:
2340 2340 warn('File not found. Did you forget to save?')
2341 2341 return
2342 2342 else:
2343 2343 self.shell.showtraceback()
2344 2344
2345 2345 def magic_xmode(self,parameter_s = ''):
2346 2346 """Switch modes for the exception handlers.
2347 2347
2348 2348 Valid modes: Plain, Context and Verbose.
2349 2349
2350 2350 If called without arguments, acts as a toggle."""
2351 2351
2352 2352 def xmode_switch_err(name):
2353 2353 warn('Error changing %s exception modes.\n%s' %
2354 2354 (name,sys.exc_info()[1]))
2355 2355
2356 2356 shell = self.shell
2357 2357 new_mode = parameter_s.strip().capitalize()
2358 2358 try:
2359 2359 shell.InteractiveTB.set_mode(mode=new_mode)
2360 2360 print 'Exception reporting mode:',shell.InteractiveTB.mode
2361 2361 except:
2362 2362 xmode_switch_err('user')
2363 2363
2364 2364 def magic_colors(self,parameter_s = ''):
2365 2365 """Switch color scheme for prompts, info system and exception handlers.
2366 2366
2367 2367 Currently implemented schemes: NoColor, Linux, LightBG.
2368 2368
2369 2369 Color scheme names are not case-sensitive."""
2370 2370
2371 2371 def color_switch_err(name):
2372 2372 warn('Error changing %s color schemes.\n%s' %
2373 2373 (name,sys.exc_info()[1]))
2374 2374
2375 2375
2376 2376 new_scheme = parameter_s.strip()
2377 2377 if not new_scheme:
2378 2378 raise UsageError(
2379 2379 "%colors: you must specify a color scheme. See '%colors?'")
2380 2380 return
2381 2381 # local shortcut
2382 2382 shell = self.shell
2383 2383
2384 2384 import IPython.utils.rlineimpl as readline
2385 2385
2386 2386 if not readline.have_readline and sys.platform == "win32":
2387 2387 msg = """\
2388 2388 Proper color support under MS Windows requires the pyreadline library.
2389 2389 You can find it at:
2390 2390 http://ipython.scipy.org/moin/PyReadline/Intro
2391 2391 Gary's readline needs the ctypes module, from:
2392 2392 http://starship.python.net/crew/theller/ctypes
2393 2393 (Note that ctypes is already part of Python versions 2.5 and newer).
2394 2394
2395 2395 Defaulting color scheme to 'NoColor'"""
2396 2396 new_scheme = 'NoColor'
2397 2397 warn(msg)
2398 2398
2399 2399 # readline option is 0
2400 2400 if not shell.has_readline:
2401 2401 new_scheme = 'NoColor'
2402 2402
2403 2403 # Set prompt colors
2404 2404 try:
2405 2405 shell.displayhook.set_colors(new_scheme)
2406 2406 except:
2407 2407 color_switch_err('prompt')
2408 2408 else:
2409 2409 shell.colors = \
2410 2410 shell.displayhook.color_table.active_scheme_name
2411 2411 # Set exception colors
2412 2412 try:
2413 2413 shell.InteractiveTB.set_colors(scheme = new_scheme)
2414 2414 shell.SyntaxTB.set_colors(scheme = new_scheme)
2415 2415 except:
2416 2416 color_switch_err('exception')
2417 2417
2418 2418 # Set info (for 'object?') colors
2419 2419 if shell.color_info:
2420 2420 try:
2421 2421 shell.inspector.set_active_scheme(new_scheme)
2422 2422 except:
2423 2423 color_switch_err('object inspector')
2424 2424 else:
2425 2425 shell.inspector.set_active_scheme('NoColor')
2426 2426
2427 2427 def magic_pprint(self, parameter_s=''):
2428 2428 """Toggle pretty printing on/off."""
2429 2429 ptformatter = self.shell.display_formatter.formatters['text/plain']
2430 2430 ptformatter.pprint = bool(1 - ptformatter.pprint)
2431 2431 print 'Pretty printing has been turned', \
2432 2432 ['OFF','ON'][ptformatter.pprint]
2433 2433
2434 2434 def magic_Exit(self, parameter_s=''):
2435 2435 """Exit IPython."""
2436 2436
2437 2437 self.shell.ask_exit()
2438 2438
2439 2439 # Add aliases as magics so all common forms work: exit, quit, Exit, Quit.
2440 2440 magic_exit = magic_quit = magic_Quit = magic_Exit
2441 2441
2442 2442 #......................................................................
2443 2443 # Functions to implement unix shell-type things
2444 2444
2445 2445 @testdec.skip_doctest
2446 2446 def magic_alias(self, parameter_s = ''):
2447 2447 """Define an alias for a system command.
2448 2448
2449 2449 '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd'
2450 2450
2451 2451 Then, typing 'alias_name params' will execute the system command 'cmd
2452 2452 params' (from your underlying operating system).
2453 2453
2454 2454 Aliases have lower precedence than magic functions and Python normal
2455 2455 variables, so if 'foo' is both a Python variable and an alias, the
2456 2456 alias can not be executed until 'del foo' removes the Python variable.
2457 2457
2458 2458 You can use the %l specifier in an alias definition to represent the
2459 2459 whole line when the alias is called. For example:
2460 2460
2461 2461 In [2]: alias bracket echo "Input in brackets: <%l>"
2462 2462 In [3]: bracket hello world
2463 2463 Input in brackets: <hello world>
2464 2464
2465 2465 You can also define aliases with parameters using %s specifiers (one
2466 2466 per parameter):
2467 2467
2468 2468 In [1]: alias parts echo first %s second %s
2469 2469 In [2]: %parts A B
2470 2470 first A second B
2471 2471 In [3]: %parts A
2472 2472 Incorrect number of arguments: 2 expected.
2473 2473 parts is an alias to: 'echo first %s second %s'
2474 2474
2475 2475 Note that %l and %s are mutually exclusive. You can only use one or
2476 2476 the other in your aliases.
2477 2477
2478 2478 Aliases expand Python variables just like system calls using ! or !!
2479 2479 do: all expressions prefixed with '$' get expanded. For details of
2480 2480 the semantic rules, see PEP-215:
2481 2481 http://www.python.org/peps/pep-0215.html. This is the library used by
2482 2482 IPython for variable expansion. If you want to access a true shell
2483 2483 variable, an extra $ is necessary to prevent its expansion by IPython:
2484 2484
2485 2485 In [6]: alias show echo
2486 2486 In [7]: PATH='A Python string'
2487 2487 In [8]: show $PATH
2488 2488 A Python string
2489 2489 In [9]: show $$PATH
2490 2490 /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:...
2491 2491
2492 2492 You can use the alias facility to acess all of $PATH. See the %rehash
2493 2493 and %rehashx functions, which automatically create aliases for the
2494 2494 contents of your $PATH.
2495 2495
2496 2496 If called with no parameters, %alias prints the current alias table."""
2497 2497
2498 2498 par = parameter_s.strip()
2499 2499 if not par:
2500 2500 stored = self.db.get('stored_aliases', {} )
2501 2501 aliases = sorted(self.shell.alias_manager.aliases)
2502 2502 # for k, v in stored:
2503 2503 # atab.append(k, v[0])
2504 2504
2505 2505 print "Total number of aliases:", len(aliases)
2506 2506 sys.stdout.flush()
2507 2507 return aliases
2508 2508
2509 2509 # Now try to define a new one
2510 2510 try:
2511 2511 alias,cmd = par.split(None, 1)
2512 2512 except:
2513 2513 print oinspect.getdoc(self.magic_alias)
2514 2514 else:
2515 2515 self.shell.alias_manager.soft_define_alias(alias, cmd)
2516 2516 # end magic_alias
2517 2517
2518 2518 def magic_unalias(self, parameter_s = ''):
2519 2519 """Remove an alias"""
2520 2520
2521 2521 aname = parameter_s.strip()
2522 2522 self.shell.alias_manager.undefine_alias(aname)
2523 2523 stored = self.db.get('stored_aliases', {} )
2524 2524 if aname in stored:
2525 2525 print "Removing %stored alias",aname
2526 2526 del stored[aname]
2527 2527 self.db['stored_aliases'] = stored
2528 2528
2529 2529 def magic_rehashx(self, parameter_s = ''):
2530 2530 """Update the alias table with all executable files in $PATH.
2531 2531
2532 2532 This version explicitly checks that every entry in $PATH is a file
2533 2533 with execute access (os.X_OK), so it is much slower than %rehash.
2534 2534
2535 2535 Under Windows, it checks executability as a match agains a
2536 2536 '|'-separated string of extensions, stored in the IPython config
2537 2537 variable win_exec_ext. This defaults to 'exe|com|bat'.
2538 2538
2539 2539 This function also resets the root module cache of module completer,
2540 2540 used on slow filesystems.
2541 2541 """
2542 2542 from IPython.core.alias import InvalidAliasError
2543 2543
2544 2544 # for the benefit of module completer in ipy_completers.py
2545 2545 del self.db['rootmodules']
2546 2546
2547 2547 path = [os.path.abspath(os.path.expanduser(p)) for p in
2548 2548 os.environ.get('PATH','').split(os.pathsep)]
2549 2549 path = filter(os.path.isdir,path)
2550 2550
2551 2551 syscmdlist = []
2552 2552 # Now define isexec in a cross platform manner.
2553 2553 if os.name == 'posix':
2554 2554 isexec = lambda fname:os.path.isfile(fname) and \
2555 2555 os.access(fname,os.X_OK)
2556 2556 else:
2557 2557 try:
2558 2558 winext = os.environ['pathext'].replace(';','|').replace('.','')
2559 2559 except KeyError:
2560 2560 winext = 'exe|com|bat|py'
2561 2561 if 'py' not in winext:
2562 2562 winext += '|py'
2563 2563 execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
2564 2564 isexec = lambda fname:os.path.isfile(fname) and execre.match(fname)
2565 2565 savedir = os.getcwd()
2566 2566
2567 2567 # Now walk the paths looking for executables to alias.
2568 2568 try:
2569 2569 # write the whole loop for posix/Windows so we don't have an if in
2570 2570 # the innermost part
2571 2571 if os.name == 'posix':
2572 2572 for pdir in path:
2573 2573 os.chdir(pdir)
2574 2574 for ff in os.listdir(pdir):
2575 2575 if isexec(ff):
2576 2576 try:
2577 2577 # Removes dots from the name since ipython
2578 2578 # will assume names with dots to be python.
2579 2579 self.shell.alias_manager.define_alias(
2580 2580 ff.replace('.',''), ff)
2581 2581 except InvalidAliasError:
2582 2582 pass
2583 2583 else:
2584 2584 syscmdlist.append(ff)
2585 2585 else:
2586 2586 no_alias = self.shell.alias_manager.no_alias
2587 2587 for pdir in path:
2588 2588 os.chdir(pdir)
2589 2589 for ff in os.listdir(pdir):
2590 2590 base, ext = os.path.splitext(ff)
2591 2591 if isexec(ff) and base.lower() not in no_alias:
2592 2592 if ext.lower() == '.exe':
2593 2593 ff = base
2594 2594 try:
2595 2595 # Removes dots from the name since ipython
2596 2596 # will assume names with dots to be python.
2597 2597 self.shell.alias_manager.define_alias(
2598 2598 base.lower().replace('.',''), ff)
2599 2599 except InvalidAliasError:
2600 2600 pass
2601 2601 syscmdlist.append(ff)
2602 2602 db = self.db
2603 2603 db['syscmdlist'] = syscmdlist
2604 2604 finally:
2605 2605 os.chdir(savedir)
2606 2606
2607 2607 def magic_pwd(self, parameter_s = ''):
2608 2608 """Return the current working directory path.
2609 2609
2610 Example
2611 -------
2612
2610 Examples
2611 --------
2613 2612 ::
2614 2613
2615 2614 In [9]: pwd
2616 2615 Out[9]: '/home/tsuser/sprint/ipython'
2617 2616 """
2618 2617 return os.getcwd()
2619 2618
2620 2619 def magic_cd(self, parameter_s=''):
2621 2620 """Change the current working directory.
2622 2621
2623 2622 This command automatically maintains an internal list of directories
2624 2623 you visit during your IPython session, in the variable _dh. The
2625 2624 command %dhist shows this history nicely formatted. You can also
2626 2625 do 'cd -<tab>' to see directory history conveniently.
2627 2626
2628 2627 Usage:
2629 2628
2630 2629 cd 'dir': changes to directory 'dir'.
2631 2630
2632 2631 cd -: changes to the last visited directory.
2633 2632
2634 2633 cd -<n>: changes to the n-th directory in the directory history.
2635 2634
2636 2635 cd --foo: change to directory that matches 'foo' in history
2637 2636
2638 2637 cd -b <bookmark_name>: jump to a bookmark set by %bookmark
2639 2638 (note: cd <bookmark_name> is enough if there is no
2640 2639 directory <bookmark_name>, but a bookmark with the name exists.)
2641 2640 'cd -b <tab>' allows you to tab-complete bookmark names.
2642 2641
2643 2642 Options:
2644 2643
2645 2644 -q: quiet. Do not print the working directory after the cd command is
2646 2645 executed. By default IPython's cd command does print this directory,
2647 2646 since the default prompts do not display path information.
2648 2647
2649 2648 Note that !cd doesn't work for this purpose because the shell where
2650 2649 !command runs is immediately discarded after executing 'command'.
2651 2650
2652 Example
2651 Examples
2653 2652 --------
2654
2655 2653 ::
2656 2654
2657 2655 In [10]: cd parent/child
2658 2656 /home/tsuser/parent/child
2659 2657 """
2660 2658
2661 2659 parameter_s = parameter_s.strip()
2662 2660 #bkms = self.shell.persist.get("bookmarks",{})
2663 2661
2664 2662 oldcwd = os.getcwd()
2665 2663 numcd = re.match(r'(-)(\d+)$',parameter_s)
2666 2664 # jump in directory history by number
2667 2665 if numcd:
2668 2666 nn = int(numcd.group(2))
2669 2667 try:
2670 2668 ps = self.shell.user_ns['_dh'][nn]
2671 2669 except IndexError:
2672 2670 print 'The requested directory does not exist in history.'
2673 2671 return
2674 2672 else:
2675 2673 opts = {}
2676 2674 elif parameter_s.startswith('--'):
2677 2675 ps = None
2678 2676 fallback = None
2679 2677 pat = parameter_s[2:]
2680 2678 dh = self.shell.user_ns['_dh']
2681 2679 # first search only by basename (last component)
2682 2680 for ent in reversed(dh):
2683 2681 if pat in os.path.basename(ent) and os.path.isdir(ent):
2684 2682 ps = ent
2685 2683 break
2686 2684
2687 2685 if fallback is None and pat in ent and os.path.isdir(ent):
2688 2686 fallback = ent
2689 2687
2690 2688 # if we have no last part match, pick the first full path match
2691 2689 if ps is None:
2692 2690 ps = fallback
2693 2691
2694 2692 if ps is None:
2695 2693 print "No matching entry in directory history"
2696 2694 return
2697 2695 else:
2698 2696 opts = {}
2699 2697
2700 2698
2701 2699 else:
2702 2700 #turn all non-space-escaping backslashes to slashes,
2703 2701 # for c:\windows\directory\names\
2704 2702 parameter_s = re.sub(r'\\(?! )','/', parameter_s)
2705 2703 opts,ps = self.parse_options(parameter_s,'qb',mode='string')
2706 2704 # jump to previous
2707 2705 if ps == '-':
2708 2706 try:
2709 2707 ps = self.shell.user_ns['_dh'][-2]
2710 2708 except IndexError:
2711 2709 raise UsageError('%cd -: No previous directory to change to.')
2712 2710 # jump to bookmark if needed
2713 2711 else:
2714 2712 if not os.path.isdir(ps) or opts.has_key('b'):
2715 2713 bkms = self.db.get('bookmarks', {})
2716 2714
2717 2715 if bkms.has_key(ps):
2718 2716 target = bkms[ps]
2719 2717 print '(bookmark:%s) -> %s' % (ps,target)
2720 2718 ps = target
2721 2719 else:
2722 2720 if opts.has_key('b'):
2723 2721 raise UsageError("Bookmark '%s' not found. "
2724 2722 "Use '%%bookmark -l' to see your bookmarks." % ps)
2725 2723
2726 2724 # at this point ps should point to the target dir
2727 2725 if ps:
2728 2726 try:
2729 2727 os.chdir(os.path.expanduser(ps))
2730 2728 if hasattr(self.shell, 'term_title') and self.shell.term_title:
2731 2729 set_term_title('IPython: ' + abbrev_cwd())
2732 2730 except OSError:
2733 2731 print sys.exc_info()[1]
2734 2732 else:
2735 2733 cwd = os.getcwd()
2736 2734 dhist = self.shell.user_ns['_dh']
2737 2735 if oldcwd != cwd:
2738 2736 dhist.append(cwd)
2739 2737 self.db['dhist'] = compress_dhist(dhist)[-100:]
2740 2738
2741 2739 else:
2742 2740 os.chdir(self.shell.home_dir)
2743 2741 if hasattr(self.shell, 'term_title') and self.shell.term_title:
2744 2742 set_term_title('IPython: ' + '~')
2745 2743 cwd = os.getcwd()
2746 2744 dhist = self.shell.user_ns['_dh']
2747 2745
2748 2746 if oldcwd != cwd:
2749 2747 dhist.append(cwd)
2750 2748 self.db['dhist'] = compress_dhist(dhist)[-100:]
2751 2749 if not 'q' in opts and self.shell.user_ns['_dh']:
2752 2750 print self.shell.user_ns['_dh'][-1]
2753 2751
2754 2752
2755 2753 def magic_env(self, parameter_s=''):
2756 2754 """List environment variables."""
2757 2755
2758 2756 return os.environ.data
2759 2757
2760 2758 def magic_pushd(self, parameter_s=''):
2761 2759 """Place the current dir on stack and change directory.
2762 2760
2763 2761 Usage:\\
2764 2762 %pushd ['dirname']
2765 2763 """
2766 2764
2767 2765 dir_s = self.shell.dir_stack
2768 2766 tgt = os.path.expanduser(parameter_s)
2769 2767 cwd = os.getcwd().replace(self.home_dir,'~')
2770 2768 if tgt:
2771 2769 self.magic_cd(parameter_s)
2772 2770 dir_s.insert(0,cwd)
2773 2771 return self.magic_dirs()
2774 2772
2775 2773 def magic_popd(self, parameter_s=''):
2776 2774 """Change to directory popped off the top of the stack.
2777 2775 """
2778 2776 if not self.shell.dir_stack:
2779 2777 raise UsageError("%popd on empty stack")
2780 2778 top = self.shell.dir_stack.pop(0)
2781 2779 self.magic_cd(top)
2782 2780 print "popd ->",top
2783 2781
2784 2782 def magic_dirs(self, parameter_s=''):
2785 2783 """Return the current directory stack."""
2786 2784
2787 2785 return self.shell.dir_stack
2788 2786
2789 2787 def magic_dhist(self, parameter_s=''):
2790 2788 """Print your history of visited directories.
2791 2789
2792 2790 %dhist -> print full history\\
2793 2791 %dhist n -> print last n entries only\\
2794 2792 %dhist n1 n2 -> print entries between n1 and n2 (n1 not included)\\
2795 2793
2796 2794 This history is automatically maintained by the %cd command, and
2797 2795 always available as the global list variable _dh. You can use %cd -<n>
2798 2796 to go to directory number <n>.
2799 2797
2800 2798 Note that most of time, you should view directory history by entering
2801 2799 cd -<TAB>.
2802 2800
2803 2801 """
2804 2802
2805 2803 dh = self.shell.user_ns['_dh']
2806 2804 if parameter_s:
2807 2805 try:
2808 2806 args = map(int,parameter_s.split())
2809 2807 except:
2810 2808 self.arg_err(Magic.magic_dhist)
2811 2809 return
2812 2810 if len(args) == 1:
2813 2811 ini,fin = max(len(dh)-(args[0]),0),len(dh)
2814 2812 elif len(args) == 2:
2815 2813 ini,fin = args
2816 2814 else:
2817 2815 self.arg_err(Magic.magic_dhist)
2818 2816 return
2819 2817 else:
2820 2818 ini,fin = 0,len(dh)
2821 2819 nlprint(dh,
2822 2820 header = 'Directory history (kept in _dh)',
2823 2821 start=ini,stop=fin)
2824 2822
2825 2823 @testdec.skip_doctest
2826 2824 def magic_sc(self, parameter_s=''):
2827 2825 """Shell capture - execute a shell command and capture its output.
2828 2826
2829 2827 DEPRECATED. Suboptimal, retained for backwards compatibility.
2830 2828
2831 2829 You should use the form 'var = !command' instead. Example:
2832 2830
2833 2831 "%sc -l myfiles = ls ~" should now be written as
2834 2832
2835 2833 "myfiles = !ls ~"
2836 2834
2837 2835 myfiles.s, myfiles.l and myfiles.n still apply as documented
2838 2836 below.
2839 2837
2840 2838 --
2841 2839 %sc [options] varname=command
2842 2840
2843 2841 IPython will run the given command using commands.getoutput(), and
2844 2842 will then update the user's interactive namespace with a variable
2845 2843 called varname, containing the value of the call. Your command can
2846 2844 contain shell wildcards, pipes, etc.
2847 2845
2848 2846 The '=' sign in the syntax is mandatory, and the variable name you
2849 2847 supply must follow Python's standard conventions for valid names.
2850 2848
2851 2849 (A special format without variable name exists for internal use)
2852 2850
2853 2851 Options:
2854 2852
2855 2853 -l: list output. Split the output on newlines into a list before
2856 2854 assigning it to the given variable. By default the output is stored
2857 2855 as a single string.
2858 2856
2859 2857 -v: verbose. Print the contents of the variable.
2860 2858
2861 2859 In most cases you should not need to split as a list, because the
2862 2860 returned value is a special type of string which can automatically
2863 2861 provide its contents either as a list (split on newlines) or as a
2864 2862 space-separated string. These are convenient, respectively, either
2865 2863 for sequential processing or to be passed to a shell command.
2866 2864
2867 2865 For example:
2868 2866
2869 2867 # all-random
2870 2868
2871 2869 # Capture into variable a
2872 2870 In [1]: sc a=ls *py
2873 2871
2874 2872 # a is a string with embedded newlines
2875 2873 In [2]: a
2876 2874 Out[2]: 'setup.py\\nwin32_manual_post_install.py'
2877 2875
2878 2876 # which can be seen as a list:
2879 2877 In [3]: a.l
2880 2878 Out[3]: ['setup.py', 'win32_manual_post_install.py']
2881 2879
2882 2880 # or as a whitespace-separated string:
2883 2881 In [4]: a.s
2884 2882 Out[4]: 'setup.py win32_manual_post_install.py'
2885 2883
2886 2884 # a.s is useful to pass as a single command line:
2887 2885 In [5]: !wc -l $a.s
2888 2886 146 setup.py
2889 2887 130 win32_manual_post_install.py
2890 2888 276 total
2891 2889
2892 2890 # while the list form is useful to loop over:
2893 2891 In [6]: for f in a.l:
2894 2892 ...: !wc -l $f
2895 2893 ...:
2896 2894 146 setup.py
2897 2895 130 win32_manual_post_install.py
2898 2896
2899 2897 Similiarly, the lists returned by the -l option are also special, in
2900 2898 the sense that you can equally invoke the .s attribute on them to
2901 2899 automatically get a whitespace-separated string from their contents:
2902 2900
2903 2901 In [7]: sc -l b=ls *py
2904 2902
2905 2903 In [8]: b
2906 2904 Out[8]: ['setup.py', 'win32_manual_post_install.py']
2907 2905
2908 2906 In [9]: b.s
2909 2907 Out[9]: 'setup.py win32_manual_post_install.py'
2910 2908
2911 2909 In summary, both the lists and strings used for ouptut capture have
2912 2910 the following special attributes:
2913 2911
2914 2912 .l (or .list) : value as list.
2915 2913 .n (or .nlstr): value as newline-separated string.
2916 2914 .s (or .spstr): value as space-separated string.
2917 2915 """
2918 2916
2919 2917 opts,args = self.parse_options(parameter_s,'lv')
2920 2918 # Try to get a variable name and command to run
2921 2919 try:
2922 2920 # the variable name must be obtained from the parse_options
2923 2921 # output, which uses shlex.split to strip options out.
2924 2922 var,_ = args.split('=',1)
2925 2923 var = var.strip()
2926 2924 # But the the command has to be extracted from the original input
2927 2925 # parameter_s, not on what parse_options returns, to avoid the
2928 2926 # quote stripping which shlex.split performs on it.
2929 2927 _,cmd = parameter_s.split('=',1)
2930 2928 except ValueError:
2931 2929 var,cmd = '',''
2932 2930 # If all looks ok, proceed
2933 2931 split = 'l' in opts
2934 2932 out = self.shell.getoutput(cmd, split=split)
2935 2933 if opts.has_key('v'):
2936 2934 print '%s ==\n%s' % (var,pformat(out))
2937 2935 if var:
2938 2936 self.shell.user_ns.update({var:out})
2939 2937 else:
2940 2938 return out
2941 2939
2942 2940 def magic_sx(self, parameter_s=''):
2943 2941 """Shell execute - run a shell command and capture its output.
2944 2942
2945 2943 %sx command
2946 2944
2947 2945 IPython will run the given command using commands.getoutput(), and
2948 2946 return the result formatted as a list (split on '\\n'). Since the
2949 2947 output is _returned_, it will be stored in ipython's regular output
2950 2948 cache Out[N] and in the '_N' automatic variables.
2951 2949
2952 2950 Notes:
2953 2951
2954 2952 1) If an input line begins with '!!', then %sx is automatically
2955 2953 invoked. That is, while:
2956 2954 !ls
2957 2955 causes ipython to simply issue system('ls'), typing
2958 2956 !!ls
2959 2957 is a shorthand equivalent to:
2960 2958 %sx ls
2961 2959
2962 2960 2) %sx differs from %sc in that %sx automatically splits into a list,
2963 2961 like '%sc -l'. The reason for this is to make it as easy as possible
2964 2962 to process line-oriented shell output via further python commands.
2965 2963 %sc is meant to provide much finer control, but requires more
2966 2964 typing.
2967 2965
2968 2966 3) Just like %sc -l, this is a list with special attributes:
2969 2967
2970 2968 .l (or .list) : value as list.
2971 2969 .n (or .nlstr): value as newline-separated string.
2972 2970 .s (or .spstr): value as whitespace-separated string.
2973 2971
2974 2972 This is very useful when trying to use such lists as arguments to
2975 2973 system commands."""
2976 2974
2977 2975 if parameter_s:
2978 2976 return self.shell.getoutput(parameter_s)
2979 2977
2980 2978 def magic_r(self, parameter_s=''):
2981 2979 """Repeat previous input.
2982 2980
2983 2981 Note: Consider using the more powerfull %rep instead!
2984 2982
2985 2983 If given an argument, repeats the previous command which starts with
2986 2984 the same string, otherwise it just repeats the previous input.
2987 2985
2988 2986 Shell escaped commands (with ! as first character) are not recognized
2989 2987 by this system, only pure python code and magic commands.
2990 2988 """
2991 2989
2992 2990 start = parameter_s.strip()
2993 2991 esc_magic = ESC_MAGIC
2994 2992 # Identify magic commands even if automagic is on (which means
2995 2993 # the in-memory version is different from that typed by the user).
2996 2994 if self.shell.automagic:
2997 2995 start_magic = esc_magic+start
2998 2996 else:
2999 2997 start_magic = start
3000 2998 # Look through the input history in reverse
3001 2999 for n in range(len(self.shell.history_manager.input_hist_parsed)-2,0,-1):
3002 3000 input = self.shell.history_manager.input_hist_parsed[n]
3003 3001 # skip plain 'r' lines so we don't recurse to infinity
3004 3002 if input != '_ip.magic("r")\n' and \
3005 3003 (input.startswith(start) or input.startswith(start_magic)):
3006 3004 #print 'match',`input` # dbg
3007 3005 print 'Executing:',input,
3008 3006 self.shell.run_cell(input)
3009 3007 return
3010 3008 print 'No previous input matching `%s` found.' % start
3011 3009
3012 3010
3013 3011 def magic_bookmark(self, parameter_s=''):
3014 3012 """Manage IPython's bookmark system.
3015 3013
3016 3014 %bookmark <name> - set bookmark to current dir
3017 3015 %bookmark <name> <dir> - set bookmark to <dir>
3018 3016 %bookmark -l - list all bookmarks
3019 3017 %bookmark -d <name> - remove bookmark
3020 3018 %bookmark -r - remove all bookmarks
3021 3019
3022 3020 You can later on access a bookmarked folder with:
3023 3021 %cd -b <name>
3024 3022 or simply '%cd <name>' if there is no directory called <name> AND
3025 3023 there is such a bookmark defined.
3026 3024
3027 3025 Your bookmarks persist through IPython sessions, but they are
3028 3026 associated with each profile."""
3029 3027
3030 3028 opts,args = self.parse_options(parameter_s,'drl',mode='list')
3031 3029 if len(args) > 2:
3032 3030 raise UsageError("%bookmark: too many arguments")
3033 3031
3034 3032 bkms = self.db.get('bookmarks',{})
3035 3033
3036 3034 if opts.has_key('d'):
3037 3035 try:
3038 3036 todel = args[0]
3039 3037 except IndexError:
3040 3038 raise UsageError(
3041 3039 "%bookmark -d: must provide a bookmark to delete")
3042 3040 else:
3043 3041 try:
3044 3042 del bkms[todel]
3045 3043 except KeyError:
3046 3044 raise UsageError(
3047 3045 "%%bookmark -d: Can't delete bookmark '%s'" % todel)
3048 3046
3049 3047 elif opts.has_key('r'):
3050 3048 bkms = {}
3051 3049 elif opts.has_key('l'):
3052 3050 bks = bkms.keys()
3053 3051 bks.sort()
3054 3052 if bks:
3055 3053 size = max(map(len,bks))
3056 3054 else:
3057 3055 size = 0
3058 3056 fmt = '%-'+str(size)+'s -> %s'
3059 3057 print 'Current bookmarks:'
3060 3058 for bk in bks:
3061 3059 print fmt % (bk,bkms[bk])
3062 3060 else:
3063 3061 if not args:
3064 3062 raise UsageError("%bookmark: You must specify the bookmark name")
3065 3063 elif len(args)==1:
3066 3064 bkms[args[0]] = os.getcwd()
3067 3065 elif len(args)==2:
3068 3066 bkms[args[0]] = args[1]
3069 3067 self.db['bookmarks'] = bkms
3070 3068
3071 3069 def magic_pycat(self, parameter_s=''):
3072 3070 """Show a syntax-highlighted file through a pager.
3073 3071
3074 3072 This magic is similar to the cat utility, but it will assume the file
3075 3073 to be Python source and will show it with syntax highlighting. """
3076 3074
3077 3075 try:
3078 3076 filename = get_py_filename(parameter_s)
3079 3077 cont = file_read(filename)
3080 3078 except IOError:
3081 3079 try:
3082 3080 cont = eval(parameter_s,self.user_ns)
3083 3081 except NameError:
3084 3082 cont = None
3085 3083 if cont is None:
3086 3084 print "Error: no such file or variable"
3087 3085 return
3088 3086
3089 3087 page.page(self.shell.pycolorize(cont))
3090 3088
3091 3089 def _rerun_pasted(self):
3092 3090 """ Rerun a previously pasted command.
3093 3091 """
3094 3092 b = self.user_ns.get('pasted_block', None)
3095 3093 if b is None:
3096 3094 raise UsageError('No previous pasted block available')
3097 3095 print "Re-executing '%s...' (%d chars)"% (b.split('\n',1)[0], len(b))
3098 3096 exec b in self.user_ns
3099 3097
3100 3098 def _get_pasted_lines(self, sentinel):
3101 3099 """ Yield pasted lines until the user enters the given sentinel value.
3102 3100 """
3103 3101 from IPython.core import interactiveshell
3104 3102 print "Pasting code; enter '%s' alone on the line to stop." % sentinel
3105 3103 while True:
3106 3104 l = interactiveshell.raw_input_original(':')
3107 3105 if l == sentinel:
3108 3106 return
3109 3107 else:
3110 3108 yield l
3111 3109
3112 3110 def _strip_pasted_lines_for_code(self, raw_lines):
3113 3111 """ Strip non-code parts of a sequence of lines to return a block of
3114 3112 code.
3115 3113 """
3116 3114 # Regular expressions that declare text we strip from the input:
3117 3115 strip_re = [r'^\s*In \[\d+\]:', # IPython input prompt
3118 3116 r'^\s*(\s?>)+', # Python input prompt
3119 3117 r'^\s*\.{3,}', # Continuation prompts
3120 3118 r'^\++',
3121 3119 ]
3122 3120
3123 3121 strip_from_start = map(re.compile,strip_re)
3124 3122
3125 3123 lines = []
3126 3124 for l in raw_lines:
3127 3125 for pat in strip_from_start:
3128 3126 l = pat.sub('',l)
3129 3127 lines.append(l)
3130 3128
3131 3129 block = "\n".join(lines) + '\n'
3132 3130 #print "block:\n",block
3133 3131 return block
3134 3132
3135 3133 def _execute_block(self, block, par):
3136 3134 """ Execute a block, or store it in a variable, per the user's request.
3137 3135 """
3138 3136 if not par:
3139 3137 b = textwrap.dedent(block)
3140 3138 self.user_ns['pasted_block'] = b
3141 3139 exec b in self.user_ns
3142 3140 else:
3143 3141 self.user_ns[par] = SList(block.splitlines())
3144 3142 print "Block assigned to '%s'" % par
3145 3143
3146 3144 def magic_quickref(self,arg):
3147 3145 """ Show a quick reference sheet """
3148 3146 import IPython.core.usage
3149 3147 qr = IPython.core.usage.quick_reference + self.magic_magic('-brief')
3150 3148
3151 3149 page.page(qr)
3152 3150
3153 3151 def magic_doctest_mode(self,parameter_s=''):
3154 3152 """Toggle doctest mode on and off.
3155 3153
3156 3154 This mode is intended to make IPython behave as much as possible like a
3157 3155 plain Python shell, from the perspective of how its prompts, exceptions
3158 3156 and output look. This makes it easy to copy and paste parts of a
3159 3157 session into doctests. It does so by:
3160 3158
3161 3159 - Changing the prompts to the classic ``>>>`` ones.
3162 3160 - Changing the exception reporting mode to 'Plain'.
3163 3161 - Disabling pretty-printing of output.
3164 3162
3165 3163 Note that IPython also supports the pasting of code snippets that have
3166 3164 leading '>>>' and '...' prompts in them. This means that you can paste
3167 3165 doctests from files or docstrings (even if they have leading
3168 3166 whitespace), and the code will execute correctly. You can then use
3169 3167 '%history -t' to see the translated history; this will give you the
3170 3168 input after removal of all the leading prompts and whitespace, which
3171 3169 can be pasted back into an editor.
3172 3170
3173 3171 With these features, you can switch into this mode easily whenever you
3174 3172 need to do testing and changes to doctests, without having to leave
3175 3173 your existing IPython session.
3176 3174 """
3177 3175
3178 3176 from IPython.utils.ipstruct import Struct
3179 3177
3180 3178 # Shorthands
3181 3179 shell = self.shell
3182 3180 oc = shell.displayhook
3183 3181 meta = shell.meta
3184 3182 disp_formatter = self.shell.display_formatter
3185 3183 ptformatter = disp_formatter.formatters['text/plain']
3186 3184 # dstore is a data store kept in the instance metadata bag to track any
3187 3185 # changes we make, so we can undo them later.
3188 3186 dstore = meta.setdefault('doctest_mode',Struct())
3189 3187 save_dstore = dstore.setdefault
3190 3188
3191 3189 # save a few values we'll need to recover later
3192 3190 mode = save_dstore('mode',False)
3193 3191 save_dstore('rc_pprint',ptformatter.pprint)
3194 3192 save_dstore('xmode',shell.InteractiveTB.mode)
3195 3193 save_dstore('rc_separate_out',shell.separate_out)
3196 3194 save_dstore('rc_separate_out2',shell.separate_out2)
3197 3195 save_dstore('rc_prompts_pad_left',shell.prompts_pad_left)
3198 3196 save_dstore('rc_separate_in',shell.separate_in)
3199 3197 save_dstore('rc_plain_text_only',disp_formatter.plain_text_only)
3200 3198
3201 3199 if mode == False:
3202 3200 # turn on
3203 3201 oc.prompt1.p_template = '>>> '
3204 3202 oc.prompt2.p_template = '... '
3205 3203 oc.prompt_out.p_template = ''
3206 3204
3207 3205 # Prompt separators like plain python
3208 3206 oc.input_sep = oc.prompt1.sep = ''
3209 3207 oc.output_sep = ''
3210 3208 oc.output_sep2 = ''
3211 3209
3212 3210 oc.prompt1.pad_left = oc.prompt2.pad_left = \
3213 3211 oc.prompt_out.pad_left = False
3214 3212
3215 3213 ptformatter.pprint = False
3216 3214 disp_formatter.plain_text_only = True
3217 3215
3218 3216 shell.magic_xmode('Plain')
3219 3217 else:
3220 3218 # turn off
3221 3219 oc.prompt1.p_template = shell.prompt_in1
3222 3220 oc.prompt2.p_template = shell.prompt_in2
3223 3221 oc.prompt_out.p_template = shell.prompt_out
3224 3222
3225 3223 oc.input_sep = oc.prompt1.sep = dstore.rc_separate_in
3226 3224
3227 3225 oc.output_sep = dstore.rc_separate_out
3228 3226 oc.output_sep2 = dstore.rc_separate_out2
3229 3227
3230 3228 oc.prompt1.pad_left = oc.prompt2.pad_left = \
3231 3229 oc.prompt_out.pad_left = dstore.rc_prompts_pad_left
3232 3230
3233 3231 ptformatter.pprint = dstore.rc_pprint
3234 3232 disp_formatter.plain_text_only = dstore.rc_plain_text_only
3235 3233
3236 3234 shell.magic_xmode(dstore.xmode)
3237 3235
3238 3236 # Store new mode and inform
3239 3237 dstore.mode = bool(1-int(mode))
3240 3238 mode_label = ['OFF','ON'][dstore.mode]
3241 3239 print 'Doctest mode is:', mode_label
3242 3240
3243 3241 def magic_gui(self, parameter_s=''):
3244 3242 """Enable or disable IPython GUI event loop integration.
3245 3243
3246 3244 %gui [GUINAME]
3247 3245
3248 3246 This magic replaces IPython's threaded shells that were activated
3249 3247 using the (pylab/wthread/etc.) command line flags. GUI toolkits
3250 3248 can now be enabled, disabled and swtiched at runtime and keyboard
3251 3249 interrupts should work without any problems. The following toolkits
3252 3250 are supported: wxPython, PyQt4, PyGTK, and Tk::
3253 3251
3254 3252 %gui wx # enable wxPython event loop integration
3255 3253 %gui qt4|qt # enable PyQt4 event loop integration
3256 3254 %gui gtk # enable PyGTK event loop integration
3257 3255 %gui tk # enable Tk event loop integration
3258 3256 %gui # disable all event loop integration
3259 3257
3260 3258 WARNING: after any of these has been called you can simply create
3261 3259 an application object, but DO NOT start the event loop yourself, as
3262 3260 we have already handled that.
3263 3261 """
3264 3262 from IPython.lib.inputhook import enable_gui
3265 3263 opts, arg = self.parse_options(parameter_s, '')
3266 3264 if arg=='': arg = None
3267 3265 return enable_gui(arg)
3268 3266
3269 3267 def magic_load_ext(self, module_str):
3270 3268 """Load an IPython extension by its module name."""
3271 3269 return self.extension_manager.load_extension(module_str)
3272 3270
3273 3271 def magic_unload_ext(self, module_str):
3274 3272 """Unload an IPython extension by its module name."""
3275 3273 self.extension_manager.unload_extension(module_str)
3276 3274
3277 3275 def magic_reload_ext(self, module_str):
3278 3276 """Reload an IPython extension by its module name."""
3279 3277 self.extension_manager.reload_extension(module_str)
3280 3278
3281 3279 @testdec.skip_doctest
3282 3280 def magic_install_profiles(self, s):
3283 3281 """Install the default IPython profiles into the .ipython dir.
3284 3282
3285 3283 If the default profiles have already been installed, they will not
3286 3284 be overwritten. You can force overwriting them by using the ``-o``
3287 3285 option::
3288 3286
3289 3287 In [1]: %install_profiles -o
3290 3288 """
3291 3289 if '-o' in s:
3292 3290 overwrite = True
3293 3291 else:
3294 3292 overwrite = False
3295 3293 from IPython.config import profile
3296 3294 profile_dir = os.path.split(profile.__file__)[0]
3297 3295 ipython_dir = self.ipython_dir
3298 3296 files = os.listdir(profile_dir)
3299 3297
3300 3298 to_install = []
3301 3299 for f in files:
3302 3300 if f.startswith('ipython_config'):
3303 3301 src = os.path.join(profile_dir, f)
3304 3302 dst = os.path.join(ipython_dir, f)
3305 3303 if (not os.path.isfile(dst)) or overwrite:
3306 3304 to_install.append((f, src, dst))
3307 3305 if len(to_install)>0:
3308 3306 print "Installing profiles to: ", ipython_dir
3309 3307 for (f, src, dst) in to_install:
3310 3308 shutil.copy(src, dst)
3311 3309 print " %s" % f
3312 3310
3313 3311 def magic_install_default_config(self, s):
3314 3312 """Install IPython's default config file into the .ipython dir.
3315 3313
3316 3314 If the default config file (:file:`ipython_config.py`) is already
3317 3315 installed, it will not be overwritten. You can force overwriting
3318 3316 by using the ``-o`` option::
3319 3317
3320 3318 In [1]: %install_default_config
3321 3319 """
3322 3320 if '-o' in s:
3323 3321 overwrite = True
3324 3322 else:
3325 3323 overwrite = False
3326 3324 from IPython.config import default
3327 3325 config_dir = os.path.split(default.__file__)[0]
3328 3326 ipython_dir = self.ipython_dir
3329 3327 default_config_file_name = 'ipython_config.py'
3330 3328 src = os.path.join(config_dir, default_config_file_name)
3331 3329 dst = os.path.join(ipython_dir, default_config_file_name)
3332 3330 if (not os.path.isfile(dst)) or overwrite:
3333 3331 shutil.copy(src, dst)
3334 3332 print "Installing default config file: %s" % dst
3335 3333
3336 3334 # Pylab support: simple wrappers that activate pylab, load gui input
3337 3335 # handling and modify slightly %run
3338 3336
3339 3337 @testdec.skip_doctest
3340 3338 def _pylab_magic_run(self, parameter_s=''):
3341 3339 Magic.magic_run(self, parameter_s,
3342 3340 runner=mpl_runner(self.shell.safe_execfile))
3343 3341
3344 3342 _pylab_magic_run.__doc__ = magic_run.__doc__
3345 3343
3346 3344 @testdec.skip_doctest
3347 3345 def magic_pylab(self, s):
3348 3346 """Load numpy and matplotlib to work interactively.
3349 3347
3350 3348 %pylab [GUINAME]
3351 3349
3352 3350 This function lets you activate pylab (matplotlib, numpy and
3353 3351 interactive support) at any point during an IPython session.
3354 3352
3355 3353 It will import at the top level numpy as np, pyplot as plt, matplotlib,
3356 3354 pylab and mlab, as well as all names from numpy and pylab.
3357 3355
3358 3356 Parameters
3359 3357 ----------
3360 3358 guiname : optional
3361 3359 One of the valid arguments to the %gui magic ('qt', 'wx', 'gtk' or
3362 3360 'tk'). If given, the corresponding Matplotlib backend is used,
3363 3361 otherwise matplotlib's default (which you can override in your
3364 3362 matplotlib config file) is used.
3365 3363
3366 3364 Examples
3367 3365 --------
3368 3366 In this case, where the MPL default is TkAgg:
3369 3367 In [2]: %pylab
3370 3368
3371 3369 Welcome to pylab, a matplotlib-based Python environment.
3372 3370 Backend in use: TkAgg
3373 3371 For more information, type 'help(pylab)'.
3374 3372
3375 3373 But you can explicitly request a different backend:
3376 3374 In [3]: %pylab qt
3377 3375
3378 3376 Welcome to pylab, a matplotlib-based Python environment.
3379 3377 Backend in use: Qt4Agg
3380 3378 For more information, type 'help(pylab)'.
3381 3379 """
3382 3380 self.shell.enable_pylab(s)
3383 3381
3384 3382 def magic_tb(self, s):
3385 3383 """Print the last traceback with the currently active exception mode.
3386 3384
3387 3385 See %xmode for changing exception reporting modes."""
3388 3386 self.shell.showtraceback()
3389 3387
3390 3388 # end Magic
General Comments 0
You need to be logged in to leave comments. Login now