diff options
author | Arjun Satarkar <me@arjunsatarkar.net> | 2024-03-13 00:59:40 +0000 |
---|---|---|
committer | Arjun Satarkar <me@arjunsatarkar.net> | 2024-03-13 00:59:40 +0000 |
commit | 4e1bda674d95b0fe3df028a9f429aa171ae63a68 (patch) | |
tree | d9b9df2a973fc6de4e27391b7f91613fdc64cef4 | |
parent | 228565b70ba3d1015ee85459c508f72ab363be0f (diff) | |
download | aps-cogs-4e1bda674d95b0fe3df028a9f429aa171ae63a68.tar aps-cogs-4e1bda674d95b0fe3df028a9f429aa171ae63a68.tar.gz aps-cogs-4e1bda674d95b0fe3df028a9f429aa171ae63a68.zip |
markov: improve exceptions
-rw-r--r-- | markov/errors.py | 40 | ||||
-rw-r--r-- | markov/markov.py | 39 |
2 files changed, 44 insertions, 35 deletions
diff --git a/markov/errors.py b/markov/errors.py index 1af7de8..875d3b7 100644 --- a/markov/errors.py +++ b/markov/errors.py @@ -1,14 +1,46 @@ class MarkovGenerationError(Exception): - pass + def __init__(self, guild_id: int, member_id: int | None): + self.guild_id = guild_id + self.member_id = member_id + + def __repr__(self): + return f"MarkovGenerationError(guild_id={self.guild_id}, member_id={self.member_id})" class NoTotalCompletionCountError(MarkovGenerationError): - pass + def __init__(self, guild_id: int, member_id: int | None, token: str): + super().__init__(guild_id, member_id) + self.token = token + + def __repr__(self): + return ( + "NoTotalCompletionCountError(guild_id={}, member_id={}, token={})".format( + self.guild_id, self.member_id, repr(self.token) + ) + ) class NoNextTokenError(MarkovGenerationError): - pass + def __init__(self, guild_id: int, member_id: int | None, token: str, offset: int): + super().__init__(guild_id, member_id) + self.token = token + self.offset = offset + + def __repr__(self): + return ( + "NoNextTokenError(guild_id={}, member_id={}, token={}, offset={})".format( + self.guild_id, self.member_id, repr(self.token), self.offset + ) + ) class InvalidCompletionCountError(MarkovGenerationError): - pass + def __init__(self, guild_id: int, member_id: int | None, token: str, offset: int): + super().__init__(guild_id, member_id) + self.token = token + self.offset = offset + + def __repr__(self): + return "InvalidCompletionCountError(guild_id={}, member_id={}, token={}, offset={})".format( + self.guild_id, self.member_id, repr(self.token), self.offset + ) diff --git a/markov/markov.py b/markov/markov.py index 19822a1..c7e980f 100644 --- a/markov/markov.py +++ b/markov/markov.py @@ -364,22 +364,23 @@ class Markov(commands.Cog): " if you are sure." ) return + guild_id_bytes = self.uint_to_bytes(ctx.guild.id) async with aiosqlite.connect(self.db_path) as db: await db.execute( "DELETE FROM guild_total_completion_count WHERE guild_id = ?;", - (self.uint_to_bytes(ctx.guild.id),), + (guild_id_bytes,), ) await db.execute( "DELETE FROM guild_pairs WHERE guild_id = ?;", - (self.uint_to_bytes(ctx.guild.id),), + (guild_id_bytes,), ) await db.execute( "DELETE FROM member_total_completion_count WHERE guild_id = ?;", - (self.uint_to_bytes(ctx.guild.id),), + (guild_id_bytes,), ) await db.execute( "DELETE FROM member_pairs WHERE guild_id = ?;", - (self.uint_to_bytes(ctx.guild.id),), + (guild_id_bytes,), ) await db.commit() await ctx.reply("All markov data for this guild has been deleted.") @@ -473,31 +474,14 @@ class Markov(commands.Cog): f"Error: no data for this {'member' if member else 'guild'} yet!" ) return - raise NoTotalCompletionCountError( - repr( - { - "guild_id": ctx.guild.id, - "member_id": member_id, - "token": token, - } - ) - ) + raise NoTotalCompletionCountError(ctx.guild.id, member_id, token) next_token = None for i in range(MAX_TOKEN_GENERATION_ITERATIONS): next_token, frequency = await get_possible_next_token( db, ctx.guild.id, member_id, token, i ) if next_token is None: - raise NoNextTokenError( - repr( - { - "guild_id": ctx.guild.id, - "member_id": member_id, - "token": token, - "offset": i, - } - ) - ) + raise NoNextTokenError(ctx.guild.id, member_id, token, i) if random.randint(1, completion_count) <= frequency: if next_token == "/": result = result[:-1] + next_token @@ -511,14 +495,7 @@ class Markov(commands.Cog): completion_count -= frequency if completion_count <= 0: raise InvalidCompletionCountError( - repr( - { - "guild_id": ctx.guild.id, - "member_id": member_id, - "token": token, - "offset": i, - } - ) + ctx.guild.id, member_id, token, i ) else: # If we went through MAX_TOKEN_GENERATION_ITERATIONS completions |