This thread is an extension from the previous that can be found here. Say, I have a code that serve two purpose:
- print a max number from a list of integer,
- make a new dir.
import argparse
import os
parser = argparse.ArgumentParser()
parser.add_argument('integers', metavar='N', type=int, nargs='+',
help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',
const=sum, default=sum,
help='sum the integers (default: find the max)')
parser.add_argument("--output_dir", type=str, default="data/xx")
def main(args):
os.makedirs(args.output_dir)
print args.accumulate(args.integers)
if __name__=='__main__':
args = parser.parse_args()
# Disable when run through terminal: For debugging process
# args = argparse.Namespace(integers = 1, output_dir= 'mydata_223ss32')
main(args)
These statement can be executed from terminal by
python test_file.py --output_dir data/xxxx 2 2 5 --sum
However, for debugging process, I want to skip the usage of terminal. The idea by hpaulj as can be found from here was simply modify the
if __name__=='__main__':
into
if __name__=='__main__':
# Disable when run through terminal: For debugging process
args = argparse.Namespace(output_dir= 'mydata')
main(args)
However, I also want to include a list of integer during the debugging process. Including both the list of integer and dir address as below output an error
args = argparse.Namespace(integers = "2 2 5", output_dir= 'mydata')
Namespacedirectly, make sure its contents match what theparserwould produce.integersistype=intandnargs='+', which will produce a list of integers, not a string, e.g.integers=[1,2,3]. I suggested this as a way of testing the rest of the code, not as a way of testing theparser`.