From 16f8ca3881fa0cb9a2cc734e632f5a37d1fd66fb Mon Sep 17 00:00:00 2001 From: dgw Date: Wed, 20 Jul 2022 14:33:23 -0400 Subject: [PATCH] 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. --- sopel/irc/__init__.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sopel/irc/__init__.py b/sopel/irc/__init__.py index fc9aca5e11..0951c74608 100644 --- a/sopel/irc/__init__.py +++ b/sopel/irc/__init__.py @@ -25,6 +25,7 @@ from __future__ import annotations import abc +from collections import deque from datetime import datetime import logging import os @@ -798,7 +799,7 @@ def say( with self.sending: recipient_id = self.make_identifier(recipient) recipient_stack = self.stack.setdefault(recipient_id, { - 'messages': [], + 'messages': deque(maxlen=10), 'flood_left': flood_burst_lines, }) @@ -846,7 +847,7 @@ def say( # Loop detection if antiloop_threshold > 0 and elapsed < antiloop_window: - messages = [m[1] for m in recipient_stack['messages'][-10:]] + messages = [m[1] for m in recipient_stack['messages']] # If what we're about to send repeated at least N times # in the anti-looping window, replace it @@ -858,11 +859,10 @@ def say( self.backend.send_privmsg(recipient, text) - # update recipient meta-data + # update recipient metadata flood_left = recipient_stack['flood_left'] - 1 recipient_stack['flood_left'] = max(0, flood_left) recipient_stack['messages'].append((time.time(), safe(text))) - recipient_stack['messages'] = recipient_stack['messages'][-10:] # Now that we've sent the first part, we need to send the rest if # requested. Doing so recursively seems simpler than iteratively.