Created
August 9, 2014 12:11
-
-
Save prophile/2ce541b3740ac7883913 to your computer and use it in GitHub Desktop.
Sanity checks on Flask decorator
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
| 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