def debug_shell(user_ns, user_global_ns, traceback=None, execWrapper=None): ipshell = None try: import IPython have_ipython = True except ImportError: have_ipython = False if not ipshell and traceback and have_ipython: try: from IPython.core.debugger import Pdb from IPython.terminal.debugger import TerminalPdb from IPython.terminal.ipapp import TerminalIPythonApp ipapp = TerminalIPythonApp.instance() ipapp.interact = False # Avoid output (banner, prints) ipapp.initialize(argv=[]) def_colors = ipapp.shell.colors pdb_obj = TerminalPdb(def_colors) pdb_obj.botframe = None # not sure. exception otherwise at quit ipshell = lambda: pdb_obj.interaction(None, traceback=traceback) except Exception: print("IPython Pdb exception:") better_exchook(*sys.exc_info(), autodebugshell=False) if not ipshell and have_ipython: try: import IPython import IPython.terminal.embed class DummyMod(object): pass module = DummyMod() module.__dict__ = user_global_ns module.__name__ = "_DummyMod" if "__name__" not in user_ns: user_ns = user_ns.copy() user_ns["__name__"] = "_DummyUserNsMod" ipshell = IPython.terminal.embed.InteractiveShellEmbed.instance( user_ns=user_ns, user_module=module) except Exception: print("IPython not available:") better_exchook(*sys.exc_info(), autodebugshell=False) else: if execWrapper: old = ipshell.run_code ipshell.run_code = lambda code: execWrapper(lambda: old(code)) if ipshell: ipshell() else: print("Use simple debug shell:") if traceback: import pdb pdb.post_mortem(traceback) else: simple_debug_shell(user_global_ns, user_ns)
def debug_shell(user_ns, user_global_ns, traceback=None, execWrapper=None): ipshell = None try: import IPython have_ipython = True except ImportError: have_ipython = False if not ipshell and traceback and have_ipython: try: from IPython.core.debugger import Pdb from IPython.terminal.debugger import TerminalPdb from IPython.terminal.ipapp import TerminalIPythonApp ipapp = TerminalIPythonApp.instance() ipapp.interact = False # Avoid output (banner, prints) ipapp.initialize(argv=[]) def_colors = ipapp.shell.colors pdb_obj = TerminalPdb(def_colors) pdb_obj.botframe = None # not sure. exception otherwise at quit ipshell = lambda: pdb_obj.interaction(None, traceback=traceback) except Exception: print("IPython Pdb exception:") better_exchook(*sys.exc_info(), autodebugshell=False) if not ipshell and have_ipython: try: import IPython import IPython.terminal.embed class DummyMod(object): pass module = DummyMod() module.__dict__ = user_global_ns module.__name__ = "_DummyMod" if "__name__" not in user_ns: user_ns = user_ns.copy() user_ns["__name__"] = "_DummyUserNsMod" ipshell = IPython.terminal.embed.InteractiveShellEmbed.instance( user_ns=user_ns, user_module=module) except Exception: print("IPython not available:") better_exchook(*sys.exc_info(), autodebugshell=False) else: if execWrapper: old = ipshell.run_code ipshell.run_code = lambda code: execWrapper(lambda: old(code)) if ipshell: ipshell() else: print("Use simple debug shell:") if traceback: import pdb pdb.post_mortem(traceback) else: simple_debug_shell(user_global_ns, user_ns)
def debug(self, kind='ipdb'): """ Run callable in debug mode. Parameters ---------- kind : str ('ipdb' or 'pdb') Which debugger to use 'ipdb' for IPython debugger or 'pdb' for debugger from the standard library Notes ----- Be careful when debugging tasks. If the task has run successfully, you overwrite products but don't save the updated source code, your DAG will enter an inconsistent state where the metadata won't match the overwritten product. """ opts = {'ipdb', 'pdb'} if kind not in opts: raise ValueError('"kind" must be one of {}, got: "{}"'.format( opts, kind)) if self.exec_status == TaskStatus.WaitingRender: raise TaskBuildError('Error in task "{}". ' 'Cannot call task.debug() on a task that has ' 'not been ' 'rendered, call DAG.render() first'.format( self.name)) if 'upstream' in self.params and self._unserializer: params = _unserialize_params(self.params, self._unserializer) else: params = self.params.to_dict() if self._serializer: params.pop('product') if kind == 'ipdb': try: # this seems to only work in a Terminal ipdb = TerminalPdb() except AttributeError: # this works in a Jupyter notebook ipdb = Pdb() ipdb.runcall(self.source.primitive, **params) elif kind == 'pdb': pdb.runcall(self.source.primitive, **params)
def dispatch_line(self, frame): """Handle line action and return the next line callback.""" callback = TerminalPdb.dispatch_line(self, frame) # If the ipdb session ended, don't return a callback for the next line if self.stoplineno == -1: return None return callback
def debug(self, kind='ipdb'): """ Opens the notebook (with injected parameters) in debug mode in a temporary location Parameters ---------- kind : str, default='ipdb' Debugger to use, 'ipdb' to use line-by-line IPython debugger, 'pdb' to use line-by-line Python debugger or 'pm' to to post-portem debugging using IPython Notes ----- Be careful when debugging tasks. If the task has run successfully, you overwrite products but don't save the updated source code, your DAG will enter an inconsistent state where the metadata won't match the overwritten product. """ if self.source.language != 'python': raise NotImplementedError( 'debug is not implemented for "{}" ' 'notebooks, only python is supported'.format( self.source.language)) opts = {'ipdb', 'pdb', 'pm'} if kind not in opts: raise ValueError('kind must be one of {}'.format(opts)) nb = _read_rendered_notebook(self.source.nb_str_rendered) fd, tmp_path = tempfile.mkstemp(suffix='.py') os.close(fd) code = jupytext.writes(nb, version=nbformat.NO_CONVERT, fmt='py') Path(tmp_path).write_text(code) if kind == 'pm': # post-mortem debugging try: subprocess.run(['ipython', tmp_path, '--pdb']) finally: Path(tmp_path).unlink() else: if kind == 'ipdb': from IPython.terminal.debugger import TerminalPdb, Pdb code = compile(source=code, filename=tmp_path, mode='exec') try: # this seems to only work in a Terminal debugger = TerminalPdb() except AttributeError: # this works in a Jupyter notebook debugger = Pdb() elif kind == 'pdb': debugger = pdb try: debugger.run(code) finally: Path(tmp_path).unlink()
import os import time import re import pandas as pd import warnings import numpy as np from spykshrk.franklab.data_containers import DataFrameClass from abc import ABCMeta, abstractmethod try: from IPython.terminal.debugger import TerminalPdb bp = TerminalPdb(color_scheme='linux').set_trace except AttributeError as err: # print('Warning: Attribute Error ({}), disabling IPython TerminalPdb.'.format(err)) bp = lambda: None idx = pd.IndexSlice class FrankFileFormatError(RuntimeError): pass class FrankFileFormatWarning(RuntimeWarning): pass class FrankHDFFormatError(RuntimeError): pass
from struct import unpack import numpy as np import pandas as pd from scipy.io import loadmat import spykshrk.franklab.filterframework_util as ff_util from spykshrk.franklab.data_containers import RippleTimes, UnitTime from spykshrk.realtime.datatypes import LFPPoint, LinearPosPoint, SpikePoint from spykshrk.franklab.errors import DataReadError, DataContentError, ConfigurationError idx = pd.IndexSlice try: from IPython.terminal.debugger import TerminalPdb bp = TerminalPdb().set_trace except AttributeError as err: #print('Warning: Attribute Error ({}), disabling IPython TerminalPdb.'.format(err)) bp = lambda: None # setup pandas string/print format for debugging/interactive pd.set_option('float_format', '{:,.1f}'.format) pd.set_option('display.max_rows', 20) pd.set_option('display.width', 120) # Constant variable definitions TIMESTAMP_SCALE = 10000 # factor to convert timestamp to s class AnimalInfo: """ animalinfo - stores details of the single animal to process and generates
def __init__(self, exc_info, *args, **kwargs): TerminalPdb.__init__(self, *args, **kwargs) self.exc_info = exc_info