From 359e1301c92084c438dff815893b1606d562f360 Mon Sep 17 00:00:00 2001 From: Martin Fleischmann Date: Tue, 9 Jul 2024 17:24:49 +0200 Subject: [PATCH] ensure that Graph._repr_ fits to 80 characters (#747) * ensure that Graph._repr_ fits to 80 characters * no h3 --- libpysal/graph/base.py | 19 +++++++++------- libpysal/graph/tests/test_base.py | 37 +++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/libpysal/graph/base.py b/libpysal/graph/base.py index 7a06951d5..71ac883a3 100644 --- a/libpysal/graph/base.py +++ b/libpysal/graph/base.py @@ -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 ( @@ -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 @@ -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 - """ # 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 diff --git a/libpysal/graph/tests/test_base.py b/libpysal/graph/tests/test_base.py index 526ca23a5..46eb4338c 100644 --- a/libpysal/graph/tests/test_base.py +++ b/libpysal/graph/tests/test_base.py @@ -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 = ( + "" + ) + assert repr(h3_g) == expected + def test_copy(self): g_copy = self.g_str.copy() assert g_copy == self.g_str