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.
This commit is contained in:
Alexander Hess 2026-12-01 22:27:07 +01:00
commit 1f9d0effae
Signed by: alexander
GPG key ID: B46EDB4DFC26805D

View file

@ -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,