不向上打印错误消息的Pythonic方法
我试图在调用库时抑制日志中的错误/警告。假设我有这个代码不向上打印错误消息的Pythonic方法,python,exception,warnings,Python,Exception,Warnings,我试图在调用库时抑制日志中的错误/警告。假设我有这个代码 try: kazoo_client.start() except: pass 这是在调用zookeeper客户端,该客户端会引发一些异常,现在我不希望在调用kazoo\u客户端时日志中出现警告/错误。start()在调用客户端时是否有方法抑制此异常如果您知道异常,请尝试: 如果不抑制,则在最后一次迭代时抛出StopIteration错误 >>&g
try:
kazoo_client.start()
except:
pass
这是在调用zookeeper客户端,该客户端会引发一些异常,现在我不希望在调用
kazoo\u客户端时日志中出现警告/错误。start()
在调用客户端时是否有方法抑制此异常如果您知道异常,请尝试:
如果不抑制,则在最后一次迭代时抛出StopIteration
错误
>>> x = (i for i in range(10))
>>> for i in range(11):
... print(next(x))
0
1
2
3
4
5
6
7
8
9
Traceback (most recent call last):
File "<ipython-input-10-562798e05ad5>", line 2, in <module>
print(next(x))
StopIteration
编辑:
要抑制所有异常,请执行以下操作:
with suppress(Exception):
kazoo_client.start()
假设python 2.7.17 尝试以下方法:
import sys, StringIO
def funky() :
"1" + 1 # This should raise an error
sys.stderr = StringIO.StringIO()
funky() # this should call the funky function
您的代码应该如下所示:
import sys, StringIO
# import kazoo somehere around here
sys.stderr = StringIO.StringIO()
kazoo_client.start()
最后是Python 3的例子:
import sys
from io import StringIO
# import kazoo somehere around here
sys.stderr = StringIO()
kazoo_client.start()
我想建议一种更通用的方法,它可以在一般情况下使用。 我给大家留下一个例子,说明如何创建一个忽略错误的装饰器
import functools
# Use the Decorator Design Pattern
def ignore_error_decorator(function_reference):
@functools.wraps(function_reference) # the decorator inherits the original function signature
def wrapper(*args):
try:
result = function_reference(*args) # execute the function
return result # If the function executed correctly, return
except Exception as e:
pass # Default ignore; You can also log the error or do what ever you want
return wrapper # Return the wrapper reference
if __name__ == '__main__':
# First alternative to use. Compose the decorator with another function
def my_first_function(a, b):
return a + b
rez_valid = ignore_error_decorator(my_first_function)(1, 3)
rez_invalid = ignore_error_decorator(my_first_function)(1, 'a')
print("Alternative_1 valid: {result}".format(result=rez_valid))
print("Alternative_1 invalid: {result}".format(result=rez_invalid)) # None is return by the exception bloc
# Second alternative. Decorating a function
@ignore_error_decorator
def my_second_function(a, b):
return a + b
rez_valid = my_second_function(1, 5)
rez_invalid = my_second_function(1, 'a')
print("Alternative_2 valid: {result}".format(result=rez_valid))
print("Alternative_2 invalid: {result}".format(result=rez_invalid)) # None is return by the exception bloc
回到你的问题上来,使用我的替代方案,你必须跑步
ignore_error_decorator(kazoo_client.start)()
阅读module@furas的文档我知道,这件事是我只想在
kazoo\u client.start()
上做一些魔术,而不是在任何其他地方我想我仍然可以看到打印出来的警告,我希望我们能通过SomeError
,因为任何例外都没有帮助。我是否遗漏了一些已编辑的内容,您可以检查吗?仍然没有被抑制:(您确定异常来自此行本身吗?您可以粘贴您得到的异常吗?是的,这很好!但现在我的实际问题已解决,但我仍然在由启动的线程中看到未处理的异常
import functools
# Use the Decorator Design Pattern
def ignore_error_decorator(function_reference):
@functools.wraps(function_reference) # the decorator inherits the original function signature
def wrapper(*args):
try:
result = function_reference(*args) # execute the function
return result # If the function executed correctly, return
except Exception as e:
pass # Default ignore; You can also log the error or do what ever you want
return wrapper # Return the wrapper reference
if __name__ == '__main__':
# First alternative to use. Compose the decorator with another function
def my_first_function(a, b):
return a + b
rez_valid = ignore_error_decorator(my_first_function)(1, 3)
rez_invalid = ignore_error_decorator(my_first_function)(1, 'a')
print("Alternative_1 valid: {result}".format(result=rez_valid))
print("Alternative_1 invalid: {result}".format(result=rez_invalid)) # None is return by the exception bloc
# Second alternative. Decorating a function
@ignore_error_decorator
def my_second_function(a, b):
return a + b
rez_valid = my_second_function(1, 5)
rez_invalid = my_second_function(1, 'a')
print("Alternative_2 valid: {result}".format(result=rez_valid))
print("Alternative_2 invalid: {result}".format(result=rez_invalid)) # None is return by the exception bloc
ignore_error_decorator(kazoo_client.start)()