describe - line and filename, variable name, class, str representation and some other info
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
# Print the line and filename, function call, the class, str representation and some other info | |
# Inspired by https://stackoverflow.com/a/8856387/5353461 | |
import inspect | |
import re | |
def describe(arg): | |
frame = inspect.currentframe() | |
callerframeinfo = inspect.getframeinfo(frame.f_back) | |
try: | |
context = inspect.getframeinfo(frame.f_back).code_context | |
caller_lines = ''.join([line.strip() for line in context]) | |
m = re.search(r'describe\s*\((.+?)\)$', caller_lines) | |
if m: | |
caller_lines = m.group(1) | |
position = str(callerframeinfo.filename) + "@" + str(callerframeinfo.lineno) | |
# Add additional info such as array shape or string length | |
additional = '' | |
if hasattr(arg, "shape"): | |
additional += "[shape={}]".format(arg.shape) | |
elif hasattr(arg, "__len__"): # shape includes length information | |
additional += "[len={}]".format(len(arg)) | |
# Use str() representation if it is printable | |
str_arg = str(arg) | |
str_arg = str_arg if str_arg.isprintable() else repr(arg) | |
print(position, "describe(" + caller_lines + ") = ", end='') | |
print(arg.__class__.__name__ + "(" + str_arg + ")", additional) | |
else: | |
print("Describe: couldn't find caller context") | |
finally: | |
del frame | |
del callerframeinfo | |
import numpy | |
describe((3, 2)) | |
describe(numpy.zeros((2, 4))) | |
describe("fo\\o\n") | |
describe("foo") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment