0

I have a function that's set up like so:

class CLI(object):

    def __init__(self, user, password, endpoint):
        self.token = auth.login(user, password)
        self.endpoint = endpoint

    def put(self):
        return

    def get(self, pk):
        value = get.endpoint(token=self.token, pk=pk)
        return value

    def update(self):
        return

    def delete(self):
        return

    def exit(self):
        sys.exit

I'm generating an argument that would provide a string for what the endpoint is however I'm curious how I can replace that in the below code as a variable:

def main():
    parser = argparse.ArgumentParser(description='Access enterPass API via Command Line')
    parser.add_argument('endpoint', type=str, help='What API endpoint to access')
    parser.add_argument('method', type=str, help='What method to call')
    args = parser.parse_args()

    endpoint = args.endpoint
    method = args.method

    value = CLI(user='myusername',
                password='mypassword,
                endpoint=endpoint).read(pk=1)

    print(value)


if __name__ == "__main__":
    main()

Instead of value = CLI(user=user, password=password, endpoint=endpoint.get(pk=1) I want value = CLI(user=user, password=password, endpoint=endpoint).method(pk=1). Likewise for where endpoint is called inside of the class.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
whoisearth
  • 4,080
  • 13
  • 62
  • 130
  • 1
    Neither your example code nor usage are valid syntactically, so it's not clear to me what your problem is or what you're expecting. Are you just looking for `getattr` (see e.g. https://stackoverflow.com/q/3061/3001761)? – jonrsharpe Sep 30 '18 at 22:28
  • @jonrsharpe thank you I actually found it right after I opened this. so ya it's a duplicate. – whoisearth Sep 30 '18 at 22:32
  • Possible duplicate of [Calling a function of a module by using its name (a string)](https://stackoverflow.com/questions/3061/calling-a-function-of-a-module-by-using-its-name-a-string) – whoisearth Sep 30 '18 at 22:32
  • 1
    Then I suggest you just delete this. – jonrsharpe Sep 30 '18 at 22:32

0 Answers0