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 <alekratz@gmail.com>
This commit is contained in:
@@ -52,10 +52,10 @@ class Chain:
|
|||||||
return self.__last_access
|
return self.__last_access
|
||||||
|
|
||||||
def add(self, text: str):
|
def add(self, text: str):
|
||||||
self.__touch()
|
|
||||||
parts: List[Any] = text.strip().split()
|
parts: List[Any] = text.strip().split()
|
||||||
if not parts:
|
if not parts:
|
||||||
return
|
return
|
||||||
|
self.__touch()
|
||||||
self.__load()
|
self.__load()
|
||||||
for fragment in windows(parts + [None], self.order + 1):
|
for fragment in windows(parts + [None], self.order + 1):
|
||||||
head = fragment[0:-1]
|
head = fragment[0:-1]
|
||||||
@@ -64,15 +64,16 @@ class Chain:
|
|||||||
self.__dirty = True
|
self.__dirty = True
|
||||||
|
|
||||||
def get(self, key: str) -> dict[str | None, int]:
|
def get(self, key: str) -> dict[str | None, int]:
|
||||||
self.__touch()
|
|
||||||
if self.__cache:
|
if self.__cache:
|
||||||
if key in self.__cache:
|
if key in self.__cache:
|
||||||
|
self.__touch()
|
||||||
return self.__cache[key]
|
return self.__cache[key]
|
||||||
else:
|
else:
|
||||||
raise KeyError(key)
|
raise KeyError(key)
|
||||||
else:
|
else:
|
||||||
# Load cache, then return key
|
# Load cache, then return key
|
||||||
self.__load()
|
self.__load()
|
||||||
|
self.__touch()
|
||||||
return self.__cache[key]
|
return self.__cache[key]
|
||||||
|
|
||||||
def set(self, key: str, value: Mapping[str | None, int]):
|
def set(self, key: str, value: Mapping[str | None, int]):
|
||||||
|
|||||||
Reference in New Issue
Block a user