Skip to content

Commit

Permalink
Move decorator out of class to "fix" issues on 3.8/3.9.
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePhar committed Nov 5, 2023
1 parent cb4fa7f commit 7aff89f
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions WebHostLib/tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,27 @@
ItemMetadata = Tuple[int, int, int]


# TODO: When py 3.8 and 3.9 support is dropped, can move this and _cache_results into TrackerData as @staticmethod.
_cache: Dict[str, Any]


def _cache_results(func: Callable[..., Any]):
"""Stores the results of any computationally expensive methods after the initial call.
If called again, returns the cached result instead, as results will not change for the lifetime of TrackerData.
"""

def method_wrapper(self, *args):
cache_key = f"{func.__name__}{''.join(f'_[{arg.__repr__()}]' for arg in args)}"
if cache_key in self._cache:
return self._cache[cache_key]

result = func(self, *args)
self._cache[cache_key] = result
return result

return method_wrapper


@dataclass
class TrackerData:
"""A helper dataclass that is instantiated each time an HTTP request comes in for tracker data.
Expand All @@ -29,7 +50,6 @@ class TrackerData:
room: Room
_multidata: Dict[str, Any]
_multisave: Dict[str, Any]
_cache: Dict[str, Any]

def __init__(self, room: Room):
"""Initialize a new RoomMultidata object for the current room."""
Expand All @@ -42,7 +62,6 @@ def __init__(self, room: Room):
self.location_name_to_id: Dict[str, Dict[str, int]] = {}

# Generate inverse lookup tables from data package, useful for trackers.
# TODO: Cache this per room and pull from database?
self.item_id_to_name: Dict[str, Dict[int, str]] = {}
self.location_id_to_name: Dict[str, Dict[int, str]] = {}
for game, game_package in self._multidata["datapackage"].items():
Expand All @@ -54,23 +73,6 @@ def __init__(self, room: Room):
self.item_name_to_id[game] = game_package["item_name_to_id"]
self.location_name_to_id[game] = game_package["item_name_to_id"]

@staticmethod
def _cache_results(func: Callable[..., Any]):
"""Stores the results of any computationally expensive methods after the initial call.
If called again, returns the cached result instead, as results will not change for the lifetime of TrackerData.
"""

def method_wrapper(self, *args):
cache_key = f"{func.__name__}{''.join(f'_[{arg.__repr__()}]' for arg in args)}"
if cache_key in self._cache:
return self._cache[cache_key]

result = func(self, *args)
self._cache[cache_key] = result
return result

return method_wrapper

def get_seed_name(self) -> str:
"""Retrieves the seed name."""
return self._multidata["seed_name"]
Expand Down

0 comments on commit 7aff89f

Please sign in to comment.