Skip to content

Commit a17d3cc

Browse files
petercorkeclaude
andcommitted
docs: vertex_class/edge_class and exceptions on connect, Edge, UGraph, DGraph, factories
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent a5df2c8 commit a17d3cc

1 file changed

Lines changed: 37 additions & 5 deletions

File tree

‎src/pgraph/PGraph.py‎

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ def Dict(cls, d: dict, reverse: bool = False, **kwargs: Any) -> _BaseGraph:
149149
:param reverse: reverse link direction, defaults to False
150150
:param kwargs: optional arguments passed to the graph constructor, for
151151
example ``vertex_class`` or ``edge_class``
152+
:raises TypeError: ``vertex_class`` or ``edge_class`` is not a
153+
suitable class, see :meth:`_BaseGraph.__init__`
152154
:return: graph
153155
:rtype: UGraph or DGraph
154156
@@ -217,14 +219,23 @@ def Adjacency(
217219
:type names: list(N) of str, optional
218220
:param kwargs: optional arguments passed to the graph constructor, for
219221
example ``vertex_class`` or ``edge_class``
222+
:raises ValueError: ``A`` is not square, the length of ``names`` or
223+
the number of rows of ``coords`` does not match ``A``, or ``A``
224+
has a non-zero diagonal element and the graph is a ``DGraph``
225+
:raises TypeError: ``vertex_class`` or ``edge_class`` is not a
226+
suitable class, see :meth:`_BaseGraph.__init__`
220227
:return: graph
221228
:rtype: UGraph or DGraph
222229
223230
Create a directed or undirected graph where non-zero elements ``A[i,j]``
224231
correspond to edges from vertex ``i`` to vertex ``j``.
225232
226233
.. warning:: For undirected graph ``A`` should be symmetric but this
227-
is not checked. Only the upper triangular part is used.
234+
is not checked. Only the strictly upper triangular part is used,
235+
so the diagonal is ignored and no self-loops are created. A
236+
directed graph rejects a non-zero diagonal with ``ValueError``.
237+
Edge counts greater than one are not turned into parallel edges;
238+
a non-zero element is a single edge with that element as its cost.
228239
229240
.. runblock:: pycon
230241
@@ -2179,6 +2190,11 @@ class UGraph(_BaseGraph):
21792190
"""
21802191
Class for undirected graphs
21812192
2193+
Vertices are :class:`UVertex` objects, or instances of the subclass given
2194+
as ``vertex_class``. Edges are :class:`Edge` objects, or instances of the
2195+
subclass given as ``edge_class``; see :class:`_BaseGraph` for the
2196+
constructor arguments and the exceptions they raise.
2197+
21822198
.. inheritance-diagram:: UGraph
21832199
21842200
:seealso: :class:`_BaseGraph` :class:`DGraph`
@@ -2233,6 +2249,11 @@ class DGraph(_BaseGraph):
22332249
"""
22342250
Class for directed graphs
22352251
2252+
Vertices are :class:`DVertex` objects, or instances of the subclass given
2253+
as ``vertex_class``. Edges are :class:`Edge` objects, or instances of the
2254+
subclass given as ``edge_class``; see :class:`_BaseGraph` for the
2255+
constructor arguments and the exceptions they raise.
2256+
22362257
.. inheritance-diagram:: DGraph
22372258
22382259
:seealso: :class:`_BaseGraph` :class:`UGraph`
@@ -2333,6 +2354,9 @@ class Edge:
23332354
edgelist of _each_ vertex.
23342355
- This class can be inherited to provide user objects with graph capability.
23352356
- Inheritance is an alternative to providing arbitrary user data.
2357+
- Pass the subclass as ``edge_class`` when constructing the graph and
2358+
the graph will create instances of it itself, see
2359+
:class:`_BaseGraph`.
23362360
23372361
An Edge points to a pair of vertices. At ``connect`` time the vertices
23382362
get references back to the Edge object.
@@ -2453,6 +2477,8 @@ def connect(self, v1: BaseVertex, v2: BaseVertex) -> None:
24532477
:type v2: BaseVertex subclass
24542478
:raises ValueError: either vertex has not been added to a graph, or
24552479
the vertices belong to different graphs
2480+
:raises TypeError: this edge is not an instance of the graph's
2481+
:attr:`_BaseGraph.edge_class`
24562482
24572483
The edge connects vertices ``v1`` and ``v2``, and is added to the
24582484
graph that those vertices belong to.
@@ -3211,8 +3237,9 @@ def connect(
32113237
32123238
:param dest: vertex to connect to
32133239
:type dest: BaseVertex subclass
3214-
:param edge: Use this as the edge object, otherwise a new ``Edge``
3215-
object is created, defaults to None
3240+
:param edge: Use this as the edge object, otherwise a new object of the
3241+
graph's :attr:`_BaseGraph.edge_class` is created,
3242+
defaults to None
32163243
:type edge: ``Edge`` subclass, optional
32173244
:param cost: the cost to traverse this edge, defaults to None
32183245
:type cost: float, optional
@@ -3222,6 +3249,8 @@ def connect(
32223249
:raises ValueError: either vertex has not been added to a graph, the
32233250
vertices belong to different graphs, or ``edge`` already connects
32243251
other vertices
3252+
:raises TypeError: ``edge`` is not an instance of the graph's
3253+
:attr:`_BaseGraph.edge_class`
32253254
:return: the edge connecting the vertices
32263255
:rtype: Edge
32273256
@@ -3287,8 +3316,9 @@ def connect(
32873316
32883317
:param dest: vertex to connect to
32893318
:type dest: BaseVertex subclass
3290-
:param edge: Use this as the edge object, otherwise a new ``Edge``
3291-
object is created, defaults to None
3319+
:param edge: Use this as the edge object, otherwise a new object of the
3320+
graph's :attr:`_BaseGraph.edge_class` is created,
3321+
defaults to None
32923322
:type edge: ``Edge`` subclass, optional
32933323
:param cost: the cost to traverse this edge, defaults to None
32943324
:type cost: float, optional
@@ -3298,6 +3328,8 @@ def connect(
32983328
:raises ValueError: either vertex has not been added to a graph, the
32993329
vertices belong to different graphs, or ``edge`` already connects
33003330
other vertices
3331+
:raises TypeError: ``edge`` is not an instance of the graph's
3332+
:attr:`_BaseGraph.edge_class`
33013333
:return: the edge connecting the vertices
33023334
:rtype: Edge
33033335

0 commit comments

Comments
 (0)