Skip to content

Commit

Permalink
optimization for loops and refac to py3 (#528)
Browse files Browse the repository at this point in the history
* 1. rewrite for loops on list comprehentions
2. rewrite python2 class on python3

* check correct reply on `is None`
  • Loading branch information
ArtemIsmagilov authored and attzonko committed Dec 11, 2024
1 parent cc4dffa commit 2dffdcd
Showing 1 changed file with 24 additions and 29 deletions.
53 changes: 24 additions & 29 deletions mmpy_bot/event_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
log = logging.getLogger("mmpy.event_handler")


class EventHandler(object):
class EventHandler:
def __init__(
self,
driver: Driver,
Expand All @@ -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):
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 2dffdcd

Please sign in to comment.