Python> Run with run-time arguments> Use import argparse

Operating environment


Xeon E5-2620 v4 (8 cores) x 2
32GB RAM
CentOS 6.8 (64bit)
openmpi-1.8.x86_64 and its-devel
mpich.x86_64 3.1-5.el6 and its-devel
gcc version 4.4.7 (And gfortran)
NCAR Command Language Version 6.3.0
WRF v3.7.Use 1.
Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) 
Python 3.6.0 on virtualenv

I want to execute by specifying the runtime argument in the Python script being implemented.

Reference http://qiita.com/munkhbat1900/items/d7f9b11fb0965085964e Reference http://qiita.com/petitviolet/items/b8ed39dd6b0a0545dd36

v0.1 > AttributeError: 'Namespace' object has no attribute 't'

The following implementation was made with reference to the above two.

test_python_170323i.py


import argparse

parser = argparse.ArgumentParser(description = "do something")

parser.add_argument(
	'-t',
	'--timeIndex',
	type = int,
	help = 'time index for netCDF file',
	required = True)

cmd_args = parser.parse_args()

print(cmd_args.t)
$ python test_python_170323i.py -t 314
Traceback (most recent call last):
  File "test_python_170323i.py", line 14, in <module>
    print(cmd_args.t)
AttributeError: 'Namespace' object has no attribute 't'

There is no attribute called t.

v0.2> Success

test_python_170323i.py


import argparse

parser = argparse.ArgumentParser(description = "do something")

parser.add_argument(
	'-t',
	'--timeIndex',
	type = int,
	help = 'time index for netCDF file',
	required = True)

cmd_args = parser.parse_args()

print(cmd_args.timeIndex)

result


$ python test_python_170323i.py -t 314
314

When --timeIndex is specified, it seems that .timeIndex is used instead of .t when reading.

https://docs.python.jp/3/library/argparse.html It seems to be related.

v0.3> Specify attribute name

It seems that you can specify the attribute name by specifying dest.

https://docs.python.jp/3/library/argparse.html 16.4.1.2. Add arguments

The accumulate attribute is specified from the command line if --sum is specified

import argparse

parser = argparse.ArgumentParser(description = "do something")

parser.add_argument(
	'-t',
	'--timeIndex',
	dest='time_index',
	type=int,
	help='time index for netCDF file',
	required=True)

cmd_args = parser.parse_args()

print(cmd_args.time_index)

Remarks

$ python calc_latlon_avg_std_170323.py --timeIndex -20
total: 3477.89
avg_lat: 40.480
avg_lon: 116.246
std_lat: 0.137
std_lon: 0.103
$ python calc_latlon_avg_std_170323.py --timeIndex 4
total: 3477.89
avg_lat: 40.480
avg_lon: 116.246
std_lat: 0.137
std_lon: 0.103

-20 is accepted when timeIndex is specified. The value is 24, which is wraparound, and the result is the same as when 4 is specified. (The timeIndex is actually specified by [0..23]).

Recommended Posts

Python> Run with run-time arguments> Use import argparse
Run Python with VBA
[Python] Use JSON with Python
Run prepDE.py with python3
Use mecab with Python3
Use DynamoDB with Python
Run Blender with python
Use Python 3.8 with Anaconda
Use python with docker
Import tsv with Python
Run iperf with python
Run a Python file with relative import in PyCharm
Use Trello API with python
Run python with PyCharm (Windows)
Run Python with CloudFlash (arm926ej-s)
Let's run Excel with Python
Import vtk with brew python
Use Twitter API with Python
Use TUN / TAP with Python
How to use Python argparse
Run Label with tkinter [Python]
Use subsonic API with python3
Run DHT22 with RasPi + Python
[Python] I want to use the -h option with argparse
Use PointGrey camera with Python (PyCapture2)
Use vl53l0x with Raspberry Pi (python)
Working with LibreOffice in Python: import
Run Rotrics DexArm with python API
Run mruby with Python or Blender
[Python] Use Basic/Digest authentication with Flask
Use NAIF SPICE TOOLKIT with Python
(Note) Be careful with python argparse
Run XGBoost with Cloud Dataflow (Python)
Use rospy with virtualenv in Python3
Run Aprili from Python with Orange
Run python3 Django1.9 with mod_wsgi (deploy)
Use Python in pyenv with NeoVim
How to use FTP with Python
Use Windows 10 speech synthesis with Python
Use OpenCV with Python 3 in Window
Until you run python with apache
Use PostgreSQL with Lambda (Python + psycopg2)
Run servo with Python on ESP32 (Windows)
Use smbus with python3 under pyenv environment
Use DeepL with python (for dissertation translation)
Use Tabpy with Cloud Run (on GKE)
Use Amazon Simple Notification Service with Python
[Python] Use string data with scikit-learn SVM
[Introduction to Python] Let's use foreach with Python
Use PIL and Pillow with Cygwin Python
[Python] How to use import sys sys.argv
[Python] Display list elements with variadic arguments
Use cryptography library cryptography with Docker Python image
Use Application Insights with Python 3 (including bottles)
Run a Python web application with Docker
Use C ++ functions from python with pybind11
Python> datetime> runtime arguments> start processing immediately
Use selenium phantomjs webdriver with python unittest
Until you can use opencv with python
Use Python and MeCab with Azure Functions
Automatically register function arguments to argparse in Python