Different argparse arguments for each mode - python

main.py --mode a [--environment] [--db_server]
main.py --mode b [--environment] [--time] [--file] [--save]
main.py --mode c [--environment] [--render] [--file]
In each different modes, I need a different menu with some common arguments. The help must be specified for each different modes. Is possible to do this with argparse or I need a different module? It must first parse the mode and then decide what menu will choose from the existing 3 different menus (with some common arguments).
Thanks.

The solution is subparsers.
import argparse
if __name__ == "__main__":
# create the top-level parser
my_parser = argparse.ArgumentParser(
prog="PROG",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
# create sub-parser
sub_parsers = my_parser.add_subparsers(
title="Operating modes",
description="Select the operating mode",
dest="mode",
required=True,
)
# create the parser for the "agent" sub-command
parser_agent = sub_parsers.add_parser("agent", help="Agent mode")
parser_agent.add_argument(
"--db_server", type=str, help="DB server name", default="localhost"
)
parser_agent.add_argument(
"--update_interval",
type=int,
help="Interval of updating policy parameters",
default=64,
)
# create the parse for the "learner" sub-command
parser_learner = sub_parsers.add_parser("learner", help="Learner mode")
parser_learner.add_argument(
"-e",
"--environment",
type=str,
help="Only OpenAI Gym/PyBullet environments are available!",
required=True,
)
parser_learner.add_argument(
"-t",
"--max_steps",
type=int,
help="Number of agent's steps",
default=int(1e6),
)
# create the parse for the "tester" sub-command
parser_tester = sub_parsers.add_parser("tester", help="Tester mode")
parser_tester.add_argument(
"-t",
"--max_steps",
type=int,
help="Number of agent's steps",
default=int(1e6),
)
parser_tester.add_argument(
"--render", action="store_true", help="Render the environment"
)
parser_tester.add_argument(
"-f", "--model_path", type=str, help="Path to saved model"
)
args = my_parser.parse_args()
print(args)
if args.mode == "agent":
print('mode 1')
elif args.mode == "learner":
print('mode 2')
elif args.mode == "tester":
print('mode 3')

Related

Python error: the following arguments are required :

I am not familiar with Python, trying to build some DNN. So when I tried to parse some arguments I got this error in main.
usage: main.py [-h] [-j N] [--resume PATH] [--epochs N] [--start-epoch N] [-b N] [--lr LR]
[--weight-decay W] [-e] [--print-freq N]
DIR
main.py: error: the following arguments are required: DIR
Here is some part of the code:
# Parse arguments and prepare program
parser = argparse.ArgumentParser(description='Training and Using ColorNet')
parser.add_argument('data', metavar='DIR', help='path to dataset')
parser.add_argument('-j', '--workers', default=0, type=int, metavar='N', help='number of data loading workers (default: 0)')
parser.add_argument('--resume', default='', type=str, metavar='PATH', help='path to .pth file checkpoint (default: none)')
parser.add_argument('--epochs', default=50, type=int, metavar='N', help='number of total epochs to run')
parser.add_argument('--start-epoch', default=0, type=int, metavar='N', help='manual epoch number (overridden if loading from checkpoint)')
parser.add_argument('-b', '--batch-size', default=16, type=int, metavar='N', help='size of mini-batch (default: 16)')
parser.add_argument('--lr', '--learning-rate', default=0.1, type=float, metavar='LR', help='learning rate at start of training')
parser.add_argument('--weight-decay', '--wd', default=1e-10, type=float, metavar='W', help='weight decay (default: 1e-4)')
parser.add_argument('-e', '--evaluate', dest='evaluate', action='store_true', help='use this flag to validate without training')
parser.add_argument('--print-freq', '-p', default=10, type=int, metavar='N', help='print frequency (default: 10)')
# Current best losses
best_losses = 1000.0
use_gpu = torch.cuda.is_available()
def main():
global args, best_losses, use_gpu
args = parser.parse_args()
print('Arguments: {}'.format(args))
I read some comments to change
parser.parse_args()
to
parser.parse_args(args)
but it didn't work :)
As DIR doesn't have a default value, you need to supply one when running the program. The easiest way to do this is via a command line interface. Consult the documentation of the library you are using for further hints on that.

SystemExit 2 error in the compilation of code when calling -e', '--episode', type=int, default=2000,

my code is a below:
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-e', '--episode', type=int, default=2000,
help='number of episode to run')
parser.add_argument('-b', '--batch_size', type=int, default=32,
help='batch size for experience replay')
parser.add_argument('-i', '--initial_invest', type=int, default=20000,
help='initial investment amount')
parser.add_argument('-m', '--mode', type=str, required=True,
help='either "train" or "test"')
parser.add_argument('-w', '--weights', type=str, help='a trained model weights')
args = parser.parse_args()
maybe_make_dir('weights')
maybe_make_dir('portfolio_val')
Error:-
usage: [-h] [-e EPISODE] [-b BATCH_SIZE] [-i INITIAL_INVEST] -m MODE
[-w WEIGHTS]
: error: the following arguments are required: -m/--mode
An exception has occurred, use %tb to see the full traceback.
SystemExit: 2
So in the above error i am not able to understand what is the mistake i have also imported all the libraries correctly.
try changing this:
parser.add_argument('-m', '--mode', type=str, required=True,
help='either "train" or "test"')
to this: (notice nargs and const)
parser.add_argument('-m', '--mode',nargs='?', const='' , type=str, required=True,
help='either "train" or "test"')
then you can add this to your code:
if len(args.mode) == 0:
print('-m either "train" or "test"')
import sys
sys.exit() #closes python script
as explained here: https://stackoverflow.com/a/58396626/13093413

Calling Julia function from a Python file

I need to integrate two files, one in python the other in julia. More precisely, I need to call a julia function, snapshot_sphere(b,h,data,m,r,d), from a python file. Moreover,I need to provide the parameters via the command line. I had difficulties in using PyJulia. For the moment an error message is persisting:
ModuleNotFoundError: No module named '_ctypes'
I found some dependencies related to libffi-dev. Installing it did not change anything. What should I do to call the julia function from the python file ?
Here is the code of the python file:
import argparse
from julia import Main
from PIL import Image, ImageShow
parser = argparse.ArgumentParser(description="Projection of the picture")
parser.add_argument(
"-f",
"--filename",
type=str,
action="store",
dest="fname",
help="Name of the picture"
)
parser.add_argument(
"-d",
"--density",
type=int,
action="store",
dest="density",
help="Number samples per Pixel"
)
parser.add_argument(
"-r",
"--radius",
type=float,
action="store",
dest="radius",
help="Radius of the sphere"
)
parser.add_argument(
"-x",
type=float,
action="store",
dest="x",
help="X coordinate of the center"
)
parser.add_argument(
"-y",
type=float,
action="store",
dest="y",
help="Y coordinate of the center"
)
parser.add_argument(
"-z",
type=float,
action="store",
dest="z",
help="Z coordinate of the center"
)
args = parser.parse_args()
print(args)
m = (args.x, args.y, args.z)
print(m)
im = Image.open(args.fname)
b, h = im.size
data = list(im.getdata())
Main.b = b
Main.h = h
Main.m = m
Main.r = args.radius
Main.d = args.density
Main.data = data
Main.include("projekt1.jl")
result = Main.eval("snapshot_sphere(b,h,data,m,r,d)")

python get changed (non-default) cli arguments?

Given an argument parser with n arguments, where I change the default value of only a small subset every run from the command line, is there a clean way of extracting a dict/namespace of all the non-default k,v arguments?
parser = argparse.ArgumentParser()
parser.add_argument("--a",type=str,default='a')
parser.add_argument("--b",type=str,default='b')
parser.add_argument("--c",type=str,default='c')
parser.add_argument("--d",type=str,default='d')
And
python run.py --a "e"
I would like to have
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--a",type=str,default='a')
parser.add_argument("--b",type=str,default='b')
parser.add_argument("--c",type=str,default='c')
parser.add_argument("--d",type=str,default='d')
non_default = parse_non_default(parser) # non_default = {'a':'e'}
You could lookup the parser and compare which values differenciate:
import argparse
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--a", type=str, default='a')
parser.add_argument("--b", type=str, default='b')
parser.add_argument("--c", type=str, default='c')
parser.add_argument("--d", type=str, default='d')
parser.add_argument("--n", type=int, default=999)
args = parser.parse_args(['--a', 'e']) # Test CLI arguments!
non_default = {
opt.dest: getattr(args, opt.dest)
for opt in parser._option_string_actions.values()
if hasattr(args, opt.dest) and opt.default != getattr(args, opt.dest)
}
print(non_default)
main()
Out:
{'a': 'e'}

argparse - how to pass argument from args into function?

import argparse
from queries import most_common_cities
parser = argparse.ArgumentParser(description='A script that does operations with database data and returns values')
parser.add_argument('-c', '--most_common_cities',
nargs=1,
type=positive_int,
help='Specify how many common cities.')
args = parser.parse_args()
if args.most_common_cities:
result = most_common_cities(n) # "n" should be an arg passed by user
print(result)
How could I pass arguments from CLI to my function arg?
When someone use command:
python argp.py --most_common_cities 5
It should return 5 most common cities.
Remove nargs=1, then args.most_common_cities will be the actual value passed in.
nargs=1 wraps it in a list.
parser.add_argument('-c', '--most_common_cities',
type=int,
help='Specify how many common cities.')
args = parser.parse_args(['-c', '5'])
n = args.most_common_cities
print(n)
print(type(n))
# 5
# <class 'int'>
I started your script with following command:
python3 test.py --most_common_cities 5
You can access the arguments with:
import argparse
parser = argparse.ArgumentParser(description='A script that does operations with database data and returns values')
parser.add_argument('-c', '--most_common_cities',
nargs=1,
type=int,
help='Specify how many common cities.')
args = parser.parse_args()
arguments = vars(parser.parse_args())
print(arguments) #{'most_common_cities': [5]}
#then you can access the value with:
arguments['most_common_cities']

Categories