diff --git a/src/pgraph/PGraph.py b/src/pgraph/PGraph.py index ae0e02b7..fd4fb7c7 100644 --- a/src/pgraph/PGraph.py +++ b/src/pgraph/PGraph.py @@ -288,6 +288,8 @@ def __str__(self) -> str: :return: one-line summary of vertex/edge/component counts :rtype: str + Counts of 1,000 or more have thousands separators. + .. runblock:: pycon >>> from pgraph import UGraph @@ -301,7 +303,7 @@ def __str__(self) -> str: :seealso: :meth:`show` """ - s = f"{self.__class__.__name__}: {self.n} {'vertex' if self.n==1 else 'vertices'}, {self.ne} edge{'s'[:self.ne^1]}, {self.nc} component{'s'[:self.nc^1]}" + s = f"{self.__class__.__name__}: {self.n:,} {'vertex' if self.n==1 else 'vertices'}, {self.ne:,} edge{'s'[:self.ne^1]}, {self.nc:,} component{'s'[:self.nc^1]}" return s def __repr__(self) -> str: diff --git a/tests/test_graph.py b/tests/test_graph.py index c0249c25..c10df002 100644 --- a/tests/test_graph.py +++ b/tests/test_graph.py @@ -3,7 +3,7 @@ import numpy as np import numpy.testing as nt -from pgraph import * +from pgraph import DGraph, Edge, UGraph, Vertex class TestUGraph(unittest.TestCase): @@ -51,6 +51,21 @@ def test_str(self): self.assertEqual(len(lines), 3) # class-name header + one line per vertex self.assertEqual(lines[0], "DGraph:") + + def test_str_has_thousands_separators(self): + # counts of 1,000 or more are easier to read with separators + g = UGraph() + vs = [g.add_vertex() for _ in range(1001)] + self.assertEqual(str(g), "UGraph: 1,001 vertices, 0 edges, 1,001 components") + for a, b in zip(vs, vs[1:]): + g.add_edge(a, b) + self.assertEqual(str(g), "UGraph: 1,001 vertices, 1,000 edges, 1 component") + # below a thousand it is as before + h = UGraph() + for _ in range(999): + h.add_vertex() + self.assertEqual(str(h), "UGraph: 999 vertices, 0 edges, 999 components") + def test_attr(self): g = UGraph()