Module that warns (a chatbot) about high rate of messages This is a module I use in chatbot type programs which issue responses to certain private messages from other users. I run incoming messages through this in order to limit the number of responses a user is able to provoke within a short amount of time.

guardian.py

import time

class Guardian:
    ''' Anti-flooding mechanism. Tracks how fast users are sending messages. '''

    def __init__(self, flood_limit):
        # Abstract number that represents how quickly you can flood
        self.FLOOD_LIMIT = flood_limit
        # "username": (int) goes down with time, goes up with every message
        self.dict = {}
        self.timer = time.time()

    def update_timer(self):
        ''' Count down the flood timer values for every logged user. '''
        now = time.time()

        time_elapsed = now - self.timer

        self.timer = now

        cooled_down_users = []

        # Check every logged user against the timer and prune cooled-down ones.
        for user in self.dict:
            self.dict[user] -= time_elapsed

            if self.dict[user] &lt; 0:
                cooled_down_users.append(user)

        for user in cooled_down_users:
            del self.dict[user]

    def is_flooding(self, user, limit=None):
        ''' Tallies a user and returns True if they are above the limit. '''
        if limit == None:
            limit = self.FLOOD_LIMIT

        self.update_timer()

        if user in self.dict:
            self.dict[user] += 2

            if self.dict[user] &gt; limit:
                return True
            else:
                return False

        else:
            self.dict[user] = 2
            return False


Usage looks something like this:

import guardian

anti_flood = guardian.Guardian()

... # Receive some message from the network, etc..

flood_warning = anti_flood.is_flooding(username, OPTIONAL_FLOOD_LIMIT_INTEGER)

if flood_warning:
    reply = "You are sending too many requests too quickly."
else:
    reply = "Here is the response you requested."

... # Send a message back, change something based on the request, and so on.


I have tried to make the module more or less general purpose, so that it could be used for other, similar tasks, such as kicking / banning users who flood a chat room. However, the way it is actively running through its internal dict of users on every message might cause issues in high traffic applications.

I can also imagine a potential issue if many different users conspire to flood the script with messages, therefore bypassing the individual limit.

I'm primarily looking for critique and guidance on readability, usability, generally anything pertaining to having my code read and used by other developers, but design, functionality, optimization, anything goes, of course.

My aim is to take a step toward becoming a useful programmer in the future, rather than just making stuff that never leaves the confines of my hard drive.
is_flooding is apparently triggered by the particular user's action. However, it calls update_timer, which affects much wider community. This seems wrong. I recommend keeping each user's activity along with the user's instance, and test only her, along the lines of

def on_message_received(user):
    user.update_tally()
    if user.flooding():
        user.warn_and_ignore_message()
    else:
        user.proceed_normally()

The (anti)-idiom

    if self.dict[user] &gt; limit:
        return True
    else:
        return False


is a long way to say

    return self.dict[user] &gt; limit

The significance of 2 in dictionary updates is very unclear.