Skip to content

Instantly share code, notes, and snippets.

@renzon
Created May 28, 2014 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renzon/aaafb25b42ba07a9efc5 to your computer and use it in GitHub Desktop.
Save renzon/aaafb25b42ba07a9efc5 to your computer and use it in GitHub Desktop.
#decorator 1
from datetime import datetime
import functools
import logging
def query_checker(*keys_to_check):
""" Checks if a given key exists in the query string dictionary, if not, never executes the real handler and return status 400 """
def real_decorator(func):
@functools.wraps(func)
def wraper(handler):
def eval():
for k in keys_to_check:
if k not in handler.request.GET:
handler.response.status_int = 400
handler.response.write('Query param: %s must be filled for this request' % keys_to_check)
return False
return True
return func(handler) if eval() else None
return wraper
return real_decorator
#decorator 2
def log_time(func):
def wraper(handler):
start = datetime.now().replace(microsecond=0)
func(handler)
end = datetime.now().replace(microsecond=0)
logging.debug(" Time to run request in seconds: %s" % str((end-start).seconds))
return wraper
#decorated function:
@log_time
@query_checker('link')
def get(self):
#implementacao
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment