From 04c0d05208e8181a834c194e3e4a5fe004ef4646 Mon Sep 17 00:00:00 2001 From: Alek Ratzloff Date: Fri, 3 Jun 2022 18:21:34 -0700 Subject: [PATCH] markov: More refined usage of Chain.__touch() This moves the self.__touch() call around in markov's Chain class such that it will only access truly available data. Signed-off-by: Alek Ratzloff --- plugins/markov.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/plugins/markov.py b/plugins/markov.py index a70ccea..24cb328 100644 --- a/plugins/markov.py +++ b/plugins/markov.py @@ -52,10 +52,10 @@ class Chain: return self.__last_access def add(self, text: str): - self.__touch() parts: List[Any] = text.strip().split() if not parts: return + self.__touch() self.__load() for fragment in windows(parts + [None], self.order + 1): head = fragment[0:-1] @@ -64,15 +64,16 @@ class Chain: self.__dirty = True def get(self, key: str) -> dict[str | None, int]: - self.__touch() if self.__cache: if key in self.__cache: + self.__touch() return self.__cache[key] else: raise KeyError(key) else: # Load cache, then return key self.__load() + self.__touch() return self.__cache[key] def set(self, key: str, value: Mapping[str | None, int]):