用于人性化异常界面的工具包。
prettyexc的Python项目详细描述
prettyex提供了通用的异常表示,以使人们可以轻松地读取异常。
您可以从pypi安装软件包
$ pip install prettyexc
示例
-
前奏曲:
>>> from prettyexc import PrettyException
- 始终放置和获取参数::
>>> class SimpleException(PrettyException): ... pass ... >>> e = SimpleException('any', 'plain', 'args', code=200, description='OK') >>> raise e Traceback (most recent call last): File "<stdin>", line 1, in <module> __main__.SimpleException: "any","plain","args",code=200,description="OK" SimpleException("any","plain","args",code=200,description="OK") >>> print [e, e] [<SimpleException("any","plain","args",code=200,description="OK")>, <SimpleException("any","plain","args",code=200,description="OK")>]
- 设置默认消息::
>>> class MessageException(PrettyException): ... message = u'You should select a user' ... >>> e = MessageException(user_id=10) >>> raise e Traceback (most recent call last): File "<stdin>", line 1, in <module> __main__.MessageException: You should select a user >>> print [e, e] [<MessageException(user_id=10)>, <MessageException(user_id=10)>]
- 设置消息格式化程序::
>>> class FormatException(PrettyException): ... message_format = u'User {user_id} has no permission.' ... >>> e = FormatException(user_id=10) >>> raise e Traceback (most recent call last): File "<stdin>", line 1, in <module> __main__.FormatException: User 10 has no permission. >>> print e.message User 10 has no permission.
dt>修补现有异常::>>> from prettyexc import patch >>> class AnException(Exception): pass ... >>> patch(AnException, PrettyException) >>> raise AnException(status=404) Traceback (most recent call last): File "<stdin>", line 1, in <module> __main__.AnException: status=404