我有一个脚本main.py,它从目录中的其他脚本中运行几个函数。如果特定的函数正确运行或失败,即错误处理,我有一个非常不新颖的想法,即给自己发送Slack通知。在我的slack_notifications.py main.py脚本中,如何使用位于单独脚本中的错误处理函数--调用文件? 这有关联问题开始得到答案,但它无法从其他地方调用错误函数。
main.py脚本:
import scraper_file
import slack_notifications
# picture scrape variable
scrape_object = 'Barack Obama'
# scraper function called
scraped_image = scraper_file.pic_scrape(scrape_object)
# error handling?
slack_notifications.scrape_status(scrape_object)我尝试过将调用的刮板函数嵌入到scrape_status()函数中,比如:scraped_image = (slack_notifications.scrape_status(scraper_file.pic_scrape(scrape_object)))和其他一些方法,但这并不能真正告诉您它是否成功运行,对吗?
slack_notifications.py脚本:
testing = "dunderhead"
def scrape_status(x):
# if this function gets a positive return from the scrape function
# if this function gets any error from the scrape function
try:
x
print(x + ' worked!')
except:
print(x + ' failed!')
if __name__ == '__main__':
scrape_status(testing)有办法这样做吗?我也一直在离开这些指示。
发布于 2021-04-01 18:22:06
第一个链接中的代码可以工作,因为函数input()是从error_handler()外部移到内部error_handler()中的,并且它是在“`try/error_handler()”中执行的。
您的代码总是在错误处理程序scrape_status()之外运行函数,并且它只发送来自已执行函数的结果-因此它从未在try/except中运行它。
您必须分别发送函数的名称(没有()和参数)和单独的参数-然后它可以在try/except中运行
def scrape_status(function, *args):
try:
function(*args)
print(function.__name__ + ' worked!')
except:
print(function.__name__ + ' failed!')但这意味着您必须将函数发送为
scrape_status(scraper_file.pic_scrape, scrape_object) 而不是
scrape_status( scraper_file.pic_scrape(scrape_object) ) 这是不可读的,所以我不知道创建通用错误处理程序是否是个好主意。
最小工作实例
def scrape_status(function, *args, **kwargs):
try:
result = function(*args, **kwargs) # some functions may return result
print(function.__name__ + ' worked!')
return result
except:
print(function.__name__ + ' failed!')
# ----
# this function needs `arg1, arg2` to work correctly
def testing(arg1, arg2):
print(arg1, arg2)
# --- main ---
print('--- test 1 ---')
# send funciton's name `testing` and argument(s) `'hello', 'world'`
scrape_status(testing, 'hello', 'world') # OK
print('--- test 2 ---')
# send funciton's name `testing` without argument(s)
scrape_status(testing) # ERROR结果:
--- test 1 ---
hello world
testing worked!
--- test 2 ---
testing failed!编辑:
我根据问题一般装潢师除了在蟒蛇里尝试包装外?中的答案使用装饰器创建了新的示例。现在,代码使用了readableand,并且它只需要在定义函数时才使用修饰器@scrape_status('')。
@scrape_status('')
def testing(arg1, arg2):
print(arg1, arg2)最低工作代码:
def scrape_status():
def decorate(function):
def applicator(*args, **kwargs):
try:
result = function(*args, **kwargs)
print(function.__name__ + ' worked!')
return result
except:
print(function.__name__ + ' failed!')
return applicator
return decorate
# ----
# this function needs `arg1, arg2` to work correctly
@scrape_status()
def testing(arg1, arg2):
print(arg1, arg2)
# --- main ---
print('--- test 1 ---')
# send function's name `testing` and argument(s) `'hello', 'world'`
testing('hello', 'world') # OK
print('--- test 2 ---')
# send function's name `testing` without argument(s)
testing() # ERROR正如我所记得的,可能有一些模块带有更复杂的装饰师--例如。如果出现错误,一个装饰器可以重复3次功能。它可以用于在input()中重复问题,如果答案是错误的,或重复下载文件,如果有问题,下载它。
https://stackoverflow.com/questions/66908511
复制相似问题