From b64b972e2031f3e07667a23de50962791d01326e Mon Sep 17 00:00:00 2001 From: ArtemIsmagilov <118372045+ArtemIsmagilov@users.noreply.github.com> Date: Thu, 5 Dec 2024 22:29:12 +0400 Subject: [PATCH] optimization for loops and refac to py3 (#528) * 1. rewrite for loops on list comprehentions 2. rewrite python2 class on python3 * check correct reply on `is None` --- mmpy_bot/event_handler.py | 53 ++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 29 deletions(-) diff --git a/mmpy_bot/event_handler.py b/mmpy_bot/event_handler.py index 7d70466a..e719932c 100644 --- a/mmpy_bot/event_handler.py +++ b/mmpy_bot/event_handler.py @@ -13,7 +13,7 @@ log = logging.getLogger("mmpy.event_handler") -class EventHandler(object): +class EventHandler: def __init__( self, driver: Driver, @@ -37,10 +37,8 @@ def start(self): def _should_ignore(self, message: Message): # Ignore message from senders specified in settings, and maybe from ourself return ( - True - if message.sender_name.lower() + message.sender_name.lower() in (name.lower() for name in self.settings.IGNORE_USERS) - else False ) or (self.ignore_own_messages and message.sender_name == self.driver.username) async def _check_queue_loop(self, webhook_queue: queue.Queue): @@ -75,37 +73,34 @@ async def _handle_post(self, post): # Find all the listeners that match this message, and have their plugins handle # the rest. - tasks = [] - for matcher, functions in self.plugin_manager.message_listeners.items(): - match = matcher.search(message.text) - if match: - groups = list([group for group in match.groups() if group != ""]) - for function in functions: - # Create an asyncio task to handle this callback - tasks.append( - asyncio.create_task( - function.plugin.call_function( - function, message, groups=groups - ) - ) - ) + tasks = [ + asyncio.create_task( + function.plugin.call_function( + function, + message, + groups=[group for group in match.groups() if group != ""], + ) + ) + for matcher, functions in self.plugin_manager.message_listeners.items() + if (match := matcher.search(message.text)) + for function in functions + ] + # Execute the callbacks in parallel asyncio.gather(*tasks) async def _handle_webhook(self, event: WebHookEvent): # Find all the listeners that match this webhook id, and have their plugins # handle the rest. - tasks = [] - for matcher, functions in self.plugin_manager.webhook_listeners.items(): - match = matcher.search(event.webhook_id) - if match: - for function in functions: - # Create an asyncio task to handle this callback - tasks.append( - asyncio.create_task( - function.plugin.call_function(function, event) - ) - ) + + tasks = [ + # Create an asyncio task to handle this callback + asyncio.create_task(function.plugin.call_function(function, event)) + for matcher, functions in self.plugin_manager.webhook_listeners.items() + if matcher.search(event.webhook_id) + for function in functions + ] + # If this webhook doesn't correspond to any listeners, signal the WebHookServer # to not wait for any response if len(tasks) == 0: