Created
September 27, 2018 19:59
-
-
Save ironfroggy/23288fc44c71d4d34c67e0613c58db67 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
lines = {} | |
def start(): | |
sys.settrace(trace) | |
def stop(): | |
sys.settrace(None) | |
for filename in lines: | |
if filename.startswith('./'): | |
print(filename, len(lines[filename]) / count_lines(filename)) | |
def count_lines(filename): | |
try: | |
return len(list(open(filename))) | |
except FileNotFoundError: | |
return 0 | |
def trace(frame, event, arg): | |
if event == 'line': | |
filename = frame.f_code.co_filename | |
lineno = frame.f_lineno | |
lines.setdefault(filename, set()).add(lineno) | |
elif event == 'call': | |
return trace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
def getch(): | |
return sys.stdin.read(1) | |
def start(): | |
sys.settrace(trace) | |
def stop(): | |
sys.settrace(None) | |
def trace(frame, event, arg): | |
try: | |
line = list(open(frame.f_code.co_filename))[frame.f_lineno - 1].strip() | |
except NameError: | |
line = None | |
if line is not None: | |
if event == 'line': | |
print(f'{frame.f_code.co_filename}:{frame.f_lineno} {line}') | |
getch() | |
elif event == 'call': | |
print(f'step in? {frame.f_code.co_filename}:{frame.f_lineno} {line}') | |
if getch() == 's': | |
return trace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys, time | |
call_stack = [] | |
call_times = [] | |
calltime_accum = {} | |
def start(): | |
sys.settrace(trace) | |
def stop(): | |
sys.settrace(None) | |
times = list(calltime_accum.items()) | |
times.sort(key=lambda e: e[1]) | |
for func, t in times: | |
print(func, t) | |
def trace(frame, event, arg): | |
try: | |
line = list(open(frame.f_code.co_filename))[frame.f_lineno - 1].strip() | |
except (NameError, FileNotFoundError): | |
line = None | |
if line is not None: | |
if event == 'call': | |
func_name = line.split('def ')[1].split('(')[0] | |
func = f'{frame.f_code.co_filename}:{func_name}' | |
call_stack.append(func) | |
call_times.append(time.time()) | |
return trace | |
elif event == 'return': | |
func = call_stack.pop() | |
start = call_times.pop() | |
end = time.time() | |
t = end - start | |
calltime_accum.setdefault(func, 0) | |
calltime_accum[func] += t |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
def start(): | |
sys.settrace(trace) | |
def stop(): | |
sys.settrace(None) | |
def trace(frame, event, arg): | |
if event == 'line': | |
line = list(open(frame.f_code.co_filename))[frame.f_lineno - 1].strip() | |
print(f'{frame.f_code.co_filename}:{frame.f_lineno} {line}') | |
elif event == 'call': | |
return trace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Usage: ./test.py littlest_{module} | |
import sys | |
exec(f'import {sys.argv[1]}') | |
def incr(x): | |
x = x + 1 | |
return x | |
def foo(): | |
x = 1 + 2 | |
x = incr(x) | |
print('Hello, World!') | |
def baz(): | |
x = 1 + 2 | |
for i in range(10): | |
x = incr(x) | |
return x | |
exec(f'{sys.argv[1]}.start()') | |
foo() | |
y = baz() | |
print('Final Result:', y) | |
exec(f'{sys.argv[1]}.stop()') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
these are neat!