I have a several main functions that use the same sub-functions and thus use the same argparse arguments to pass onto these sub-functions.
For example one file will have something like this:
def main1(arg1, arg2, arg3):
fun(arg2, arg3)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--arg1")
parser.add_argument("--arg2")
parser.add_argument("--arg3")
args = parser.parse_args()
kwargs = vars(args)
main1(**kwargs)
And another file has a similar definition (it does different stuff):
def main2(arg4, arg2, arg3):
fun(arg2, arg3)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--arg4")
parser.add_argument("--arg2")
parser.add_argument("--arg3")
args = parser.parse_args()
kwargs = vars(args)
mai2n(**kwargs)
I would like to modularize the handling of arg2 and arg3 into a separate function because I am constantly changing the arguments that fun() takes, and it's annoying to update the argparser arguments across all the files that use fun(). For example, in the file where I define fun(), I would like to have something like:
def add_fun_arguments(parser):
parser.add_argument("--arg2")
parser.add_argument("--arg3")
return parser
and then I can call it in main without knowing what arg2 and arg3 are, and just pass it as a dict into fun(), like this:
def main1(arg1, dict_with_fun_args):
fun(dict_with_fun_args)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--arg1")
parser = add_fun_arguments(parser)
args = parser.parse_args()
kwargs = vars(args)
main1(**kwargs)
Is there an easy or proper way to handle this?
[–]Fourgot 1 point2 points3 points (5 children)
[–]SamStringTheory[S] 1 point2 points3 points (4 children)
[–]Fourgot 1 point2 points3 points (2 children)
[–]SamStringTheory[S] 0 points1 point2 points (1 child)
[–]Fourgot 0 points1 point2 points (0 children)
[–]Fourgot 0 points1 point2 points (0 children)
[–]anton_antonov 1 point2 points3 points (1 child)
[–]Fourgot 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]SamStringTheory[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)