Skip to content

Commit

Permalink
ensure that Graph._repr_ fits to 80 characters (#747)
Browse files Browse the repository at this point in the history
* ensure that Graph._repr_ fits to 80 characters

* no h3
  • Loading branch information
martinfleis authored Jul 9, 2024
1 parent 5465287 commit 359e130
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
19 changes: 11 additions & 8 deletions libpysal/graph/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,10 @@ def __getitem__(self, item):

def __repr__(self):
if len(self.unique_ids) > 5:
unique_ids = f"{str(self.unique_ids[:5].tolist())[:-1]}, ...]"
ids = str(self.unique_ids[:5].tolist())[:-1] + ", "
if len(ids) > 72:
ids = str(self.unique_ids[:5].tolist())[:72]
unique_ids = f"{ids}...]"
else:
unique_ids = self.unique_ids.tolist()
return (
Expand Down Expand Up @@ -1341,7 +1344,7 @@ def build_h3(cls, ids, order=1, weight="distance"):
>>> import geopandas as gpd
>>> from geodatasets import get_path
>>> from tobler.util import h3fy
>>> gdf = gpd.read_file(get_path("geoda guerry")
>>> gdf = gpd.read_file(get_path("geoda guerry"))
>>> h3 = h3fy(gdf, resolution=4)
>>> h3.head()
geometry
Expand All @@ -1352,13 +1355,13 @@ def build_h3(cls, ids, order=1, weight="distance"):
8418609ffffffff POLYGON ((387747.482 2509794.492, 364375.032 2...
8418491ffffffff POLYGON ((320872.289 1846157.662, 296923.464 1...
>>> G = graph.Graph.build_h3(h3.index)
>>> G
>>> h3_contiguity = graph.Graph.build_h3(h3.index)
>>> h3_contiguity
<Graph of 320 nodes and 1740 nonzero edges indexed by
['841f94dffffffff', '841fa67ffffffff', '84186a3ffffffff', '8418609ffffffff', '8418491ffffffff', ...]>
""" # noqa: E501
neigbors, weights = _build_from_h3(ids, order=order)
g = cls.from_dicts(neigbors, weights)
['841f94dffffffff', '841fa67ffffffff', '84186a3ffffffff', ...]>
"""
neighbors, weights = _build_from_h3(ids, order=order)
g = cls.from_dicts(neighbors, weights)

if weight == "distance":
return g
Expand Down
37 changes: 37 additions & 0 deletions libpysal/graph/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,43 @@ def test___repr__(self):
)
assert repr(nybb) == expected

h3 = {
"821f87fffffffff": ("821fb7fffffffff", "821f97fffffffff"),
"821fb7fffffffff": (
"821f87fffffffff",
"821f97fffffffff",
"82186ffffffffff",
"821867fffffffff",
),
"821f97fffffffff": (
"821f87fffffffff",
"821fb7fffffffff",
"823967fffffffff",
"82396ffffffffff",
"82186ffffffffff",
),
"823967fffffffff": (
"821f97fffffffff",
"82396ffffffffff",
"82186ffffffffff",
),
"82396ffffffffff": ("821f97fffffffff", "823967fffffffff"),
"82186ffffffffff": (
"821fb7fffffffff",
"821f97fffffffff",
"823967fffffffff",
"821867fffffffff",
),
"821867fffffffff": ("821fb7fffffffff", "82186ffffffffff"),
}
h3_g = graph.Graph.from_dicts(h3)
expected = (
"<Graph of 7 nodes and 22 nonzero edges indexed by\n"
" ['821f87fffffffff', '821fb7fffffffff', '821f97fffffffff',"
" '823967fffffff...]>"
)
assert repr(h3_g) == expected

def test_copy(self):
g_copy = self.g_str.copy()
assert g_copy == self.g_str
Expand Down

0 comments on commit 359e130

Please sign in to comment.