File: getsize.pyx

package info (click to toggle)
python-memprof 0.3.6-5.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 252 kB
  • sloc: python: 345; sh: 7; makefile: 4
file content (65 lines) | stat: -rw-r--r-- 1,506 bytes parent folder | download | duplicates (4)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

import types
import io
import memprof
from .mp_utils import *

if PY3:
    builtin = (int, float, str, complex)
else:
    builtin = (int, float, str, long, complex, file)

notinteresting = (
    types.ModuleType,
    types.FunctionType,
    types.LambdaType,
    io.IOBase,
    type(None),
    types.MethodType,
    types.GetSetDescriptorType,
    types.GeneratorType,
    types.BuiltinFunctionType,
    types.BuiltinMethodType,
    builtin
    )

def isInteresting(x):
    if isinstance(x, notinteresting) or isinstance(x, memprof.MemProf):
        return False
    return True

def getSize(x):      
    ids = set()

    def size_of(x):
        if id(x) in ids:
            return 0

        ids.add(id(x))

        if hasattr(x, "nbytes"):
            nbytes = x.nbytes
            return nbytes if isinstance(nbytes,int) else 0

        size = sys.getsizeof(x)

        if isinstance(x, builtin):
            return size

        items = []

        # Go through objects (avoiding modules, MemProf, functions and methods thanks to isInteresting)
        if hasattr(x, "__dict__"):
            items = x.__dict__.values()
        elif isinstance(x, dict):
            items = x.values()
        # Go through iterables skipping strings (and files, thanks to isInteresting)
        elif hasattr(x, '__iter__'):
            items = x

        for it in items:
            if not isinstance(it, builtin) and isInteresting(it):
                size += size_of(it)

        return size
    return size_of(x)