Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

debugging argpars in python

May I know what is the best practice to debug an argpars function.

Say I have a py file test_file.py with the following lines

# Script start
import argparse
import os
parser = argparse.ArgumentParser()
parser.add_argument(“–output_dir”, type=str, default=”/data/xx”)
args = parser.parse_args()
os.makedirs(args.output_dir)
# Script stop

The above script can be executed from terminal by:

python test_file.py –output_dir data/xx

However, for debugging process, I would like to avoid using terminal. Thus the workaround would be

# other line were commented for debugging process
# Thus, active line are
# Script start
import os
args = {“output_dir”:”data/xx”}
os.makedirs(args.output_dir)
#Script stop

However, I am unable to execute the modified script. May I know what have I miss?

like image 491
rpb Avatar asked Oct 17 '22 17:10

rpb


1 Answers

When used as a script, parse_args will produce a Namespace object, which displays as:

argparse.Namespace(output_dir='data/xx')

then

args.output_dir

will be the value of that attribute

In the test you could do one several things:

args = parser.parse_args([....])  # a 'fake' sys.argv[1:] list

args = argparse.Namespace(output_dir= 'mydata')

and use args as before. Or simply call the

os.makedirs('data/xx')

I would recommend organizing the script as:

# Script start
import argparse
import os
# this parser definition could be in a function
parser = argparse.ArgumentParser()
parser.add_argument(“–output_dir”, type=str, default=”/data/xx”)

def main(args):
    os.makedirs(args.output_dir)

if __name__=='__main__':
    args = parser.parse_args()
    main(args)

That way the parse_args step isn't run when the file is imported. Whether you pass the args Namespace to main or pass values like args.output_dir, or a dictionary, etc. is your choice.

like image 198
hpaulj Avatar answered Oct 30 '22 16:10

hpaulj