Ricky Robinson
Ricky Robinson

Reputation: 22893

Python: "importing" input arguments

I have two python scripts A and B, both with a main and with different input arguments (I use argparse). I launch them normally with:

$ python A.py --paramA1 valA1 --paramA2 valA2
$ python B.py --paramB1 valB1 --paramB2 valB2

I'd like to be able to call B from within A and to specify B's parameters when launching A. In short, I want to call A with A's parameters and B's parameters. Here's what it should look like from the outside:

$ python A.py --paramA1 valA1 --paramA2 valA2 --paramB1 valB1 --paramB2 valB2

Is there a more elegant way to do this than copying and pasting B's argparse code into A and then calling B on them?

EDIT: To simplify things, here's some example code:

A.py:

import argparse
def main():
    parser = argparse.ArgumentParser(description="Args for A.py")
    parser.add_argument("--param1A", type=int)
    parser.add_argument("--param2A", type=int)
    args = parser.parse_args()
    valA1 = args.param1A
    valA2 = args.param2A
    ...
    return 0

B.py:

import argparse
def main():
    parser = argparse.ArgumentParser(description="Args for B.py")
    parser.add_argument("--param1B", type=int)
    parser.add_argument("--param2B", type=int)
    args = parser.parse_args()
    valA1 = args.param1B
    valA2 = args.param2B
    ...
    return 0

What exactly would the suggested C.py, which includes arguments for both A.py and B.py look like? And what would A.py and B.py look like then?

EDIT 2: I forgot to mention that one of the parameters of B.py has to be created in A.py, so the order of execution is A then B, and A has to be able to pass a parameter to B.

Upvotes: 1

Views: 568

Answers (2)

Ioan Alexandru Cucu
Ioan Alexandru Cucu

Reputation: 12269

You can create a module C.py which contains the common argparse logic and is imported by both A and B.

When calling B from A, you could also pass B the already parsed args. I mean, both A and B should have something like a main function which takes the already parsed args, therefore not having to do the parsing twice.

C.py

import argparse

def create_parser():
    parser = argparse.ArgumentParser(description="Args for B.py")
    parser.add_argument("--param1B", type=int)
    parser.add_argument("--param2B", type=int)
    return parser

B.py

import argparse
import sys
import C

def main(args):
    valB1 = args.param1B
    valB2 = args.param2B
    # do stuff
    return 0

if __name__ == '__main__':
    parser = C.create_parser()
    args = parser.parse_args()
    sys.exit(main(args))

A.py

import argparse
import sys
import C
import B

def main(args):
    valA1 = args.param1A
    valA2 = args.param2A
    # do stuff
    return 0


if __name__ == '__main__':
    parser = C.create_parser()
    parser.add_argument("--param1A", type=int)
    parser.add_argument("--param2A", type=int)
    args = parser.parse_args()
    B.main(args)
    sys.exit(main(args))

Upvotes: 2

wagnerpeer
wagnerpeer

Reputation: 947

I think what @Ioan Alexandru Cucu described is something like the following:

import argparse

class A():
    __init__(self, param1A, param2A, param1B=None, param2B=None):
        if(param1B and param2B):
            myB = B(param1B, param2B)
        #Do processing here ...
        return 0


class B():
    __init__(self, param1B, param2B):
        #Do processing here ...
        return 0

if(__name__ == '__main__'):
    parser = argparse.ArgumentParser(description="Args for A and/or B")

    parser.add_argument("--param1A", type=int)
    parser.add_argument("--param2A", type=int)

    parser.add_argument("--param1B", type=int)
    parser.add_argument("--param2B", type=int)

    args = parser.parse_args()

    if(args.param1A and args.param2A and args.param1B and args.param2B):
        A(args.param1A, args.param2A, args.param1B, args.param2B)
    if(args.param1A and args.param2A):
        A(args.param1A, args.param2A)
    elif(args.param1B and args.param2B):
        B(args.param1B, args.param2B)

Instead of having multiple classes in one module you can have multiple modules with the different classes in them.

Upvotes: 1

Related Questions