##// END OF EJS Templates
- Manual updates...
fperez -
Show More

The requested changes are too big and content was truncated. Show full diff

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