From bc06f8939a8611cce669de998eb9451f8e7152b7 Mon Sep 17 00:00:00 2001 From: Kumi Date: Fri, 17 May 2024 10:54:54 +0200 Subject: [PATCH] refactor: applying lots of linting This commit removes unnecessary imports across several modules, enhancing code readability and potentially improving performance. Notably, `KeysUploadError` and `requests` were removed where no longer used, reflecting a cleaner dependency structure. Furthermore, logging calls have been standardized, removing dynamic string generation in favor of static messages. This change not only makes the logs more consistent but also slightly reduces the computational overhead associated with log generation. The removal of unused type hints also contributes to a more focused and maintainable code base. Additionally, the commit includes minor text adjustments for user messages, replacing dynamic content with fixed strings where the dynamism was not needed. This enhances both the clarity and security of user-directed messages by avoiding unnecessary string formatting operations. Finally, the simplification of the migration script and the adjustment in the tools module underscore an ongoing effort to maintain clean and efficient code infrastructure. --- src/gptbot/callbacks/roommember.py | 2 +- src/gptbot/classes/trackingmore.py | 3 +-- src/gptbot/classes/wolframalpha.py | 2 +- src/gptbot/commands/calculate.py | 2 +- src/gptbot/commands/imagine.py | 2 +- src/gptbot/commands/newroom.py | 6 +++--- src/gptbot/commands/roomsettings.py | 2 +- src/gptbot/commands/tts.py | 2 +- src/gptbot/migrations/migration_2.py | 2 -- src/gptbot/tools/imagedescription.py | 2 +- 10 files changed, 11 insertions(+), 14 deletions(-) diff --git a/src/gptbot/callbacks/roommember.py b/src/gptbot/callbacks/roommember.py index 302bfe2..662de19 100644 --- a/src/gptbot/callbacks/roommember.py +++ b/src/gptbot/callbacks/roommember.py @@ -1,4 +1,4 @@ -from nio import RoomMemberEvent, MatrixRoom, KeysUploadError +from nio import RoomMemberEvent, MatrixRoom async def roommember_callback(room: MatrixRoom, event: RoomMemberEvent, bot): if event.membership == "leave": diff --git a/src/gptbot/classes/trackingmore.py b/src/gptbot/classes/trackingmore.py index 248f03c..10642fa 100644 --- a/src/gptbot/classes/trackingmore.py +++ b/src/gptbot/classes/trackingmore.py @@ -1,9 +1,8 @@ import trackingmore -import requests from .logging import Logger -from typing import Dict, List, Tuple, Generator, Optional +from typing import Tuple, Optional class TrackingMore: api_key: str diff --git a/src/gptbot/classes/wolframalpha.py b/src/gptbot/classes/wolframalpha.py index 46a84fe..2aea4ef 100644 --- a/src/gptbot/classes/wolframalpha.py +++ b/src/gptbot/classes/wolframalpha.py @@ -3,7 +3,7 @@ import requests from .logging import Logger -from typing import Dict, List, Tuple, Generator, Optional +from typing import Generator, Optional class WolframAlpha: api_key: str diff --git a/src/gptbot/commands/calculate.py b/src/gptbot/commands/calculate.py index d30c456..8ec6580 100644 --- a/src/gptbot/commands/calculate.py +++ b/src/gptbot/commands/calculate.py @@ -23,7 +23,7 @@ async def command_calculate(room: MatrixRoom, event: RoomMessageText, bot): bot.logger.log("Querying calculation API...") for subpod in bot.calculation_api.generate_calculation_response(prompt, text, results_only, user=room.room_id): - bot.logger.log(f"Sending subpod...") + bot.logger.log("Sending subpod...") if isinstance(subpod, bytes): await bot.send_image(room, subpod) else: diff --git a/src/gptbot/commands/imagine.py b/src/gptbot/commands/imagine.py index cc7d4e8..336f39e 100644 --- a/src/gptbot/commands/imagine.py +++ b/src/gptbot/commands/imagine.py @@ -16,7 +16,7 @@ async def command_imagine(room: MatrixRoom, event: RoomMessageText, bot): return for image in images: - bot.logger.log(f"Sending image...") + bot.logger.log("Sending image...") await bot.send_image(room, image) bot.log_api_usage(event, room, f"{bot.image_api.api_code}-{bot.image_api.image_model}", tokens_used) diff --git a/src/gptbot/commands/newroom.py b/src/gptbot/commands/newroom.py index 79ae6a8..84e0d78 100644 --- a/src/gptbot/commands/newroom.py +++ b/src/gptbot/commands/newroom.py @@ -13,7 +13,7 @@ async def command_newroom(room: MatrixRoom, event: RoomMessageText, bot): if isinstance(new_room, RoomCreateError): bot.logger.log(f"Failed to create room: {new_room.message}") - await bot.send_message(room, f"Sorry, I was unable to create a new room. Please try again later, or create a room manually.", True) + await bot.send_message(room, "Sorry, I was unable to create a new room. Please try again later, or create a room manually.", True) return bot.logger.log(f"Inviting {event.sender} to new room...") @@ -21,7 +21,7 @@ async def command_newroom(room: MatrixRoom, event: RoomMessageText, bot): if isinstance(invite, RoomInviteError): bot.logger.log(f"Failed to invite user: {invite.message}") - await bot.send_message(room, f"Sorry, I was unable to invite you to the new room. Please try again later, or create a room manually.", True) + await bot.send_message(room, "Sorry, I was unable to invite you to the new room. Please try again later, or create a room manually.", True) return with closing(bot.database.cursor()) as cursor: @@ -43,4 +43,4 @@ async def command_newroom(room: MatrixRoom, event: RoomMessageText, bot): await bot.matrix_client.joined_rooms() await bot.send_message(room, f"Alright, I've created a new room called '{room_name}' and invited you to it. You can find it at {new_room.room_id}", True) - await bot.send_message(bot.matrix_client.rooms[new_room.room_id], f"Welcome to the new room! What can I do for you?") + await bot.send_message(bot.matrix_client.rooms[new_room.room_id], "Welcome to the new room! What can I do for you?") diff --git a/src/gptbot/commands/roomsettings.py b/src/gptbot/commands/roomsettings.py index 42d5c63..9e7d253 100644 --- a/src/gptbot/commands/roomsettings.py +++ b/src/gptbot/commands/roomsettings.py @@ -114,7 +114,7 @@ async def command_roomsettings(room: MatrixRoom, event: RoomMessageText, bot): await bot.send_message(room, f"The current chat model is: '{value}'.", True) return - message = f"""The following settings are available: + message = """The following settings are available: - system_message [message]: Get or set the system message to be sent to the chat model - classification [true/false]: Get or set whether the room uses classification diff --git a/src/gptbot/commands/tts.py b/src/gptbot/commands/tts.py index d048a54..b1713f8 100644 --- a/src/gptbot/commands/tts.py +++ b/src/gptbot/commands/tts.py @@ -15,7 +15,7 @@ async def command_tts(room: MatrixRoom, event: RoomMessageText, bot): await bot.send_message(room, "Sorry, I couldn't generate an audio file. Please try again later.", True) return - bot.logger.log(f"Sending audio file...") + bot.logger.log("Sending audio file...") await bot.send_file(room, content, "audio.mp3", "audio/mpeg", "m.audio") return diff --git a/src/gptbot/migrations/migration_2.py b/src/gptbot/migrations/migration_2.py index 63011f6..7d602d8 100644 --- a/src/gptbot/migrations/migration_2.py +++ b/src/gptbot/migrations/migration_2.py @@ -1,7 +1,5 @@ # Migration for Matrix Store - No longer used -from datetime import datetime -from contextlib import closing def migration(conn): pass \ No newline at end of file diff --git a/src/gptbot/tools/imagedescription.py b/src/gptbot/tools/imagedescription.py index 2c83d98..ba21fb5 100644 --- a/src/gptbot/tools/imagedescription.py +++ b/src/gptbot/tools/imagedescription.py @@ -1,4 +1,4 @@ -from .base import BaseTool, Handover +from .base import BaseTool class Imagedescription(BaseTool): DESCRIPTION = "Describe the content of the images in the conversation."