Skip to content

Commit 4025b10

Browse files
petercorkeclaude
andcommitted
refactor: BaseGraph is the public name of the common graph base class
The class was named _BaseGraph when PGraph was split into a base class and UGraph/DGraph, but it is in the signature of every graph method, is shown in the documentation, and is what a user writes in a type hint, while its counterpart BaseVertex has no underscore. _BaseGraph was never released, so there is no alias. BaseGraph and BaseVertex are exported from pgraph, with an __all__. Mechanical: with the rename reversed, PGraph.py and exporters.py are byte-identical to the previous commit. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent d134581 commit 4025b10

3 files changed

Lines changed: 62 additions & 44 deletions

File tree

‎src/pgraph/PGraph.py‎

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from spatialmath.base.graphics import axes_logic
1717

1818

19-
class _BaseGraph(ABC):
19+
class BaseGraph(ABC):
2020

2121
#: concrete vertex type for this graph kind, provided by :class:`UGraph`
2222
#: and :class:`DGraph` -- lets :meth:`add_vertex` and :meth:`vertex_copy`
@@ -140,7 +140,7 @@ def __repr__(self) -> str:
140140
return str(self)
141141

142142
@classmethod
143-
def Dict(cls, d: dict, reverse: bool = False, **kwargs: Any) -> _BaseGraph:
143+
def Dict(cls, d: dict, reverse: bool = False, **kwargs: Any) -> BaseGraph:
144144
"""
145145
Create graph from parent/child dictionary
146146
@@ -150,7 +150,7 @@ def Dict(cls, d: dict, reverse: bool = False, **kwargs: Any) -> _BaseGraph:
150150
:param kwargs: optional arguments passed to the graph constructor, for
151151
example ``vertex_class`` or ``edge_class``
152152
:raises TypeError: ``vertex_class`` or ``edge_class`` is not a
153-
suitable class, see :meth:`_BaseGraph.__init__`
153+
suitable class, see :meth:`BaseGraph.__init__`
154154
:return: graph
155155
:rtype: UGraph or DGraph
156156
@@ -207,7 +207,7 @@ def Adjacency(
207207
coords: NDArray | None = None,
208208
names: list[str] | None = None,
209209
**kwargs: Any,
210-
) -> _BaseGraph:
210+
) -> BaseGraph:
211211
"""
212212
Create graph from adjacency matrix
213213
@@ -222,7 +222,7 @@ def Adjacency(
222222
:raises ValueError: ``A`` is not square, or the length of ``names`` or
223223
the number of rows of ``coords`` does not match ``A``
224224
:raises TypeError: ``vertex_class`` or ``edge_class`` is not a
225-
suitable class, see :meth:`_BaseGraph.__init__`
225+
suitable class, see :meth:`BaseGraph.__init__`
226226
:return: graph
227227
:rtype: UGraph or DGraph
228228
@@ -306,12 +306,12 @@ def edge_class(self) -> type[Edge]:
306306
"""
307307
return self._edge_class
308308

309-
def copy(self) -> _BaseGraph:
309+
def copy(self) -> BaseGraph:
310310
"""
311311
Deepcopy of graph
312312
313313
:return: deep copy
314-
:rtype: _BaseGraph
314+
:rtype: BaseGraph
315315
316316
.. runblock:: pycon
317317
@@ -370,7 +370,7 @@ def add_vertex(
370370
>>> print(v2.name)
371371
372372
If the graph was constructed with a required ``dim`` (see
373-
:meth:`_BaseGraph.__init__`), every embedded vertex must have a
373+
:meth:`BaseGraph.__init__`), every embedded vertex must have a
374374
coordinate of exactly that length -- adding one of the wrong length
375375
raises ``ValueError``:
376376
@@ -2478,18 +2478,18 @@ def path_Astar(
24782478
# -------------------------------------------------------------------------- #
24792479

24802480

2481-
class UGraph(_BaseGraph):
2481+
class UGraph(BaseGraph):
24822482
"""
24832483
Class for undirected graphs
24842484
24852485
Vertices are :class:`UVertex` objects, or instances of the subclass given
24862486
as ``vertex_class``. Edges are :class:`Edge` objects, or instances of the
2487-
subclass given as ``edge_class``; see :class:`_BaseGraph` for the
2487+
subclass given as ``edge_class``; see :class:`BaseGraph` for the
24882488
constructor arguments and the exceptions they raise.
24892489
24902490
.. inheritance-diagram:: UGraph
24912491
2492-
:seealso: :class:`_BaseGraph` :class:`DGraph`
2492+
:seealso: :class:`BaseGraph` :class:`DGraph`
24932493
"""
24942494

24952495
def _graphcolor(self) -> None:
@@ -2537,18 +2537,18 @@ def _graphcolor(self) -> None:
25372537
self._ncomponents = 0 if lastlabel is None else lastlabel + 1
25382538

25392539

2540-
class DGraph(_BaseGraph):
2540+
class DGraph(BaseGraph):
25412541
"""
25422542
Class for directed graphs
25432543
25442544
Vertices are :class:`DVertex` objects, or instances of the subclass given
25452545
as ``vertex_class``. Edges are :class:`Edge` objects, or instances of the
2546-
subclass given as ``edge_class``; see :class:`_BaseGraph` for the
2546+
subclass given as ``edge_class``; see :class:`BaseGraph` for the
25472547
constructor arguments and the exceptions they raise.
25482548
25492549
.. inheritance-diagram:: DGraph
25502550
2551-
:seealso: :class:`_BaseGraph` :class:`UGraph`
2551+
:seealso: :class:`BaseGraph` :class:`UGraph`
25522552
"""
25532553

25542554
def _graphcolor(self) -> int | None:
@@ -2654,7 +2654,7 @@ class Edge:
26542654
- Inheritance is an alternative to providing arbitrary user data.
26552655
- Pass the subclass as ``edge_class`` when constructing the graph and
26562656
the graph will create instances of it itself, see
2657-
:class:`_BaseGraph`.
2657+
:class:`BaseGraph`.
26582658
26592659
An Edge points to a pair of vertices. At ``connect`` time the vertices
26602660
get references back to the Edge object.
@@ -2828,7 +2828,7 @@ def connect(self, v1: BaseVertex, v2: BaseVertex) -> None:
28282828
:raises ValueError: either vertex has not been added to a graph, or
28292829
the vertices belong to different graphs
28302830
:raises TypeError: this edge is not an instance of the graph's
2831-
:attr:`_BaseGraph.edge_class`
2831+
:attr:`BaseGraph.edge_class`
28322832
28332833
The edge connects vertices ``v1`` and ``v2``, and is added to the
28342834
graph that those vertices belong to.
@@ -2948,13 +2948,13 @@ def remove(self) -> None:
29482948
``e.remove()`` removes ``e`` from its graph's own edge collection and
29492949
from its connected vertices' edge lists (both, for an undirected
29502950
edge; the source only, for a directed one -- see
2951-
:meth:`_BaseGraph.remove_edge`), and clears ``e.v1``/``e.v2`` to
2951+
:meth:`BaseGraph.remove_edge`), and clears ``e.v1``/``e.v2`` to
29522952
``None``. The ``Edge`` object itself is not deleted -- it becomes an
29532953
orphaned, disconnected shell, and calling ``remove()`` on it again
29542954
raises ``ValueError``.
29552955
29562956
.. note:: This is a thin convenience wrapper around
2957-
:meth:`_BaseGraph.remove_edge`.
2957+
:meth:`BaseGraph.remove_edge`.
29582958
29592959
:rtype: None
29602960
@@ -2968,7 +2968,7 @@ def remove(self) -> None:
29682968
>>> e.remove()
29692969
>>> print(g)
29702970
2971-
:seealso: :meth:`_BaseGraph.remove_edge` :meth:`BaseVertex.remove`
2971+
:seealso: :meth:`BaseGraph.remove_edge` :meth:`BaseVertex.remove`
29722972
"""
29732973
if self.v1 is None or self.v1._graph is None:
29742974
raise ValueError("edge is not connected to a graph")
@@ -3001,15 +3001,15 @@ def __init__(self, coord: ArrayLike | None = None, name: str | None = None):
30013001
:type name: str, optional
30023002
30033003
Creates a vertex but does not add it to a graph -- use
3004-
:meth:`_BaseGraph.add_vertex` for that.
3004+
:meth:`BaseGraph.add_vertex` for that.
30053005
30063006
.. runblock:: pycon
30073007
30083008
>>> from pgraph import UVertex
30093009
>>> v1 = UVertex(coord=[0,0], name='v1')
30103010
>>> print(v1)
30113011
3012-
:seealso: :meth:`_BaseGraph.add_vertex`
3012+
:seealso: :meth:`BaseGraph.add_vertex`
30133013
"""
30143014
self._edgelist: list[Edge] = []
30153015
if coord is None:
@@ -3020,7 +3020,7 @@ def __init__(self, coord: ArrayLike | None = None, name: str | None = None):
30203020
self.label: int | None = None
30213021
self._connectivitychange = True
30223022
self._edgelist = []
3023-
self._graph: _BaseGraph | None = None # reference to owning graph
3023+
self._graph: BaseGraph | None = None # reference to owning graph
30243024
# print('BaseVertex init', type(self))
30253025

30263026
def __str__(self) -> str:
@@ -3057,7 +3057,7 @@ def __repr__(self) -> str:
30573057
coord = ", ".join([f"{x:.4g}" for x in self.coord])
30583058
return f"{self.__class__.__name__}[{self.name:s}, coord=({coord})]"
30593059

3060-
def copy(self, cls: type[_BaseGraph] | None = None) -> BaseVertex:
3060+
def copy(self, cls: type[BaseGraph] | None = None) -> BaseVertex:
30613061
"""
30623062
Copy a vertex
30633063
@@ -3094,7 +3094,7 @@ def adjacent(self) -> list[BaseVertex]:
30943094
30953095
``v.adjacent()`` is a list of the vertices that this vertex is joined
30963096
to by an edge. "Adjacent" is the graph-theory term, and it is the
3097-
relation that :meth:`_BaseGraph.adjacency` tabulates: row ``i`` of the
3097+
relation that :meth:`BaseGraph.adjacency` tabulates: row ``i`` of the
30983098
adjacency matrix is non-zero exactly at the columns of the vertices
30993099
returned by ``g[i].adjacent()``.
31003100
@@ -3120,7 +3120,7 @@ def adjacent(self) -> list[BaseVertex]:
31203120
>>> print(v1.adjacent())
31213121
31223122
:seealso: :meth:`isadjacent` :meth:`incidences` :meth:`edges`
3123-
:meth:`_BaseGraph.adjacency`
3123+
:meth:`BaseGraph.adjacency`
31243124
"""
31253125
return [e.next(self) for e in self._edgelist]
31263126

@@ -3198,7 +3198,7 @@ def connect(
31983198
:param dest: The vertex to connect to
31993199
:type dest: ``BaseVertex`` subclass
32003200
:param edge: Use this as the edge object, otherwise a new object of the
3201-
graph's :attr:`_BaseGraph.edge_class` is created from the
3201+
graph's :attr:`BaseGraph.edge_class` is created from the
32023202
vertices being connected, and the ``cost`` and ``data``
32033203
parameters, defaults to None
32043204
:type edge: ``Edge`` subclass, optional
@@ -3211,7 +3211,7 @@ def connect(
32113211
vertices belong to different graphs, or ``edge`` already connects
32123212
other vertices
32133213
:raises TypeError: ``edge`` is not an instance of the graph's
3214-
:attr:`_BaseGraph.edge_class`
3214+
:attr:`BaseGraph.edge_class`
32153215
:return: the edge connecting the vertices
32163216
:rtype: Edge
32173217
@@ -3302,7 +3302,7 @@ def _detach(self, edge: Edge, dest: BaseVertex) -> None:
33023302
:raises NotImplementedError: not provided by this vertex kind
33033303
33043304
The twin of :meth:`_attach`: it undoes exactly what that recorded.
3305-
Called by :meth:`_BaseGraph.remove_edge` before the edge's endpoints
3305+
Called by :meth:`BaseGraph.remove_edge` before the edge's endpoints
33063306
are cleared. An edge that is absent from a list is skipped rather
33073307
than treated as an error.
33083308
"""
@@ -3409,7 +3409,7 @@ def heuristic_distance(self, v2: BaseVertex) -> float:
34093409
:rtype: float
34103410
34113411
Distance is computed according to the graph's heuristic, see
3412-
:meth:`_BaseGraph.heuristic`. The vertex must belong to a graph,
3412+
:meth:`BaseGraph.heuristic`. The vertex must belong to a graph,
34133413
since the heuristic is a property of the graph, not the vertex.
34143414
34153415
.. runblock:: pycon
@@ -3420,7 +3420,7 @@ def heuristic_distance(self, v2: BaseVertex) -> float:
34203420
>>> v2 = g.add_vertex(coord=[4, 3], name='v2')
34213421
>>> print(v1.heuristic_distance(v2))
34223422
3423-
:seealso: :meth:`distance` :meth:`_BaseGraph.heuristic`
3423+
:seealso: :meth:`distance` :meth:`BaseGraph.heuristic`
34243424
"""
34253425
if self._graph is None:
34263426
raise ValueError("vertex is not connected to a graph")
@@ -3569,7 +3569,7 @@ def closest(self) -> tuple[BaseVertex | None, float]:
35693569
>>> v4 = g.add_vertex(coord=[4,3], name='v4')
35703570
>>> print(v1.closest())
35713571
3572-
:seealso: :meth:`_BaseGraph.closest`
3572+
:seealso: :meth:`BaseGraph.closest`
35733573
"""
35743574
if self._graph is None:
35753575
raise ValueError("vertex is not connected to a graph")
@@ -3588,7 +3588,7 @@ def remove(self) -> None:
35883588
not deleted.
35893589
35903590
.. note:: This is a thin convenience wrapper around
3591-
:meth:`_BaseGraph.remove_vertex`.
3591+
:meth:`BaseGraph.remove_vertex`.
35923592
35933593
:rtype: None
35943594
@@ -3602,7 +3602,7 @@ def remove(self) -> None:
36023602
>>> v2.remove()
36033603
>>> print(g)
36043604
3605-
:seealso: :meth:`_BaseGraph.remove_vertex` :meth:`Edge.remove`
3605+
:seealso: :meth:`BaseGraph.remove_vertex` :meth:`Edge.remove`
36063606
"""
36073607
if self._graph is None:
36083608
raise ValueError("vertex is not connected to a graph")

‎src/pgraph/__init__.py‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,19 @@
1-
from pgraph.PGraph import UGraph, DGraph, UVertex, DVertex, Edge
1+
from pgraph.PGraph import (
2+
BaseGraph,
3+
BaseVertex,
4+
UGraph,
5+
DGraph,
6+
UVertex,
7+
DVertex,
8+
Edge,
9+
)
10+
11+
__all__ = [
12+
"BaseGraph",
13+
"BaseVertex",
14+
"UGraph",
15+
"DGraph",
16+
"UVertex",
17+
"DVertex",
18+
"Edge",
19+
]

‎src/pgraph/exporters.py‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Export a graph as text (GraphViz DOT, Mermaid, GraphML, ELK JSON) or as a
33
rendered image (via the GraphViz command line tools).
44
5-
This module is reached through :meth:`pgraph.PGraph._BaseGraph.export`, which
5+
This module is reached through :meth:`pgraph.PGraph.BaseGraph.export`, which
66
dispatches on the format name or the file extension. Each text exporter is a
77
plain function ``f(graph, **options) -> str``, registered with
88
:func:`register_exporter`, so a user can add a format without touching the
@@ -27,7 +27,7 @@ def gexf(graph, *, version="1.3") -> str:
2727
from typing import IO, TYPE_CHECKING, Any, Callable
2828

2929
if TYPE_CHECKING:
30-
from .PGraph import BaseVertex, Edge, _BaseGraph
30+
from .PGraph import BaseVertex, Edge, BaseGraph
3131

3232
TextExporter = Callable[..., str]
3333

@@ -114,13 +114,13 @@ def format_from_filename(filename: str | os.PathLike | IO | None) -> str | None:
114114
# helpers shared by the exporters
115115

116116

117-
def _is_directed(graph: _BaseGraph) -> bool:
117+
def _is_directed(graph: BaseGraph) -> bool:
118118
from .PGraph import DGraph # not at module level: PGraph imports this module
119119

120120
return isinstance(graph, DGraph)
121121

122122

123-
def _ordered_edges(graph: _BaseGraph) -> list[tuple[Edge, BaseVertex, BaseVertex]]:
123+
def _ordered_edges(graph: BaseGraph) -> list[tuple[Edge, BaseVertex, BaseVertex]]:
124124
"""
125125
Edges in a reproducible order
126126
@@ -144,7 +144,7 @@ def _ordered_edges(graph: _BaseGraph) -> list[tuple[Edge, BaseVertex, BaseVertex
144144
return edges
145145

146146

147-
def _component_colors(graph: _BaseGraph) -> dict[BaseVertex, str]:
147+
def _component_colors(graph: BaseGraph) -> dict[BaseVertex, str]:
148148
"""
149149
Map each vertex to a ``#rrggbb`` colour for its component, as plot() does
150150
"""
@@ -194,7 +194,7 @@ def _dot_quote(text: str) -> str:
194194

195195
@register_exporter("dot", ".dot", ".gv")
196196
def dot(
197-
graph: _BaseGraph,
197+
graph: BaseGraph,
198198
*,
199199
direction: str | None = None,
200200
colorcomponents: bool = False,
@@ -243,7 +243,7 @@ def dot(
243243

244244
@register_exporter("mermaid", ".mmd", ".mermaid")
245245
def mermaid(
246-
graph: _BaseGraph,
246+
graph: BaseGraph,
247247
*,
248248
direction: str = "TB",
249249
colorcomponents: bool = False,
@@ -292,7 +292,7 @@ def mermaid(
292292

293293

294294
@register_exporter("graphml", ".graphml")
295-
def graphml(graph: _BaseGraph) -> str:
295+
def graphml(graph: BaseGraph) -> str:
296296
"""
297297
GraphML XML text
298298
@@ -357,7 +357,7 @@ def key(key_id: str, domain: str, name: str, type_: str) -> str:
357357

358358
@register_exporter("elk", ".elk.json", ".elk")
359359
def elk(
360-
graph: _BaseGraph,
360+
graph: BaseGraph,
361361
*,
362362
size: tuple[float, float] = (60, 40),
363363
layout_options: dict[str, str] | None = None,
@@ -433,7 +433,7 @@ def elk(
433433

434434

435435
def render(
436-
graph: _BaseGraph,
436+
graph: BaseGraph,
437437
fmt: str,
438438
*,
439439
engine: str | None = None,

0 commit comments

Comments
 (0)