Skip to content

Commit b64b972

Browse files
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`
1 parent 2866624 commit b64b972

File tree

1 file changed

+24
-29
lines changed

1 file changed

+24
-29
lines changed

mmpy_bot/event_handler.py

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
log = logging.getLogger("mmpy.event_handler")
1414

1515

16-
class EventHandler(object):
16+
class EventHandler:
1717
def __init__(
1818
self,
1919
driver: Driver,
@@ -37,10 +37,8 @@ def start(self):
3737
def _should_ignore(self, message: Message):
3838
# Ignore message from senders specified in settings, and maybe from ourself
3939
return (
40-
True
41-
if message.sender_name.lower()
40+
message.sender_name.lower()
4241
in (name.lower() for name in self.settings.IGNORE_USERS)
43-
else False
4442
) or (self.ignore_own_messages and message.sender_name == self.driver.username)
4543

4644
async def _check_queue_loop(self, webhook_queue: queue.Queue):
@@ -75,37 +73,34 @@ async def _handle_post(self, post):
7573

7674
# Find all the listeners that match this message, and have their plugins handle
7775
# the rest.
78-
tasks = []
79-
for matcher, functions in self.plugin_manager.message_listeners.items():
80-
match = matcher.search(message.text)
81-
if match:
82-
groups = list([group for group in match.groups() if group != ""])
83-
for function in functions:
84-
# Create an asyncio task to handle this callback
85-
tasks.append(
86-
asyncio.create_task(
87-
function.plugin.call_function(
88-
function, message, groups=groups
89-
)
90-
)
91-
)
76+
tasks = [
77+
asyncio.create_task(
78+
function.plugin.call_function(
79+
function,
80+
message,
81+
groups=[group for group in match.groups() if group != ""],
82+
)
83+
)
84+
for matcher, functions in self.plugin_manager.message_listeners.items()
85+
if (match := matcher.search(message.text))
86+
for function in functions
87+
]
88+
9289
# Execute the callbacks in parallel
9390
asyncio.gather(*tasks)
9491

9592
async def _handle_webhook(self, event: WebHookEvent):
9693
# Find all the listeners that match this webhook id, and have their plugins
9794
# handle the rest.
98-
tasks = []
99-
for matcher, functions in self.plugin_manager.webhook_listeners.items():
100-
match = matcher.search(event.webhook_id)
101-
if match:
102-
for function in functions:
103-
# Create an asyncio task to handle this callback
104-
tasks.append(
105-
asyncio.create_task(
106-
function.plugin.call_function(function, event)
107-
)
108-
)
95+
96+
tasks = [
97+
# Create an asyncio task to handle this callback
98+
asyncio.create_task(function.plugin.call_function(function, event))
99+
for matcher, functions in self.plugin_manager.webhook_listeners.items()
100+
if matcher.search(event.webhook_id)
101+
for function in functions
102+
]
103+
109104
# If this webhook doesn't correspond to any listeners, signal the WebHookServer
110105
# to not wait for any response
111106
if len(tasks) == 0:

0 commit comments

Comments
 (0)