debugging argpars in python

2024/10/7 6:38:17

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?

Answer

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:] listargs = 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.

https://en.xdnf.cn/q/70274.html

Related Q&A

Non-blocking server in Twisted

I am building an application that needs to run a TCP server on a thread other than the main. When trying to run the following code:reactor.listenTCP(ServerConfiguration.tcpport, TcpCommandFactory()) re…

Reading changing file in Python 3 and Python 2

I was trying to read a changing file in Python, where a script can process newly appended lines. I have the script below which prints out the lines in a file and does not terminate.with open(tmp.txt,r)…

How to remove timestamps from celery pprint output?

When running the celery worker then each line of the output of the pprint is always prefixed by the timestamp and also is being stripped. This makes it quite unreadable:[2015-11-05 16:01:12,122: WARNIN…

How to get max() to return variable names instead of values in Python?

So I would like to get the maximum value from 3 variables, x,y,z.x = 1 y = 2 z = 3 max(x, y, z) # returns 3 but I want "z"However this returns the value of z i.e 3. How do I get the name of …

SymPy Imaginary Number

Im messing around with writing some SymPy code to handle symbolic expressions with imaginary numbers.To start out, I want to get it to take x and y as real numbers and find the solution where x=iy. So …

Django 1.11 404 Page while Debug=True

Without making things difficult, I just want to show a special 404 render with staticfiles.If you set DEBUG = False you can use in urls.pyhandler404 = app.views.handler404But it is without staticfiles.…

zip()-like built-in function filling unequal lengths from left with None value

Is there a built-in function that works like zip(), but fills the results so that the length of the resulting list is the length of the longest input and fills the list from the left with e.g. None?Th…

Collection comparison is reflexive, yet does not short circuit. Why?

In python, the built in collections compare elements with the explicit assumption that they are reflexive:In enforcing reflexivity of elements, the comparison of collections assumes that for a collecti…

Anisotropic diffusion 2d images [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.We don’t allow questi…

Export environment variables at runtime with airflow

I am currently converting workflows that were implemented in bash scripts before to Airflow DAGs. In the bash scripts, I was just exporting the variables at run time withexport HADOOP_CONF_DIR="/e…