Last active
December 26, 2015 19:39
Revisions
-
lenolib renamed this gist
Oct 28, 2013 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,7 @@ 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 @@ -32,7 +32,7 @@ def print_structure(obj, depth=5, indent=0, size_limit=10, index=""): else: #TODO adapt for mappings (key,value) print('') for index, item in enumerate(obj): deprint( item, depth=depth-1, indent=indent+4, @@ -48,12 +48,12 @@ if __name__ == '__main__': ) ) deprint(myvar) print("") import pandas as pd comp = ( ((1,(1,(1,(1,(1,2))))),2), myvar,pd.DataFrame(range(3)) ) deprint(comp) -
lenolib created this gist
Oct 28, 2013 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,59 @@ from __future__ import print_function from collections import Iterable, Sized def print_structure(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): print_structure( 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] ) ) print_structure(myvar) print("") import pandas as pd comp = ( ((1,(1,(1,(1,(1,2))))),2), myvar,pd.DataFrame(range(3)) ) print_structure(comp)