From 1f9d0effae84bc3582556db879797c114041767b Mon Sep 17 00:00:00 2001 From: Alexander Hess Date: Tue, 1 Dec 2026 22:27:07 +0100 Subject: [PATCH] Cache `City.map` with `functools.cached_property` This replaces the manual caching in the uninitialized `._map` attribute with a `hasattr()` check. `.clear_map()` now only discards the cached map. --- src/velofy/db/cities.py | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/src/velofy/db/cities.py b/src/velofy/db/cities.py index 570baa6..879742c 100644 --- a/src/velofy/db/cities.py +++ b/src/velofy/db/cities.py @@ -79,7 +79,6 @@ class City(meta.Base): ) # We do not implement a `.__init__()` method and use SQLAlchemy's default. - # The uninitialized attribute `._map` is computed on the fly. note:d334120ei def __repr__(self) -> str: """Non-literal text representation.""" @@ -129,31 +128,26 @@ class City(meta.Base): return self.northeast.northing - self.southwest.northing def clear_map(self) -> t.Self: # pragma: no cover - """Create a new `folium.Map` object aligned with the city's viewport. - - The map is available via the `.map` property. Note that it is mutable - and changed from various locations in the code base. + """Discard the `.map` such that the next access creates a new one. Returns: self: enabling method chaining - """ # note:d334120e - self._map = folium.Map( - location=[self.center_latitude, self.center_longitude], - zoom_start=self.initial_zoom, - ) + """ + vars(self).pop("map", None) return self - @property # pragma: no cover + @functools.cached_property # pragma: no cover def map(self) -> folium.Map: """A `folium.Map` object aligned with the city's viewport. - See docstring for `.clear_map()` for further info. + The map is mutable and changed from various locations in the code base. + Use `.clear_map()` to start over with an empty map. """ - if not hasattr(self, "_map"): # note:d334120e - self.clear_map() - - return self._map + return folium.Map( + location=[self.center_latitude, self.center_longitude], + zoom_start=self.initial_zoom, + ) def draw_restaurants( self,