i obtain different arguments from command lines:
def get_args():
parser = ArgumentParser(description='neural network project')
parser.add_argument('--epochs', type=int, default=150)
parser.add_argument('--decay_epoch', type=int, default=100)
parser.add_argument('--batch_size', type=int, default=1)
parser.add_argument('--lr', type=float, default=.0002)
parser.add_argument('--load_height', type=int, default=286)
parser.add_argument('--load_width', type=int, default=286)
args = parser.parse_args()
return args
I would like to save all the argument obtained from get_args in a dictionary (to save then in a file .json or in a .txt).
argparse.ArgumentParser.parse_args returns an argparse.Namespace object, which is trivial to turn into a dict. Straight from the documentation:
This class is deliberately simple, just an object subclass with a
readable string representation. If you prefer to have dict-like view
of the attributes, you can use the standard Python idiom, vars():
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo')
>>> args = parser.parse_args(['--foo', 'BAR'])
>>> vars(args)
{'foo': 'BAR'}
Create a dictionary and add them by parser keyword.
neural_dict = {}
neural_dict['EPOCHS'] = args.epochs
neural_dict['DECAY_EPOCH'] = args.decay_epoch
.
.
.
To add to dictionary using a for loop:
neural_dict = {}
parser = argparse.ArgumentParser(description='neural network project')
parser.add_argument('--epochs', type=int, default=150)
parser.add_argument('--decay_epoch', type=int, default=100)
parser.add_argument('--batch_size', type=int, default=1)
parser.add_argument('--lr', type=float, default=.0002)
parser.add_argument('--load_height', type=int, default=286)
parser.add_argument('--load_width', type=int, default=286)
args = parser.parse_args()
#chepner is right...use his method instead
neural_dict = vars(args)
Related
I am using a codebase that expects a large set of argument via command line using argparse library and I neet to call that code inside a loop and inject the arguments via dictionary and not via command line without changing that codebase, I call the code as follow:
parser = argparse.ArgumentParser('Training', parents=[get_args_parser()])
args = parser.parse_args()
main(args)
Where get_args_parser() is a large list of arguments and defaults such as :
def get_args_parser():
parser = argparse.ArgumentParser('Set transformer detector', add_help=False)
parser.add_argument('--lr', default=1e-4, type=float)
parser.add_argument('--lr_backbone', default=1e-5, type=float)
parser.add_argument('--batch_size', default=2, type=int)
parser.add_argument('--weight_decay', default=1e-4, type=float)
parser.add_argument('--epochs', default=300, type=int)
parser.add_argument('--lr_drop', default=200, type=int)
...
If i need to pass a dictionary , as arguments , like:
argdict = {'lr_drop':20,'batch_size':5}
How can I do it?
you should use like this:
import argparse
ap = argparse.ArgumentParser()
ap.add_argument("-d", "--dict", required=True, help="Your dict as string", default="{}")
args = vars(ap.parse_args())
argdict = eval(args["dict"])
print(argdict)
# or you cam print some dict specific var
print(argdict["name"]) #Jasar
the you can call your file like it:
python3 file.py -d '{"name":"Jasar"}'
using some clues by #Jaser and #chepner , what i did is as follow:
args_to_argdict = {'a':1 , 'b':49 ,'c': 'text' }
parser = argparse.ArgumentParser(parents=[get_args_parser()])
args = parser.parse_args()
arg_dict = vars(args)
for key,value in args_to_argdict.items():
arg_dict[key]= value
so that the args value change , then i run the main :
main(args)
with the modified args .
I have an issue with argparse. I pass different values from prompt with argparse and I want to use them with different functions. How should I arrange my code?
So far the issue is that when the method split_dataset is called the program stacks
def split_dataset(destpath):
start = t.clock()
load_atomrefs(os.path.join(destpath, 'atomref.npz'))
load_data(os.path.join(destpath, 'qm9.db'))
total_time = (t.clock()-start)
print(f"Download of DataSet Completed in {total_time}s")
def params_initialization(train_batch,test_batch,lr,**kwargs):
parameters = {'train_batch' : train_batch, 'test_batch' : test_batch, 'lr' : lr }
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.set_defaults(method = split_dataset)
parser.add_argument('--destpath', help='Path to QM9 directory')
parser.add_argument('--train_batch', type=int, help='Batch size for training', default=32)
parser.add_argument('--test_batch', type=int, help='Batch size for testing',default=32)
parser.add_argument('--lr', type=float, help='Learning rate',
default=1e-3)
parser.add_argument('--ntrain', help='Number of training examples',
type=int, default=-1)
parser.add_argument('--nval', help='Number of validation examples',
type=int, default=-1)
args = parser.parse_args()
if not os.path.exists(args.destpath):
os.makedirs(args.destpath)
args.method(**vars(args))
Use parser.add_argument_group and add call the group arguments in another function
def function_to_call_group(parser_group):
parser_group.add_argument(--argument)
if __name__ == '__main__':
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
group1 = parser.add_argument_group("Group1")
function_to_call_group(group1)
# add other arguments to parser
args = parser.parse_args()
When module optparse is used, then I can get all default values for all command line arguments like this:
import optparse
if __name__ == '__main__':
parser = optparse.OptionParser(usage='pokus --help')
parser.add_option("-d", "--debug", action='store_true', dest="debug",
default=False, help='Enabling debugging.')
options, args = parser.parse_args()
print(parser.defaults)
Since optparse is deprecated it is wise to rewrite your code to use argparse module. However I can't find any way how to get all default values of all command line arguments added to parser object:
import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser(usage='pokus --help')
parser.add_argument("-d", "--debug", action='store_true', dest='debug',
default=False, help='Enabling debugging.')
args = parser.parse_args()
# <---- How to get default values for all arguments here?
# Not: vars(args)
I want to get all default values when I run program with (./app.py -d) or without any command line argument (./app.py).
I found solution:
import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser(usage='pokus --help')
parser.add_argument("-d", "--debug", action='store_true', dest='debug',
default=False, help='Enabling debugging.')
parser.add_argument("-e", "--example", action='store', dest='example',
default="", help='Example of argument.')
# Arguments from command line and default values
args = vars(parser.parse_args())
# Only default values
defaults = vars(parser.parse_args([]))
Then you can compare args and defaults values and distinguish between default values and values from command line.
If you do not want to parse an empty input string, you can use the method get_default in the parser object:
import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser(usage='pokus --help')
parser.add_argument("-d", "--debug", action='store_true', dest='debug',
default=False, help='Enabling debugging.')
args = parser.parse_args()
# To get a single default:
d_default = parser.get_default('d')
# To get all defaults:
all_defaults = {}
for key in vars(args):
all_defaults[key] = parser.get_default(key)
# Edit: Adding an alternative one-liner (using dict comprehension):
all_defaults = {key: parser.get_default(key) for key in vars(args)}
Somewhat late to the party, but this is a function (with bonus unittest) that I've used in a couple of cases to get hold of the default arguments without having to parse first (parsing first can be annoying if you have required arguments that aren't available yet)
def get_argparse_defaults(parser):
defaults = {}
for action in parser._actions:
if not action.required and action.dest != "help":
defaults[action.dest] = action.default
return defaults
def get_argparse_required(parser):
required = []
for action in parser._actions:
if action.required:
required.append(action.dest)
return required
parser = argparse.ArgumentParser()
optional_defaults_dict = get_argparse_defaults(parser)
required_list = get_argparse_required(parser)
class TestDefaultArgs(unittest.TestCase):
def test_get_args(self):
parser = argparse.ArgumentParser()
parser.add_argument('positional_arg')
parser.add_argument('--required_option', required=True)
parser.add_argument('--optional_with_default', required=False, default="default_value")
parser.add_argument('--optional_without_default', required=False)
required_args = get_argparse_required(parser)
self.assertEqual(['positional_arg', 'required_option'], required_args)
default_args = get_argparse_defaults(parser)
self.assertEqual({'optional_with_default': 'default_value',
'optional_without_default': None},
default_args)
For your information, here's the code, at the start of parsing that initializes the defaults:
def parse_known_args(...):
....
# add any action defaults that aren't present
for action in self._actions:
if action.dest is not SUPPRESS:
if not hasattr(namespace, action.dest):
if action.default is not SUPPRESS:
setattr(namespace, action.dest, action.default)
# add any parser defaults that aren't present
for dest in self._defaults:
if not hasattr(namespace, dest):
setattr(namespace, dest, self._defaults[dest])
...
So it loops through the parser._actions list, collecting the action.default attribute. (An action is a Action class object that was created by the parser.add_argument method.). It also checks self._defaults. This is the dictionary modified by a parse.set_defaults method. That can be used to set defaults that aren't linked directly to an action.
After parsing the command line, default strings in the namespace may be evaluated (with the action.type), turning, for example a default='1' into an integer 1.
Handling of defaults in argparse isn't trivial. Your parse_args([]) probably is simplest, provided the parser is ok with that (i.e. doesn't have any required arguments).
I don't know now optparse sets the defaults attribute. There is a non-trival method, optparse.OptionParser.get_default_values.
For the above example:
import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser(usage='pokus --help')
parser.add_argument("-d", "--debug", action='store_true', dest='debug',
default=False, help='Enabling debugging.')
A. To get all the values with their defaults in a tuple format:
In[1]: args = parser.parse_known_args()[0]
In[2]: args._get_kwargs()
Out[1]: [('debug', False)]
to access to each item:
In[3]: args.debug
Out[2]: False
B. To get the values and their default as dictionary format
In[4]: dict_args = parser.parse_known_args()[0].__dict__
In[5]: dict_args
Out[3]: {'debug': False}
And to access each key:
In[6]: dict_args['debug']
Out[4]: False
Or print them iteratively:
In[7]: for key in dict_args:
... print('value for %s is: %s'% (key, dict_args[key]))
Out[5]: value for debug is: False
I'm writing a Python script to process a machine-readable file and output a human-readable report on the data contained within.
I would like to give the option of outputting the data to stdout (-s) (by default) or to a txt (-t) or csv (-c) file. I would like to have a switch for the default behaviour, as many commands do.
In terms of Usage:, I'd like to see something like script [-s | -c | -t] input file, and have -s be the default if no arguments are passed.
I currently have (for the relevant args, in brief):
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
group.add_argument('-s', '--stdout', action='store_true')
group.add_argument('-c', '--csv', action='store_true')
group.add_argument('-t', '--txt', action='store_true')
args = parser.parse_args()
if not any((args.stdout, args.csv, args.txt)):
args.stdout = True
So if none of -s, -t, or -c are set, stdout (-s) is forced to True, exactly as if -s had been passed.
Is there a better way to achieve this? Or would another approach entirely be generally considered 'better' for some reason?
Note: I'm using Python 3.5.1/2 and I'm not worried about compatibility with other versions, as there is no plan to share this script with others at this point. It's simply to make my life easier.
You could have each of your actions update the same variable, supplying stdout as the default value for that variable.
Consider this program:
import argparse
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
group.add_argument(
'-s', '--stdout', action='store_const', dest='type', const='s', default='s')
group.add_argument(
'-c', '--csv', action='store_const', dest='type', const='c')
group.add_argument(
'-t', '--txt', action='store_const', dest='type', const='t')
args = parser.parse_args()
print args
Your code could look like:
if args.type == 's':
ofile = sys.stdout
elif args.type == 'c':
ofile = ...
...
First alternative:
Rather than arbitrarily choose one of the .add_argument()s to specify the default type, you can use parser.set_defaults() to specify the default type.
import argparse
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
group.add_argument('-s', '--stdout', action='store_const', dest='type', const='s')
group.add_argument('-c', '--csv', action='store_const', dest='type', const='c')
group.add_argument('-t', '--txt', action='store_const', dest='type', const='t')
parser.set_defaults(type='s')
args = parser.parse_args()
print args
Second alternative:
Rather than specify the type as an enumerated value, you could store a callable into the type, and then invoke the callable:
import argparse
def do_stdout():
# do everything that is required to support stdout
print("stdout!")
return
def do_csv():
# do everything that is required to support CSV file
print("csv!")
return
def do_text():
# do everything that is required to support TXT file
print("text!")
return
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
group.add_argument('-s', '--stdout', action='store_const', dest='type', const=do_stdout)
group.add_argument('-c', '--csv', action='store_const', dest='type', const=do_csv)
group.add_argument('-t', '--txt', action='store_const', dest='type', const=do_text)
parser.set_defaults(type=do_stdout)
args = parser.parse_args()
print args
args.type()
You can "cheat" with sys.argv :
import sys
def main():
if len(sys.argv) == 2 and sys.argv[1] not in ['-s', '-c', '-t', '-h']:
filename = sys.argv[1]
print "mode : stdout", filename
else:
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
group.add_argument('-s', '--stdout')
group.add_argument('-c', '--csv')
group.add_argument('-t', '--txt')
args = parser.parse_args()
if args.stdout:
print "mode stdout :", args.stdout
if args.csv:
print "mode csv :", args.csv
if args.txt:
print "mode txt :", args.txt
if __name__ == "__main__":
main()
From the OPTPARSE library reference:
option_list = [
make_option("-f", "--filename",
action="store", type="string", dest="filename"),
make_option("-q", "--quiet",
action="store_false", dest="verbose"),
]
parser = OptionParser(option_list=option_list)
Like the above example, I want to make a option list using make_option and pass it to a decorator which instantiates the parser and adds the arguments.
How can this be achieved in argparse? Is there a way to populate the parser other than parse_args()?
You can give argparse.ArgumentParser a list of parent parsers:
parent = argparse.ArgumentParser(add_help=False)
parent.add_argument('-f','--filename')
parent.add_argument('-q','--quiet',action='store_false',dest='verbose')
parser = argparse.ArgumentParser(parents=[parent])
...
namespace = parser.parse_args()
def process_args():
parser = argparse.ArgumentParser()
parser.add_argument('-f', '--filename', dest='filename', type=string, action='store')
args = parser.parse_args()
return args
if __name__ == '__main__':
args = process_args()