Skip to content

Instantly share code, notes, and snippets.

@theY4Kman
Created October 4, 2018 04:53
Show Gist options
  • Save theY4Kman/a2d326999b443825edda52a6c68301f5 to your computer and use it in GitHub Desktop.
Save theY4Kman/a2d326999b443825edda52a6c68301f5 to your computer and use it in GitHub Desktop.
# XXX######################################################################################
# XXX######################################################################################
import os
import linecache
import sys
import time
from trace import Trace
class MyTrace(Trace):
def localtrace_trace_and_count(self, frame, why, arg):
if why == "line":
# record the file name and line number of every trace
filename = frame.f_code.co_filename
lineno = frame.f_lineno
key = filename, lineno
self.counts[key] = self.counts.get(key, 0) + 1
if self.start_time:
print('%.2f' % (time.monotonic() - self.start_time), end=' ')
thread = threading.current_thread()
bname = os.path.basename(filename)
print("%s(%d) %r: %s" % (bname, lineno, thread,
linecache.getline(filename, lineno)), end='')
return self.localtrace
tracer = MyTrace(
ignoredirs=[sys.prefix, sys.exec_prefix, '/opt/local/Library/Frameworks/Python.framework/Versions/3.6'],
trace=1,
timing=1,
)
orig_names = tracer.ignore.names
tracer.ignore.names = lambda filename, modname: 0 if 'requests_mock' in filename else orig_names(filename, modname)
# XXX######################################################################################
# XXX######################################################################################
# XXX######################################################################################
# XXX######################################################################################
#XXX######################################################################################
#XXX######################################################################################
import sys
import threading
class TraceTimer(threading.Timer):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.trace = sys.gettrace()
def run(self):
sys.settrace(self.trace)
super(TraceTimer, self).run()
Timer = TraceTimer
#XXX######################################################################################
#XXX######################################################################################
#XXX######################################################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment