【问题标题】:How to use an error handling function on another function?如何在另一个函数上使用错误处理函数?
【发布时间】:2021-06-28 16:30:14
【问题描述】:

我有一个脚本main.py,它运行目录中其他脚本的一些函数。如果特定功能运行正确或失败(即错误处理),我有一个非常原始的想法,即向自己发送 Slack 通知。 我将如何使用位于单独脚本中的错误处理函数 - 在我的 main.py 脚本中调用文件 slack_notifications.py This linked 问题开始得到答案,但它没有不要从其他地方调用错误函数。

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)

有没有办法做到这一点?我也离开了these instructions

【问题讨论】:

  • 你的scrape_status 似乎完全没用。它将运行 try/except 太晚 - 在错误发生后 - 所以它无法捕获它。您必须将函数名称(不带 () )发送到 scrape_status 并与 () 一起使用以在 try/except 内执行它 - 然后它可以捕获它。或者只是你必须在try/except 内运行scraper_file.pic_scrape(scrape_object),如果你直接在scraper_file.pic_scrape(scrape_object) 内使用try/except 而不是为scrape_Status() 创建通用函数this 会更简单
  • yoiur 中的代码首先起作用,因为函数 input() 已从外部 error_handler 移动到内部 error_handler 并且您必须对 scraper_file.pic_scrape(scrape_object) 执行相同操作 - 您必须将其移动到 @ 987654348@ 但您不能使用scraper_file.pic_scrape(scrape_object) 执行此操作,因为它在scrape_status 之外运行。你所有的想法都过于复杂了。最好将所有代码放在一个文件中,不要尝试创建通用函数scrape_status

标签: python error-handling


【解决方案1】:

您的第一个链接中的代码有效,因为函数 input() 已从外部 error_handler() 移动到内部 error_handler() 并在 `try/except 内部执行。

您的代码始终在错误处理程序 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!

编辑:

我根据问题General decorator to wrap try except in python? 的答案使用装饰器创建了新示例。现在代码可读了,只有在定义函数时才需要使用装饰器@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()重复问题,或者如果下载文件有问题,则可以重复下载文件。

【讨论】:

  • 知道了——我同意,看起来确实毫无用处。下班后试试这个!
  • 也许在General decorator to wrap try except in python? 的答案中使用一些装饰器可能更具可读性。或者也许有这个模块
  • 我使用问题`General decorator to wrap try except in python?.`中的装饰器创建了代码,现在代码很好读。
猜你喜欢
  • 1970-01-01
  • 2019-04-02
  • 1970-01-01
  • 1970-01-01
  • 2020-11-25
  • 1970-01-01
  • 2017-03-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多