I have a program that is currently configured via command line arguments that are parsed in main. However, I want to have a function that sends out an email to a configured email address in case some specific issues arise during execution.
What would be the best/most convenient/ most pythonic way of providing the configred email address to this notify function?
I know I could pass the args through the whole program, but this feels really cumbersome to add to every call, especially since the notify function might be added to some relatively low-level functions. I could also save the configs to a file and have the notify function read from that file? I'm pretty lost here as to what would be the best way.
main:
import argparse
import called_module
def main():
parser = argparse.ArgumentParser(description='Example')
parser.add_argument('--config', help='Path to config file')
args = parser.parse_args()
# Call the function from the called module and pass the parsed arguments
output = called_module.do_something_with_config(args)
if __name__ == '__main__':
main()
called_module.py
def do_something_with_config(arg):
data = read_file(arg_directory)
result = stuff(data)
return result
# maybe a different module
def stuff(data):
try:
get_metric(data)
except:
notifyer = Mailsender()
notifyer.notify("Please fix the data that is needed in do_something_with_config")
Now yes, I could simply pass args to Notifyer, but that means almost every function needs to have args passed. This seems wrong to me. Maybe I am overthinking it?
I just want Mailsender to have access to one of the arg options.