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