All Projects → liran-funaro → objsize

liran-funaro / objsize

Licence: GPL-3.0 License
Traversal over Python's objects sub-tree and calculating the total size of the sub-tree (deep size).

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to objsize

recursive-iterator
It iterates through a graph recursively
Stars: ✭ 87 (+262.5%)
Mutual labels:  traversal
Traverser
Traverser is a Java library that helps software engineers implement advanced iteration of a data structure.
Stars: ✭ 45 (+87.5%)
Mutual labels:  traversal
Dynamic-Parkour-System
Dynamic Parkour System is a FREE plugin for Unity that allows anyone to import any model and have an already working controller with parkour capabilities like in Assassin's Creed games.
Stars: ✭ 694 (+2791.67%)
Mutual labels:  traversal
TextFieldsTraversalController
A controller to manage the traversal of a collection of textfields.
Stars: ✭ 15 (-37.5%)
Mutual labels:  traversal
javascript-easy-object
Now easily access or modify an object in javascript with javascript-easy-object.
Stars: ✭ 13 (-45.83%)
Mutual labels:  traversal
react-binary-tree
Binary Tree Traversal Visualisation
Stars: ✭ 25 (+4.17%)
Mutual labels:  traversal
django-cte-forest
django-cte-forest implements efficient adjacency list trees using Django and PostgreSQL Common Table Expressions (CTE).
Stars: ✭ 24 (+0%)
Mutual labels:  traversal
Enzyme
JavaScript Testing utilities for React
Stars: ✭ 19,781 (+82320.83%)
Mutual labels:  traversal
php-dom-wrapper
Simple DOM wrapper library to manipulate and traverse HTML documents similar to jQuery
Stars: ✭ 103 (+329.17%)
Mutual labels:  traversal
aiohttp traversal
Traversal based router for aiohttp.web
Stars: ✭ 21 (-12.5%)
Mutual labels:  traversal
python-obj-system
Tutorials on advanced python topics, and literate programming framework to write them (see README.md)
Stars: ✭ 49 (+104.17%)
Mutual labels:  python-objects

objsize

Traversal over Python's objects sub-tree and calculating the total size of the sub-tree in bytes (deep size).

This module uses python internal GC implementation to traverse all decedent objects. It attempts to ignore singletons (e.g., None) and type objects (i.e., 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 in bytes.
  • 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 in bytes.

>>> 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 in bytes 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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].