Skip to content

Instantly share code, notes, and snippets.

@lenolib
Last active December 26, 2015 19:39
Show Gist options
  • Save lenolib/7202378 to your computer and use it in GitHub Desktop.
Save lenolib/7202378 to your computer and use it in GitHub Desktop.
Provides a decomposed heirachical overview of objects and their component types.
from __future__ import print_function
from collections import Iterable, Sized
def deprint(obj, depth=5, indent=0, size_limit=10, index=""):
if not depth:
print("".join([" "*indent,"[reached depth limit]"]))
return
type_str = type(obj).__name__
if type(obj).__module__ != '__builtin__':
type_str = ".".join([type(obj).__module__, type_str])
print("".join([" "*indent, type_str]), end=' ')
if isinstance(obj, Sized):
length = len(obj)
print('({}) {}'.format(length, index), end='')
else:
if isinstance(obj, Iterable):
print("(Iterable but not Sized)", end='')
print(index)
return
if isinstance(obj, basestring):
print('')
return
if (isinstance(obj, Iterable)
and isinstance(obj, Sized) ):
if len(obj) > size_limit:
print("[#elements > {}]".format(size_limit))
return
else: #TODO adapt for mappings (key,value)
print('')
for index, item in enumerate(obj):
deprint(
item,
depth=depth-1,
indent=indent+4,
index=" #{}".format(index)
)
if __name__ == '__main__':
myvar = dict(
hej=[1,2,u"en string"],
var=dict(
a_key=[3,4,5],
b_key=['some string',3,4]
)
)
deprint(myvar)
print("")
import pandas as pd
comp = ( ((1,(1,(1,(1,(1,2))))),2), myvar,pd.DataFrame(range(3)) )
deprint(comp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment