Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: implement Graph.__repr__ #711

Merged
merged 3 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions libpysal/graph/base.py
jGaboardi marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,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"<Graph of {self.n} nodes and {self.nonzero} nonzero edges indexed by\n"
f" {unique_ids}>"
)

def copy(self, deep=True):
"""Make a copy of this Graph's adjacency table and transformation

Expand Down
20 changes: 20 additions & 0 deletions libpysal/graph/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
"<Graph of 10 nodes and 25 nonzero edges indexed by\n"
" [0, 1, 2, 3, 4, ...]>"
)
assert repr(self.g_int) == expected

expected = (
"<Graph of 10 nodes and 25 nonzero edges indexed by\n"
" ['a', 'b', 'c', 'd', 'e', ...]>"
)
assert repr(self.g_str) == expected

nybb = graph.Graph.build_contiguity(self.nybb)
expected = (
"<Graph of 5 nodes and 10 nonzero edges indexed by\n"
" ['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
Expand Down