Skip to content

Instantly share code, notes, and snippets.

@pyrofolium
Created October 14, 2017 02:14
Show Gist options
  • Save pyrofolium/c3b6f829a5422255b8adfca327b89579 to your computer and use it in GitHub Desktop.
Save pyrofolium/c3b6f829a5422255b8adfca327b89579 to your computer and use it in GitHub Desktop.
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