Skip to content

Commit 16f8ca3

Browse files
committed
irc: use deque for "stack" of recent messages
Doesn't save any code lines, but does make the process of updating what has been said recently look a bit less voodoo-y. I very much prefer declaring how long the list of "messages" should be where it's initialized instead of pruning it later.
1 parent 6101e3a commit 16f8ca3

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

sopel/irc/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from __future__ import annotations
2626

2727
import abc
28+
from collections import deque
2829
from datetime import datetime
2930
import logging
3031
import os
@@ -798,7 +799,7 @@ def say(
798799
with self.sending:
799800
recipient_id = self.make_identifier(recipient)
800801
recipient_stack = self.stack.setdefault(recipient_id, {
801-
'messages': [],
802+
'messages': deque(maxlen=10),
802803
'flood_left': flood_burst_lines,
803804
})
804805

@@ -846,7 +847,7 @@ def say(
846847

847848
# Loop detection
848849
if antiloop_threshold > 0 and elapsed < antiloop_window:
849-
messages = [m[1] for m in recipient_stack['messages'][-10:]]
850+
messages = [m[1] for m in recipient_stack['messages']]
850851

851852
# If what we're about to send repeated at least N times
852853
# in the anti-looping window, replace it
@@ -858,11 +859,10 @@ def say(
858859

859860
self.backend.send_privmsg(recipient, text)
860861

861-
# update recipient meta-data
862+
# update recipient metadata
862863
flood_left = recipient_stack['flood_left'] - 1
863864
recipient_stack['flood_left'] = max(0, flood_left)
864865
recipient_stack['messages'].append((time.time(), safe(text)))
865-
recipient_stack['messages'] = recipient_stack['messages'][-10:]
866866

867867
# Now that we've sent the first part, we need to send the rest if
868868
# requested. Doing so recursively seems simpler than iteratively.

0 commit comments

Comments
 (0)