Skip to content

Commit 149abc1

Browse files
authored
Merge pull request #57 from petercorke/fix/graph-str-thousands
fix: Graph.__str__ separates thousands in its counts
2 parents 5c64e4b + 2a0aac2 commit 149abc1

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

‎src/pgraph/PGraph.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,8 @@ def __str__(self) -> str:
288288
:return: one-line summary of vertex/edge/component counts
289289
:rtype: str
290290
291+
Counts of 1,000 or more have thousands separators.
292+
291293
.. runblock:: pycon
292294
293295
>>> from pgraph import UGraph
@@ -301,7 +303,7 @@ def __str__(self) -> str:
301303
302304
:seealso: :meth:`show`
303305
"""
304-
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]}"
306+
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]}"
305307
return s
306308

307309
def __repr__(self) -> str:

‎tests/test_graph.py‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import numpy as np
44
import numpy.testing as nt
55

6-
from pgraph import *
6+
from pgraph import DGraph, Edge, UGraph, Vertex
77

88
class TestUGraph(unittest.TestCase):
99

@@ -51,6 +51,21 @@ def test_str(self):
5151
self.assertEqual(len(lines), 3) # class-name header + one line per vertex
5252
self.assertEqual(lines[0], "DGraph:")
5353

54+
55+
def test_str_has_thousands_separators(self):
56+
# counts of 1,000 or more are easier to read with separators
57+
g = UGraph()
58+
vs = [g.add_vertex() for _ in range(1001)]
59+
self.assertEqual(str(g), "UGraph: 1,001 vertices, 0 edges, 1,001 components")
60+
for a, b in zip(vs, vs[1:]):
61+
g.add_edge(a, b)
62+
self.assertEqual(str(g), "UGraph: 1,001 vertices, 1,000 edges, 1 component")
63+
# below a thousand it is as before
64+
h = UGraph()
65+
for _ in range(999):
66+
h.add_vertex()
67+
self.assertEqual(str(h), "UGraph: 999 vertices, 0 edges, 999 components")
68+
5469
def test_attr(self):
5570

5671
g = UGraph()

0 commit comments

Comments
 (0)