Skip to content

Instantly share code, notes, and snippets.

@dansteingart
Last active August 29, 2015 14:08
Show Gist options
  • Save dansteingart/2469cafd3044fc86305d to your computer and use it in GitHub Desktop.
Save dansteingart/2469cafd3044fc86305d to your computer and use it in GitHub Desktop.
An attempt to have a "rational" whos() (ala matlab)
import __main__
def whos(lss):
fil = __main__.__file__
thisthinghere = open(fil).read().split("\n")
vs = []
for l in thisthinghere:
if l.find("=") > -1:
vs.append(l.split("=")[0].strip())
keys = lss.keys()
out = {}
for v in vs:
try:
out[v] = lss[v]
except:
"not in list"
keys = out.keys()
keys.sort()
for k in keys:
val = str(out[k])
if len (val) > 10:
if val[-1] == ")":val = val[0:10]+"..."+val[-10:]
elif val[-1] == "]" :val = val[0:10]+"..."+val[-10:]
else: val = val[0:10]
print k,":",val
return out
#import into your script and call with whos(locals())
@dansteingart
Copy link
Author

An attempt to have a "rational" whos() (ala matlab) function in python that only displays variables set in the script (e.g. not the sausage in matplotlib and numpy). More for seeing what variables are rather than debugging. Great for working thermodynamics problems.

This examines the file for variable assignment, and then cross references that assignment with the local variable space. If there's a match the variable is printed.

This is probably exceptionally unstable, but it's worked the last 1000 times I've run it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment