home *** CD-ROM | disk | FTP | other *** search
Wrap
# Source Generated with Decompyle++ # File: in.pyc (Python 2.6) import __builtin__ import bdb import inspect import os import pdb import pydoc import sys import re import tempfile import time import cPickle as pickle import textwrap from cStringIO import StringIO from getopt import getopt, GetoptError from pprint import pprint, pformat try: import cProfile as profile import pstats except ImportError: try: import profile import pstats except ImportError: profile = None pstats = None except: None<EXCEPTION MATCH>ImportError None<EXCEPTION MATCH>ImportError import IPython from IPython import Debugger, OInspect, wildcard from IPython.FakeModule import FakeModule from IPython.Itpl import Itpl, itpl, printpl, itplns from IPython.PyColorize import Parser from IPython.ipstruct import Struct from IPython.macro import Macro from IPython.genutils import * from IPython import platutils import IPython.generics as IPython import IPython.ipapi as IPython from IPython.ipapi import UsageError from IPython.testing import decorators as testdec def on_off(tag): return [ 'OFF', 'ON'][tag] class Bunch: pass def compress_dhist(dh): head = dh[:-10] tail = dh[-10:] newhead = [] done = set() for h in head: if h in done: continue newhead.append(h) done.add(h) return newhead + tail class Magic: auto_status = [ 'Automagic is OFF, % prefix IS needed for magic functions.', 'Automagic is ON, % prefix NOT needed for magic functions.'] def __init__(self, shell): self.options_table = { } if profile is None: self.magic_prun = self.profile_missing_notice self.shell = shell self._magic_state = Bunch() def profile_missing_notice(self, *args, **kwargs): error('The profile module could not be found. It has been removed from the standard\npython packages because of its non-free license. To use profiling, install the\npython-profiler package from non-free.') def default_option(self, fn, optstr): if fn not in self.lsmagic(): error('%s is not a magic function' % fn) self.options_table[fn] = optstr def lsmagic(self): class_magic = lambda fn: if fn.startswith('magic_'): passcallable(Magic.__dict__[fn]) inst_magic = lambda fn: if fn.startswith('magic_'): passcallable(self.__dict__[fn]) inst_bound_magic = lambda fn: if fn.startswith('magic_'): passcallable(self.__class__.__dict__[fn]) magics = filter(class_magic, Magic.__dict__.keys()) + filter(inst_magic, self.__dict__.keys()) + filter(inst_bound_magic, self.__class__.__dict__.keys()) out = [] for fn in set(magics): out.append(fn.replace('magic_', '', 1)) out.sort() return out def extract_input_slices(self, slices, raw = False): if raw: hist = self.shell.input_hist_raw else: hist = self.shell.input_hist cmds = [] for chunk in slices: if ':' in chunk: (ini, fin) = map(int, chunk.split(':')) elif '-' in chunk: (ini, fin) = map(int, chunk.split('-')) fin += 1 else: ini = int(chunk) fin = ini + 1 cmds.append(hist[ini:fin]) return cmds def _ofind(self, oname, namespaces = None): oname = oname.strip() alias_ns = None if namespaces is None: namespaces = [ ('Interactive', self.shell.user_ns), ('IPython internal', self.shell.internal_ns), ('Python builtin', __builtin__.__dict__), ('Alias', self.shell.alias_table)] alias_ns = self.shell.alias_table found = 0 obj = None ospace = None ds = None ismagic = 0 isalias = 0 parent = None oname_parts = oname.split('.') oname_head = oname_parts[0] oname_rest = oname_parts[1:] for nsname, ns in namespaces: try: obj = ns[oname_head] except KeyError: continue continue for part in oname_rest: try: parent = obj obj = getattr(obj, part) continue break continue else: found = 1 ospace = nsname if ns == alias_ns: isalias = 1 break if not found: if oname.startswith(self.shell.ESC_MAGIC): oname = oname[1:] obj = getattr(self, 'magic_' + oname, None) if obj is not None: found = 1 ospace = 'IPython internal' ismagic = 1 if not found and oname_head in ("''", '""', '[]', '{}', '()'): obj = eval(oname_head) found = 1 ospace = 'Interactive' return { 'found': found, 'obj': obj, 'namespace': ospace, 'ismagic': ismagic, 'isalias': isalias, 'parent': parent } def arg_err(self, func): print 'Error in arguments:' print OInspect.getdoc(func) def format_latex(self, strng): escape_re = re.compile('(%|_|\\$|#|&)', re.MULTILINE) cmd_name_re = re.compile('^(%s.*?):' % self.shell.ESC_MAGIC, re.MULTILINE) cmd_re = re.compile('(?P<cmd>%s.+?\\b)(?!\\}\\}:)' % self.shell.ESC_MAGIC, re.MULTILINE) par_re = re.compile('\\\\$', re.MULTILINE) newline_re = re.compile('\\\\n') strng = cmd_name_re.sub('\\n\\\\bigskip\\n\\\\texttt{\\\\textbf{ \\1}}:', strng) strng = cmd_re.sub('\\\\texttt{\\g<cmd>}', strng) strng = par_re.sub('\\\\\\\\', strng) strng = escape_re.sub('\\\\\\1', strng) strng = newline_re.sub('\\\\textbackslash{}n', strng) return strng def format_screen(self, strng): par_re = re.compile('\\\\$', re.MULTILINE) strng = par_re.sub('', strng) return strng def parse_options(self, arg_str, opt_str, *long_opts, **kw): caller = sys._getframe(1).f_code.co_name.replace('magic_', '') arg_str = '%s %s' % (self.options_table.get(caller, ''), arg_str) mode = kw.get('mode', 'string') if mode not in ('string', 'list'): raise ValueError, 'incorrect mode given: %s' % mode mode not in ('string', 'list') list_all = kw.get('list_all', 0) posix = kw.get('posix', True) odict = { } args = arg_str.split() if len(args) >= 1: argv = arg_split(arg_str, posix) try: (opts, args) = getopt(argv, opt_str, *long_opts) except GetoptError: e = None raise UsageError('%s ( allowed: "%s" %s)' % (e.msg, opt_str, ' '.join(long_opts))) for o, a in opts: if o.startswith('--'): o = o[2:] else: o = o[1:] try: odict[o].append(a) continue except AttributeError: odict[o] = [ odict[o], a] continue except KeyError: if list_all: odict[o] = [ a] else: odict[o] = a list_all except: None<EXCEPTION MATCH>AttributeError opts = Struct(odict) if mode == 'string': args = ' '.join(args) return (opts, args) def magic_lsmagic(self, parameter_s = ''): mesc = self.shell.ESC_MAGIC print 'Available magic functions:\n' + mesc + (' ' + mesc).join(self.lsmagic()) print '\n' + Magic.auto_status[self.shell.rc.automagic] def magic_magic(self, parameter_s = ''): mode = '' try: if parameter_s.split()[0] == '-latex': mode = 'latex' if parameter_s.split()[0] == '-brief': mode = 'brief' if parameter_s.split()[0] == '-rest': mode = 'rest' rest_docs = [] except: pass magic_docs = [] for fname in self.lsmagic(): mname = 'magic_' + fname for space in (Magic, self, self.__class__): try: fn = space.__dict__[mname] except KeyError: continue if mode == 'brief': if fn.__doc__: fndoc = fn.__doc__.split('\n', 1)[0] else: fndoc = 'No documentation' elif fn.__doc__: fndoc = fn.__doc__.rstrip() else: fndoc = 'No documentation' if mode == 'rest': rest_docs.append('**%s%s**::\n\n\t%s\n\n' % (self.shell.ESC_MAGIC, fname, fndoc)) continue magic_docs.append('%s%s:\n\t%s\n' % (self.shell.ESC_MAGIC, fname, fndoc)) magic_docs = ''.join(magic_docs) if mode == 'rest': return ''.join(rest_docs) if mode == 'latex': print self.format_latex(magic_docs) return None magic_docs = self.format_screen(magic_docs) if mode == 'brief': return magic_docs outmsg = "\nIPython's 'magic' functions\n===========================\n\nThe magic function system provides a series of functions which allow you to\ncontrol the behavior of IPython itself, plus a lot of system-type\nfeatures. All these functions are prefixed with a % character, but parameters\nare given without parentheses or quotes.\n\nNOTE: If you have 'automagic' enabled (via the command line option or with the\n%automagic function), you don't need to type in the % explicitly. By default,\nIPython ships with automagic on, so you should only rarely need the % escape.\n\nExample: typing '%cd mydir' (without the quotes) changes you working directory\nto 'mydir', if it exists.\n\nYou can define your own magic functions to extend the system. See the supplied\nipythonrc and example-magic.py files for details (in your ipython\nconfiguration directory, typically $HOME/.ipython/).\n\nYou can also define your own aliased names for magic functions. In your\nipythonrc file, placing a line like:\n\n execute __IPYTHON__.magic_pf = __IPYTHON__.magic_profile\n\nwill define %pf as a new name for %profile.\n\nYou can also call magics in code using the ipmagic() function, which IPython\nautomatically adds to the builtin namespace. Type 'ipmagic?' for details.\n\nFor a list of the available magic functions, use %lsmagic. For a description\nof any of them, type %magic_name?, e.g. '%cd?'.\n\nCurrently the magic system has the following functions:\n" mesc = self.shell.ESC_MAGIC outmsg = '%s\n%s\n\nSummary of magic functions (from %slsmagic):\n\n%s%s\n\n%s' % (outmsg, magic_docs, mesc, mesc, (' ' + mesc).join(self.lsmagic()), Magic.auto_status[self.shell.rc.automagic]) page(outmsg, screen_lines = self.shell.rc.screen_length) def magic_autoindent(self, parameter_s = ''): self.shell.set_autoindent() print 'Automatic indentation is:', [ 'OFF', 'ON'][self.shell.autoindent] def magic_automagic(self, parameter_s = ''): rc = self.shell.rc arg = parameter_s.lower() if parameter_s in ('on', '1', 'true'): rc.automagic = True elif parameter_s in ('off', '0', 'false'): rc.automagic = False else: rc.automagic = not (rc.automagic) print '\n' + Magic.auto_status[rc.automagic] def magic_autocall(self, parameter_s = ''): rc = self.shell.rc if parameter_s: arg = int(parameter_s) else: arg = 'toggle' if arg not in (0, 1, 2, 'toggle'): error('Valid modes: (0->Off, 1->Smart, 2->Full') return None if arg in (0, 1, 2): rc.autocall = arg elif rc.autocall: self._magic_state.autocall_save = rc.autocall rc.autocall = 0 else: try: rc.autocall = self._magic_state.autocall_save except AttributeError: rc.autocall = self._magic_state.autocall_save = 1 print 'Automatic calling is:', [ 'OFF', 'Smart', 'Full'][rc.autocall] magic_autocall = testdec.skip_doctest(magic_autocall) def magic_system_verbose(self, parameter_s = ''): if parameter_s: val = bool(eval(parameter_s)) else: val = None self.shell.rc_set_toggle('system_verbose', val) print 'System verbose printing is:', [ 'OFF', 'ON'][self.shell.rc.system_verbose] def magic_page(self, parameter_s = ''): (opts, args) = self.parse_options(parameter_s, 'r') raw = 'r' in opts if not args or args: pass oname = '_' info = self._ofind(oname) if info['found']: if not raw or str: pass txt = pformat(info['obj']) page(txt) else: print 'Object `%s` not found' % oname def magic_profile(self, parameter_s = ''): if self.shell.rc.profile: printpl('Current IPython profile: $self.shell.rc.profile.') else: print 'No profile active.' def magic_pinfo(self, parameter_s = '', namespaces = None): detail_level = 0 (pinfo, qmark1, oname, qmark2) = re.match('(pinfo )?(\\?*)(.*?)(\\??$)', parameter_s).groups() if pinfo and qmark1 or qmark2: detail_level = 1 if '*' in oname: self.magic_psearch(oname) else: self._inspect('pinfo', oname, detail_level = detail_level, namespaces = namespaces) def magic_pdef(self, parameter_s = '', namespaces = None): self._inspect('pdef', parameter_s, namespaces) def magic_pdoc(self, parameter_s = '', namespaces = None): self._inspect('pdoc', parameter_s, namespaces) def magic_psource(self, parameter_s = '', namespaces = None): self._inspect('psource', parameter_s, namespaces) def magic_pfile(self, parameter_s = ''): out = self._inspect('pfile', parameter_s) if out == 'not found': try: filename = get_py_filename(parameter_s) except IOError: msg = None print msg return None page(self.shell.inspector.format(file(filename).read())) def _inspect(self, meth, oname, namespaces = None, **kw): try: oname = oname.strip().encode('ascii') except UnicodeEncodeError: print 'Python identifiers can only contain ascii characters.' return 'not found' info = Struct(self._ofind(oname, namespaces)) if info.found: try: IPython.generics.inspect_object(info.obj) return None except IPython.ipapi.TryNext: pass path = oname.split('.') root = '.'.join(path[:-1]) if info.parent is not None: try: target = getattr(info.parent, '__class__') try: target = getattr(target, path[-1]) if isinstance(target, property): oname = root + '.__class__.' + path[-1] info = Struct(self._ofind(oname)) except AttributeError: pass except AttributeError: pass except: None<EXCEPTION MATCH>AttributeError None<EXCEPTION MATCH>AttributeError pmethod = getattr(self.shell.inspector, meth) if not info.ismagic or self.format_screen: pass formatter = None if meth == 'pdoc': pmethod(info.obj, oname, formatter) elif meth == 'pinfo': pmethod(info.obj, oname, formatter, info, **kw) else: pmethod(info.obj, oname) else: print 'Object `%s` not found.' % oname return 'not found' return info.found def magic_psearch(self, parameter_s = ''): try: parameter_s = parameter_s.encode('ascii') except UnicodeEncodeError: print 'Python identifiers can only contain ascii characters.' return None def_search = [ 'user', 'builtin'] (opts, args) = self.parse_options(parameter_s, 'cias:e:', list_all = True) opt = opts.get shell = self.shell psearch = shell.inspector.psearch if opts.has_key('i'): ignore_case = True elif opts.has_key('c'): ignore_case = False else: ignore_case = not (shell.rc.wildcards_case_sensitive) def_search.extend(opt('s', [])) ns_exclude = ns_exclude = opt('e', []) ns_search = _[1] try: psearch(args, shell.ns_table, ns_search, show_all = opt('a'), ignore_case = ignore_case) except: [] [] shell.showtraceback() def magic_who_ls(self, parameter_s = ''): user_ns = self.shell.user_ns internal_ns = self.shell.internal_ns user_config_ns = self.shell.user_config_ns out = [] typelist = parameter_s.split() for i in user_ns: if not i.startswith('_'): pass if not i.startswith('_i'): if not i in internal_ns: pass if not (i in user_config_ns): if typelist: if type(user_ns[i]).__name__ in typelist: out.append(i) else: out.append(i) typelist out.sort() return out def magic_who(self, parameter_s = ''): varlist = self.magic_who_ls(parameter_s) if not varlist: if parameter_s: print 'No variables match your requested type.' else: print 'Interactive namespace is empty.' return None count = 0 for i in varlist: print i + '\t', count += 1 if count > 8: count = 0 print continue print def magic_whos(self, parameter_s = ''): varnames = self.magic_who_ls(parameter_s) if not varnames: if parameter_s: print 'No variables match your requested type.' else: print 'Interactive namespace is empty.' return None seq_types = [ types.DictType, types.ListType, types.TupleType] try: import numpy except ImportError: varnames varnames ndarray_type = None except: varnames ndarray_type = numpy.ndarray.__name__ try: import Numeric except ImportError: varnames varnames array_type = None except: varnames array_type = Numeric.ArrayType.__name__ def get_vars(i): return self.shell.user_ns[i] abbrevs = { 'IPython.macro.Macro': 'Macro' } def type_name(v): tn = type(v).__name__ return abbrevs.get(tn, tn) varlist = map(get_vars, varnames) typelist = [] for vv in varlist: tt = type_name(vv) if tt == 'instance': typelist.append(abbrevs.get(str(vv.__class__), str(vv.__class__))) continue ((varnames,),) typelist.append(tt) varlabel = 'Variable' typelabel = 'Type' datalabel = 'Data/Info' colsep = 3 vformat = '$vname.ljust(varwidth)$vtype.ljust(typewidth)' vfmt_short = '$vstr[:25]<...>$vstr[-25:]' aformat = '%s: %s elems, type `%s`, %s bytes' varwidth = max(max(map(len, varnames)), len(varlabel)) + colsep typewidth = max(max(map(len, typelist)), len(typelabel)) + colsep print varlabel.ljust(varwidth) + typelabel.ljust(typewidth) + ' ' + datalabel + '\n' + '-' * (varwidth + typewidth + len(datalabel) + 1) kb = 1024 Mb = 1048576 for vname, var, vtype in zip(varnames, varlist, typelist): print itpl(vformat), if vtype in seq_types: print len(var) continue if vtype in [ array_type, ndarray_type]: vshape = str(var.shape).replace(',', '').replace(' ', 'x')[1:-1] if vtype == ndarray_type: vsize = var.size vbytes = vsize * var.itemsize vdtype = var.dtype else: vsize = Numeric.size(var) vbytes = vsize * var.itemsize() vdtype = var.typecode() None if vbytes < 100000 else vbytes < Mb try: vstr = str(var) except UnicodeEncodeError: vstr = unicode(var).encode(sys.getdefaultencoding(), 'backslashreplace') vstr = vstr.replace('\n', '\\n') if len(vstr) < 50: print vstr continue printpl(vfmt_short) def magic_reset(self, parameter_s = ''): if parameter_s == '-f': ans = True else: ans = self.shell.ask_yes_no('Once deleted, variables cannot be recovered. Proceed (y/[n])? ') if not ans: print 'Nothing done.' return None user_ns = self.shell.user_ns for i in self.magic_who_ls(): del user_ns[i] self.shell.clear_main_mod_cache() def magic_logstart(self, parameter_s = ''): (opts, par) = self.parse_options(parameter_s, 'ort') log_output = 'o' in opts log_raw_input = 'r' in opts timestamp = 't' in opts rc = self.shell.rc logger = self.shell.logger if par: try: (logfname, logmode) = par.split() logfname = par logmode = 'backup' else: logfname = logger.logfname logmode = logger.logmode old_logfile = rc.opts.get('logfile', '') if logfname: logfname = os.path.expanduser(logfname) rc.opts.logfile = logfname loghead = self.shell.loghead_tpl % (rc.opts, rc.args) try: started = logger.logstart(logfname, loghead, logmode, log_output, timestamp, log_raw_input) except: rc.opts.logfile = old_logfile warn("Couldn't start log: %s" % sys.exc_info()[1]) if timestamp: logger.timestamp = False if log_raw_input: input_hist = self.shell.input_hist_raw else: input_hist = self.shell.input_hist if log_output: log_write = logger.log_write output_hist = self.shell.output_hist for n in range(1, len(input_hist) - 1): log_write(input_hist[n].rstrip()) if n in output_hist: log_write(repr(output_hist[n]), 'output') continue else: logger.log_write(input_hist[1:]) if timestamp: logger.timestamp = True print 'Activating auto-logging. Current session state plus future input saved.' logger.logstate() def magic_logstop(self, parameter_s = ''): self.logger.logstop() def magic_logoff(self, parameter_s = ''): self.shell.logger.switch_log(0) def magic_logon(self, parameter_s = ''): self.shell.logger.switch_log(1) def magic_logstate(self, parameter_s = ''): self.shell.logger.logstate() def magic_pdb(self, parameter_s = ''): par = parameter_s.strip().lower() if par: try: new_pdb = { 'off': 0, '0': 0, 'on': 1, '1': 1 }[par] except KeyError: print 'Incorrect argument. Use on/1, off/0, or nothing for a toggle.' return None None<EXCEPTION MATCH>KeyError new_pdb = not (self.shell.call_pdb) self.shell.call_pdb = new_pdb print 'Automatic pdb calling has been turned', on_off(new_pdb) def magic_debug(self, parameter_s = ''): self.shell.debugger(force = True) def magic_prun(self, parameter_s = '', user_mode = 1, opts = None, arg_lst = None, prog_ns = None): opts_def = Struct(D = [ ''], l = [], s = [ 'time'], T = [ '']) parameter_s = parameter_s.replace('"', '\\"').replace("'", "\\'") if user_mode: (opts, arg_str) = self.parse_options(parameter_s, 'D:l:rs:T:', list_all = 1) namespace = self.shell.user_ns else: try: filename = get_py_filename(arg_lst[0]) except IOError: msg = None error(msg) return None arg_str = 'execfile(filename,prog_ns)' namespace = locals() opts.merge(opts_def) prof = profile.Profile() try: prof = prof.runctx(arg_str, namespace, namespace) sys_exit = '' except SystemExit: sys_exit = '*** SystemExit exception caught in code being profiled.' stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s) lims = opts.l if lims: lims = [] for lim in opts.l: try: lims.append(int(lim)) continue except ValueError: try: lims.append(float(lim)) except ValueError: lims.append(lim) except: None<EXCEPTION MATCH>ValueError None<EXCEPTION MATCH>ValueError stdout_trap = StringIO() if hasattr(stats, 'stream'): stats.stream = stdout_trap stats.print_stats(*lims) else: sys_stdout = sys.stdout try: sys.stdout = stdout_trap stats.print_stats(*lims) finally: sys.stdout = sys_stdout output = stdout_trap.getvalue() output = output.rstrip() page(output, screen_lines = self.shell.rc.screen_length) print sys_exit, dump_file = opts.D[0] text_file = opts.T[0] if dump_file: prof.dump_stats(dump_file) print '\n*** Profile stats marshalled to file', `dump_file` + '.', sys_exit if text_file: pfile = file(text_file, 'w') pfile.write(output) pfile.close() print '\n*** Profile printout saved to text file', `text_file` + '.', sys_exit if opts.has_key('r'): return stats return None magic_prun = testdec.skip_doctest(magic_prun) def magic_run(self, parameter_s = '', runner = None, file_finder = get_py_filename): (opts, arg_lst) = self.parse_options(parameter_s, 'nidtN:b:pD:l:rs:T:e', mode = 'list', list_all = 1) try: filename = file_finder(arg_lst[0]) except IndexError: warn('you must provide at least a filename.') print '\n%run:\n', OInspect.getdoc(self.magic_run) return None except IOError: msg = None error(msg) return None elif filename.lower().endswith('.ipy'): self.api.runlines(open(filename).read()) return None exit_ignore = opts.has_key('e') save_argv = sys.argv sys.argv = [ filename] + arg_lst[1:] if opts.has_key('i'): prog_ns = self.shell.user_ns _Magic__name__save = self.shell.user_ns['__name__'] prog_ns['__name__'] = '__main__' main_mod = self.shell.new_main_mod(prog_ns) elif opts.has_key('n'): name = os.path.splitext(os.path.basename(filename))[0] else: name = '__main__' main_mod = self.shell.new_main_mod() prog_ns = main_mod.__dict__ prog_ns['__name__'] = name prog_ns['__file__'] = filename main_mod_name = prog_ns['__name__'] if main_mod_name == '__main__': restore_main = sys.modules['__main__'] else: restore_main = False sys.modules[main_mod_name] = main_mod stats = None try: self.shell.savehist() if opts.has_key('p'): stats = self.magic_prun('', 0, opts, arg_lst, prog_ns) elif opts.has_key('d'): deb = Debugger.Pdb(self.shell.rc.colors) bdb.Breakpoint.next = 1 bdb.Breakpoint.bplist = { } bdb.Breakpoint.bpbynumber = [ None] maxtries = 10 bp = int(opts.get('b', [ 1])[0]) checkline = deb.checkline(filename, bp) if not checkline: for bp in range(bp + 1, bp + maxtries + 1): if deb.checkline(filename, bp): break continue else: msg = '\nI failed to find a valid line to set a breakpoint\nafter trying up to line: %s.\nPlease set a valid breakpoint manually with the -b option.' % bp return None deb.do_break('%s:%s' % (filename, bp)) print "NOTE: Enter 'c' at the", '%s prompt to start your script.' % deb.prompt try: deb.run('execfile("%s")' % filename, prog_ns) (etype, value, tb) = sys.exc_info() self.shell.InteractiveTB(etype, value, tb, tb_offset = 3) elif runner is None: runner = self.shell.safe_execfile if opts.has_key('t'): try: nruns = int(opts['N'][0]) if nruns < 1: error('Number of runs must be >=1') return None except KeyError: nruns = 1 if nruns == 1: t0 = clock2() runner(filename, prog_ns, prog_ns, exit_ignore = exit_ignore) t1 = clock2() t_usr = t1[0] - t0[0] t_sys = t1[1] - t0[1] print '\nIPython CPU timings (estimated):' print ' User : %10s s.' % t_usr print ' System: %10s s.' % t_sys else: runs = range(nruns) t0 = clock2() for nr in runs: runner(filename, prog_ns, prog_ns, exit_ignore = exit_ignore) t1 = clock2() t_usr = t1[0] - t0[0] t_sys = t1[1] - t0[1] print '\nIPython CPU timings (estimated):' print 'Total runs performed:', nruns print ' Times : %10s %10s' % ('Total', 'Per run') print ' User : %10s s, %10s s.' % (t_usr, t_usr / nruns) print ' System: %10s s, %10s s.' % (t_sys, t_sys / nruns) else: runner(filename, prog_ns, prog_ns, exit_ignore = exit_ignore) if opts.has_key('i'): self.shell.user_ns['__name__'] = _Magic__name__save else: self.shell.cache_main_mod(prog_ns, filename) prog_ns.pop('__name__', None) self.shell.user_ns.update(prog_ns) finally: self.shell.user_ns['__builtins__'] = __builtin__ sys.argv = save_argv if restore_main: sys.modules['__main__'] = restore_main else: del sys.modules[main_mod_name] self.shell.reloadhist() return stats magic_run = testdec.skip_doctest(magic_run) def magic_runlog(self, parameter_s = ''): for f in parameter_s.split(): self.shell.safe_execfile(f, self.shell.user_ns, self.shell.user_ns, islog = 1) def magic_timeit(self, parameter_s = ''): import timeit import math units = [ u's', u'ms', u'us', 'ns'] scaling = [ 1, 1000, 1e+06, 1e+09] (opts, stmt) = self.parse_options(parameter_s, 'n:r:tcp:', posix = False) if stmt == '': return None timefunc = timeit.default_timer number = int(getattr(opts, 'n', 0)) repeat = int(getattr(opts, 'r', timeit.default_repeat)) precision = int(getattr(opts, 'p', 3)) if hasattr(opts, 't'): timefunc = time.time if hasattr(opts, 'c'): timefunc = clock timer = timeit.Timer(timer = timefunc) src = timeit.template % { 'stmt': timeit.reindent(stmt, 8), 'setup': 'pass' } tc_min = 0.1 t0 = clock() code = compile(src, '<magic-timeit>', 'exec') tc = clock() - t0 ns = { } exec code in self.shell.user_ns, ns timer.inner = ns['inner'] if number == 0: number = 1 for i in range(1, 10): if timer.timeit(number) >= 0.2: break number *= 10 best = min(timer.repeat(repeat, number)) / number if best > 0: order = min(-int(math.floor(math.log10(best)) // 3), 3) else: order = 3 print u'%d loops, best of %d: %.*g %s per loop' % (number, repeat, precision, best * scaling[order], units[order]) if tc > tc_min: print 'Compiler time: %.2f s' % tc magic_timeit = testdec.skip_doctest(magic_timeit) def magic_time(self, parameter_s = ''): expr = self.shell.prefilter(parameter_s, False) tc_min = 0.1 try: mode = 'eval' t0 = clock() code = compile(expr, '<timed eval>', mode) tc = clock() - t0 except SyntaxError: mode = 'exec' t0 = clock() code = compile(expr, '<timed exec>', mode) tc = clock() - t0 glob = self.shell.user_ns clk = clock2 wtime = time.time wall_st = wtime() if mode == 'eval': st = clk() out = eval(code, glob) end = clk() else: st = clk() exec code in glob end = clk() out = None wall_end = wtime() wall_time = wall_end - wall_st cpu_user = end[0] - st[0] cpu_sys = end[1] - st[1] cpu_tot = cpu_user + cpu_sys print 'CPU times: user %.2f s, sys: %.2f s, total: %.2f s' % (cpu_user, cpu_sys, cpu_tot) print 'Wall time: %.2f s' % wall_time if tc > tc_min: print 'Compiler : %.2f s' % tc return out magic_time = testdec.skip_doctest(magic_time) def magic_macro(self, parameter_s = ''): (opts, args) = self.parse_options(parameter_s, 'r', mode = 'list') if not args: macs = _[1] macs.sort() return macs if len(args) == 1: raise UsageError("%macro insufficient args; usage '%macro name n1-n2 n3-4...") len(args) == 1 name = args[0] ranges = args[1:] lines = self.extract_input_slices(ranges, opts.has_key('r')) macro = Macro(lines) self.shell.user_ns.update({ name: macro }) print 'Macro `%s` created. To execute, type its name (without quotes).' % name print 'Macro contents:' print macro, magic_macro = testdec.skip_doctest(magic_macro) def magic_save(self, parameter_s = ''): (opts, args) = self.parse_options(parameter_s, 'r', mode = 'list') fname = args[0] ranges = args[1:] if not fname.endswith('.py'): fname += '.py' if os.path.isfile(fname): ans = raw_input('File `%s` exists. Overwrite (y/[N])? ' % fname) if ans.lower() not in ('y', 'yes'): print 'Operation cancelled.' return None cmds = ''.join(self.extract_input_slices(ranges, opts.has_key('r'))) f = file(fname, 'w') f.write(cmds) f.close() print 'The following commands were written to file `%s`:' % fname print cmds def _edit_macro(self, mname, macro): filename = self.shell.mktempfile(macro.value) self.shell.hooks.editor(filename) mfile = open(filename) mvalue = mfile.read() mfile.close() self.shell.user_ns[mname] = Macro(mvalue) def magic_ed(self, parameter_s = ''): return self.magic_edit(parameter_s) def magic_edit(self, parameter_s = '', last_call = [ '', '']): def make_filename(arg): try: filename = get_py_filename(arg) except IOError: if args.endswith('.py'): filename = arg else: filename = None except: args.endswith('.py') return filename class DataIsObject(Exception): pass (opts, args) = self.parse_options(parameter_s, 'prxn:') opts_p = opts.has_key('p') opts_r = opts.has_key('r') lineno = opts.get('n', None) if opts_p: args = '_%s' % last_call[0] if not self.shell.user_ns.has_key(args): args = last_call[1] try: last_call[0] = self.shell.outputcache.prompt_count if not opts_p: last_call[1] = parameter_s except: pass use_temp = 1 if re.match('\\d', args): ranges = args.split() data = ''.join(self.extract_input_slices(ranges, opts_r)) elif args.endswith('.py'): filename = make_filename(args) data = '' use_temp = 0 elif args: try: data = eval(args, self.shell.user_ns) if type(data) not in StringTypes: raise DataIsObject type(data) not in StringTypes except (NameError, SyntaxError): filename = make_filename(args) if filename is None: warn("Argument given (%s) can't be found as a variable or as a filename." % args) return None data = '' use_temp = 0 except DataIsObject: if isinstance(data, Macro): self._edit_macro(args, data) return None try: filename = inspect.getabsfile(data) if 'fakemodule' in filename.lower() and inspect.isclass(data): attrs = [ getattr(data, aname) for aname in dir(data) ] for attr in attrs: filename = inspect.getabsfile(attr) if filename and 'fakemodule' not in filename.lower(): data = attr break continue isinstance(data, Macro) if not inspect.ismethod(attr) else [] datafile = 1 except TypeError: isinstance(data, Macro) isinstance(data, Macro) filename = make_filename(args) datafile = 1 warn('Could not find file where `%s` is defined.\nOpening a file named `%s`' % (args, filename)) except: isinstance(data, Macro) if datafile: try: if lineno is None: lineno = inspect.getsourcelines(data)[1] except IOError: isinstance(data, Macro) isinstance(data, Macro) filename = make_filename(args) if filename is None: warn('The file `%s` where `%s` was defined cannot be read.' % (filename, data)) return None except: isinstance(data, Macro)<EXCEPTION MATCH>IOError isinstance(data, Macro) use_temp = 0 except: None<EXCEPTION MATCH>(NameError, SyntaxError) None<EXCEPTION MATCH>(NameError, SyntaxError) data = '' if use_temp: filename = self.shell.mktempfile(data) print 'IPython will make a temporary file named:', filename print 'Editing...', sys.stdout.flush() try: self.shell.hooks.editor(filename, lineno) except IPython.ipapi.TryNext: warn('Could not open editor') return None if args.strip() == 'pasted_block': self.shell.user_ns['pasted_block'] = file_read(filename) if opts.has_key('x'): print else: print 'done. Executing edited code...' if opts_r: self.shell.runlines(file_read(filename)) else: self.shell.safe_execfile(filename, self.shell.user_ns, self.shell.user_ns) if use_temp: try: return open(filename).read() except IOError: msg = None if msg.filename == filename: warn('File not found. Did you forget to save?') return None self.shell.showtraceback() except: None<EXCEPTION MATCH>IOError None<EXCEPTION MATCH>IOError magic_edit = testdec.skip_doctest(magic_edit) def magic_xmode(self, parameter_s = ''): def xmode_switch_err(name): warn('Error changing %s exception modes.\n%s' % (name, sys.exc_info()[1])) shell = self.shell new_mode = parameter_s.strip().capitalize() try: shell.InteractiveTB.set_mode(mode = new_mode) print 'Exception reporting mode:', shell.InteractiveTB.mode except: xmode_switch_err('user') if shell.isthreaded: try: shell.sys_excepthook.set_mode(mode = new_mode) xmode_switch_err('threaded') def magic_colors(self, parameter_s = ''): def color_switch_err(name): warn('Error changing %s color schemes.\n%s' % (name, sys.exc_info()[1])) new_scheme = parameter_s.strip() if not new_scheme: raise UsageError("%colors: you must specify a color scheme. See '%colors?'") return new_scheme shell = self.shell import IPython.rlineimpl as readline if not (readline.have_readline) and sys.platform == 'win32': msg = "Proper color support under MS Windows requires the pyreadline library.\nYou can find it at:\nhttp://ipython.scipy.org/moin/PyReadline/Intro\nGary's readline needs the ctypes module, from:\nhttp://starship.python.net/crew/theller/ctypes\n(Note that ctypes is already part of Python versions 2.5 and newer).\n\nDefaulting color scheme to 'NoColor'" new_scheme = 'NoColor' warn(msg) if not shell.has_readline: new_scheme = 'NoColor' try: shell.outputcache.set_colors(new_scheme) except: color_switch_err('prompt') shell.rc.colors = shell.outputcache.color_table.active_scheme_name try: shell.InteractiveTB.set_colors(scheme = new_scheme) shell.SyntaxTB.set_colors(scheme = new_scheme) except: color_switch_err('exception') if shell.isthreaded: try: shell.sys_excepthook.set_colors(scheme = new_scheme) color_switch_err('system exception handler') if shell.rc.color_info: try: shell.inspector.set_active_scheme(new_scheme) color_switch_err('object inspector') else: shell.inspector.set_active_scheme('NoColor') def magic_color_info(self, parameter_s = ''): self.shell.rc.color_info = 1 - self.shell.rc.color_info self.magic_colors(self.shell.rc.colors) print 'Object introspection functions have now coloring:', [ 'OFF', 'ON'][self.shell.rc.color_info] def magic_Pprint(self, parameter_s = ''): self.shell.rc.pprint = 1 - self.shell.rc.pprint print 'Pretty printing has been turned', [ 'OFF', 'ON'][self.shell.rc.pprint] def magic_exit(self, parameter_s = ''): self.shell.exit() def magic_quit(self, parameter_s = ''): self.shell.exit() def magic_Exit(self, parameter_s = ''): self.shell.ask_exit() def magic_alias(self, parameter_s = ''): par = parameter_s.strip() if not par: stored = self.db.get('stored_aliases', { }) atab = self.shell.alias_table aliases = atab.keys() aliases.sort() res = [] showlast = [] for alias in aliases: special = False try: tgt = atab[alias][1] except (TypeError, AttributeError): tgt = atab[alias] special = True if alias in stored and special and alias.lower() != os.path.splitext(tgt)[0].lower() or ' ' in tgt: showlast.append((alias, tgt)) continue res.append((alias, tgt)) res.extend(showlast) print 'Total number of aliases:', len(aliases) return res try: (alias, cmd) = par.split(None, 1) except: par print OInspect.getdoc(self.magic_alias) nargs = cmd.count('%s') if nargs > 0 and cmd.find('%l') >= 0: error('The %s and %l specifiers are mutually exclusive in alias definitions.') else: self.shell.alias_table[alias] = (nargs, cmd) self.shell.alias_table_validate(verbose = 0) magic_alias = testdec.skip_doctest(magic_alias) def magic_unalias(self, parameter_s = ''): aname = parameter_s.strip() if aname in self.shell.alias_table: del self.shell.alias_table[aname] stored = self.db.get('stored_aliases', { }) if aname in stored: print 'Removing %stored alias', aname del stored[aname] self.db['stored_aliases'] = stored def magic_rehashx(self, parameter_s = ''): ip = self.api del ip.db['rootmodules'] path = [ os.path.abspath(os.path.expanduser(p)) for p in os.environ.get('PATH', '').split(os.pathsep) ] path = filter(os.path.isdir, path) alias_table = self.shell.alias_table syscmdlist = [] savedir = os.getcwd() try: if os.name == 'posix': for pdir in path: os.chdir(pdir) for ff in os.listdir(pdir): if isexec(ff) and ff not in self.shell.no_alias: alias_table[ff.replace('.', '')] = (0, ff) syscmdlist.append(ff) continue None if os.name == 'posix' else (None,) else: for pdir in path: os.chdir(pdir) for ff in os.listdir(pdir): (base, ext) = os.path.splitext(ff) if isexec(ff) and base.lower() not in self.shell.no_alias: if ext.lower() == '.exe': ff = base alias_table[base.lower().replace('.', '')] = (0, ff) syscmdlist.append(ff) continue self.shell.alias_table_validate() db = ip.db db['syscmdlist'] = syscmdlist finally: os.chdir(savedir) def magic_pwd(self, parameter_s = ''): return os.getcwd() def magic_cd(self, parameter_s = ''): parameter_s = parameter_s.strip() oldcwd = os.getcwd() numcd = re.match('(-)(\\d+)$', parameter_s) if numcd: nn = int(numcd.group(2)) try: ps = self.shell.user_ns['_dh'][nn] except IndexError: print 'The requested directory does not exist in history.' return None opts = { } elif parameter_s.startswith('--'): ps = None fallback = None pat = parameter_s[2:] dh = self.shell.user_ns['_dh'] for ent in reversed(dh): if pat in os.path.basename(ent) and os.path.isdir(ent): ps = ent break if fallback is None and pat in ent and os.path.isdir(ent): fallback = ent continue if ps is None: ps = fallback if ps is None: print 'No matching entry in directory history' return None opts = { } else: parameter_s = re.sub('\\\\(?! )', '/', parameter_s) (opts, ps) = self.parse_options(parameter_s, 'qb', mode = 'string') if ps == '-': try: ps = self.shell.user_ns['_dh'][-2] except IndexError: raise UsageError('%cd -: No previous directory to change to.') except: None<EXCEPTION MATCH>IndexError None<EXCEPTION MATCH>IndexError if not os.path.isdir(ps) or opts.has_key('b'): bkms = self.db.get('bookmarks', { }) if bkms.has_key(ps): target = bkms[ps] print '(bookmark:%s) -> %s' % (ps, target) ps = target elif opts.has_key('b'): raise UsageError("Bookmark '%s' not found. Use '%%bookmark -l' to see your bookmarks." % ps) if ps: try: os.chdir(os.path.expanduser(ps)) if self.shell.rc.term_title: platutils.set_term_title('IPy ' + abbrev_cwd()) except OSError: print sys.exc_info()[1] cwd = os.getcwd() dhist = self.shell.user_ns['_dh'] if oldcwd != cwd: dhist.append(cwd) self.db['dhist'] = compress_dhist(dhist)[-100:] else: os.chdir(self.shell.home_dir) if self.shell.rc.term_title: platutils.set_term_title('IPy ~') cwd = os.getcwd() dhist = self.shell.user_ns['_dh'] if oldcwd != cwd: dhist.append(cwd) self.db['dhist'] = compress_dhist(dhist)[-100:] if 'q' not in opts and self.shell.user_ns['_dh']: print self.shell.user_ns['_dh'][-1] def magic_env(self, parameter_s = ''): return os.environ.data def magic_pushd(self, parameter_s = ''): dir_s = self.shell.dir_stack tgt = os.path.expanduser(parameter_s) cwd = os.getcwd().replace(self.home_dir, '~') if tgt: self.magic_cd(parameter_s) dir_s.insert(0, cwd) return self.magic_dirs() def magic_popd(self, parameter_s = ''): if not self.shell.dir_stack: raise UsageError('%popd on empty stack') self.shell.dir_stack top = self.shell.dir_stack.pop(0) self.magic_cd(top) print 'popd ->', top def magic_dirs(self, parameter_s = ''): return self.shell.dir_stack def magic_dhist(self, parameter_s = ''): dh = self.shell.user_ns['_dh'] if parameter_s: try: args = map(int, parameter_s.split()) except: self.arg_err(Magic.magic_dhist) return None if len(args) == 1: ini = max(len(dh) - args[0], 0) fin = len(dh) elif len(args) == 2: (ini, fin) = args else: self.arg_err(Magic.magic_dhist) return None len(args) == 1 ini = 0 fin = len(dh) nlprint(dh, header = 'Directory history (kept in _dh)', start = ini, stop = fin) def magic_sc(self, parameter_s = ''): (opts, args) = self.parse_options(parameter_s, 'lv') try: (var, _) = args.split('=', 1) var = var.strip() (_, cmd) = parameter_s.split('=', 1) except ValueError: (var, cmd) = ('', '') (out, err) = self.shell.getoutputerror(cmd) if err: print >>Term.cerr, err if opts.has_key('l'): out = SList(out.split('\n')) else: out = LSString(out) if opts.has_key('v'): print '%s ==\n%s' % (var, pformat(out)) if var: self.shell.user_ns.update({ var: out }) else: return out return var magic_sc = testdec.skip_doctest(magic_sc) def magic_sx(self, parameter_s = ''): if parameter_s: (out, err) = self.shell.getoutputerror(parameter_s) if err: print >>Term.cerr, err return SList(out.split('\n')) def magic_bg(self, parameter_s = ''): self.shell.jobs.new(parameter_s, self.shell.user_ns) def magic_r(self, parameter_s = ''): start = parameter_s.strip() esc_magic = self.shell.ESC_MAGIC if self.shell.rc.automagic: start_magic = esc_magic + start else: start_magic = start for n in range(len(self.shell.input_hist) - 2, 0, -1): input = self.shell.input_hist[n] if input != '_ip.magic("r")\n': if input.startswith(start) or input.startswith(start_magic): print 'Executing:', input, self.shell.runlines(input) return None print 'No previous input matching `%s` found.' % start def magic_bookmark(self, parameter_s = ''): (opts, args) = self.parse_options(parameter_s, 'drl', mode = 'list') if len(args) > 2: raise UsageError('%bookmark: too many arguments') len(args) > 2 bkms = self.db.get('bookmarks', { }) if opts.has_key('d'): try: todel = args[0] except IndexError: raise UsageError('%bookmark -d: must provide a bookmark to delete') try: del bkms[todel] except KeyError: raise UsageError("%%bookmark -d: Can't delete bookmark '%s'" % todel) except: None<EXCEPTION MATCH>KeyError None<EXCEPTION MATCH>KeyError if opts.has_key('r'): bkms = { } elif opts.has_key('l'): bks = bkms.keys() bks.sort() if bks: size = max(map(len, bks)) else: size = 0 fmt = '%-' + str(size) + 's -> %s' print 'Current bookmarks:' for bk in bks: print fmt % (bk, bkms[bk]) elif not args: raise UsageError('%bookmark: You must specify the bookmark name') elif len(args) == 1: bkms[args[0]] = os.getcwd() elif len(args) == 2: bkms[args[0]] = args[1] self.db['bookmarks'] = bkms def magic_pycat(self, parameter_s = ''): try: filename = get_py_filename(parameter_s) cont = file_read(filename) except IOError: try: cont = eval(parameter_s, self.user_ns) except NameError: cont = None except: None<EXCEPTION MATCH>NameError None<EXCEPTION MATCH>NameError if cont is None: print 'Error: no such file or variable' return None page(self.shell.pycolorize(cont), screen_lines = self.shell.rc.screen_length) def _rerun_pasted(self): b = self.user_ns.get('pasted_block', None) if b is None: raise UsageError('No previous pasted block available') b is None print "Re-executing '%s...' (%d chars)" % (b.split('\n', 1)[0], len(b)) exec b in self.user_ns def _get_pasted_lines(self, sentinel): iplib = iplib import IPython print "Pasting code; enter '%s' alone on the line to stop." % sentinel while True: l = iplib.raw_input_original(':') if l == sentinel: return None yield l l == sentinel def _strip_pasted_lines_for_code(self, raw_lines): strip_re = [ '^\\s*In \\[\\d+\\]:', '^\\s*(\\s?>)+', '^\\s*\\.{3,}', '^\\++'] strip_from_start = map(re.compile, strip_re) lines = [] for l in raw_lines: for pat in strip_from_start: l = pat.sub('', l) lines.append(l) block = '\n'.join(lines) + '\n' return block def _execute_block(self, block, par): if not par: b = textwrap.dedent(block) self.user_ns['pasted_block'] = b exec b in self.user_ns else: self.user_ns[par] = SList(block.splitlines()) print "Block assigned to '%s'" % par def magic_cpaste(self, parameter_s = ''): (opts, args) = self.parse_options(parameter_s, 'rs:', mode = 'string') par = args.strip() if opts.has_key('r'): self._rerun_pasted() return None sentinel = opts.get('s', '--') block = self._strip_pasted_lines_for_code(self._get_pasted_lines(sentinel)) self._execute_block(block, par) def magic_paste(self, parameter_s = ''): (opts, args) = self.parse_options(parameter_s, 'r:', mode = 'string') par = args.strip() if opts.has_key('r'): self._rerun_pasted() return None text = self.shell.hooks.clipboard_get() block = self._strip_pasted_lines_for_code(text.splitlines()) self._execute_block(block, par) def magic_quickref(self, arg): import IPython.usage as IPython qr = IPython.usage.quick_reference + self.magic_magic('-brief') page(qr) def magic_upgrade(self, arg): ip = self.getapi() ipinstallation = path(IPython.__file__).dirname() upgrade_script = '%s "%s"' % (sys.executable, ipinstallation / 'upgrade_dir.py') src_config = ipinstallation / 'UserConfig' userdir = path(ip.options.ipythondir) cmd = '%s "%s" "%s"' % (upgrade_script, src_config, userdir) print '>', cmd shell(cmd) def magic_doctest_mode(self, parameter_s = ''): ipaste = InterpreterPasteInput import IPython.Extensions Struct = Struct import IPython.ipstruct shell = self.shell oc = shell.outputcache rc = shell.rc meta = shell.meta dstore = meta.setdefault('doctest_mode', Struct()) save_dstore = dstore.setdefault mode = save_dstore('mode', False) save_dstore('rc_pprint', rc.pprint) save_dstore('xmode', shell.InteractiveTB.mode) save_dstore('rc_separate_out', rc.separate_out) save_dstore('rc_separate_out2', rc.separate_out2) save_dstore('rc_prompts_pad_left', rc.prompts_pad_left) save_dstore('rc_separate_in', rc.separate_in) if mode == False: ipaste.activate_prefilter() oc.prompt1.p_template = '>>> ' oc.prompt2.p_template = '... ' oc.prompt_out.p_template = '' oc.input_sep = oc.prompt1.sep = '' oc.output_sep = '' oc.output_sep2 = '' oc.prompt1.pad_left = oc.prompt2.pad_left = oc.prompt_out.pad_left = False rc.pprint = False shell.magic_xmode('Plain') else: ipaste.deactivate_prefilter() oc.prompt1.p_template = rc.prompt_in1 oc.prompt2.p_template = rc.prompt_in2 oc.prompt_out.p_template = rc.prompt_out oc.input_sep = oc.prompt1.sep = dstore.rc_separate_in oc.output_sep = dstore.rc_separate_out oc.output_sep2 = dstore.rc_separate_out2 oc.prompt1.pad_left = oc.prompt2.pad_left = oc.prompt_out.pad_left = dstore.rc_prompts_pad_left rc.pprint = dstore.rc_pprint shell.magic_xmode(dstore.xmode) dstore.mode = bool(1 - int(mode)) print 'Doctest mode is:', [ 'OFF', 'ON'][dstore.mode]