Skip to content

Instantly share code, notes, and snippets.

@prophile
Created August 9, 2014 12:11
Show Gist options
  • Select an option

  • Save prophile/2ce541b3740ac7883913 to your computer and use it in GitHub Desktop.

Select an option

Save prophile/2ce541b3740ac7883913 to your computer and use it in GitHub Desktop.
Sanity checks on Flask decorator
from __future__ import print_function
from flask import Flask
from functools import wraps
import json
import sys
app = Flask(__name__)
def example_wrapper(f):
print(f.func_dict)
@wraps(f)
def wrapped(*args, **kwargs):
return json.dumps(f(*args, **kwargs))
# Sanity check: make sure we're not already registered
for endpoint, func in app.view_functions.iteritems():
if func is f:
print("Warning: wrapping already-registered endpoint '{}'; "
"are the decorators in the right order? (@app.route should be"
" the first).".format(endpoint), file=sys.stderr)
break
return wrapped
@example_wrapper
@app.route('/')
def eyes():
return {'bees': 'faces'}
if __name__ == '__main__':
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment