#πŸ”’ Handling config/args in subsequent calls

30 messages Β· Page 1 of 1 (latest)

vital badge
#

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.

shut kindleBOT
#

@vital badge

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

vital badge
#

(For example, in flask you would jsut use current_app.args or something)

#

Let me know if this doesn't make sense

terse heart
#

don't use bare except:, use except Exception or specify which exception exactly you are expecting

#

!e ```py
try:
exit()
except:
print("bad")

print("still running after exit btw (and would also for KeyboardInterrupt)")

shut kindleBOT
#

@terse heart :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | bad
002 | still running after exit btw (and would also for KeyboardInterrupt)
vital badge
#

The pseudocode is pretty abstracted but thanks

terse heart
#

flask gets away with it because you don't have many other alternatives

vital badge
#

What's better?

terse heart
#

in FastAPI, you would preferably use dependency injection instead, which means your routes define which extra things they require as function parameters

terse heart
#

if you really, really don't want to pass it all the way there, an alternative could be using the Logging library and using a custom handler to send an email whenever you get an error or a critical message logged

vital badge
#

I'll pass it if that's the only way, but I have been pretty curious to see if there's a better way

#

It just feels super clunky to have 2-3 intermediate modules that don't need the email have it passed to them just for one function that might need it

terse heart
vital badge
#

Is that basically Threadlocal storage?

#

That sounds pretty like what I'm looking for. Wish there was more info on how this worked

Multiple calls to logging.getLogger('someLogger') return a reference to the same logger object. This is true not only within the same module, but also across modules as long as it is in the same Python interpreter process.

terse heart
#

This is not a generic solution for anything you would current_app.args for, this is just one way to do the email thing

vital badge
#

That's still good but of course I'm interested in a general solution as well πŸ™‚ Thanks

#

huh

#

I'm a big fool

terse heart
vital badge
#

fair

vital badge
#

So is using this horrible

config.py

# This module is used to store configuration
print("Config initiated")

main.py

import config

def main(_config):
    config.log_to = _config.log_to

MailSender.py

import config

class MailSender:
    def __init__(self, user, password):
        self.log_to = config.log_to
terse heart
#

imo yes

shut kindleBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.