From ed9c3ddaa276c49a77f58fb369c6ec19d7ffe2fe Mon Sep 17 00:00:00 2001 From: Alek Ratzloff Date: Sat, 4 Jun 2022 16:16:52 -0700 Subject: [PATCH] markov: Reply chance is loaded lazily Just like the actual chain data structure, this value is now loaded lazily, since it's stored in the filesystem. Signed-off-by: Alek Ratzloff --- plugins/markov.py | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/plugins/markov.py b/plugins/markov.py index dc3b6c0..8a84bfc 100644 --- a/plugins/markov.py +++ b/plugins/markov.py @@ -38,7 +38,7 @@ def windows(items: Sequence[Any], size: int): class Chain: def __init__(self, order: int, chance: float, path: Path): self.order = order - self.reply_chance = chance + self.__reply_chance = chance self.path = path self.__cache = chain_default() self.__last_access = 0.0 @@ -47,6 +47,19 @@ class Chain: def __touch(self): self.__last_access = asyncio.get_running_loop().time() + @property + def reply_chance(self) -> float: + self.__load() + return self.__reply_chance + + @reply_chance.setter + def reply_chance(self, val: float): + if not (isinstance(val, float) or isinstance(val, int)): + return NotImplemented + self.__load() + self.__reply_chance = val + self.__dirty = True + @property def last_access(self) -> float: return self.__last_access @@ -95,7 +108,7 @@ class Chain: obj = json.load(fp) # Load the save object - self.reply_chance = obj["reply_chance"] + self.__reply_chance = obj["reply_chance"] self.__cache = defaultdict( chain_inner_default, { @@ -119,7 +132,7 @@ class Chain: self.path.parent.mkdir(parents=True, exist_ok=True) # Build the save object obj = { - "reply_chance": self.reply_chance, + "reply_chance": self.__reply_chance, "chain": { key: { ("" if word is None else word): weight @@ -239,6 +252,13 @@ class Markov(Plugin): message = chain.generate() if message: self.send_to(conn, channel, f"{who.nick}: {message}") + case ["chance"]: + chain = self.get_chain(channel, who.nick) + self.send_to( + conn, + channel, + f"{who.nick}: current reply chance is {chain.reply_chance}", + ) case ["chance", chance]: chain = self.get_chain(channel, who.nick) try: