From c7e75d260bddf0ba361a0d5c9c3f71ce2fe99bfb Mon Sep 17 00:00:00 2001 From: Martin Fleischmann Date: Mon, 27 May 2024 16:57:09 +0200 Subject: [PATCH 1/2] ENH: implement Graph.__repr__ --- libpysal/graph/base.py | 10 ++++++++++ libpysal/graph/tests/test_base.py | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/libpysal/graph/base.py b/libpysal/graph/base.py index d8301bd79..b25e04e3e 100644 --- a/libpysal/graph/base.py +++ b/libpysal/graph/base.py @@ -140,6 +140,16 @@ def __getitem__(self, item): ) return self._adjacency.loc[item] + def __repr__(self): + if len(self.unique_ids) > 5: + unique_ids = f"{str(self.unique_ids[:5].tolist())[:-1]}, ...]" + else: + unique_ids = self.unique_ids.tolist() + return ( + f"\n " + f"Indexed by: {unique_ids}" + ) + def copy(self, deep=True): """Make a copy of this Graph's adjacency table and transformation diff --git a/libpysal/graph/tests/test_base.py b/libpysal/graph/tests/test_base.py index 0197d7ede..e633b8782 100644 --- a/libpysal/graph/tests/test_base.py +++ b/libpysal/graph/tests/test_base.py @@ -137,6 +137,26 @@ def test_init(self): with pytest.raises(ValueError, match="'transformation' needs to be"): graph.Graph(self.adjacency_int_binary, transformation="foo") + def test___repr__(self): + expected = ( + "\n" + " Indexed by: [0, 1, 2, 3, 4, ...]" + ) + assert repr(self.g_int) == expected + + expected = ( + "\n" + " Indexed by: ['a', 'b', 'c', 'd', 'e', ...]" + ) + assert repr(self.g_str) == expected + + nybb = graph.Graph.build_contiguity(self.nybb) + expected = ( + "\n" + " Indexed by: ['Staten Island', 'Queens', 'Brooklyn', 'Manhattan', 'Bronx']" + ) + assert repr(nybb) == expected + def test_copy(self): g_copy = self.g_str.copy() assert g_copy == self.g_str From 07e3c663f2a524b8ff0f032b717d77385711d9c9 Mon Sep 17 00:00:00 2001 From: Martin Fleischmann Date: Tue, 28 May 2024 10:02:05 +0200 Subject: [PATCH 2/2] update the formatting --- libpysal/graph/base.py | 4 ++-- libpysal/graph/tests/test_base.py | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/libpysal/graph/base.py b/libpysal/graph/base.py index d20a5ec91..7a1c3f15a 100644 --- a/libpysal/graph/base.py +++ b/libpysal/graph/base.py @@ -155,8 +155,8 @@ def __repr__(self): else: unique_ids = self.unique_ids.tolist() return ( - f"\n " - f"Indexed by: {unique_ids}" + f"" ) def copy(self, deep=True): diff --git a/libpysal/graph/tests/test_base.py b/libpysal/graph/tests/test_base.py index 9eed3bae1..db17bff0b 100644 --- a/libpysal/graph/tests/test_base.py +++ b/libpysal/graph/tests/test_base.py @@ -139,21 +139,21 @@ def test_init(self): def test___repr__(self): expected = ( - "\n" - " Indexed by: [0, 1, 2, 3, 4, ...]" + "" ) assert repr(self.g_int) == expected expected = ( - "\n" - " Indexed by: ['a', 'b', 'c', 'd', 'e', ...]" + "" ) assert repr(self.g_str) == expected nybb = graph.Graph.build_contiguity(self.nybb) expected = ( - "\n" - " Indexed by: ['Staten Island', 'Queens', 'Brooklyn', 'Manhattan', 'Bronx']" + "" ) assert repr(nybb) == expected