def __init__(self, color_scheme='NoColor', call_pdb=False, ostream=None, parent=None, config=None): # Whether to call the interactive pdb debugger after printing # tracebacks or not super(TBTools, self).__init__(parent=parent, config=config) self.call_pdb = call_pdb # Output stream to write to. Note that we store the original value in # a private attribute and then make the public ostream a property, so # that we can delay accessing sys.stdout until runtime. The way # things are written now, the sys.stdout object is dynamically managed # so a reference to it should NEVER be stored statically. This # property approach confines this detail to a single location, and all # subclasses can simply access self.ostream for writing. self._ostream = ostream # Create color table self.color_scheme_table = exception_colors() self.set_colors(color_scheme) self.old_scheme = color_scheme # save initial value for toggles if call_pdb: self.pdb = debugger.Pdb() else: self.pdb = None
def get_active_physical_ports(self, aggregation_port): """ Return textFSM table with physical ports joined as "aggregation_port" """ from IPython import embed embed() from IPython.core import debugger debug = debugger.Pdb().set_trace debug() raw_out = self._send_command('display link-aggregation verbose ' + str(aggregation_port)) port_entries = textfsm_extractor(self, "display_link_aggregation_verbose", raw_out) a_ports = list() for row in port_entries: # Return only active ports if row['status'].lower() == 's': a_ports.append(self.normalize_port_name(row['port_name'])) if a_ports: print( f' --- Active ports of the aggregation_port {aggregation_port} ---' ) print( dumps(a_ports, sort_keys=True, indent=4, separators=(',', ': '))) return a_ports else: raise HpNoActiePortsInAggregation
def __init__(self, color_scheme='NoColor', call_pdb=False): # Whether to call the interactive pdb debugger after printing # tracebacks or not self.call_pdb = call_pdb # Create color table self.color_scheme_table = exception_colors() self.set_colors(color_scheme) self.old_scheme = color_scheme # save initial value for toggles if call_pdb: self.pdb = debugger.Pdb(self.color_scheme_table.active_scheme_name) else: self.pdb = None
def call_pydb(self, args): """Invoke pydb with the supplied parameters.""" try: import pydb except ImportError: raise ImportError("pydb doesn't seem to be installed.") if not hasattr(pydb.pydb, "runv"): raise ImportError("You need pydb version 1.19 or later installed.") argl = arg_split(args) # print argl # dbg if len(inspect.getargspec(pydb.runv)[0]) == 2: pdb = debugger.Pdb(color_scheme=self.colors) ip.history_saving_wrapper( lambda : pydb.runv(argl, pdb) )() else: ip.history_saving_wrapper( lambda : pydb.runv(argl) )()
def debugger(self,force=False): """Call up the pdb debugger if desired, always clean up the tb reference. Keywords: - force(False): by default, this routine checks the instance call_pdb flag and does not actually invoke the debugger if the flag is false. The 'force' option forces the debugger to activate even if the flag is false. If the call_pdb flag is set, the pdb interactive debugger is invoked. In all cases, the self.tb reference to the current traceback is deleted to prevent lingering references which hamper memory management. Note that each call to pdb() does an 'import readline', so if your app requires a special setup for the readline completers, you'll have to fix that by hand after invoking the exception handler.""" if force or self.call_pdb: if self.pdb is None: self.pdb = debugger.Pdb( self.color_scheme_table.active_scheme_name) # the system displayhook may have changed, restore the original # for pdb display_trap = DisplayTrap(hook=sys.__displayhook__) with display_trap: self.pdb.reset() # Find the right frame so we don't pop up inside ipython itself if hasattr(self,'tb') and self.tb is not None: etb = self.tb else: etb = self.tb = sys.last_traceback while self.tb is not None and self.tb.tb_next is not None: self.tb = self.tb.tb_next if etb and etb.tb_next: etb = etb.tb_next self.pdb.botframe = etb.tb_frame self.pdb.interaction(self.tb.tb_frame, self.tb) if hasattr(self,'tb'): del self.tb
from trainer import Trainer from causal_graph import get_causal_graph from utils import prepare_dirs_and_logger, save_configs #Generic configuration arguments from config import get_config #Submodel specific configurations from causal_controller.config import get_config as get_cc_config from causal_dcgan.config import get_config as get_dcgan_config from causal_began.config import get_config as get_began_config from causal_began import CausalBEGAN from causal_dcgan import CausalGAN from IPython.core import debugger debug = debugger.Pdb().set_trace def get_trainer(): print('tf: resetting default graph!') tf.reset_default_graph()#for repeated calls in ipython ####GET CONFIGURATION#### #TODO:load configurations from previous model when loading previous model ##if load_path: #load config files from dir #except if pt_load_path, get cc_config from before #overwrite is_train, is_pretrain with current args--sort of a mess ##else: config,_=get_config()
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)
from IPython.core import debugger ; debug = debugger.Pdb().set_trace from matplotlib.widgets import Slider from recorder import Recorder import matplotlib.gridspec as gridspec import matplotlib.pyplot as plt import numpy as np import argparse import sys PRINTER = Recorder() parser = argparse.ArgumentParser() parser.add_argument("--id") args = parser.parse_args() if args.id == None: PRINTER.info_text("Specify region ID with '--id <number>'") PRINTER.line() sys.exit() ID = int(args.id) DATA = [] with open("resources/region-data/database.csv") as f: for line in f: if line.split(",")[0] == "%05d" % ID: DATA = line.split(",") DATE = DATA[3] TIME = DATA[4]
class MyMagics(Magics): if sys.platform.lower().startswith('win'): if hasattr(__IPYTHON__, 'rc'): __IPYTHON__.rc.editor = 'scite' else: __IPYTHON__.editor = 'scite' ############################################################################ dbstop = None try: if not dbstop: from IPython.Debugger import Tracer dbstop = Tracer() # IPython v 0.10 and earlier except: pass try: if not dbstop: from IPython.core.debugger import Tracer dbstop = Tracer() # IPython v 0.11 and later except: pass try: if not dbstop: from IPython.core import debugger dbstop = debugger.Pdb( ).set_trace # Some other f***ing version of IWishYoudStopMovingTheGoalposts except: pass sys.dbstop = __IPYTHON__.dbstop = dbstop # because calling sys.dbstop() is easier than having to remember and type # "from IPython.Debugger import Tracer; Tracer()()" just to invoke the debugger # (and *much* easier than having to remember the version-specific variants of that) # Note however that you can also run in debug mode and set breakpoints with: # %run -d -b LINENUMBER file.py # %run -d -b OTHERFILENAME:LINENUMBER file.py ############################################################################ @line_magic def edit(self, arg): """\ A more Matlab-like replacement for IPython's default editing behaviour: search the path for possible matches; edit them in the background; don't execute the result (NB: the latter can also be achieved in IPython with %edit -x). Set your editor with the line 'editor WHATEVER' in ipythonrc, or by otherwise setting __IPYTHON__.rc.editor = 'WHATEVER' Examples: edit site.py # edits existing file on path with .py extension $BLAH/Lib/site.py edit ipythonrc # edits existing file on path without .py extension $HOME/.ipython/ipythonrc edit site # adds .py extension and edits existing file on path $BLAH/Lib/site.py edit IPython/iplib.py # edits existing file in subdirectory on path $BLAH/IPython/iplib.py edit IPython/iplib # edits existing file in subdirectory on path $BLAH/IPython/iplib.py edit IPython.iplib # edits existing file in package on path $BLAH/IPython/iplib.py edit xml # edits __init__.py file in existing package on path $BLAH/xml/__init__.py edit foo.py # edits new file with .py extension ./foo.py edit foo.txt # edits new file with non-.py extension ./foo.txt edit foo # adds .py extension and edits new file ./foo.py edit IPython/foo.py # edits new file in existing subdirectory on path $BLAH/IPython/foo.py edit IPython/foo # edits new file in existing subdirectory on path $BLAH/IPython/foo.py edit IPython.foo # edits new file in existing subdirectory on path $BLAH/IPython/foo.py Most Windows or Unixoid editors will behave well if you give them the name of a not-yet- existing file to edit. On OSX, however, if you're using an editor script that redirects to `open -a Some.app file.txt`, this will fail if file.txt doesn't exist. For this reason, you can use `edit -t ....` where the -t stands for "touch": the file will be created if it doesn't already exist, before being opened in the editor. (Typos will then sometimes mean that you're left with unwanted 0-byte files hanging around, needing to be deleted by hand.) """### # NB: one (unlikely) case is not handled: a new file with a non-.py extension # in an existing subdirectory on the path, e.g. edit IPython/foo.txt if hasattr(__IPYTHON__, 'rc'): editor = __IPYTHON__.rc.editor else: editor = __IPYTHON__.editor windows = sys.platform.lower().startswith('win') if windows: editor = 'start ' + editor def edit(x, mode='existing'): if not x: return os.system(editor) or True x = os.path.abspath(x) if mode == 'existing' and not os.path.isfile(x): return False if os.path.isdir(x): return sys.stderr.write( 'cannot edit directory %s\n' % x) and False if mode == '-t' and not os.path.isfile(x): open(x, 'a').close() os.system(editor + ' "' + x.strip('"') + '"') return True if edit(arg): return # that allows for spaces in the path, e.g. edit C:\Documents and Settings\me\Desktop\blah.py # but if the whole thing isn't an existing file, we'll assume a space-delimited list of filenames. arg = arg.strip() if arg.startswith('(') and arg.endswith(')'): arg = eval('[' + arg[1:-1] + ']', getworkspace()) else: if windows: arg = arg.replace( '\\', '\\\\' ) # otherwise shlex.split will decode/eat backslashes that might be important as file separators import shlex arg = shlex.split(arg) # shlex.split copes with quoted substrings creation_policy = arg.pop(0) if (arg and arg[0] == '-t') else 'auto' for original in arg: stem = original if stem.lower().endswith('.py'): stem = stem[:-3] original_withpy = stem + '.py' path_withoutpy = os.path.join(*stem.split('.')) path_withpy = path_withoutpy + '.py' potential_location = '' for p in sys.path: if edit(os.path.join(p, original)): break elif edit(os.path.join(p, original_withpy)): break elif edit(os.path.join(p, path_withpy)): break elif edit(os.path.join(p, path_withoutpy)): break elif stem == original and edit( os.path.join(p, path_withoutpy, '__init__.py')): break elif potential_location == '': pp = os.path.join(p, path_withoutpy) dd = os.path.realpath(os.path.dirname(pp)) d = os.path.realpath(p) if len(dd) > len(d) and os.path.isdir(dd): potential_location = pp else: # not found anywhere else, so edit as new if potential_location == '': potential_location = original fn, xtn = os.path.splitext(potential_location) if xtn == '': xtn = '.py' edit(fn + xtn, creation_policy) ############################################################################ @line_magic def addpath(self, d=None): """\ Make absolute paths out of the input(s) and append them to sys.path if they are not already there. Supply a space-delimited list of arguments, or one argument enclosed in quotes (which may, in that case, contain a space). """### if d == None: dd = (os.getcwd(), ) else: d = d.replace('\\ ', ' ').strip() if (d.startswith('"') and d.endswith('"')) or (d.startswith("'") and d.endswith("'")): dd = (d[1:-1], ) else: dd = d.split() for d in dd: d = os.path.realpath(d) if os.path.isdir(d): if not d in sys.path: sys.path.append(d) else: print(('no such directory "%s"' % d)) ############################################################################ @line_magic def desk(self, subdir=''): """\ cd to the Desktop, but keep a global record of where we were (stored in __IPYTHON__.undeskdir) so that we can snap back with %undesk """### if sys.platform.lower().startswith('win'): homedrive = os.environ.get('HOMEDRIVE') homepath = os.environ.get('HOMEPATH') userprofile = os.environ.get('USERPROFILE') if userprofile == None: d = os.path.join(homedrive, homepath, 'Desktop') else: d = os.path.join(userprofile, 'Desktop') else: d = os.path.join(os.environ.get('HOME'), 'Desktop') if subdir != '': return os.path.realpath(os.path.join(d, subdir)) here = os.path.realpath(os.curdir) if os.path.realpath(d) == here: print("already at desktop") else: __IPYTHON__.undeskdir = here print(('changing directory from ' + here)) print((' to ' + d)) print('type %undesk to change back') os.chdir(d) ############################################################################ @line_magic def undesk(self, arg): """\ Undo a previous call to %desk """ ### d = getattr(__IPYTHON__, 'undeskdir', None) if d == None: print('no global record of previous call to %desk') else: print(('changing directory from ' + os.path.realpath(os.curdir))) print((' back to ' + d)) os.chdir(d) ############################################################################ @line_magic def pip(self, d=''): try: import pip except ImportError: import sys sys.stderr.write(""" The pip module is not installed. See the instructions at http://www.pip-installer.org/en/latest/installing.html In short: (1) Ensure setuptools (i.e. easy_install) is installed first (download and run ez_setup.py if not). (2) Download and run get-pip.py (3) Quit and relaunch Python (4) Then the first thing you should do with pip is: pip install --upgrade setuptools """) return try: return pip.main(d.split()) except SystemExit: pass ############################################################################ @line_magic def easy_install(self, d=None): """\ Run the easy_install command from IPython, invoking the setuptools script Scripts/easy_install-script.py (on Windows) or easy_install (on anything else). Particularly useful on Windows, where IPython may well be the best command-line you have. """### def readpth(pth): sitedir = os.path.split( pth )[0] # assume easy-install.pth is directly in the site-packages dir lines = [x.strip() for x in open(pth, 'rt').readlines()] return [ os.path.realpath(os.path.join(sitedir, x)) for x in lines if not x.startswith('import') and not x.startswith('#') ] # assume a line is a relative path unless it begins with # or import def isbelow(x, p): x, p = os.path.realpath(x), os.path.realpath(p) return x == p or x.startswith(os.path.join(p, '')) pth = [ os.path.join(os.path.realpath(x), 'easy-install.pth') for x in sys.path ] # assume 'easy-install.pth' is on the path pth = ([x for x in pth if os.path.isfile(x)] + [None])[0] if pth != None: oldpaths = readpth(pth) ############ This is the part that actually runs easy_install. ############ The rest, before and after, is to attempt to keep the session going with intelligent updates to ############ sys.path and sys.modules (still won't work for upgrades to IPython and some messy others like simplejson). home = os.environ.get('PYTHONHOME') if sys.platform.lower().startswith('win'): if home == None: home = os.path.dirname(sys.executable) getmagic('run')( '"' + os.path.join(home, 'Scripts', 'easy_install-script.py') + '" ' + d) else: # TODO: need to set $PYTHONHOME for system call using env? If so, how to fill it in if home==None? bang('"' + sys.executable + '" "`which easy_install`" ' + d) ############ if pth == None: print( "restart this python session to take advantage of the new package" ) return newpaths = readpth(pth) addpaths = [x for x in newpaths if x not in oldpaths] gone = [x for x in oldpaths if x not in newpaths] rmpaths = [] for x in sys.path: if not len(x): continue for stem in gone: if isbelow(x, stem): rmpaths.append(x) break stripped = [x for x in sys.path if x not in rmpaths] for i in range(len(stripped)): if len(stripped[i]): stripped[i] = os.path.realpath(stripped[i]) sys.path[:] = stripped + addpaths if len(addpaths): print(('\n '.join(['Added to sys.path:'] + list(map(repr, addpaths))))) if len(rmpaths): print(('\n '.join(['Removed from sys.path:'] + list(map(repr, rmpaths))))) rmmod = [] for k, v in list(sys.modules.items()): v = getattr(v, '__file__', None) if not isinstance(v, str): continue for p in rmpaths: if isbelow(v, p): rmmod.append(k) break rmmod = [sys.modules.pop(k) for k in sorted(rmmod)] if len(rmmod): print(('\n '.join(['Removed from sys.modules:'] + list(map(repr, rmmod))))) ############################################################################ @line_magic def search(self, dd): """\ search TERM search TERM TOP_LEVEL_DIR search TERM TOP_LEVEL_DIR FILE_PATTERN Searches case-insensitively for TERM through all files matching FILE_PATTERN (default: '*') in the directory tree starting at TOP_LEVEL_DIR (default: '.') and prints the filename, line number and line content for any matches. Because, on Windows, native file content searches are not reliable and IPython is going to be the best command-line you have. Also, for convenience, the following syntax does the simpler job of matching file names rather than file content: search --filename ... """### import os, sys, fnmatch, re, getopt args = [ ''.join(matches) for matches in re.findall( '|'.join([r'"([^"]*)"', r"'([^']*)'", r'(\S+)']), dd) ] opts, args = getopt.getopt(args, '', ['filename']) opts = dict(opts) if not 1 <= len(args) <= 3: sys.stderr.write( 'arguments: SEARCH_STRING [TOP_DIR] [FILE_PATTERN]\n') sys.stderr.flush() return search_string, root_dir, file_pattern = (args + ['', '', ''])[:3] sys.stdout.write('\n') sys.stdout.flush() if not root_dir: root_dir = '.' matches = [] if not os.path.isdir(root_dir): sys.stderr.write(str(root_dir) + ' is not a directory\n') sys.stderr.flush() return search_string = str(search_string).lower() limit = 200 for root, dirnames, filenames in os.walk(root_dir): if file_pattern: filenames = fnmatch.filter(filenames, file_pattern) for filename in filenames: file_path = os.path.join(root, filename) if '--filename' in opts: if search_string in str( os.path.split(filename)[1]).lower(): sys.stdout.write('%s\n' % (file_path)) sys.stdout.flush() matches.append(file_path) continue matches.append(file_path) try: file_handle = open(file_path) except IOError: sys.stderr.write('/!\\ failed to open %s\n\n' % file_path) sys.stderr.flush() for line_number, line in enumerate(file_handle): hit = search_string in str(line).lower() if hit: try: sys.stdout.write( '%s:%d:\n %s\n\n' % (file_path.replace('\\', '/'), line_number + 1, str(line).strip()[:limit])) except UnicodeDecodeError: sys.stdout.write( '%s:%d matches (no preview available)\n\n' % (file_path, line_number + 1)) sys.stdout.flush() if len(matches) == 0: sys.stderr.write('/!\\ no files matched the input pattern\n') sys.stderr.flush() else: sys.stdout.write('\n') ############################################################################ @line_magic def reimport(self, dd): """\ The syntax %reimport foo, bar.* is a shortcut for the following: import foo; foo = reload(foo) import bar; bar = reload(bar); from bar import * """### ipython = getworkspace() for d in dd.replace(',', ' ').split(' '): if len(d): bare = d.endswith('.*') if bare: d = d[:-2] exec('import xx; xx = reload(xx)'.replace('xx', d), ipython) if bare: exec('from xx import *'.replace('xx', d), ipython) ############################################################################ @line_magic def loadpylab(self, d=''): """\ The syntax %loadpylab imports matplotlib and pylab into the IPython workspace, ensuring that, if matplotlib has not previously been loaded, it is loaded in interactive mode. %loadpylab * is the same, but additionally imports the requested symbols from pylab, directly into the workspace. """### import sys ipython = getworkspace() try: import matplotlib except ImportError: print("WARNING: failed to import matplotlib") else: if not 'matplotlib.backends' in sys.modules: matplotlib.interactive(True) try: exec('import matplotlib, pylab', ipython) except ImportError: print("WARNING: failed to import pylab") #if __OLDER_IPYTHON__: if len(d): exec('from pylab import ' + d, ipython) ############################################################################ @line_magic def njh(self, *pargs): """\ Imports a number of packages, including the BCPy2000 sub-packages WavTools and SigTools, but also copy, struct, ctypes, numpy, scipy and matplotlib and pylab (the latter in interactive mode). """### ipython = getworkspace() exec('import copy,struct,ctypes,time,numpy,scipy', ipython) try: exec('import WavTools,SigTools', ipython) except ImportError: exec('import BCPy2000.Paths', ipython) exec('import WavTools,SigTools', ipython) getmagic('loadpylab')() ############################################################################ @line_magic def pp(self, name=''): """\ An ipython command-line shortcut to SigTools.summarize, to give a quick look at object attributes--- especially useful for numpy arrays. """### if name == None: return ipython = getworkspace() val = eval(name, ipython) from . import SigTools print((SigTools.summarize(val))) ############################################################################ @line_magic def l(self, dd='.'): if dd in [None, '']: dd = '.' dd = dd.replace('\\ ', ' ').strip().strip('"\'') if sys.platform.lower().startswith('win'): cmd = 'dir "%s"' % dd.replace('/', '\\') else: cmd = 'ls -AFlh %s' % dd bang(cmd, shell=True) ############################################################################ @line_magic def bootstrap_shortcuts(self, *pargs): """\ import BCPy2000.Shortcuts allows you access to these useful shortcuts (edit, search, desk, addpath, easy_install...). This shortcut writes the import command into an .ipy file in the default ipython profile directory of your home directory, so that the shortcuts will be loaded automatically whenever you start IPython (versions 0.13 and later) in future. """### code = """\ try: import BCPy2000.Shortcuts except ImportError: print ("Failed to import BCPy2000.Shortcuts") """ import os, sys if __OLDER_IPYTHON__: sys.stderr.write( "\nFAILED: this bootstrapper only works for newer versions of IPython.\nYou'll have to add the following lines manually to ipy_user_conf.py\nin the _ipython or .ipython subdirectory of your home directory:\n\n%s\n" % code) return if sys.platform.lower().startswith('win'): home = os.environ['USERPROFILE'] else: home = os.environ['HOME'] startup_dir = os.path.join(home, '.ipython', 'profile_default', 'startup') startup_file = os.path.join( startup_dir, 'BCPy2000Shortcuts.ipy' ) # cannot be a .py file, must be an .ipy - the latter seem to be executed later, when ipython is running and its instance can be accessed with get_ipython() if not os.path.isdir(startup_dir): os.makedirs(startup_dir) open(startup_file, 'wt').write(code) sys.stderr.write( '\nSUCCESS: the file\n %s\nhas been created containing the following lines:\n\n%s\n' % (startup_file, code))
def ipy_set_trace(): from IPython.core import debugger debugger.Pdb().set_trace()
def run(self, parameter_s='', runner=None, file_finder=get_py_filename): """Run the named file inside IPython as a program. Usage:\\ %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args] Parameters after the filename are passed as command-line arguments to the program (put in sys.argv). Then, control returns to IPython's prompt. This is similar to running at a system prompt:\\ $ python file args\\ but with the advantage of giving you IPython's tracebacks, and of loading all variables into your interactive namespace for further use (unless -p is used, see below). The file is executed in a namespace initially consisting only of __name__=='__main__' and sys.argv constructed as indicated. It thus sees its environment as if it were being run as a stand-alone program (except for sharing global objects such as previously imported modules). But after execution, the IPython interactive namespace gets updated with all variables defined in the program (except for __name__ and sys.argv). This allows for very convenient loading of code for interactive work, while giving each program a 'clean sheet' to run in. Options: -n: __name__ is NOT set to '__main__', but to the running file's name without extension (as python does under import). This allows running scripts and reloading the definitions in them without calling code protected by an ' if __name__ == "__main__" ' clause. -i: run the file in IPython's namespace instead of an empty one. This is useful if you are experimenting with code written in a text editor which depends on variables defined interactively. -e: ignore sys.exit() calls or SystemExit exceptions in the script being run. This is particularly useful if IPython is being used to run unittests, which always exit with a sys.exit() call. In such cases you are interested in the output of the test results, not in seeing a traceback of the unittest module. -t: print timing information at the end of the run. IPython will give you an estimated CPU time consumption for your script, which under Unix uses the resource module to avoid the wraparound problems of time.clock(). Under Unix, an estimate of time spent on system tasks is also given (for Windows platforms this is reported as 0.0). If -t is given, an additional -N<N> option can be given, where <N> must be an integer indicating how many times you want the script to run. The final timing report will include total and per run results. For example (testing the script uniq_stable.py):: In [1]: run -t uniq_stable IPython CPU timings (estimated):\\ User : 0.19597 s.\\ System: 0.0 s.\\ In [2]: run -t -N5 uniq_stable IPython CPU timings (estimated):\\ Total runs performed: 5\\ Times : Total Per run\\ User : 0.910862 s, 0.1821724 s.\\ System: 0.0 s, 0.0 s. -d: run your program under the control of pdb, the Python debugger. This allows you to execute your program step by step, watch variables, etc. Internally, what IPython does is similar to calling: pdb.run('execfile("YOURFILENAME")') with a breakpoint set on line 1 of your file. You can change the line number for this automatic breakpoint to be <N> by using the -bN option (where N must be an integer). For example:: %run -d -b40 myscript will set the first breakpoint at line 40 in myscript.py. Note that the first breakpoint must be set on a line which actually does something (not a comment or docstring) for it to stop execution. When the pdb debugger starts, you will see a (Pdb) prompt. You must first enter 'c' (without quotes) to start execution up to the first breakpoint. Entering 'help' gives information about the use of the debugger. You can easily see pdb's full documentation with "import pdb;pdb.help()" at a prompt. -p: run program under the control of the Python profiler module (which prints a detailed report of execution times, function calls, etc). You can pass other options after -p which affect the behavior of the profiler itself. See the docs for %prun for details. In this mode, the program's variables do NOT propagate back to the IPython interactive namespace (because they remain in the namespace where the profiler executes them). Internally this triggers a call to %prun, see its documentation for details on the options available specifically for profiling. There is one special usage for which the text above doesn't apply: if the filename ends with .ipy, the file is run as ipython script, just as if the commands were written on IPython prompt. -m: specify module name to load instead of script path. Similar to the -m option for the python interpreter. Use this option last if you want to combine with other %run options. Unlike the python interpreter only source modules are allowed no .pyc or .pyo files. For example:: %run -m example will run the example module. """ # get arguments and set sys.argv for program to be run. opts, arg_lst = self.parse_options(parameter_s, 'nidtN:b:pD:l:rs:T:em:', mode='list', list_all=1) if "m" in opts: modulename = opts["m"][0] modpath = find_mod(modulename) if modpath is None: warn('%r is not a valid modulename on sys.path' % modulename) return arg_lst = [modpath] + arg_lst try: filename = file_finder(arg_lst[0]) except IndexError: warn('you must provide at least a filename.') print '\n%run:\n', oinspect.getdoc(self.run) return except IOError as e: try: msg = str(e) except UnicodeError: msg = e.message error(msg) return if filename.lower().endswith('.ipy'): self.shell.safe_execfile_ipy(filename) return # Control the response to exit() calls made by the script being run exit_ignore = 'e' in opts # Make sure that the running script gets a proper sys.argv as if it # were run from a system shell. save_argv = sys.argv # save it for later restoring # simulate shell expansion on arguments, at least tilde expansion args = [os.path.expanduser(a) for a in arg_lst[1:]] sys.argv = [filename] + args # put in the proper filename # protect sys.argv from potential unicode strings on Python 2: if not py3compat.PY3: sys.argv = [py3compat.cast_bytes(a) for a in sys.argv] if 'i' in opts: # Run in user's interactive namespace prog_ns = self.shell.user_ns __name__save = self.shell.user_ns['__name__'] prog_ns['__name__'] = '__main__' main_mod = self.shell.new_main_mod(prog_ns) else: # Run in a fresh, empty namespace if 'n' in opts: 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 # Since '%run foo' emulates 'python foo.py' at the cmd line, we must # set the __file__ global in the script's namespace prog_ns['__file__'] = filename # pickle fix. See interactiveshell for an explanation. But we need to # make sure that, if we overwrite __main__, we replace it at the end main_mod_name = prog_ns['__name__'] if main_mod_name == '__main__': restore_main = sys.modules['__main__'] else: restore_main = False # This needs to be undone at the end to prevent holding references to # every single object ever created. sys.modules[main_mod_name] = main_mod try: stats = None with self.shell.readline_no_record: if 'p' in opts: stats = self.prun('', None, False, opts, arg_lst, prog_ns) else: if 'd' in opts: 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 = 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 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) error(msg) return # if we find a good linenumber, set the breakpoint deb.do_break('%s:%s' % (filename, bp)) # Start file run print "NOTE: Enter 'c' at the", print "%s prompt to start your script." % deb.prompt ns = { 'execfile': py3compat.execfile, 'prog_ns': prog_ns } try: #save filename so it can be used by methods on the deb object deb._exec_filename = filename deb.run('execfile("%s", prog_ns)' % filename, 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) else: if runner is None: runner = self.default_runner if runner is None: runner = self.shell.safe_execfile if 't' in opts: # timed execution try: nruns = int(opts['N'][0]) if nruns < 1: error('Number of runs must be >=1') return except (KeyError): nruns = 1 twall0 = time.time() 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 : %10.2f s." % t_usr print " System : %10.2f 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 : %10.2f %10.2f" % ( 'Total', 'Per run') print " User : %10.2f s, %10.2f s." % ( t_usr, t_usr / nruns) print " System : %10.2f s, %10.2f s." % ( t_sys, t_sys / nruns) twall1 = time.time() print "Wall time: %10.2f s." % (twall1 - twall0) else: # regular execution runner(filename, prog_ns, prog_ns, exit_ignore=exit_ignore) if 'i' in opts: self.shell.user_ns['__name__'] = __name__save else: # The shell MUST hold a reference to prog_ns so after %run # exits, the python deletion mechanism doesn't zero it out # (leaving dangling references). self.shell.cache_main_mod(prog_ns, filename) # update IPython interactive namespace # Some forms of read errors on the file may mean the # __name__ key was never set; using pop we don't have to # worry about a possible KeyError. prog_ns.pop('__name__', None) self.shell.user_ns.update(prog_ns) finally: # It's a bit of a mystery why, but __builtins__ can change from # being a module to becoming a dict missing some key data after # %run. As best I can see, this is NOT something IPython is doing # at all, and similar problems have been reported before: # http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-10/0188.html # Since this seems to be done by the interpreter itself, the best # we can do is to at least restore __builtins__ for the user on # exit. self.shell.user_ns['__builtins__'] = builtin_mod # Ensure key global structures are restored sys.argv = save_argv if restore_main: sys.modules['__main__'] = restore_main else: # Remove from sys.modules the reference to main_mod we'd # added. Otherwise it will trap references to objects # contained therein. del sys.modules[main_mod_name] return stats
def trace_mac_address(self, mac_address): """ Search for mac_address, get switch port and return lldp/cdp neighbour of that port """ result = { 'found': False, 'cdp_answer': False, 'lldp_answer': False, 'local_port': '', 'remote_port': '', 'next_device': '', 'next_device_descr': '', } try: mac_address = self.hp_mac_format(mac_address) raw_out = self._send_command('display mac-address ' + mac_address) if 'No mac address found' in raw_out: raise HpNoMacFound else: result['found'] = True msg = f' --- Found {mac_address} mac address --- \n' mac_table = textfsm_extractor(self, "display_mac_address", raw_out) print(msg) logger.info(msg) print( dumps(mac_table, sort_keys=True, indent=4, separators=(',', ': '))) for row in mac_table: for k, pname in row.items(): if k == 'interface' and pname != None: # send lldp neighbour command if ('BAGG' in pname) or ('Bridge-Aggregation' in pname): # Check and format the interface name agg_port_name = self.normalize_port_name(pname) # get first physical port of the aggregated port result['local_port'] = agg_port_name physical_port = self.get_active_physical_ports( agg_port_name)[0] lldp_neighbours = self.get_lldp_neighbors_detail( interface=physical_port) cdp_neighbours = self.get_cdp_neighbors_detail( interface=physical_port) if lldp_neighbours: result['lldp_answer'] = True result['remote_port'] = lldp_neighbours[0][ "remote_port"] result['next_device'] = lldp_neighbours[0][ "remote_system_name"] result['next_device_descr'] = lldp_neighbours[ 0]['remote_system_description'] msg = f' --- LLDP Neighbour System Name: {result["next_device"]}' elif cdp_neighbours: result['cdp_answer'] = True result['remote_port'] = cdp_neighbours[0][ "remote_port"] result['next_device'] = cdp_neighbours[0][ "remote_system_name"] result['next_device_descr'] = cdp_neighbours[ 0]['remote_system_description'] msg = f' --- CDP Neighbour System Name: {result["next_device"]}' print(msg) logger.info(msg) return result elif ('XGE' in pname) or ('GE' in pname): pname = self.normalize_port_name(pname) result['local_port'] = pname from IPython import embed embed() from IPython.core import debugger debug = debugger.Pdb().set_trace debug() lldp_neighbours = self.get_lldp_neighbors_detail( interface=pname) cdp_neighbours = self.get_cdp_neighbors_detail( interface=pname) if lldp_neighbours: result['lldp_answer'] = True result['remote_port'] = lldp_neighbours[0][ "remote_port"] result['next_device'] = lldp_neighbours[0][ "remote_system_name"] msg = f' --- LLDP Neighbour System Name: {result["next_device"]}' elif cdp_neighbours: result['cdp_answer'] = True result['remote_port'] = cdp_neighbours[0][ "remote_port"] result['next_device'] = cdp_neighbours[0][ "remote_system_name"] msg = f' --- CDP Neighbour System Name: {result["next_device"]}' print(msg) logger.info(msg) return result else: raise NotImplementedError except HpMacFormatError as e: msg = f'Unrecognised Mac format: {mac_address}' logger.error(msg) print(msg) return result except HpNoMacFound as e: msg = f' --- No mac address {mac_address} found: {e} ---' print(msg) logger.info(msg) return result except Exception as e: raise e
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)