Skip to content

Instantly share code, notes, and snippets.

@lenolib
Last active December 26, 2015 19:39

Revisions

  1. lenolib renamed this gist Oct 28, 2013. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions Composition printing → Decomposed printing
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    from __future__ import print_function
    from collections import Iterable, Sized

    def print_structure(obj, depth=5, indent=0, size_limit=10, index=""):
    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):
    print_structure(
    deprint(
    item,
    depth=depth-1,
    indent=indent+4,
    @@ -48,12 +48,12 @@ if __name__ == '__main__':
    )
    )

    print_structure(myvar)
    deprint(myvar)

    print("")
    import pandas as pd

    comp = ( ((1,(1,(1,(1,(1,2))))),2), myvar,pd.DataFrame(range(3)) )
    print_structure(comp)
    deprint(comp)


  2. lenolib created this gist Oct 28, 2013.
    59 changes: 59 additions & 0 deletions Composition printing
    Original 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)