Created
February 21, 2014 11:56
-
-
Save JobsDong/9133069 to your computer and use it in GitHub Desktop.
python 另类包装器 __call__的用法
This file contains 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
class api_route(object): | |
"""api的包装器 | |
Attributes: | |
_api_methods: 字典,key是路径名,value是函数对象 | |
""" | |
_api_methods = {} | |
def __init__(self, uri): | |
"""根据路径进行注册 | |
uri: 路径 | |
""" | |
self._uri = uri | |
def __call__(self, api_method): | |
self._api_methods[self._uri] = api_method | |
@classmethod | |
def get_api_routes(cls): | |
"""获取注册的所有函数 | |
不允许对返回的结果进行修改 | |
Returns: | |
routes: 字典,key是路径,value是函数对象 | |
""" | |
return cls._api_methods | |
@api_route(r"/api/dummy") | |
def api_dummy(params): | |
return result(200, "success", params) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment