objsize.traverse
Handling of traversal.
Classes and Functions
- safe_is_instance(obj, type_tuple)[source]
Return whether an object is an instance of a class or of a subclass thereof. See
isinstance()
for more information.Catches
ReferenceError
because applyingisinstance()
onweakref.proxy()
objects attempts to dereference the proxy objects, which may yield an exception.
Filters objects that are likely to be shared among many objects.
Filters objects that are likely to be shared among many objects, but includes functions and lambdas.
- default_get_referents()
See https://docs.python.org/3/library/gc.html#gc.get_referents
- default_object_filter(obj)
By default, we filter shared objects, i.e., types, modules, functions, and lambdas
- default_get_size()
See https://docs.python.org/3/library/sys.html#sys.getsizeof
- class ObjSizeSettings(get_referents_func=None, filter_func=None, get_size_func=None, exclude=None, exclude_modules_globals=None)[source]
Object traversal and size settings.
- Parameters:
filter_func (Callable[[Any], bool] | None) – Receives an objects and return
True
if the object—and its subtree—should be traversed. Default:shared_object_filter()
. By default, this excludes shared objects, i.e., types, modules, functions, and lambdas.get_referents_func (Callable[[...], Iterable[Any]] | None) – Receives any number of objects and returns iterable over the objects that are referred by these objects. Default:
gc.get_referents()
.get_size_func (Callable[[Any], int] | None) – A function that determines the object size. Default:
sys.getsizeof()
.exclude (Iterable | None) – Objects that will be excluded from this calculation, as well as their subtrees.
exclude_modules_globals (bool | None) – If True (default), loaded modules globals will be added to the
exclude_set
.
- replace(get_referents_func=None, filter_func=None, get_size_func=None, exclude=None, exclude_modules_globals=None)[source]
Replaces some of the settings into a new settings object.
- update(get_referents_func=None, filter_func=None, get_size_func=None, exclude=None, exclude_modules_globals=None)[source]
Updates some of the settings in place.
- new_context(*, marked_set=None, exclude_set=None)[source]
See
TraversalContext
.
- class TraversalContext(settings=None, marked_set=None, exclude_set=None)[source]
Object traversal context.
- Parameters:
settings (ObjSizeSettings | None) – See
ObjSizeSettings
marked_set (Set[int] | None) – An existing set of marked objects’ ID, i.e., id(obj). Objects that their ID is in this set will not be traversed. If a set is given, it will be updated with all the traversed objects’ ID.
exclude_set (Set[int] | None) – Similar to the marked set, but contains excluded objects’ ID.
- traverse_exclusive_bfs(*objs)[source]
Traverse all the arguments’ subtree, excluding non-exclusive objects. That is, objects that are referenced by objects that are not in this subtree.
- Parameters:
objs (object(s)) – One or more object(s).
- Yields:
object – The traversed objects, one by one.
- Return type:
See also
traverse_bfs()
to understand which objects are traversed.
- get_deep_size(*objs)[source]
Calculates the deep size of all the arguments.
- Parameters:
objs (object(s)) – One or more object(s).
- Returns:
The objects’ deep size in bytes.
- Return type:
See also
traverse_bfs()
to understand which objects are traversed.
- get_exclusive_deep_size(*objs)[source]
Calculates the deep size of all the arguments, excluding non-exclusive objects.
- Parameters:
objs (object(s)) – One or more object(s).
- Returns:
The objects’ deep size in bytes.
- Return type:
See also
traverse_exclusive_bfs()
to understand which objects are traversed.