Created
October 14, 2017 02:14
-
-
Save pyrofolium/c3b6f829a5422255b8adfca327b89579 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
# Given the function: | |
def hello(name): | |
print "hello %s!" % name | |
# Implement logic such that the function cannot be called more than 3 times per second. If that rate is exceeded, raise an exception. | |
for i in range(10): | |
hello('brian') | |
def rate_limiter(func): | |
last_time_called = None | |
def inner(*args, **kwargs): | |
current_time_stamp = time.time() | |
if last_time_called is None or current_time_stamp - last_time_called > (1.0/3.0): | |
last_time_called = current_time_stamp | |
return func(*args, **kwargs) | |
else: | |
raise Exception() | |
return inner |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment