mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2025-06-28 17:55:12 +00:00
Move mixins and decorators to tfw module from tfw.components
This commit is contained in:
4
lib/tfw/decorators/__init__.py
Normal file
4
lib/tfw/decorators/__init__.py
Normal file
@ -0,0 +1,4 @@
|
||||
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
|
||||
# All Rights Reserved. See LICENSE file for details.
|
||||
|
||||
from .rate_limiter import RateLimiter
|
25
lib/tfw/decorators/rate_limiter.py
Normal file
25
lib/tfw/decorators/rate_limiter.py
Normal file
@ -0,0 +1,25 @@
|
||||
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
|
||||
# All Rights Reserved. See LICENSE file for details.
|
||||
|
||||
from functools import wraps
|
||||
from time import time, sleep
|
||||
|
||||
|
||||
class RateLimiter:
|
||||
def __init__(self, rate_per_second):
|
||||
self.min_interval = 1 / float(rate_per_second)
|
||||
self.last_call = time()
|
||||
|
||||
def __call__(self, fun):
|
||||
@wraps(fun)
|
||||
def wrapper(*args, **kwargs):
|
||||
self._limit_rate()
|
||||
fun(*args, **kwargs)
|
||||
return wrapper
|
||||
|
||||
def _limit_rate(self):
|
||||
since_last_call = time() - self.last_call
|
||||
to_next_call = self.min_interval - since_last_call
|
||||
self.last_call = time()
|
||||
if to_next_call > 0:
|
||||
sleep(to_next_call)
|
Reference in New Issue
Block a user