-
Notifications
You must be signed in to change notification settings - Fork 1
/
object.py
47 lines (32 loc) · 1.2 KB
/
object.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import sys
from types import ModuleType, FunctionType
from gc import get_referents
def getsize(obj):
"""
Get a sum of sizes from an object and it's members in bytes.
Custom objects know their class.
Function objects seem to know way too much, including modules.
Exclude modules as well.
Author: Aaron Hall @ https://stackoverflow.com/questions/449560/how-do-i-determine-the-size-of-an-object-in-python
"""
_BLACKLIST = type, ModuleType, FunctionType
if isinstance(obj, _BLACKLIST):
# raise TypeError('getsize() does not take argument of type: '+ str(type(obj)))
return sys.getsizeof(obj)
seen_ids = set()
size = 0
objects = [obj]
while objects:
need_referents = []
for obj in objects:
if not isinstance(obj, _BLACKLIST) and id(obj) not in seen_ids:
seen_ids.add(id(obj))
size += sys.getsizeof(obj)
need_referents.append(obj)
objects = get_referents(*need_referents)
return size
def interconnect(*objs):
""" Connect each obj to eachother by their dunder name. """
for obj in objs:
for obj2 in objs:
setattr(obj, obj2.__name__, obj2)