-
-
Save marceloneppel/a830cbde84aa50248608bfe66c6c75e2 to your computer and use it in GitHub Desktop.
utility that can print context source code on any frame of stack and variables you care against Python inspect
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
collective_code_location_message = '\n' | |
def print_dict(dict, verbose=True): | |
#callerframerecord = inspect.stack()[1] # 0 represents this line | |
# # 1 represents line at caller | |
#frame = callerframerecord[0] | |
#info = inspect.getframeinfo(frame) | |
#if info.function == 'print_sql_result': | |
# code_probe('',2) | |
#else: | |
# code_probe('', 1) | |
message = 'print_dict:\n' | |
for row in dict: | |
message += '{}: {}\n'.format(uniform_to_utf8(row), | |
uniform_to_utf8(dict[row])) | |
if verbose: | |
print message | |
return message | |
def print_list(list, verbose=True): | |
#callerframerecord = inspect.stack()[1] # 0 represents this line | |
# # 1 represents line at caller | |
#frame = callerframerecord[0] | |
#info = inspect.getframeinfo(frame) | |
#if info.function == 'print_sql_result': | |
# code_probe('',2) | |
#else: | |
# code_probe('', 1) | |
message = 'print_list:\n' | |
for i in list: | |
i = uniform_to_utf8(i) | |
message += '{} '.format(i) | |
if verbose: | |
print message | |
return message | |
def print_sql_result(result): | |
print 'print_sql_result' | |
for row in result: | |
if isinstance(row, tuple): | |
print_list(row) | |
elif isinstance(row, dict): | |
print_dict(row) | |
def uniform_to_utf8(string): | |
if isinstance(string, unicode): | |
return string.encode('utf-8') | |
else: | |
return string | |
def uniform_to_unicode(string): | |
if isinstance(string, str): | |
return string.decode('utf-8') | |
else: | |
return string | |
def code_probe(arg='', relative_stack=0, offset=[-1,-1], | |
if_show_variables=True, | |
show_local=False, show_global=False, spec_variable=[]): | |
from inspect import currentframe, getframeinfo | |
import inspect, linecache | |
callerframerecord = inspect.stack()[1+relative_stack] # 0 represents this line | |
# 1 represents line at caller | |
frame = callerframerecord[0] | |
info = inspect.getframeinfo(frame) | |
""":type: Traceback""" | |
'''this method can get the current line''' | |
#lines_str = info.code_context[0] | |
lines = [linecache.getline(info.filename, info.lineno+i) for i in range(offset[0], offset[1]+1)] | |
lines_str = '' | |
for i,j in enumerate(lines): | |
if i == -offset[0]: | |
j = j[:-1] + ' <=========\n' | |
lines_str += j | |
message = '#[{}:{}{}]: {}====================={}' \ | |
'\n{}\n'.format(info.filename, info.lineno, offset, info.function, arg, lines_str) | |
if if_show_variables: | |
locals_str = 'local variables:===================\n'+\ | |
print_dict(frame.f_locals, verbose=False)+'\n' if show_local else '' | |
globals_str = 'global variables:===================\n'+\ | |
print_dict(frame.f_globals, verbose=False)+'\n' if show_global else '' | |
spec_v_str = '' | |
if spec_variable == []: | |
last_line = linecache.getline(info.filename, info.lineno-1) | |
import re | |
spec_variable = [ re.search(r'\b(\w*)[^\w]', last_line).group(1) ] | |
for i in spec_variable: | |
spec_v_str += '"{}"'.format(i) | |
if i in frame.f_locals: | |
spec_v_str += ' locals : {}\n'.format(frame.f_locals[i]) | |
if i in frame.f_globals: | |
spec_v_str += ' globals : {}\n'.format(frame.f_globals[i]) | |
if i not in frame.f_locals and i not in frame.f_globals: | |
spec_v_str += ' not found\n' | |
message += '{}' \ | |
'{}' \ | |
'spec_variables:===================\n' \ | |
'{}'.format(locals_str, | |
globals_str, | |
spec_v_str) | |
message += '#[end]=====================\n' | |
print message | |
global collective_code_location_message | |
import datetime | |
collective_code_location_message += '{}<<<<<<<<<<<<<<\n{}'\ | |
.format(datetime.datetime.now(), message) | |
return message | |
if __name__ == "__main__": | |
blah = 'blah' | |
'blan' | |
foo = 1 + 2 | |
code_probe('foo', offset=[-3,4], if_show_variables=True, show_global=False, spec_variable=['blah']) | |
bar = 'bar' | |
code_probe(if_show_variables=True) | |
print 'new line' | |
print 'another new line' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment