Skip to content

Commit

Permalink
Simplify syntax for @Rate() decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
calebj committed Apr 18, 2016
1 parent 91cd3ac commit fe11527
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions sopel/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,23 +248,21 @@ def add_attribute(function):
return add_attribute


def rate(user_rate=0, channel_rate=0, global_rate=0):
"""Decorate a function to limit how often it can be triggered. Without
arguments, the limit defaults to per-user. The global and channel parameters
are self-explanatory. A value of zero means no limit.
If a function is given a rate of 20, a single user may only use that
function once every 20 seconds. This limit applies to each user
individually. Users on the admin list in Sopel’s configuration are exempted
from rate limits.
def rate(user=0, channel=0, server=0):
"""Decorate a function to limit how often it can be triggered on a per-user
basis, in a channel, or across the server (bot). A value of zero means no
limit. If a function is given a rate of 20, a single user may only use that
function once every 20 seconds in the scope corresponding to the parameter.
Users on the admin list in Sopel’s configuration are exempted from rate
limits.
Rate-limited functions that use scheduled future commands should import
threading.Timer() instead of sched, or rate limiting will not work properly.
"""
def add_attribute(function):
function.rate = user_rate
function.channel_rate = channel_rate
function.global_rate = global_rate
function.rate = user
function.channel_rate = channel
function.global_rate = server
return function
return add_attribute

Expand Down

0 comments on commit fe11527

Please sign in to comment.