I have a script, main.py, that runs a few functions from other scripts in the directory. I had the very unoriginal idea to send myself Slack notifications if specific functions ran correctly or failed, i.e. error handling. How would I use an error handling function located in a separate script - call the file slack_notifications.py - in my main.py script? This linked question starts to get at the answer, but it doesn't get to the calling the error function from somewhere else.
main.py script:
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)
I have tried embedding the called scraper function into the scrape_status() function, like this: scraped_image = (slack_notifications.scrape_status(scraper_file.pic_scrape(scrape_object))) and a few other ways, but that doesn't really tell you if it ran successfully, right?
slack_notifications.py script:
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)
Is there a way to do this? I have been going off these instructions, too.
scrape_statusseems totally useless. It will runtry/excepttoo late - after error hapends - so it can't catch it. You would have to rather send function's name (without()) intoscrape_statusand use it with()to execute it insidetry/except- and then it can catch it. OR simply you have to runscraper_file.pic_scrape(scrape_object)insidetry/exceptand it would be simpler if you usetry/exceptdirectly insidescraper_file.pic_scrape(scrape_object)instead of creating universal functionscrape_Status()forthisinput()was moved from outsideerror_handlerinto insideerror_handlerand you would have to do the same withscraper_file.pic_scrape(scrape_object)- you have to move it intoscrape_statusbut you can't do this usingscraper_file.pic_scrape(scrape_object)because it runs it outsidescrape_status. All your idea is overcomplicated. Better put all code in one file and don't try to create universal functionscrape_status