def alias_magic(self, line=""): """Create an alias for an existing line or cell magic. Examples -------- :: In [1]: %alias_magic t timeit Created `%t` as an alias for `%timeit`. Created `%%t` as an alias for `%%timeit`. In [2]: %t -n1 pass 1 loops, best of 3: 954 ns per loop In [3]: %%t -n1 ...: pass ...: 1 loops, best of 3: 954 ns per loop In [4]: %alias_magic --cell whereami pwd UsageError: Cell magic function `%%pwd` not found. In [5]: %alias_magic --line whereami pwd Created `%whereami` as an alias for `%pwd`. In [6]: %whereami Out[6]: u'/home/testuser' In [7]: %alias_magic h history -p "-l 30" --line Created `%h` as an alias for `%history -l 30`. """ args = magic_arguments.parse_argstring(self.alias_magic, line) shell = self.shell mman = self.shell.magics_manager escs = "".join(magic_escapes.values()) target = args.target.lstrip(escs) name = args.name.lstrip(escs) params = args.params if params and ( (params.startswith('"') and params.endswith('"')) or (params.startswith("'") and params.endswith("'")) ): params = params[1:-1] # Find the requested magics. m_line = shell.find_magic(target, "line") m_cell = shell.find_magic(target, "cell") if args.line and m_line is None: raise UsageError( "Line magic function `%s%s` not found." % (magic_escapes["line"], target) ) if args.cell and m_cell is None: raise UsageError( "Cell magic function `%s%s` not found." % (magic_escapes["cell"], target) ) # If --line and --cell are not specified, default to the ones # that are available. if not args.line and not args.cell: if not m_line and not m_cell: raise UsageError("No line or cell magic with name `%s` found." % target) args.line = bool(m_line) args.cell = bool(m_cell) params_str = "" if params is None else " " + params if args.line: mman.register_alias(name, target, "line", params) print( "Created `%s%s` as an alias for `%s%s%s`." % ( magic_escapes["line"], name, magic_escapes["line"], target, params_str, ) ) if args.cell: mman.register_alias(name, target, "cell", params) print( "Created `%s%s` as an alias for `%s%s%s`." % ( magic_escapes["cell"], name, magic_escapes["cell"], target, params_str, ) )
def alias_magic(self, line=''): """Create an alias for an existing line or cell magic. Examples -------- :: In [1]: %alias_magic t timeit Created `%t` as an alias for `%timeit`. Created `%%t` as an alias for `%%timeit`. In [2]: %t -n1 pass 1 loops, best of 3: 954 ns per loop In [3]: %%t -n1 ...: pass ...: 1 loops, best of 3: 954 ns per loop In [4]: %alias_magic --cell whereami pwd UsageError: Cell magic function `%%pwd` not found. In [5]: %alias_magic --line whereami pwd Created `%whereami` as an alias for `%pwd`. In [6]: %whereami Out[6]: u'/home/testuser' """ args = magic_arguments.parse_argstring(self.alias_magic, line) shell = self.shell mman = self.shell.magics_manager escs = ''.join(magic_escapes.values()) target = args.target.lstrip(escs) name = args.name.lstrip(escs) # Find the requested magics. m_line = shell.find_magic(target, 'line') m_cell = shell.find_magic(target, 'cell') if args.line and m_line is None: raise UsageError('Line magic function `%s%s` not found.' % (magic_escapes['line'], target)) if args.cell and m_cell is None: raise UsageError('Cell magic function `%s%s` not found.' % (magic_escapes['cell'], target)) # If --line and --cell are not specified, default to the ones # that are available. if not args.line and not args.cell: if not m_line and not m_cell: raise UsageError( 'No line or cell magic with name `%s` found.' % target ) args.line = bool(m_line) args.cell = bool(m_cell) if args.line: mman.register_alias(name, target, 'line') print('Created `%s%s` as an alias for `%s%s`.' % ( magic_escapes['line'], name, magic_escapes['line'], target)) if args.cell: mman.register_alias(name, target, 'cell') print('Created `%s%s` as an alias for `%s%s`.' % ( magic_escapes['cell'], name, magic_escapes['cell'], target))
def reload_ext(self, module_str): """Reload an IPython extension by its module name.""" if not module_str: raise UsageError('Missing module name.') self.shell.extension_manager.reload_extension(module_str)
def alias_magic(self, line=''): """Create an alias for an existing line or cell magic. Examples -------- :: In [1]: %alias_magic t timeit In [2]: %t -n1 pass 1 loops, best of 3: 954 ns per loop In [3]: %%t -n1 ...: pass ...: 1 loops, best of 3: 954 ns per loop In [4]: %alias_magic --cell whereami pwd UsageError: Cell magic function `%%pwd` not found. In [5]: %alias_magic --line whereami pwd In [6]: %whereami Out[6]: u'/home/testuser' """ args = magic_arguments.parse_argstring(self.alias_magic, line) shell = self.shell escs = ''.join(list(magic_escapes.values())) target = args.target.lstrip(escs) name = args.name.lstrip(escs) # Find the requested magics. m_line = shell.find_magic(target, 'line') m_cell = shell.find_magic(target, 'cell') if args.line and m_line is None: raise UsageError('Line magic function `%s%s` not found.' % (magic_escapes['line'], target)) if args.cell and m_cell is None: raise UsageError('Cell magic function `%s%s` not found.' % (magic_escapes['cell'], target)) # If --line and --cell are not specified, default to the ones # that are available. if not args.line and not args.cell: if not m_line and not m_cell: raise UsageError( 'No line or cell magic with name `%s` found.' % target ) args.line = bool(m_line) args.cell = bool(m_cell) if args.line: def wrapper(line): return m_line(line) wrapper.__name__ = str(name) wrapper.__doc__ = "Alias for `%s%s`." % \ (magic_escapes['line'], target) shell.register_magic_function(wrapper, 'line', name) if args.cell: def wrapper(line, cell): return m_cell(line, cell) wrapper.__name__ = str(name) wrapper.__doc__ = "Alias for `%s%s`." % \ (magic_escapes['cell'], target) shell.register_magic_function(wrapper, 'cell', name)
def error(self, message): """ Raise a catchable error instead of exiting. """ raise UsageError(message)
def _run_with_debugger(self, code, code_ns, filename=None, bp_line=None, bp_file=None): """ Run `code` in debugger with a break point. Parameters ---------- code : str Code to execute. code_ns : dict A namespace in which `code` is executed. filename : str `code` is ran as if it is in `filename`. bp_line : int, optional Line number of the break point. bp_file : str, optional Path to the file in which break point is specified. `filename` is used if not given. Raises ------ UsageError If the break point given by `bp_line` is not valid. """ deb = debugger.Pdb(self.shell.colors) # reset Breakpoint state, which is moronically kept # in a class bdb.Breakpoint.next = 1 bdb.Breakpoint.bplist = {} bdb.Breakpoint.bpbynumber = [None] if bp_line is not None: # Set an initial breakpoint to stop execution maxtries = 10 bp_file = bp_file or filename checkline = deb.checkline(bp_file, bp_line) if not checkline: for bp in range(bp_line + 1, bp_line + maxtries + 1): if deb.checkline(bp_file, bp): break else: msg = ("\nI failed to find a valid line to set " "a breakpoint\n" "after trying up to line: %s.\n" "Please set a valid breakpoint manually " "with the -b option." % bp) raise UsageError(msg) # if we find a good linenumber, set the breakpoint deb.do_break('%s:%s' % (bp_file, bp_line)) if filename: # Mimic Pdb._runscript(...) deb._wait_for_mainpyfile = True deb.mainpyfile = deb.canonic(filename) # Start file run print "NOTE: Enter 'c' at the %s prompt to continue execution." % deb.prompt try: if filename: # save filename so it can be used by methods on the deb object deb._exec_filename = filename deb.run(code, code_ns) except: etype, value, tb = sys.exc_info() # Skip three frames in the traceback: the %run one, # one inside bdb.py, and the command-line typed by the # user (run by exec in pdb itself). self.shell.InteractiveTB(etype, value, tb, tb_offset=3)
def store(self, parameter_s=''): """Lightweight persistence for python variables. Example:: In [1]: l = ['hello',10,'world'] In [2]: %store l In [3]: exit (IPython session is closed and started again...) ville@badger:~$ ipython In [1]: l NameError: name 'l' is not defined In [2]: %store -r In [3]: l Out[3]: ['hello', 10, 'world'] Usage: * ``%store`` - Show list of all variables and their current values * ``%store spam`` - Store the *current* value of the variable spam to disk * ``%store -d spam`` - Remove the variable and its value from storage * ``%store -z`` - Remove all variables from storage * ``%store -r`` - Refresh all variables from store (overwrite current vals) * ``%store -r spam bar`` - Refresh specified variables from store (delete current val) * ``%store foo >a.txt`` - Store value of foo to new file a.txt * ``%store foo >>a.txt`` - Append value of foo to file a.txt It should be noted that if you change the value of a variable, you need to %store it again if you want to persist the new value. Note also that the variables will need to be pickleable; most basic python types can be safely %store'd. Also aliases can be %store'd across sessions. """ opts, argsl = self.parse_options(parameter_s, 'drz', mode='string') args = argsl.split(None, 1) ip = self.shell db = ip.db # delete if 'd' in opts: try: todel = args[0] except IndexError: raise UsageError('You must provide the variable to forget') else: try: del db['autorestore/' + todel] except: raise UsageError("Can't delete variable '%s'" % todel) # reset elif 'z' in opts: for k in db.keys('autorestore/*'): del db[k] elif 'r' in opts: if args: for arg in args: try: obj = db['autorestore/' + arg] except KeyError: print("no stored variable %s" % arg) else: ip.user_ns[arg] = obj else: restore_data(ip) # run without arguments -> list variables & values elif not args: vars = db.keys('autorestore/*') vars.sort() if vars: size = max(map(len, vars)) else: size = 0 print('Stored variables and their in-db values:') fmt = '%-' + str(size) + 's -> %s' get = db.get for var in vars: justkey = os.path.basename(var) # print 30 first characters from every var print(fmt % (justkey, repr(get(var, '<unavailable>'))[:50])) # default action - store the variable else: # %store foo >file.txt or >>file.txt if len(args) > 1 and args[1].startswith('>'): fnam = os.path.expanduser(args[1].lstrip('>').lstrip()) if args[1].startswith('>>'): fil = open(fnam, 'a') else: fil = open(fnam, 'w') with fil: obj = ip.ev(args[0]) print("Writing '%s' (%s) to file '%s'." % (args[0], obj.__class__.__name__, fnam)) if not isinstance(obj, str): from pprint import pprint pprint(obj, fil) else: fil.write(obj) if not obj.endswith('\n'): fil.write('\n') return # %store foo try: obj = ip.user_ns[args[0]] except KeyError: # it might be an alias name = args[0] try: cmd = ip.alias_manager.retrieve_alias(name) except ValueError: raise UsageError("Unknown variable '%s'" % name) staliases = db.get('stored_aliases', {}) staliases[name] = cmd db['stored_aliases'] = staliases print("Alias stored: %s (%s)" % (name, cmd)) return else: modname = getattr(inspect.getmodule(obj), '__name__', '') if modname == '__main__': print( textwrap.dedent("""\ Warning:%s is %s Proper storage of interactively declared classes (or instances of those classes) is not possible! Only instances of classes in real modules on file system can be %%store'd. """ % (args[0], obj))) return #pickled = pickle.dumps(obj) db['autorestore/' + args[0]] = obj print("Stored '%s' (%s)" % (args[0], obj.__class__.__name__))
def addscript(self, pars="", cell=None): """ This works both as **%addscript** and as **%%addscript**. This magic command can either take a local filename, element in the namespace or history range (see %history), or the current cell content. Usage: %addscript -p project n1-n2 n3-n4 ... n5 .. n6 ... or %%addscript -p project ...code lines ... Options: -p <string> Name of the project where the script will be stored. If not provided, a project with a standard name : `proj` is searched. -o <string> script name. -s <symbols> Specify function or classes to load from python source. -a append to the current script instead of overwriting it. -n Search symbol in the current namespace. Examples -------- .. sourcecode:: ipython In[1]: %addscript myscript.py In[2]: %addscript 7-27 In[3]: %addscript -s MyClass,myfunction myscript.py In[4]: %addscript MyClass In[5]: %addscript mymodule.myfunction """ opts, args = self.parse_options(pars, "p:o:s:n:a") # append = 'a' in opts # mode = 'a' if append else 'w' search_ns = "n" in opts if not args and not cell and not search_ns: # pragma: no cover raise UsageError("Missing filename, input history range, " "or element in the user namespace.\n " "If no argument are given then the cell content " "should " "not be empty") name = "script" if "o" in opts: name = opts["o"] proj = "proj" if "p" in opts: proj = opts["p"] if proj not in self.shell.user_ns: # pragma: no cover raise ValueError( f"Cannot find any project with name `{proj}` in the namespace." ) # get the proj object projobj = self.shell.user_ns[proj] contents = "" if search_ns: contents += ("\n" + self.shell.find_user_code( opts["n"], search_ns=search_ns) + "\n") args = " ".join(args) if args.strip(): contents += ("\n" + self.shell.find_user_code(args, search_ns=search_ns) + "\n") if "s" in opts: # pragma: no cover try: blocks, not_found = extract_symbols(contents, opts["s"]) except SyntaxError: # non python code logging.error("Unable to parse the input as valid Python code") return None if len(not_found) == 1: warnings.warn(f"The symbol `{not_found[0]}` was not found") elif len(not_found) > 1: sym = get_text_list(not_found, wrap_item_with="`") warnings.warn(f"The symbols {sym} were not found") contents = "\n".join(blocks) if cell: contents += "\n" + cell # import delayed to avoid circular import error from spectrochempy.core.scripts.script import Script script = Script(name, content=contents) projobj[name] = script return f"Script {name} created."
def parse_options(self, arg_str, opt_str, *long_opts, **kw): """Parse options passed to an argument string. The interface is similar to that of :func:`getopt.getopt`, but it returns a :class:`~IPython.utils.struct.Struct` with the options as keys and the stripped argument string still as a string. arg_str is quoted as a true sys.argv vector by using shlex.split. This allows us to easily expand variables, glob files, quote arguments, etc. Parameters ---------- arg_str : str The arguments to parse. opt_str : str The options specification. mode : str, default 'string' If given as 'list', the argument string is returned as a list (split on whitespace) instead of a string. list_all : bool, default False Put all option values in lists. Normally only options appearing more than once are put in a list. posix : bool, default True Whether to split the input line in POSIX mode or not, as per the conventions outlined in the :mod:`shlex` module from the standard library. """ # inject default options at the beginning of the input line caller = sys._getframe(1).f_code.co_name 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) # Get options list_all = kw.get('list_all', 0) posix = kw.get('posix', os.name == 'posix') strict = kw.get('strict', True) # Check if we have more than one argument to warrant extra processing: odict = {} # Dictionary with options args = arg_str.split() if len(args) >= 1: # If the list of inputs only has 0 or 1 thing in it, there's no # need to look for options argv = arg_split(arg_str, posix, strict) # Do regular option processing try: opts, args = getopt(argv, opt_str, long_opts) except GetoptError as e: 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) except AttributeError: odict[o] = [odict[o], a] except KeyError: if list_all: odict[o] = [a] else: odict[o] = a # Prepare opts,args for return opts = Struct(odict) if mode == 'string': args = ' '.join(args) return opts, args
def _run_with_debugger(self, code, code_ns, break_point, filename): """ Run `code` in debugger with a break point. Parameters ---------- code : str Code to execute. code_ns : dict A namespace in which `code` is executed. break_point : str Line number in the file specified by `filename` argument or a string in the format ``file:line``. In the latter case, `filename` is ignored. See also :func:`.parse_breakpoint`. filename : str Path to the file in which break point is specified. Raises ------ UsageError If no meaningful break point is given by `break_point` and `filename`. """ deb = debugger.Pdb(self.shell.colors) # reset Breakpoint state, which is moronically kept # in a class bdb.Breakpoint.next = 1 bdb.Breakpoint.bplist = {} bdb.Breakpoint.bpbynumber = [None] # Set an initial breakpoint to stop execution maxtries = 10 bp_file, bp_line = parse_breakpoint(break_point, filename) checkline = deb.checkline(bp_file, bp_line) if not checkline: for bp in range(bp_line + 1, bp_line + maxtries + 1): if deb.checkline(bp_file, bp): break else: msg = ("\nI failed to find a valid line to set " "a breakpoint\n" "after trying up to line: %s.\n" "Please set a valid breakpoint manually " "with the -b option." % bp) raise UsageError(msg) # if we find a good linenumber, set the breakpoint deb.do_break('%s:%s' % (bp_file, bp_line)) # Mimic Pdb._runscript(...) deb._wait_for_mainpyfile = True deb.mainpyfile = deb.canonic(filename) # Start file run print "NOTE: Enter 'c' at the", print "%s prompt to start your script." % deb.prompt try: #save filename so it can be used by methods on the deb object deb._exec_filename = filename deb.run(code, code_ns) except: etype, value, tb = sys.exc_info() # Skip three frames in the traceback: the %run one, # one inside bdb.py, and the command-line typed by the # user (run by exec in pdb itself). self.shell.InteractiveTB(etype, value, tb, tb_offset=3)
def _R_repr(obj): if isinstance(obj, bool): return 'TRUE' if obj else 'FALSE' elif isinstance(obj, (int, float, str)): return repr(obj) elif isinstance(obj, Sequence): if len(obj) == 0: return 'c()' # if the data is of homogeneous type, let us use c() # otherwise use list() # this can be confusion but list can be difficult to handle if homogeneous_type(obj): return 'c(' + ','.join(_R_repr(x) for x in obj) + ')' else: return 'list(' + ','.join(_R_repr(x) for x in obj) + ')' elif obj is None: return 'NULL' elif isinstance(obj, dict): return 'list(' + ','.join('{}={}'.format(x, _R_repr(y)) for x, y in obj.items()) + ')' elif isinstance(obj, set): return 'list(' + ','.join(_R_repr(x) for x in obj) + ')' else: import numpy import pandas if isinstance(obj, (numpy.intc, numpy.intp, numpy.int8, numpy.int16, numpy.int32, numpy.int64,\ numpy.uint8, numpy.uint16, numpy.uint32, numpy.uint64, numpy.float16, numpy.float32, \ numpy.float64)): return repr(obj) elif isinstance(obj, numpy.matrixlib.defmatrix.matrix): try: import feather except ImportError: raise UsageError( 'The feather-format module is required to pass numpy matrix as R matrix' 'See https://github.com/wesm/feather/tree/master/python for details.' ) feather_tmp_ = tempfile.NamedTemporaryFile(suffix='.feather', delete=False).name feather.write_dataframe(pandas.DataFrame(obj).copy(), feather_tmp_) return 'data.matrix(read_feather("{}"))'.format(feather_tmp_) elif isinstance(obj, numpy.ndarray): return 'c(' + ','.join(_R_repr(x) for x in obj) + ')' elif isinstance(obj, pandas.DataFrame): try: import feather except ImportError: raise UsageError( 'The feather-format module is required to pass pandas DataFrame as R data.frame' 'See https://github.com/wesm/feather/tree/master/python for details.' ) feather_tmp_ = tempfile.NamedTemporaryFile(suffix='.feather', delete=False).name try: data = obj.copy() feather.write_dataframe(data, feather_tmp_) except: # if data cannot be written, we try to manipulate data # frame to have consistent types and try again for c in data.columns: if not homogeneous_type(data[c]): data[c] = [str(x) for x in data[c]] feather.write_dataframe(data, feather_tmp_) return 'read_feather("{}")'.format(feather_tmp_) else: return repr('Unsupported datatype {}'.format(short_repr(obj)))
def mon(self, parameter_s=''): """Monitor a given attribute. %mon -a <attribute name> - activates monitoring of given attribute %mon -d <attribute name> - deactivates monitoring of given attribute %mon -r - deactivates monitoring of all attributes %mon -i <id> - displays detailed information for the event with given id %mon -l <dev filter> <attr filter> - shows event table filtered with the regular expression for attribute name %mon - shows event table (= %mon -i .* .*)""" db = __get_db() if db is None: print("You are not connected to any Tango Database.") return # make sure parameter_s is a str and not a unicode parameter_s = str(parameter_s) opts, args = self.parse_options(parameter_s, 'adril', mode='list') if len(args) > 3: raise UsageError("%mon: too many arguments") if 'd' in opts: try: todel = args[0] except IndexError: raise UsageError("%mon -d: must provide an attribute to unmonitor") else: try: dev, _, attr = todel.rpartition("/") subscriptions = __get_device_subscriptions(dev) attr_id = subscriptions[attr.lower()] del subscriptions[attr.lower()] d = __get_device_proxy(dev) d.unsubscribe_event(attr_id) print("Stopped monitoring '%s'" % todel) except KeyError: raise UsageError("%%mon -d: Not monitoring '%s'" % todel) elif 'a' in opts: try: toadd = args[0] except IndexError: raise UsageError("%mon -a: must provide an attribute to monitor") dev, _, attr = toadd.rpartition("/") subscriptions = __get_device_subscriptions(dev) attr_id = subscriptions.get(attr.lower()) if attr_id is not None: raise UsageError("%%mon -a: Already monitoring '%s'" % toadd) d = __get_device_proxy(dev) w = __get_event_log() model = w.model() attr_id = d.subscribe_event(attr, PyTango.EventType.CHANGE_EVENT, model, []) subscriptions[attr.lower()] = attr_id print("'%s' is now being monitored. Type 'mon' to see all events" % toadd) elif 'r' in opts: for d, v in db._db_cache.devices.items(): d, subs = v[3], v[4] for _id in subs.values(): d.unsubscribe_event(_id) v[4] = {} elif 'i' in opts: try: evtid = int(args[0]) except IndexError: raise UsageError("%mon -i: must provide an event ID") except ValueError: raise UsageError("%mon -i: must provide a valid event ID") try: w = __get_event_log() e = w.getEvents()[evtid] if e.err: print(str(PyTango.DevFailed(*e.errors))) else: print(str(e)) except IndexError: raise UsageError("%mon -i: must provide a valid event ID") elif 'l' in opts: try: dexpr = args[0] aexpr = args[1] except IndexError: raise UsageError("%mon -l: must provide valid device and " \ "attribute filters") w = __get_event_log() w.show(dexpr, aexpr) else: w = __get_event_log() w.show()
def bokeh(self, arg, line=None): """ Set up Bokeh to work interactively. This function lets you activate bokeh interactive support at any point during an IPython session. It does not import any other bokeh objects into the interactive namespace. Examples -------- In [1]: %install_ext url_for_bokeh_extension In [2]: %load_ext bokeh_magic To load it each time IPython starts, list it in your configuration file: c.InteractiveShellApp.extensions = ['bokeh_magic'] To enable bokeh for usage with the IPython Notebook:: In [3]: %bokeh --notebook [-n] Then you can use a several `modes` (show, hold, figure):: In [4]: %bokeh --show [-s] # to enable the autoshow function In [5]: %bokeh --show-off [-s-off] to disable the autoshow function You can add concatenate `modes` as arguments:: In [6]: %bokeh --notebook [-n] --show-off [-s-off] Note: In order to actually use this magic, you need to have get_ipython(), so you need to have a running IPython kernel. """ # Get the current running IPython instance. ip = get_ipython() # Parse the arguments. args = parse_argstring(self.bokeh, arg) # Activate/deactivate the execution of func accordingly with the args. if args.notebook: # Configuring embedded BokehJS mode. if not self.has_run: self.notebook_output() if args.figure: if not self.has_run: self.notebook_output() # Register the figure function. if self.is_ipytwo: ip.events.register('pre_run_cell', figure) print("Automatic figure() is enabled.") else: #ip.set_hook('pre_run_code_hook', figure) # not working print( "The --figure mode is not supported for this version of IPython." ) elif args.figure_off: if not self.has_run: self.notebook_output() if self.is_ipytwo: try: # Unregister a figure function. ip.events.unregister('pre_run_cell', figure) print("Automatic figure() is disabled.") except ValueError: raise UsageError( """You have to enable the --figure mode before trying to disable it.""" ) else: print( "The --figure mode is not supported for this version of IPython." ) if args.hold: if not self.has_run: self.notebook_output() # Register the hold function. if self.is_ipytwo: ip.events.register('pre_run_cell', self.notebook_hold) print("Automatic hold() is enabled.") else: ip.set_hook('pre_run_code_hook', hold) print( "Automatic hold() is irreversible for IPython 1.x. Just restart your kernel to disable." ) elif args.hold_off: if not self.has_run: self.notebook_output() if self.is_ipytwo: try: # Unregister a figure function. ip.events.unregister('pre_run_cell', self.notebook_hold) print("Automatic hold() is disabled.") except ValueError: raise UsageError( """You have to enable the --hold mode before trying to disable it.""" ) else: print( "Automatic hold() can not be disabled for IPython 1.x without restarting your kernel. Did you activate it before?" ) if args.show: if not self.has_run: self.notebook_output() # Register a function for calling after code execution. if self.is_ipytwo: ip.events.register('post_run_cell', self.notebook_show) else: ip.register_post_execute(self.notebook_show) print("Automatic show() is enabled.") elif args.show_off: if not self.has_run: self.notebook_output() if self.is_ipytwo: try: # Unregister a function ip.events.unregister('post_run_cell', self.notebook_show) print("Automatic show() is disabled.") except ValueError: raise UsageError( """You have to enable the --show mode before trying to disable it.""" ) else: try: # Unregister a function from the _post_execute dict. del ip._post_execute[self.notebook_show] print("Automatic show() is disabled.") except KeyError: raise UsageError( """You have to enable the --show mode before trying to disable it.""" )
def bowtie(self, line=''): """Build and serve a Bowtie app.""" opts, appvar = self.parse_options(line, 'w:h:b:p:') width = opts.get('w', 1500) height = opts.get('h', 1000) border = opts.get('b', 2) port = opts.get('p', 9991) host = '0.0.0.0' sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) result = sock.connect_ex((host, port)) if result == 0: raise Exception('Port {} is unavailable on host {}, aborting.'.format(port, host)) global_ns = self.shell.user_global_ns local_ns = self.shell.user_ns try: # pylint: disable=eval-used app = eval(appvar, global_ns, local_ns) except NameError: raise UsageError('Could not find App {}'.format(appvar)) if not isinstance(app, App): raise UsageError('App is of type {} needs to be type <bowtie.App>'.format(type(app))) # pylint: disable=protected-access app._build(notebook=get_notebook_name()) filepath = './{}/src/server.py'.format(_DIRECTORY) if os.path.isfile(filepath): self.server = Popen(['python', '-u', filepath], stdout=PIPE, stderr=STDOUT) else: raise FileNotFoundError('Cannot find "{}". Did you build the app?'.format(filepath)) def flush_stdout(cmd): """Flush stdout from command continuously.""" while True: line = cmd.stdout.readline() if line == b'' and cmd.poll() is not None: return cmd.poll() print(line.decode('utf-8'), end='') raise Exception() thread = Thread(target=flush_stdout, args=(self.server,)) thread.daemon = True thread.start() while self.server.poll() is None: try: if requests.get('http://localhost:9991').ok: break except requests.exceptions.RequestException: continue time.sleep(1) else: print(self.server.stdout.read().decode('utf-8'), end='') clear_output() display(HTML( '<iframe src=http://localhost:9991 width={} height={} ' 'frameBorder={}></iframe>'.format(width, height, border) ))
def cd(self, parameter_s=''): """Change the current working directory. This command automatically maintains an internal list of directories you visit during your IPython session, in the variable _dh. The command %dhist shows this history nicely formatted. You can also do 'cd -<tab>' to see directory history conveniently. Usage: cd 'dir': changes to directory 'dir'. cd -: changes to the last visited directory. cd -<n>: changes to the n-th directory in the directory history. cd --foo: change to directory that matches 'foo' in history cd -b <bookmark_name>: jump to a bookmark set by %bookmark (note: cd <bookmark_name> is enough if there is no directory <bookmark_name>, but a bookmark with the name exists.) 'cd -b <tab>' allows you to tab-complete bookmark names. Options: -q: quiet. Do not print the working directory after the cd command is executed. By default IPython's cd command does print this directory, since the default prompts do not display path information. Note that !cd doesn't work for this purpose because the shell where !command runs is immediately discarded after executing 'command'. Examples -------- :: In [10]: cd parent/child /home/tsuser/parent/child """ oldcwd = os.getcwdu() numcd = re.match(r'(-)(\d+)$', parameter_s) # jump in directory history by number 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 else: opts = {} elif parameter_s.startswith('--'): ps = None fallback = None pat = parameter_s[2:] dh = self.shell.user_ns['_dh'] # first search only by basename (last component) 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 # if we have no last part match, pick the first full path match if ps is None: ps = fallback if ps is None: print "No matching entry in directory history" return else: opts = {} else: #turn all non-space-escaping backslashes to slashes, # for c:\windows\directory\names\ parameter_s = re.sub(r'\\(?! )', '/', parameter_s) opts, ps = self.parse_options(parameter_s, 'qb', mode='string') # jump to previous if ps == '-': try: ps = self.shell.user_ns['_dh'][-2] except IndexError: raise UsageError('%cd -: No previous directory to change to.') # jump to bookmark if needed else: if not os.path.isdir(ps) or 'b' in opts: bkms = self.shell.db.get('bookmarks', {}) if ps in bkms: target = bkms[ps] print '(bookmark:%s) -> %s' % (ps, target) ps = target else: if 'b' in opts: raise UsageError( "Bookmark '%s' not found. " "Use '%%bookmark -l' to see your bookmarks." % ps) # strip extra quotes on Windows, because os.chdir doesn't like them ps = unquote_filename(ps) # at this point ps should point to the target dir if ps: try: os.chdir(os.path.expanduser(ps)) if hasattr(self.shell, 'term_title') and self.shell.term_title: set_term_title('IPython: ' + abbrev_cwd()) except OSError: print sys.exc_info()[1] else: cwd = os.getcwdu() dhist = self.shell.user_ns['_dh'] if oldcwd != cwd: dhist.append(cwd) self.shell.db['dhist'] = compress_dhist(dhist)[-100:] else: os.chdir(self.shell.home_dir) if hasattr(self.shell, 'term_title') and self.shell.term_title: set_term_title('IPython: ' + '~') cwd = os.getcwdu() dhist = self.shell.user_ns['_dh'] if oldcwd != cwd: dhist.append(cwd) self.shell.db['dhist'] = compress_dhist(dhist)[-100:] if not 'q' in opts and self.shell.user_ns['_dh']: print self.shell.user_ns['_dh'][-1]
def cd(self, parameter_s=''): """Change the current working directory. This command automatically maintains an internal list of directories you visit during your IPython session, in the variable ``_dh``. The command :magic:`%dhist` shows this history nicely formatted. You can also do ``cd -<tab>`` to see directory history conveniently. Usage: - ``cd 'dir'``: changes to directory 'dir'. - ``cd -``: changes to the last visited directory. - ``cd -<n>``: changes to the n-th directory in the directory history. - ``cd --foo``: change to directory that matches 'foo' in history - ``cd -b <bookmark_name>``: jump to a bookmark set by %bookmark - Hitting a tab key after ``cd -b`` allows you to tab-complete bookmark names. .. note:: ``cd <bookmark_name>`` is enough if there is no directory ``<bookmark_name>``, but a bookmark with the name exists. Options: -q Be quiet. Do not print the working directory after the cd command is executed. By default IPython's cd command does print this directory, since the default prompts do not display path information. .. note:: Note that ``!cd`` doesn't work for this purpose because the shell where ``!command`` runs is immediately discarded after executing 'command'. Examples -------- :: In [10]: cd parent/child /home/tsuser/parent/child """ try: oldcwd = os.getcwd() except FileNotFoundError: # Happens if the CWD has been deleted. oldcwd = None numcd = re.match(r'(-)(\d+)$',parameter_s) # jump in directory history by number 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 else: opts = {} elif parameter_s.startswith('--'): ps = None fallback = None pat = parameter_s[2:] dh = self.shell.user_ns['_dh'] # first search only by basename (last component) 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 # if we have no last part match, pick the first full path match if ps is None: ps = fallback if ps is None: print("No matching entry in directory history") return else: opts = {} else: opts, ps = self.parse_options(parameter_s, 'qb', mode='string') # jump to previous if ps == '-': try: ps = self.shell.user_ns['_dh'][-2] except IndexError as e: raise UsageError('%cd -: No previous directory to change to.') from e # jump to bookmark if needed else: if not os.path.isdir(ps) or 'b' in opts: bkms = self.shell.db.get('bookmarks', {}) if ps in bkms: target = bkms[ps] print('(bookmark:%s) -> %s' % (ps, target)) ps = target else: if 'b' in opts: raise UsageError("Bookmark '%s' not found. " "Use '%%bookmark -l' to see your bookmarks." % ps) # at this point ps should point to the target dir if ps: try: os.chdir(os.path.expanduser(ps)) if hasattr(self.shell, 'term_title') and self.shell.term_title: set_term_title(self.shell.term_title_format.format(cwd=abbrev_cwd())) except OSError: print(sys.exc_info()[1]) else: cwd = os.getcwd() dhist = self.shell.user_ns['_dh'] if oldcwd != cwd: dhist.append(cwd) self.shell.db['dhist'] = compress_dhist(dhist)[-100:] else: os.chdir(self.shell.home_dir) if hasattr(self.shell, 'term_title') and self.shell.term_title: set_term_title(self.shell.term_title_format.format(cwd="~")) cwd = os.getcwd() dhist = self.shell.user_ns['_dh'] if oldcwd != cwd: dhist.append(cwd) self.shell.db['dhist'] = compress_dhist(dhist)[-100:] if not 'q' in opts and not self.cd_force_quiet and self.shell.user_ns['_dh']: print(self.shell.user_ns['_dh'][-1])
def macro(self, parameter_s=''): """Define a macro for future re-execution. It accepts ranges of history, filenames or string objects. Usage:\\ %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ... Options: -r: use 'raw' input. By default, the 'processed' history is used, so that magics are loaded in their transformed version to valid Python. If this option is given, the raw input as typed at the command line is used instead. -q: quiet macro definition. By default, a tag line is printed to indicate the macro has been created, and then the contents of the macro are printed. If this option is given, then no printout is produced once the macro is created. This will define a global variable called `name` which is a string made of joining the slices and lines you specify (n1,n2,... numbers above) from your input history into a single string. This variable acts like an automatic function which re-executes those lines as if you had typed them. You just type 'name' at the prompt and the code executes. The syntax for indicating input ranges is described in %history. Note: as a 'hidden' feature, you can also use traditional python slice notation, where N:M means numbers N through M-1. For example, if your history contains (print using %hist -n ):: 44: x=1 45: y=3 46: z=x+y 47: print x 48: a=5 49: print 'x',x,'y',y you can create a macro with lines 44 through 47 (included) and line 49 called my_macro with:: In [55]: %macro my_macro 44-47 49 Now, typing `my_macro` (without quotes) will re-execute all this code in one pass. You don't need to give the line-numbers in order, and any given line number can appear multiple times. You can assemble macros with any lines from your input history in any order. The macro is a simple object which holds its value in an attribute, but IPython's display system checks for macros and executes them as code instead of printing them when you type their name. You can view a macro's contents by explicitly printing it with:: print macro_name """ opts, args = self.parse_options(parameter_s, 'rq', mode='list') if not args: # List existing macros return sorted(k for k,v in self.shell.user_ns.iteritems() if\ isinstance(v, Macro)) if len(args) == 1: raise UsageError( "%macro insufficient args; usage '%macro name n1-n2 n3-4...") name, codefrom = args[0], " ".join(args[1:]) #print 'rng',ranges # dbg try: lines = self.shell.find_user_code(codefrom, 'r' in opts) except (ValueError, TypeError) as e: print e.args[0] return macro = Macro(lines) self.shell.define_macro(name, macro) if not ('q' in opts): print 'Macro `%s` created. To execute, type its name (without quotes).' % name print '=== Macro contents: ===' print macro,
def magic_store(self, parameter_s=''): """Lightweight persistence for python variables. Example: ville@badger[~]|1> A = ['hello',10,'world']\\ ville@badger[~]|2> %store A\\ ville@badger[~]|3> Exit (IPython session is closed and started again...) ville@badger:~$ ipython -p pysh\\ ville@badger[~]|1> print A ['hello', 10, 'world'] Usage: %store - Show list of all variables and their current values\\ %store <var> - Store the *current* value of the variable to disk\\ %store -d <var> - Remove the variable and its value from storage\\ %store -z - Remove all variables from storage\\ %store -r - Refresh all variables from store (delete current vals)\\ %store foo >a.txt - Store value of foo to new file a.txt\\ %store foo >>a.txt - Append value of foo to file a.txt\\ It should be noted that if you change the value of a variable, you need to %store it again if you want to persist the new value. Note also that the variables will need to be pickleable; most basic python types can be safely %stored. Also aliases can be %store'd across sessions. """ opts,argsl = self.parse_options(parameter_s,'drz',mode='string') args = argsl.split(None,1) ip = self.getapi() db = ip.db # delete if opts.has_key('d'): try: todel = args[0] except IndexError: raise UsageError('You must provide the variable to forget') else: try: del db['autorestore/' + todel] except: raise UsageError("Can't delete variable '%s'" % todel) # reset elif opts.has_key('z'): for k in db.keys('autorestore/*'): del db[k] elif opts.has_key('r'): refresh_variables(ip) # run without arguments -> list variables & values elif not args: vars = self.db.keys('autorestore/*') vars.sort() if vars: size = max(map(len,vars)) else: size = 0 print 'Stored variables and their in-db values:' fmt = '%-'+str(size)+'s -> %s' get = db.get for var in vars: justkey = os.path.basename(var) # print 30 first characters from every var print fmt % (justkey,repr(get(var,'<unavailable>'))[:50]) # default action - store the variable else: # %store foo >file.txt or >>file.txt if len(args) > 1 and args[1].startswith('>'): fnam = os.path.expanduser(args[1].lstrip('>').lstrip()) if args[1].startswith('>>'): fil = open(fnam,'a') else: fil = open(fnam,'w') obj = ip.ev(args[0]) print "Writing '%s' (%s) to file '%s'." % (args[0], obj.__class__.__name__, fnam) if not isinstance (obj,basestring): from pprint import pprint pprint(obj,fil) else: fil.write(obj) if not obj.endswith('\n'): fil.write('\n') fil.close() return # %store foo try: obj = ip.user_ns[args[0]] except KeyError: # it might be an alias # This needs to be refactored to use the new AliasManager stuff. if args[0] in self.alias_table: staliases = db.get('stored_aliases',{}) staliases[ args[0] ] = self.alias_table[ args[0] ] db['stored_aliases'] = staliases print "Alias stored:", args[0], self.alias_table[ args[0] ] return else: raise UsageError("Unknown variable '%s'" % args[0]) else: if isinstance(inspect.getmodule(obj), FakeModule): print textwrap.dedent("""\ Warning:%s is %s Proper storage of interactively declared classes (or instances of those classes) is not possible! Only instances of classes in real modules on file system can be %%store'd. """ % (args[0], obj) ) return #pickled = pickle.dumps(obj) self.db[ 'autorestore/' + args[0] ] = obj print "Stored '%s' (%s)" % (args[0], obj.__class__.__name__)
def time(self, line='', cell=None, local_ns=None): """Time execution of a Python statement or expression. The CPU and wall clock times are printed, and the value of the expression (if any) is returned. Note that under Win32, system time is always reported as 0, since it can not be measured. This function can be used both as a line and cell magic: - In line mode you can time a single-line statement (though multiple ones can be chained with using semicolons). - In cell mode, you can time the cell body (a directly following statement raises an error). This function provides very basic timing functionality. Use the timeit magic for more controll over the measurement. Examples -------- :: In [1]: %time 2**128 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s Wall time: 0.00 Out[1]: 340282366920938463463374607431768211456L In [2]: n = 1000000 In [3]: %time sum(range(n)) CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s Wall time: 1.37 Out[3]: 499999500000L In [4]: %time print 'hello world' hello world CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s Wall time: 0.00 Note that the time needed by Python to compile the given expression will be reported if it is more than 0.1s. In this example, the actual exponentiation is done by Python at compilation time, so while the expression can take a noticeable amount of time to compute, that time is purely due to the compilation: In [5]: %time 3**9999; CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s Wall time: 0.00 s In [6]: %time 3**999999; CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s Wall time: 0.00 s Compiler : 0.78 s """ # fail immediately if the given expression can't be compiled if line and cell: raise UsageError("Can't use statement directly after '%%time'!") if cell: expr = self.shell.input_transformer_manager.transform_cell(cell) else: expr = self.shell.input_transformer_manager.transform_cell(line) # Minimum time above which parse time will be reported tp_min = 0.1 t0 = clock() expr_ast = ast.parse(expr) tp = clock() - t0 # Apply AST transformations expr_ast = self.shell.transform_ast(expr_ast) # Minimum time above which compilation time will be reported tc_min = 0.1 if len(expr_ast.body) == 1 and isinstance(expr_ast.body[0], ast.Expr): mode = 'eval' source = '<timed eval>' expr_ast = ast.Expression(expr_ast.body[0].value) else: mode = 'exec' source = '<timed exec>' t0 = clock() code = compile(expr_ast, source, mode) tc = clock() - t0 # skew measurement as little as possible glob = self.shell.user_ns wtime = time.time # time execution wall_st = wtime() if mode == 'eval': st = clock2() out = eval(code, glob, local_ns) end = clock2() else: st = clock2() exec code in glob, local_ns end = clock2() out = None wall_end = wtime() # Compute actual times and report wall_time = wall_end - wall_st cpu_user = end[0] - st[0] cpu_sys = end[1] - st[1] cpu_tot = cpu_user + cpu_sys # On windows cpu_sys is always zero, so no new information to the next print if sys.platform != 'win32': print "CPU times: user %s, sys: %s, total: %s" % \ (_format_time(cpu_user),_format_time(cpu_sys),_format_time(cpu_tot)) print "Wall time: %s" % _format_time(wall_time) if tc > tc_min: print "Compiler : %s" % _format_time(tc) if tp > tp_min: print "Parser : %s" % _format_time(tp) return out
def save(self, parameter_s=''): """Save a set of lines or a macro to a given filename. Usage:\\ %save [options] filename n1-n2 n3-n4 ... n5 .. n6 ... Options: -r: use 'raw' input. By default, the 'processed' history is used, so that magics are loaded in their transformed version to valid Python. If this option is given, the raw input as typed as the command line is used instead. -f: force overwrite. If file exists, %save will prompt for overwrite unless -f is given. -a: append to the file instead of overwriting it. This function uses the same syntax as %history for input ranges, then saves the lines to the filename you specify. It adds a '.py' extension to the file if you don't do so yourself, and it asks for confirmation before overwriting existing files. If `-r` option is used, the default extension is `.ipy`. """ opts, args = self.parse_options(parameter_s, 'fra', mode='list') if not args: raise UsageError('Missing filename.') raw = 'r' in opts force = 'f' in opts append = 'a' in opts mode = 'a' if append else 'w' ext = u'.ipy' if raw else u'.py' fname, codefrom = args[0], " ".join(args[1:]) if not fname.endswith((u'.py', u'.ipy')): fname += ext file_exists = os.path.isfile(fname) if file_exists and not force and not append: try: overwrite = self.shell.ask_yes_no( 'File `%s` exists. Overwrite (y/[N])? ' % fname, default='n') except StdinNotImplementedError: print( "File `%s` exists. Use `%%save -f %s` to force overwrite" % (fname, parameter_s)) return if not overwrite: print('Operation cancelled.') return try: cmds = self.shell.find_user_code(codefrom, raw) except (TypeError, ValueError) as e: print(e.args[0]) return out = py3compat.cast_unicode(cmds) with io.open(fname, mode, encoding="utf-8") as f: if not file_exists or not append: f.write(u"# coding: utf-8\n") f.write(out) # make sure we end on a newline if not out.endswith(u'\n'): f.write(u'\n') print('The following commands were written to file `%s`:' % fname) print(cmds)
def enable_gui(gui): from .eventloops import enable_gui as real_enable_gui try: real_enable_gui(gui) except ValueError as e: raise UsageError("%s" % e)
def load(self, arg_s): """Load code into the current frontend. Usage:\\ %load [options] source where source can be a filename, URL, input history range, macro, or element in the user namespace Options: -r <lines>: Specify lines or ranges of lines to load from the source. Ranges could be specified as x-y (x..y) or in python-style x:y (x..(y-1)). Both limits x and y can be left blank (meaning the beginning and end of the file, respectively). -s <symbols>: Specify function or classes to load from python source. -y : Don't ask confirmation for loading source above 200 000 characters. -n : Include the user's namespace when searching for source code. This magic command can either take a local filename, a URL, an history range (see %history) or a macro as argument, it will prompt for confirmation before loading source with more than 200 000 characters, unless -y flag is passed or if the frontend does not support raw_input:: %load myscript.py %load 7-27 %load myMacro %load http://www.example.com/myscript.py %load -r 5-10 myscript.py %load -r 10-20,30,40: foo.py %load -s MyClass,wonder_function myscript.py %load -n MyClass %load -n my_module.wonder_function """ opts, args = self.parse_options(arg_s, 'yns:r:') if not args: raise UsageError('Missing filename, URL, input history range, ' 'macro, or element in the user namespace.') search_ns = 'n' in opts contents = self.shell.find_user_code(args, search_ns=search_ns) if 's' in opts: try: blocks, not_found = extract_symbols(contents, opts['s']) except SyntaxError: # non python code error("Unable to parse the input as valid Python code") return if len(not_found) == 1: warn('The symbol `%s` was not found' % not_found[0]) elif len(not_found) > 1: warn('The symbols %s were not found' % get_text_list(not_found, wrap_item_with='`')) contents = '\n'.join(blocks) if 'r' in opts: ranges = opts['r'].replace(',', ' ') lines = contents.split('\n') slices = extract_code_ranges(ranges) contents = [lines[slice(*slc)] for slc in slices] contents = '\n'.join( strip_initial_indent(chain.from_iterable(contents))) l = len(contents) # 200 000 is ~ 2500 full 80 character lines # so in average, more than 5000 lines if l > 200000 and 'y' not in opts: try: ans = self.shell.ask_yes_no(("The text you're trying to load seems pretty big" \ " (%d characters). Continue (y/[N]) ?" % l), default='n') except StdinNotImplementedError: # assume yes if raw input not implemented ans = True if ans is False: print('Operation cancelled.') return contents = "# %load {}\n".format(arg_s) + contents self.shell.set_next_input(contents, replace=True)
def lprun(self, parameter_s=''): """ Execute a statement under the line-by-line profiler from the line_profiler module. Usage: %lprun -f func1 -f func2 <statement> The given statement (which doesn't require quote marks) is run via the LineProfiler. Profiling is enabled for the functions specified by the -f options. The statistics will be shown side-by-side with the code through the pager once the statement has completed. Options: -f <function>: LineProfiler only profiles functions and methods it is told to profile. This option tells the profiler about these functions. Multiple -f options may be used. The argument may be any expression that gives a Python function or method object. However, one must be careful to avoid spaces that may confuse the option parser. -m <module>: Get all the functions/methods in a module One or more -f or -m options are required to get any useful results. -D <filename>: dump the raw statistics out to a pickle file on disk. The usual extension for this is ".lprof". These statistics may be viewed later by running line_profiler.py as a script. -T <filename>: dump the text-formatted statistics with the code side-by-side out to a text file. -r: return the LineProfiler object after it has completed profiling. -s: strip out all entries from the print-out that have zeros. -u: specify time unit for the print-out in seconds. """ # Escape quote markers. opts_def = Struct(D=[''], T=[''], f=[], m=[], u=None) parameter_s = parameter_s.replace('"', r'\"').replace("'", r"\'") opts, arg_str = self.parse_options(parameter_s, 'rsf:m:D:T:u:', list_all=True) opts.merge(opts_def) global_ns = self.shell.user_global_ns local_ns = self.shell.user_ns # Get the requested functions. funcs = [] for name in opts.f: try: funcs.append(eval(name, global_ns, local_ns)) except Exception as e: raise UsageError('Could not find function %r.\n%s: %s' % (name, e.__class__.__name__, e)) profile = LineProfiler(*funcs) # Get the modules, too for modname in opts.m: try: mod = __import__(modname, fromlist=['']) profile.add_module(mod) except Exception as e: raise UsageError('Could not find module %r.\n%s: %s' % (modname, e.__class__.__name__, e)) if opts.u is not None: try: output_unit = float(opts.u[0]) except Exception as e: raise TypeError("Timer unit setting must be a float.") else: output_unit = None # Add the profiler to the builtins for @profile. if PY3: import builtins else: import __builtin__ as builtins if 'profile' in builtins.__dict__: had_profile = True old_profile = builtins.__dict__['profile'] else: had_profile = False old_profile = None builtins.__dict__['profile'] = profile try: try: profile.runctx(arg_str, global_ns, local_ns) message = '' except SystemExit: message = """*** SystemExit exception caught in code being profiled.""" except KeyboardInterrupt: message = ( "*** KeyboardInterrupt exception caught in code being " "profiled.") finally: if had_profile: builtins.__dict__['profile'] = old_profile # Trap text output. stdout_trap = StringIO() profile.print_stats(stdout_trap, output_unit=output_unit, stripzeros='s' in opts) output = stdout_trap.getvalue() output = output.rstrip() page(output) print(message, end="") dump_file = opts.D[0] if dump_file: profile.dump_stats(dump_file) print('\n*** Profile stats pickled to file %r. %s' % (dump_file, message)) text_file = opts.T[0] if text_file: pfile = open(text_file, 'w') pfile.write(output) pfile.close() print('\n*** Profile printout saved to text file %r. %s' % (text_file, message)) return_value = None if 'r' in opts: return_value = profile return return_value
def ensure_valid_filename(fname): if fname == '': raise UsageError("Invalid filename '{}'".format(fname)) if not fname.endswith('.json'): fname = fname + ".json" return fname
def config(self, s): """configure IPython %config Class[.trait=value] This magic exposes most of the IPython config system. Any Configurable class should be able to be configured with the simple line:: %config Class.trait=value Where `value` will be resolved in the user's namespace, if it is an expression or variable name. Examples -------- To see what classes are available for config, pass no arguments:: In [1]: %config Available objects for config: TerminalInteractiveShell HistoryManager PrefilterManager AliasManager IPCompleter PromptManager DisplayFormatter To view what is configurable on a given class, just pass the class name:: In [2]: %config IPCompleter IPCompleter options ----------------- IPCompleter.omit__names=<Enum> Current: 2 Choices: (0, 1, 2) Instruct the completer to omit private method names Specifically, when completing on ``object.<tab>``. When 2 [default]: all names that start with '_' will be excluded. When 1: all 'magic' names (``__foo__``) will be excluded. When 0: nothing will be excluded. IPCompleter.merge_completions=<CBool> Current: True Whether to merge completion results into a single list If False, only the completion results from the first non-empty completer will be returned. IPCompleter.limit_to__all__=<CBool> Current: False Instruct the completer to use __all__ for the completion Specifically, when completing on ``object.<tab>``. When True: only those names in obj.__all__ will be included. When False [default]: the __all__ attribute is ignored IPCompleter.greedy=<CBool> Current: False Activate greedy completion This will enable completion on elements of lists, results of function calls, etc., but can be unsafe because the code is actually evaluated on TAB. but the real use is in setting values:: In [3]: %config IPCompleter.greedy = True and these values are read from the user_ns if they are variables:: In [4]: feeling_greedy=False In [5]: %config IPCompleter.greedy = feeling_greedy """ from IPython.config.loader import Config # some IPython objects are Configurable, but do not yet have # any configurable traits. Exclude them from the effects of # this magic, as their presence is just noise: configurables = [ c for c in self.shell.configurables if c.__class__.class_traits(config=True) ] classnames = [c.__class__.__name__ for c in configurables] line = s.strip() if not line: # print available configurable names print "Available objects for config:" for name in classnames: print " ", name return elif line in classnames: # `%config TerminalInteractiveShell` will print trait info for # TerminalInteractiveShell c = configurables[classnames.index(line)] cls = c.__class__ help = cls.class_get_help(c) # strip leading '--' from cl-args: help = re.sub(re.compile(r'^--', re.MULTILINE), '', help) print help return elif reg.match(line): cls, attr = line.split('.') return getattr(configurables[classnames.index(cls)], attr) elif '=' not in line: msg = "Invalid config statement: %r, "\ "should be `Class.trait = value`." ll = line.lower() for classname in classnames: if ll == classname.lower(): msg = msg + '\nDid you mean %s (note the case)?' % classname break raise UsageError(msg % line) # otherwise, assume we are setting configurables. # leave quotes on args when splitting, because we want # unquoted args to eval in user_ns cfg = Config() exec "cfg." + line in locals(), self.shell.user_ns for configurable in configurables: try: configurable.update_config(cfg) except Exception as e: error(e)
def config(self, s): """configure IPython %config Class[.trait=value] This magic exposes most of the IPython config system. Any Configurable class should be able to be configured with the simple line:: %config Class.trait=value Where `value` will be resolved in the user's namespace, if it is an expression or variable name. Examples -------- To see what classes are available for config, pass no arguments:: In [1]: %config Available objects for config: AliasManager DisplayFormatter HistoryManager IPCompleter LoggingMagics MagicsManager OSMagics PrefilterManager ScriptMagics TerminalInteractiveShell To view what is configurable on a given class, just pass the class name:: In [2]: %config IPCompleter IPCompleter(Completer) options ---------------------------- IPCompleter.backslash_combining_completions=<Bool> Enable unicode completions, e.g. \\alpha<tab> . Includes completion of latex commands, unicode names, and expanding unicode characters back to latex commands. Current: True IPCompleter.debug=<Bool> Enable debug for the Completer. Mostly print extra information for experimental jedi integration. Current: False IPCompleter.greedy=<Bool> Activate greedy completion PENDING DEPRECATION. this is now mostly taken care of with Jedi. This will enable completion on elements of lists, results of function calls, etc., but can be unsafe because the code is actually evaluated on TAB. Current: False IPCompleter.jedi_compute_type_timeout=<Int> Experimental: restrict time (in milliseconds) during which Jedi can compute types. Set to 0 to stop computing types. Non-zero value lower than 100ms may hurt performance by preventing jedi to build its cache. Current: 400 IPCompleter.limit_to__all__=<Bool> DEPRECATED as of version 5.0. Instruct the completer to use __all__ for the completion Specifically, when completing on ``object.<tab>``. When True: only those names in obj.__all__ will be included. When False [default]: the __all__ attribute is ignored Current: False IPCompleter.merge_completions=<Bool> Whether to merge completion results into a single list If False, only the completion results from the first non-empty completer will be returned. Current: True IPCompleter.omit__names=<Enum> Instruct the completer to omit private method names Specifically, when completing on ``object.<tab>``. When 2 [default]: all names that start with '_' will be excluded. When 1: all 'magic' names (``__foo__``) will be excluded. When 0: nothing will be excluded. Choices: any of [0, 1, 2] Current: 2 IPCompleter.profile_completions=<Bool> If True, emit profiling data for completion subsystem using cProfile. Current: False IPCompleter.profiler_output_dir=<Unicode> Template for path at which to output profile data for completions. Current: '.completion_profiles' IPCompleter.use_jedi=<Bool> Experimental: Use Jedi to generate autocompletions. Default to True if jedi is installed. Current: True but the real use is in setting values:: In [3]: %config IPCompleter.greedy = True and these values are read from the user_ns if they are variables:: In [4]: feeling_greedy=False In [5]: %config IPCompleter.greedy = feeling_greedy """ from traitlets.config.loader import Config # some IPython objects are Configurable, but do not yet have # any configurable traits. Exclude them from the effects of # this magic, as their presence is just noise: configurables = sorted(set([ c for c in self.shell.configurables if c.__class__.class_traits(config=True) ]), key=lambda x: x.__class__.__name__) classnames = [c.__class__.__name__ for c in configurables] line = s.strip() if not line: # print available configurable names print("Available objects for config:") for name in classnames: print(" ", name) return elif line in classnames: # `%config TerminalInteractiveShell` will print trait info for # TerminalInteractiveShell c = configurables[classnames.index(line)] cls = c.__class__ help = cls.class_get_help(c) # strip leading '--' from cl-args: help = re.sub(re.compile(r'^--', re.MULTILINE), '', help) print(help) return elif reg.match(line): cls, attr = line.split('.') return getattr(configurables[classnames.index(cls)], attr) elif '=' not in line: msg = "Invalid config statement: %r, "\ "should be `Class.trait = value`." ll = line.lower() for classname in classnames: if ll == classname.lower(): msg = msg + '\nDid you mean %s (note the case)?' % classname break raise UsageError(msg % line) # otherwise, assume we are setting configurables. # leave quotes on args when splitting, because we want # unquoted args to eval in user_ns cfg = Config() exec("cfg." + line, self.shell.user_ns, locals()) for configurable in configurables: try: configurable.update_config(cfg) except Exception as e: error(e)
def colors(self, parameter_s=''): """Switch color scheme for prompts, info system and exception handlers. Currently implemented schemes: NoColor, Linux, LightBG. Color scheme names are not case-sensitive. Examples -------- To get a plain black and white terminal:: %colors nocolor """ 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 # local shortcut shell = self.shell import IPython.utils.rlineimpl as readline if not shell.colors_force and \ not readline.have_readline and \ (sys.platform == "win32" or sys.platform == "cli"): msg = """\ Proper color support under MS Windows requires the pyreadline library. You can find it at: http://ipython.org/pyreadline.html Gary's readline needs the ctypes module, from: http://starship.python.net/crew/theller/ctypes (Note that ctypes is already part of Python versions 2.5 and newer). Defaulting color scheme to 'NoColor'""" new_scheme = 'NoColor' warn(msg) # readline option is 0 if not shell.colors_force and not shell.has_readline: new_scheme = 'NoColor' # Set prompt colors try: shell.prompt_manager.color_scheme = new_scheme except: color_switch_err('prompt') else: shell.colors = \ shell.prompt_manager.color_scheme_table.active_scheme_name # Set exception colors try: shell.InteractiveTB.set_colors(scheme = new_scheme) shell.SyntaxTB.set_colors(scheme = new_scheme) except: color_switch_err('exception') # Set info (for 'object?') colors if shell.color_info: try: shell.inspector.set_active_scheme(new_scheme) except: color_switch_err('object inspector') else: shell.inspector.set_active_scheme('NoColor')
def writeandexecute(self, parameter_s='', cell=None): """Writes the content of the cell to a file and then executes the cell. Usage: %%writeandexecute [-d] -i <identifier> <filename> code code... Options: -i <identifier>: surrounds the code written to the file with a line containing the identifier. The use of an identifier enables you to easily overwrite a given code section in the output file. <filename>: the file to which the code should be written. Can be specified without a extension and can also include a directory (`dir/file`) -d: Write some debugging output Default: -- (no debugging output) This magic can be used to write the content of a cell to a .py file and afterwards execute the cell. This can be used as a replacement for the --script parameter to the notebook server. Code is replaced on the next execution (using the needed identifier) and other code can be appended by using the same file name. Examples -------- %%writeandexecute -i my_code_block functions.py print "Hello world" This would create a file "functions.py" with the following content ``` # -*- coding: utf-8 -*- # -- ==my_code_block== -- print "Hello world" # -- ==my_code_block== -- ``` Cell content is transformed, so %%magic commands are executed, but `get_ipython()` must be available. """ opts, args = self.parse_options(parameter_s, 'i:d') if cell is None: raise UsageError('Nothing to save!') if not ('i' in opts) or not opts['i']: raise UsageError('Missing indentifier: include "-i=<indentifier>"') identifier = opts['i'] debug = False if not "d" in opts else True if not args: raise UsageError('Missing filename') filename = args code_content = self.shell.input_transformer_manager.transform_cell( cell) self._save_to_file(filename, identifier, code_content, debug=debug) ip = get_ipython() ip.run_cell(cell)
def psource(self, parameter_s='', namespaces=None): """Print (or run through pager) the source code for an object.""" if not parameter_s: raise UsageError('Missing object name.') self.shell._inspect('psource',parameter_s, namespaces)
def hexint_aligned(s): """A hexint() that must be word aligned""" i = hexint(s) if i & 3: raise UsageError("Value must be word aligned: %x" % i) return i