Python 类方法classmethod 示例
来源:互联网 发布:linux查看ip地址命令 编辑:程序博客网 时间:2024/06/05 20:35
#https://github.com/pavlov99/json-rpc/blob/master/jsonrpc/manager.pyimport jsonimport loggingfrom .utils import is_invalid_paramsfrom .exceptions import ( JSONRPCInvalidParams, JSONRPCInvalidRequest, JSONRPCInvalidRequestException, JSONRPCMethodNotFound, JSONRPCParseError, JSONRPCServerError, JSONRPCDispatchException,)from .jsonrpc1 import JSONRPC10Responsefrom .jsonrpc2 import ( JSONRPC20BatchRequest, JSONRPC20BatchResponse, JSONRPC20Response,)from .jsonrpc import JSONRPCRequestlogger = logging.getLogger(__name__)class JSONRPCResponseManager(object): """ JSON-RPC response manager. Method brings syntactic sugar into library. Given dispatcher it handles request (both single and batch) and handles errors. Request could be handled in parallel, it is server responsibility. :param str request_str: json string. Will be converted into JSONRPC20Request, JSONRPC20BatchRequest or JSONRPC10Request :param dict dispather: dict<function_name:function>. """ RESPONSE_CLASS_MAP = { "1.0": JSONRPC10Response, "2.0": JSONRPC20Response, } @classmethod def handle(cls, request_str, dispatcher): if isinstance(request_str, bytes): request_str = request_str.decode("utf-8") try: json.loads(request_str) except (TypeError, ValueError): return JSONRPC20Response(error=JSONRPCParseError()._data) try: request = JSONRPCRequest.from_json(request_str) except JSONRPCInvalidRequestException: return JSONRPC20Response(error=JSONRPCInvalidRequest()._data) return cls.handle_request(request, dispatcher) @classmethod def handle_request(cls, request, dispatcher): """ Handle request data. At this moment request has correct jsonrpc format. :param dict request: data parsed from request_str. :param jsonrpc.dispatcher.Dispatcher dispatcher: .. versionadded: 1.8.0 """ rs = request if isinstance(request, JSONRPC20BatchRequest) \ else [request] responses = [r for r in cls._get_responses(rs, dispatcher) if r is not None] # notifications if not responses: return if isinstance(request, JSONRPC20BatchRequest): return JSONRPC20BatchResponse(*responses) else: return responses[0] @classmethod def _get_responses(cls, requests, dispatcher): """ Response to each single JSON-RPC Request. :return iterator(JSONRPC20Response): .. versionadded: 1.9.0 TypeError inside the function is distinguished from Invalid Params. """ for request in requests: def response(**kwargs): return cls.RESPONSE_CLASS_MAP[request.JSONRPC_VERSION]( _id=request._id, **kwargs) try: method = dispatcher[request.method] except KeyError: output = response(error=JSONRPCMethodNotFound()._data) else: try: result = method(*request.args, **request.kwargs) except JSONRPCDispatchException as e: output = response(error=e.error._data) except Exception as e: data = { "type": e.__class__.__name__, "args": e.args, "message": str(e), } logger.exception("API Exception: {0}".format(data)) if isinstance(e, TypeError) and is_invalid_params( method, *request.args, **request.kwargs): output = response( error=JSONRPCInvalidParams(data=data)._data) else: output = response( error=JSONRPCServerError(data=data)._data) else: output = response(result=result) finally: if not request.is_notification: yield output
阅读全文
0 0
- Python 类方法classmethod 示例
- python classmethod类方法
- python-静态方法staticmethod、类方法classmethod、属性方法property
- python 静态方法staticmethod和类方法classmethod
- classmethod类方法
- python 类方法(classmethod)小实验代码
- python 类中@staticmethod,@classmethod和实例方法
- Python 实例方法、@staticmethod和@classmethod
- Python 实例方法、@staticmethod和@classmethod
- Python 实例方法、@staticmethod和@classmethod
- python的classmethod和staticmethod方法
- Python 实例方法、@staticmethod和@classmethod
- python classmethod
- Python @classmethod
- PYTHON @classmethod
- 静态方法和类方法, staticmethod & classmethod
- python之静态和类方法:staticmethod和classmethod内置函数
- Python中staticmethod方法和classmethod方法区别
- 如何阅读程式
- 硬件设计,建立这样的知识体系更易懂~
- BT宝塔Linux VPS/服务器Web管理面板安装和基本功能体验
- window 静态ip设置
- 网站收藏
- Python 类方法classmethod 示例
- PPT 风格设计 整齐 有感觉
- Java基础——定时任务使用实例
- MySQL--连接的语法结构
- 如何将ipython的历史记录导出到.py文件中?
- Mac 和 Windows 文件相互共享
- 《Spring 5 官方文档》15.使用JDBC实现数据访问
- (1)struct简介
- EXCEL自定义菜单+SQL数据库查询的综合应用