0

I am new to python and I am exploring python I have many different functions in 1 file want to expose those functions to client. This is my app.py file

 import sys
 

def get_data(e_id, t_id):
#some code

def get_2_data(e_id, t_id)
#some code

def get_3_data(t_id)
#some code

if __name__ == '__main__':
    get_data(sys.argv[1], sys.argv[2])

Here I want to get specific function data. Currently I am running python app.py 1234 1. The function which is defined under main gets called. But I want get_3_data() data. How to call the particular function or someone wants to fetch get_2_data(). How to expose those functions. Any suggestion. I dont want any HTTP call or API. I want to call by method name.

CodeCool
  • 193
  • 2
  • 12
  • 1
    "I want to call by method name." Wha do you mean, exactly? You can `import` your module, and use those functions just fine. – juanpa.arrivillaga Mar 16 '21 at 11:33
  • you could add a third argument to specify which function you want to call, and then call each function inside an `if` condition, based on the argument passed – Muhammad Huzaifa Mar 16 '21 at 12:02
  • This question shouldn't have been closed. The mentioned answer is not the right answer. The OP want to call sub-functions/sub-commands from command line, and the clean and Pythonic way to do this is to use `argparse` with `ArgumentParser.add_subparsers`. – Rivers Mar 16 '21 at 12:31

1 Answers1

1

The clean way to do this is by using the module argparse.

Here is how to do this:

import argparse

def get_data(args):
    e_id = args.e_id
    t_id = args.t_id
    print(f'Function get_data. e_id: {e_id}. t_id: {t_id}')


def get_data_2(args):
    e_id = args.e_id
    t_id = args.t_id
    print(f'Function get_data_2. e_id: {e_id}. t_id: {t_id}')


def get_data_3(args):
    t_id = args.t_id
    print(f'Function get_data_3. t_id: {t_id}')


if __name__ == '__main__':

    # Create the arguments parser
    argparser = argparse.ArgumentParser()

    # Create the subparsers
    subparsers = argparser.add_subparsers()

    # Add a parser for the first function
    get_data_parser = subparsers.add_parser('get_data')
    # Set the function name
    get_data_parser.set_defaults(func=get_data)
    # Add its arguments
    get_data_parser.add_argument('e_id')
    get_data_parser.add_argument('t_id')
    

    # Add a parser for the second function
    get_data_2_parser = subparsers.add_parser('get_data_2')
    # Set the function name
    get_data_2_parser.set_defaults(func=get_data_2)
    # Add its arguments
    get_data_2_parser.add_argument('e_id')
    get_data_2_parser.add_argument('t_id')

    # Add a parser for the third function
    get_data_3_parser = subparsers.add_parser('get_data_3')
    # Set the function name
    get_data_3_parser.set_defaults(func=get_data_3)
    # Add its arguments
    get_data_3_parser.add_argument('t_id')

    # Get the arguments from the comand line
    args = argparser.parse_args()

    # Call the selected function
    args.func(args)

As showed in this example, you will have to change your functions a little bit:

  • They will take only one argument called args (created in the main function with args = argparser.parse_args())
  • And then, in each function, you will get the needed parameters by their names (the ones added with add_argument, like in get_data_3_parser.add_argument('t_id'). So, to get the argument called t_id, you will write t_id = args.t_id.

argparse documentation: https://docs.python.org/3/library/argparse.html

argparse tutorial: https://docs.python.org/3/howto/argparse.html

From the documentation:

Many programs split up their functionality into a number of sub-commands, for example, the svn program can invoke sub-commands like svn checkout, svn update, and svn commit. Splitting up functionality this way can be a particularly good idea when a program performs several different functions which require different kinds of command-line arguments. ArgumentParser supports the creation of such sub-commands with the add_subparsers() method.

Rivers
  • 1,783
  • 1
  • 8
  • 27