Skip to content

Commit

Permalink
remove lru_cache, use defaultdict
Browse files Browse the repository at this point in the history
  • Loading branch information
purarue committed Sep 9, 2023
1 parent a673956 commit ee419de
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions scramble_history/cstimer_scramble_type.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import NamedTuple, Optional
from functools import lru_cache
from collections import defaultdict


class CSTimerScramble(NamedTuple):
Expand Down Expand Up @@ -200,10 +200,17 @@ class CSTimerScramble(NamedTuple):
CSTimerScramble(scramble_code="eide", category="jokes", name=None),
]

# create a defaultdict which creates a list when a key is not found
SCRAMBLE_MAP = defaultdict(list)
for scramble in SCRAMBLES:
SCRAMBLE_MAP[scramble.scramble_code].append(scramble)


@lru_cache(maxsize=None)
def parse_scramble_type(code: str) -> CSTimerScramble:
for scr in SCRAMBLES:
if code == scr.scramble_code:
return scr
try:
resp = SCRAMBLE_MAP[code]
if resp:
return resp[0]
except KeyError:
pass
raise KeyError(f"Could not find matching scramble code {code}")

0 comments on commit ee419de

Please sign in to comment.