traverse - Object traversal

The main function is find().

class pinspect.traverse.DiGraphAcyclic(incoming_graph_data=None, **attr)[source]

Bases: networkx.classes.digraph.DiGraph

Directed Acyclic Graph.

add_edge(self, u, v_obj, label=None, **attr)[source]

Adds id(v_obj) node in the graph, if not present, and then adds an edge from u to id(v_obj).

Parameters
uint or str

A node from.

v_objobject

A node object to. The node of the object is id(v_obj).

labelstr

Edge label.

Returns
bool

If the edge has been successfully added or not. If adding the edge from u to v_obj closes a cycle, returns False. Otherwise, returns True.

add_node(self, obj, **attr)[source]

Adds obj in the graph, if not present.

Parameters
objobject

An object to add in the graph.

Returns
obj_idint

Node id.

Notes

Due to the fact that two objects with non-overlapping lifetime might have the same identifier (address in memory), adding a node might overwrite the node with the same ID.

class pinspect.traverse.GraphBuilder(obj, key, ignore_key='', ignore_class=(), max_depth=10)[source]

Bases: object

traverse(self, obj, parent_edge=None, level=0)[source]
strip(self, with_methods=True)[source]
pinspect.traverse.find(obj, key, ignore_key='', ignore_class=(), verbose=True, visualize=True)[source]

Traverse the object obj and find methods and attributes that match the key.

Parameters
objobject

An object to inspect for key.

keystr

A key to look for.

ignore_keystr or list, optional

A string or a list of strings to ignore obj attributes and methods from being accessed and executed. Apart from user-provided strings, all methods that contain one of the following key-words will be ignored:

‘save’, ‘write’, ‘remove’, ‘delete’, ‘duplicate’

For the total list of ignored key-words, see NON_EXECUTABLE in utils.py.

ignore_classlist, optional

A list of class types to ignore. Apart from user-provided class types, all numpy functions will not be executed.

verbosebool, optional

If set to True, prints found matches in console. Default is True.

visualizebool, optional

If set to True, renders a graph in a web browser, using pyvis package.

Returns
graphnx.DiGraph

Stripped graph with edges and nodes that match the key.

Raises
ValueError

If the key is a part of ignore_key.