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 applying isinstance() on weakref.proxy() objects attempts to dereference the proxy objects, which may yield an exception.

Parameters:
  • obj (Any) – Any object

  • type_tuple – A type or a tuple of types

Returns:

True if the objects matches one of the types

Return type:

bool

shared_object_or_function_filter(obj)[source]

Filters objects that are likely to be shared among many objects.

Parameters:

obj (Any)

Return type:

bool

shared_object_filter(obj)[source]

Filters objects that are likely to be shared among many objects, but includes functions and lambdas.

Parameters:

obj (Any)

Return type:

bool

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

Parameters:

obj (Any)

Return type:

bool

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.

Return type:

A new settings instance

Parameters:
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.

Parameters:
new_context(*, marked_set=None, exclude_set=None)[source]

See TraversalContext.

Parameters:
  • marked_set (Set[int] | None)

  • exclude_set (Set[int] | None)

traverse_bfs(*objs, marked_set=None, exclude_set=None)[source]

See TraversalContext.traverse_bfs()

Parameters:
Return type:

Iterator[Any]

traverse_exclusive_bfs(*objs, marked_set=None, exclude_set=None)[source]

See TraversalContext.traverse_exclusive_bfs()

Parameters:
Return type:

Iterator[Any]

get_deep_size(*objs, marked_set=None, exclude_set=None)[source]

See TraversalContext.get_deep_size()

Parameters:
Return type:

int

get_exclusive_deep_size(*objs, marked_set=None, exclude_set=None)[source]

See TraversalContext.get_exclusive_deep_size()

Parameters:
Return type:

int

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_bfs(*objs, exclude=False)[source]

Traverse all the arguments’ subtree.

Parameters:
  • objs (object(s)) – One or more object(s).

  • exclude (bool) – If true, all objects will be added to the exclude set.

Yields:

object – The traversed objects, one by one.

Return type:

Iterator[Any]

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:

Iterator[Any]

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:

int

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:

int

See also

traverse_exclusive_bfs()

to understand which objects are traversed.