#NEWLINE# def deepsize( obj , max_depth = 4 ) : #NEWLINE# #INDENT# '\n    Get not only size of the given object, but also the size of\n    objects referenced by the object, down to `max_depth` distance\n    from the object.\n\n    Args:\n        obj (object): the object to be measured.\n        max_depth (int, optional): maximum referential distance\n            from `obj` that `deepsize()` should cover for\n            measuring objects referenced by `obj`.\n\n    Returns:\n        size (int): deepsize of `obj` in Bytes.\n\n    Notes:\n        This measure is necessarily approximate since some\n        memory is shared between objects. The `max_depth` of 4 is roughly\n        tested to give reasonable size information about database models\n        and their handlers.\n\n    ' #NEWLINE# def _recurse( o , dct , depth ) : #NEWLINE# #INDENT# if ( ( max_depth >= 0 ) and ( depth > max_depth ) ) : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# for ref in get_referents ( o ) : #NEWLINE# #INDENT# idr = id ( ref ) #NEWLINE# if ( not ( idr in dct ) ) : #NEWLINE# #INDENT# dct [ idr ] = ( ref , getsizeof ( ref , default = 0 ) ) #NEWLINE# _recurse ( ref , dct , ( depth + 1 ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# sizedict = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# _recurse ( obj , sizedict , 0 ) #NEWLINE# size = ( getsizeof ( obj ) + sum ( [ size [ 1 ] for p in sizedict . values ( ) ] ) ) #NEWLINE# return size