pybinsim/utility.py
def total_size(o, handlers={}, verbose=False):
    """ Returns the approximate memory footprint an object and all of its contents.

    Automatically finds the contents of the following builtin containers and
    their subclasses:  tuple, list, deque, dict, set and frozenset.
    To search other containers, add handlers to iterate over their contents:

        handlers = {SomeContainerClass: iter,
                    OtherContainerClass: OtherContainerClass.get_elements}

    """
    def dict_handler(d): return chain.from_iterable(d.items())
    all_handlers = {tuple: iter,
                    list: iter,
                    deque: iter,
                    dict: dict_handler,
                    set: iter,
                    frozenset: iter,
                    }
    all_handlers.update(handlers)     # user handlers take precedence
    seen = set()                      # track which object id's have already been seen
    # estimate sizeof object without __sizeof__
    default_size = getsizeof(0)

    def sizeof(o):
        if id(o) in seen:       # do not double count the same object
            return 0
        seen.add(id(o))
        s = getsizeof(o, default_size)

        if verbose:
            print(s, type(o), repr(o), file=stderr)

        for typ, handler in all_handlers.items():
            if isinstance(o, typ):
                s += sum(map(sizeof, handler(o)))
                break
        return s

    return sizeof(o)
 Returns the approximate memory footprint an object and all of its contents.

    Automatically finds the contents of the following builtin containers and
    their subclasses:  tuple, list, deque, dict, set and frozenset.
    To search other containers, add handlers to iterate over their contents:

        handlers = {SomeContainerClass: iter,
                    OtherContainerClass: OtherContainerClass.get_elements}

    
Returns the approximate memory footprint an object and all of its contents.
Automatically finds the contents of the following builtin containers and
their subclasses:  tuple, list, deque, dict, set and frozenset.
To search other containers, add handlers to iterate over their contents.


Returns the approximate memory footprint an object and all of its contents . Automatically finds the contents of the following builtin containers and their subclasses : tuple list deque dict set and frozenset . To search other containers add handlers to iterate over their contents .
def total_size(o, handlers={}, verbose=False):
    def dict_handler(d): return chain.from_iterable(d.items())
    all_handlers = {tuple: iter,
                    list: iter,
                    deque: iter,
                    dict: dict_handler,
                    set: iter,
                    frozenset: iter,
                    }
    all_handlers.update(handlers)     
    seen = set()                      
    default_size = getsizeof(0)
    def sizeof(o):
        if id(o) in seen:       
            return 0
        seen.add(id(o))
        s = getsizeof(o, default_size)
        if verbose:
            print(s, type(o), repr(o), file=stderr)
        for typ, handler in all_handlers.items():
            if isinstance(o, typ):
                s += sum(map(sizeof, handler(o)))
                break
        return s
    return sizeof(o)
def total_size ( o , handlers = { } , verbose = False ) : def dict_handler ( d ) : return chain . from_iterable ( d . items ( ) ) all_handlers = { tuple : iter , list : iter , deque : iter , dict : dict_handler , set : iter , frozenset : iter , } all_handlers . update ( handlers ) seen = set ( ) default_size = getsizeof ( 0 ) def sizeof ( o ) : if id ( o ) in seen : return 0 seen . add ( id ( o ) ) s = getsizeof ( o , default_size ) if verbose : print ( s , type ( o ) , repr ( o ) , file = stderr ) for typ , handler in all_handlers . items ( ) : if isinstance ( o , typ ) : s += sum ( map ( sizeof , handler ( o ) ) ) break return s return sizeof ( o )
Returns the approximate memory footprint an object and all of its contents.
Returns the approximate memory footprint an object and all of its contents .
""" Returns the approximate memory footprint an object and all of its contents.

    Automatically finds the contents of the following builtin containers and
    their subclasses:  tuple, list, deque, dict, set and frozenset.
    To search other containers, add handlers to iterate over their contents:

        handlers = {SomeContainerClass: iter,
                    OtherContainerClass: OtherContainerClass.get_elements}

    """ # user handlers take precedence # track which object id's have already been seen # estimate sizeof object without __sizeof__ # do not double count the same object
o
handlers
verbose