diff --git a/libpysal/graph/base.py b/libpysal/graph/base.py index 4c9529e76..4a579a99e 100644 --- a/libpysal/graph/base.py +++ b/libpysal/graph/base.py @@ -151,6 +151,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"" + ) + 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 100450f8f..5d4eed885 100644 --- a/libpysal/graph/tests/test_base.py +++ b/libpysal/graph/tests/test_base.py @@ -138,6 +138,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 = ( + "" + ) + assert repr(self.g_int) == expected + + expected = ( + "" + ) + assert repr(self.g_str) == expected + + nybb = graph.Graph.build_contiguity(self.nybb) + expected = ( + "" + ) + assert repr(nybb) == expected + def test_copy(self): g_copy = self.g_str.copy() assert g_copy == self.g_str