Traversal over Python's objects sub-tree and calculatingthe total size of the sub-tree (deep size).
Project description
# objsize
Traversal over Python's objects sub-tree and calculating
the total size of the sub-tree (deep size).
This module uses python internal GC implementation
to traverse all decedent objects.
It ignores type objects (`isinstance(o, type)`)
such as classes and modules, as they are common among all objects.
It is implemented without recursive calls for best performance.
# Install
`pip install objsize`
# Usage
```python
my_data = (list(range(5)), list(range(10)))
class MyClass:
def __init__(self, x, y):
self.x = x
self.y = y
self.d = {'x': x, 'y': y, 'self': self}
def __repr__(self):
return "MyClass"
my_obj = MyClass(*my_data)
from objsize import get_deep_size
# Calculates my_obj deep size, including its stored data.
print(get_deep_size(my_obj))
# 1012
from objsize import get_exclusive_deep_size
# Calculates my_obj deep size, ignoring non exclusive
# objects such as the ones stored in my_data.
print(get_exclusive_deep_size(my_obj))
# 408
from objsize import traverse_bfs
# Traverse all the objects in my_obj sub tree.
for o in traverse_bfs(my_obj):
print(o)
# {'x': [0, 1, 2, 3, 4], 'y': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 'd': {'x': [0, 1, 2, 3, 4], 'y': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 'self': MyClass}}
# [0, 1, 2, 3, 4]
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# {'x': [0, 1, 2, 3, 4], 'y': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 'self': MyClass}
# 4
# 3
# 2
# 1
# 0
# 9
# 8
# 7
# 6
# 5
from objsize import traverse_exclusive_bfs
# Traverse all the objects in my_obj sub tree, ignoring non exclusive ones.
for o in traverse_exclusive_bfs(my_obj):
print(o)
# {'x': [0, 1, 2, 3, 4], 'y': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 'd': {'x': [0, 1, 2, 3, 4], 'y': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 'self': MyClass}}
# {'x': [0, 1, 2, 3, 4], 'y': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 'self': MyClass}
```
# License
[GPL](LICENSE.txt)
Traversal over Python's objects sub-tree and calculating
the total size of the sub-tree (deep size).
This module uses python internal GC implementation
to traverse all decedent objects.
It ignores type objects (`isinstance(o, type)`)
such as classes and modules, as they are common among all objects.
It is implemented without recursive calls for best performance.
# Install
`pip install objsize`
# Usage
```python
my_data = (list(range(5)), list(range(10)))
class MyClass:
def __init__(self, x, y):
self.x = x
self.y = y
self.d = {'x': x, 'y': y, 'self': self}
def __repr__(self):
return "MyClass"
my_obj = MyClass(*my_data)
from objsize import get_deep_size
# Calculates my_obj deep size, including its stored data.
print(get_deep_size(my_obj))
# 1012
from objsize import get_exclusive_deep_size
# Calculates my_obj deep size, ignoring non exclusive
# objects such as the ones stored in my_data.
print(get_exclusive_deep_size(my_obj))
# 408
from objsize import traverse_bfs
# Traverse all the objects in my_obj sub tree.
for o in traverse_bfs(my_obj):
print(o)
# {'x': [0, 1, 2, 3, 4], 'y': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 'd': {'x': [0, 1, 2, 3, 4], 'y': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 'self': MyClass}}
# [0, 1, 2, 3, 4]
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# {'x': [0, 1, 2, 3, 4], 'y': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 'self': MyClass}
# 4
# 3
# 2
# 1
# 0
# 9
# 8
# 7
# 6
# 5
from objsize import traverse_exclusive_bfs
# Traverse all the objects in my_obj sub tree, ignoring non exclusive ones.
for o in traverse_exclusive_bfs(my_obj):
print(o)
# {'x': [0, 1, 2, 3, 4], 'y': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 'd': {'x': [0, 1, 2, 3, 4], 'y': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 'self': MyClass}}
# {'x': [0, 1, 2, 3, 4], 'y': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 'self': MyClass}
```
# License
[GPL](LICENSE.txt)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
objsize-0.3.0.tar.gz
(18.1 kB
view hashes)
Built Distribution
Close
Hashes for objsize-0.3.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c185fa29780771af5f97d01267e42a11c0edb2d41bf3627b203d113e33e7e060 |
|
MD5 | d9ad57175955f0e21343a84ca4e5daa6 |
|
BLAKE2b-256 | 16ea5d4a8b11fdd76de3a5368de2c3c7e4a7f6939d8e94459b04ddeb69e9f893 |