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:
parent
2a627072d8
commit
1f9d0effae
1 changed files with 10 additions and 16 deletions
|
|
@ -79,7 +79,6 @@ class City(meta.Base):
|
||||||
)
|
)
|
||||||
|
|
||||||
# We do not implement a `.__init__()` method and use SQLAlchemy's default.
|
# 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:
|
def __repr__(self) -> str:
|
||||||
"""Non-literal text representation."""
|
"""Non-literal text representation."""
|
||||||
|
|
@ -129,31 +128,26 @@ class City(meta.Base):
|
||||||
return self.northeast.northing - self.southwest.northing
|
return self.northeast.northing - self.southwest.northing
|
||||||
|
|
||||||
def clear_map(self) -> t.Self: # pragma: no cover
|
def clear_map(self) -> t.Self: # pragma: no cover
|
||||||
"""Create a new `folium.Map` object aligned with the city's viewport.
|
"""Discard the `.map` such that the next access creates a new one.
|
||||||
|
|
||||||
The map is available via the `.map` property. Note that it is mutable
|
|
||||||
and changed from various locations in the code base.
|
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
self: enabling method chaining
|
self: enabling method chaining
|
||||||
""" # note:d334120e
|
"""
|
||||||
self._map = folium.Map(
|
vars(self).pop("map", None)
|
||||||
location=[self.center_latitude, self.center_longitude],
|
|
||||||
zoom_start=self.initial_zoom,
|
|
||||||
)
|
|
||||||
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
@property # pragma: no cover
|
@functools.cached_property # pragma: no cover
|
||||||
def map(self) -> folium.Map:
|
def map(self) -> folium.Map:
|
||||||
"""A `folium.Map` object aligned with the city's viewport.
|
"""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
|
return folium.Map(
|
||||||
self.clear_map()
|
location=[self.center_latitude, self.center_longitude],
|
||||||
|
zoom_start=self.initial_zoom,
|
||||||
return self._map
|
)
|
||||||
|
|
||||||
def draw_restaurants(
|
def draw_restaurants(
|
||||||
self,
|
self,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue