objectgraph.rst 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. :mod:`altgraph.ObjectGraph` --- Graphs of objecs with an identifier
  2. ===================================================================
  3. .. module:: altgraph.ObjectGraph
  4. :synopsis: A graph of objects that have a "graphident" attribute.
  5. .. class:: ObjectGraph([graph[, debug]])
  6. A graph of objects that have a "graphident" attribute. The
  7. value of this attribute is the key for the object in the
  8. graph.
  9. The optional *graph* is a previously constructed
  10. :class:`Graph <altgraph.Graph.Graph>`.
  11. The optional *debug* level controls the amount of debug output
  12. (see :meth:`msg`, :meth:`msgin` and :meth:`msgout`).
  13. .. note:: the altgraph library does not generate output, the
  14. debug attribute and message methods are present for use
  15. by subclasses.
  16. .. data:: ObjectGraph.graph
  17. An :class:`Graph <altgraph.Graph.Graph>` object that contains
  18. the graph data.
  19. .. method:: ObjectGraph.addNode(node)
  20. Adds a *node* to the graph.
  21. .. note:: re-adding a node that was previously removed
  22. using :meth:`removeNode` will reinstate the previously
  23. removed node.
  24. .. method:: ObjectGraph.createNode(self, cls, name, \*args, \**kwds)
  25. Creates a new node using ``cls(*args, **kwds)`` and adds that
  26. node using :meth:`addNode`.
  27. Returns the newly created node.
  28. .. method:: ObjectGraph.removeNode(node)
  29. Removes a *node* from the graph when it exists. The *node* argument
  30. is either a node object, or the graphident of a node.
  31. .. method:: ObjectGraph.createReferences(fromnode, tonode[, edge_data])
  32. Creates a reference from *fromnode* to *tonode*. The optional
  33. *edge_data* is associated with the edge.
  34. *Fromnode* and *tonode* can either be node objects or the graphident
  35. values for nodes.
  36. .. method:: removeReference(fromnode, tonode)
  37. Removes the reference from *fromnode* to *tonode* if it exists.
  38. .. method:: ObjectGraph.getRawIdent(node)
  39. Returns the *graphident* attribute of *node*, or the graph itself
  40. when *node* is :data:`None`.
  41. .. method:: getIdent(node)
  42. Same as :meth:`getRawIdent`, but only if the node is part
  43. of the graph.
  44. *Node* can either be an actual node object or the graphident of
  45. a node.
  46. .. method:: ObjectGraph.findNode(node)
  47. Returns a given node in the graph, or :data:`Node` when it cannot
  48. be found.
  49. *Node* is either an object with a *graphident* attribute or
  50. the *graphident* attribute itself.
  51. .. method:: ObjectGraph.__contains__(node)
  52. Returns True if *node* is a member of the graph. *Node* is either an
  53. object with a *graphident* attribute or the *graphident* attribute itself.
  54. .. method:: ObjectGraph.flatten([condition[, start]])
  55. Yield all nodes that are entirely reachable by *condition*
  56. starting fromt he given *start* node or the graph root.
  57. .. note:: objects are only reachable from the graph root
  58. when there is a reference from the root to the node
  59. (either directly or through another node)
  60. .. method:: ObjectGraph.nodes()
  61. Yield all nodes in the graph.
  62. .. method:: ObjectGraph.get_edges(node)
  63. Returns two iterators that yield the nodes reaching by
  64. outgoing and incoming edges.
  65. .. method:: ObjectGraph.filterStack(filters)
  66. Filter the ObjectGraph in-place by removing all edges to nodes that
  67. do not match every filter in the given filter list
  68. Returns a tuple containing the number of:
  69. (*nodes_visited*, *nodes_removed*, *nodes_orphaned*)
  70. .. method:: ObjectGraph.edgeData(fromNode, toNode):
  71. Return the edge data associated with the edge from *fromNode*
  72. to *toNode*. Raises :exc:`KeyError` when no such edge exists.
  73. .. versionadded: 0.12
  74. .. method:: ObjectGraph.updateEdgeData(fromNode, toNode, edgeData)
  75. Replace the data associated with the edge from *fromNode* to
  76. *toNode* by *edgeData*.
  77. Raises :exc:`KeyError` when the edge does not exist.
  78. Debug output
  79. ------------
  80. .. data:: ObjectGraph.debug
  81. The current debug level.
  82. .. method:: ObjectGraph.msg(level, text, \*args)
  83. Print a debug message at the current indentation level when the current
  84. debug level is *level* or less.
  85. .. method:: ObjectGraph.msgin(level, text, \*args)
  86. Print a debug message when the current debug level is *level* or less,
  87. and increase the indentation level.
  88. .. method:: ObjectGraph.msgout(level, text, \*args)
  89. Decrease the indentation level and print a debug message when the
  90. current debug level is *level* or less.