Skip to content

Instantly share code, notes, and snippets.

@HaleTom
Last active June 2, 2022 21:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HaleTom/125f0c0b0a1fb4fbf4311e6aa763844b to your computer and use it in GitHub Desktop.
Save HaleTom/125f0c0b0a1fb4fbf4311e6aa763844b to your computer and use it in GitHub Desktop.
describe - line and filename, variable name, class, str representation and some other info
# 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