From 40292b6e53d1c3b39f5d1d0a30ca07ff422c4615 Mon Sep 17 00:00:00 2001 From: Stuart Axon Date: Fri, 21 Feb 2020 13:51:27 +0000 Subject: [PATCH 1/2] Update decorators.py Add a reset_ratelimit method. This mostly useful during unit testing. Use case: I have a large amount of unit tests, a lot of them indirectly call my ratelimited function. I tried a lot of other things to get round this, but this seemed to work the best (in combination with faketime so things run instantly). --- ratelimit/decorators.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/ratelimit/decorators.py b/ratelimit/decorators.py index b290500..af714f1 100644 --- a/ratelimit/decorators.py +++ b/ratelimit/decorators.py @@ -83,6 +83,7 @@ def wrapper(*args, **kargs): return return func(*args, **kargs) + wrapper.reset_ratelimit = self.reset_ratelimit return wrapper def __period_remaining(self): @@ -94,6 +95,12 @@ def __period_remaining(self): ''' elapsed = self.clock() - self.last_reset return self.period - elapsed + + def reset_ratelimit(self): + ''' + Reset the ratelimit. + ''' + self.last_reset = self.clock() def sleep_and_retry(func): ''' From 0b51a2845cc6e479b84b548db2976468ef0ccb84 Mon Sep 17 00:00:00 2001 From: Stuart Axon Date: Fri, 21 Feb 2020 14:04:51 +0000 Subject: [PATCH 2/2] Update decorators.py reset_ratelimit should reset num_calls to zero. --- ratelimit/decorators.py | 1 + 1 file changed, 1 insertion(+) diff --git a/ratelimit/decorators.py b/ratelimit/decorators.py index af714f1..f96d26d 100644 --- a/ratelimit/decorators.py +++ b/ratelimit/decorators.py @@ -101,6 +101,7 @@ def reset_ratelimit(self): Reset the ratelimit. ''' self.last_reset = self.clock() + self.num_calls = 0 def sleep_and_retry(func): '''