Temporary queue to prevent duplication I am making a Reddit bot and to prevent my bot from posting to the same comment twice, instead of making a database, I decided I would rather create a temporary list (or "queue") that once appended to, checks to ensure the maximum length of the queue hasn't been exceeded. Once exceeded, however, the queue is cleared.

class TemporaryQueue:
    def __init__(self, max_items=10):
        self._queue = []
        self.max_items = max_items

    def __repr__(self):
        return "&lt;TemporaryQueue max_items={}&gt;".format(self.max_items)

    def __iter__(self):
        yield from self._queue

    def __eq__(self, other):
        return other in self._queue

    def _exceed_check(self):
        if len(self._queue) &gt; self.max_items:
            return True
        return False

    def append(self, value):
        if self._exceed_check():
            self._queue.clear()

        return self._queue.append(value)


Example usage:

for submission in reddit.subreddit("funny").stream.submissions():
    queue = TemporaryQueue()

    if submission.id not in queue:
        if "foo" in title:
            submission.reply("Bar!")
            queue.append(submission.id)
collections.deque

Why not use a collections.deque with a max_len?

from collections import deque

for submission in reddit.subreddit("funny").stream.submissions():
    queue = deque(max_len=10)

    if submission.id not in queue:
        if "foo" in title:
            submission.reply("Bar!")
            queue.append(submission.id)