summaryrefslogtreecommitdiff
path: root/tools.py
diff options
context:
space:
mode:
Diffstat (limited to 'tools.py')
-rw-r--r--tools.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/tools.py b/tools.py
index 6835228..ba36f6d 100644
--- a/tools.py
+++ b/tools.py
@@ -1,6 +1,30 @@
import numpy as np
import time
import datetime
+import sys
+
+def debugger_hook(type, value, tb):
+ if hasattr(sys, 'ps1') or not sys.stderr.isatty():
+ # we are in interactive mode or we don't have a tty-like
+ # device, so we call the default hook
+ sys.__excepthook__(type, value, tb)
+ else:
+ import traceback, pdb
+ # we are NOT in interactive mode, print the exception...
+ traceback.print_exception(type, value, tb)
+ print
+ # ...then start the debugger in post-mortem mode.
+ pdb.pm()
+
+def enable_debug_on_crash():
+ '''Start the PDB console when an uncaught exception propagates to the top.'''
+ sys.excepthook = debugger_hook
+
+# Allow profile decorator to exist, but do nothing if not running under kernprof
+try:
+ profile_if_possible = profile
+except NameError:
+ profile_if_possible = lambda x: x
def timeit(func):
def f(*args, **kwargs):