Python argparse in a separate function inside the class and calling args from init

I have a question about passing args to variables inside init

Here is my working version of the code.

class A: def __init__(self): self.id = args.id self.pw = args.pw self.endpoint = args.endpoint def B: ..do something.. if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument('-i', '--id', type=str, help = 'username') parser.add_argument('-p', '--pw', type=str, help ='password') parser.add_argument('-e', '--end_point', type=str , help='end point of api') args = parser.parse_args() 

The above code works, but now I'm trying to put all argparse codes inside a function inside class A and assign arg init. I looked on the Internet and I did not find a good solution.

 class A: def __init__(self): self.id = self.parse_args(id) self.pw = self.parse_args(pw) self.endpoint = self.parse_args(endpoint) def B: ..do something.. def parse_args(self,args): parser = argparse.ArgumentParser() parser.add_argument('-i', '--id', type=str, help = 'username') parser.add_argument('-p', '--pw', type=str, help ='password') parser.add_argument('-e', '--end_point', type=str , help='end point of api') return parser.parse_args(args) 

Good, so to be precise, I’m not sure how to approach this problem.

In the above example, I just called args.variable to work args, but in this case I call self.id = self.parse_args.id?

The parse_args function returns args, and I also tried self.id = self.parse_args (id), and this gives me

  TypeError: 'builtin_function_or_method' object is not iterable 

Part of the reason I want to separate args from a separate function is to simplify my unit test with argparse.

Any help would be appreciated.

+5
source share
1 answer

So, in the first case, you should do

 if __name__ ... .... args = parser.parse_args() a = A() 

A.__init__ can see args because it is global.

I do not understand why you want to make part of the argparse A code; you don’t want it to run every time you use A() , right? You can make only one set of values.

I think it will be a test to make parse_args code a method that can be run as desired after the class is created.

Here is an approach that, I think, has pretty good flexibility:

 import argparse class A: def __init__(self, id=None, pw=None, endpoint=None): self.id = id self.pw = pw self.endpoint = endpoint def parse_args(self, argv=None): parser = argparse.ArgumentParser() parser.add_argument('-i', '--id', type=str, help = 'username') parser.add_argument('-p', '--pw', type=str, help ='password') parser.add_argument('-e', '--end_point', type=str , help='end point of api') args = parser.parse_args(argv) self.id = args.id self.pw = args.pw self.endpoint = args.end_point def __str__(self): return 'A(%s,...)'%self.id if __name__ == "__main__": a = A() print(a) a.parse_args() print(a) b = A(id='you') print(b) b.parse_args(['--id','me']) print(b) 

Values ​​can be specified when creating an object, from the command line or from custom argv

 1610:~/mypy$ python stack39967787.py --id joe A(None,...) A(joe,...) A(you,...) A(me,...) 

===================

My first method (temporarily removed)

 class A(): def __init__(self): args = self.parse_args() self.a = args.a etc @static_method def parse_args(self): parser = .... return parser.parse_args() 

=======================

Your class A can be used as a Namespace , allowing parse_args directly update attributes (it uses setattr .

 import argparse class A: def __init__(self, id=None, pw=None, endpoint=None): self.id = id self.pw = pw self.endpoint = endpoint if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument('-i', '--id', type=str, help = 'username') parser.add_argument('-p', '--pw', type=str, help ='password') parser.add_argument('-e', '--endpoint', type=str , help='end point of api') args = parser.parse_args() print(args) a = A() parser.parse_args(namespace=a) print(vars(a)) 

production:

 1719:~/mypy$ python stack39967787_1.py --id joe --pw=xxxx -e1 Namespace(endpoint='1', id='joe', pw='xxxx') {'endpoint': '1', 'id': 'joe', 'pw': 'xxxx'} 
+9
source

Source: https://habr.com/ru/post/1257994/


All Articles