Skip to main content

Traversal over Python's objects sub-tree and calculating the 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 (i.e., isinstance(o, type)) such as classes and modules, as they are common among all objects. It is implemented without recursive calls for best performance.

Features

  • Calculate single/multiple object(s) deep size.
  • Exclude non exclusive objects.
  • Traverse single/multiple objects(s) sub tree.

Pympler also supports determening an object deep size via pympler.asizeof(). There are two main differences between objsize and pympler.

  1. objsize has additional features:
    • Traversing the object sub-tree: iterating all of the object's descendants one by one.
    • Excluding non-exclusive objects. That is, objects that are also referenced from somewhere else in the program. This is true for calculating the object's deep size and for traversing its descendants.
  2. objsize has a simple and robust implementation with significantly fewer lines of code, compared to pympler. The Pympler implementation uses recursion, and thus have to use a maximal depth argument to avoid reaching Python's max depth. objsize, however, uses BFS which is more efficient and simple to follow. Moreover, the Pympler implementation carefully takes care of any object type. objsize archives the same goal with a simple and generic implementation, which has fewer lines of code.

Install

pip install objsize

Basic Usage

Calculate an object size including all its members.

>>> import objsize
>>> objsize.get_deep_size(dict(arg1='hello', arg2='world'))
348

It is possible to calculate the deep size of multiple objects by passing multiple arguments:

>>> objsize.get_deep_size(['hello', 'world'], dict(arg1='hello', arg2='world'), {'hello', 'world'})
652

Complex Data

objsize can calculate the size of an object's entire sub-tree regardless of the type of objects in it, and its depth.

Here is a complex data structure, for example, that include a self reference:

my_data = (list(range(3)), list(range(3,6)))

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)

We can calculate my_obj deep size, including its stored data.

>>> objsize.get_deep_size(my_obj)
796

We might want to ignore non exclusive objects such as the ones stored in my_data.

>>> objsize.get_exclusive_deep_size(my_obj)
408

Traversal

A user can implement its own function over the entire sub tree using the traversal method, which traverse all the objects in the sub tree.

>>> for o in objsize.traverse_bfs(my_obj):
...     print(o)
... 
MyClass
{'x': [0, 1, 2], 'y': [3, 4, 5], 'd': {'x': [0, 1, 2], 'y': [3, 4, 5], 'self': MyClass}}
[0, 1, 2]
[3, 4, 5]
{'x': [0, 1, 2], 'y': [3, 4, 5], 'self': MyClass}
2
1
0
5
4
3

Similirarly to before, non exclusive objects can be ignored.

>>> for o in objsize.traverse_exclusive_bfs(my_obj):
...     print(o)
... 
MyClass
{'x': [0, 1, 2], 'y': [3, 4, 5], 'd': {'x': [0, 1, 2], 'y': [3, 4, 5], 'self': MyClass}}
{'x': [0, 1, 2], 'y': [3, 4, 5], 'self': MyClass}

License

GPL

Project details


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.2.tar.gz (18.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

objsize-0.3.2-py2.py3-none-any.whl (4.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file objsize-0.3.2.tar.gz.

File metadata

  • Download URL: objsize-0.3.2.tar.gz
  • Upload date:
  • Size: 18.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0

File hashes

Hashes for objsize-0.3.2.tar.gz
Algorithm Hash digest
SHA256 9d7ce7b775d7d686fc2d1654d1cdec0ea09e22924c0d4cc935764b364531d44f
MD5 3f12752b1086067f3905772c92e8744c
BLAKE2b-256 70b3e46916a18a5dc17b013363f1ea9d3779e298e36bbbdb677c26eee4b3a417

See more details on using hashes here.

File details

Details for the file objsize-0.3.2-py2.py3-none-any.whl.

File metadata

  • Download URL: objsize-0.3.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0

File hashes

Hashes for objsize-0.3.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 5e4e6b46d331cdc64295599fd65191a5936bba002294413bae7f6968fb181e45
MD5 57533067d39d49ba7a161fb2f79aaa88
BLAKE2b-256 a7620fa389d0c77ca41c30e01539fb1abfd480ef38a310ca0908dbd3d218750a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page