argparse - how pass to a method with kwargs or argv

本秂侑毒 提交于 2019-12-11 10:52:08

问题


I've been looking for a way to use **kwargs or *argv with argparse. I will from hard code to a dynamic way.

Here is my hard code and a example how I will use it.

def get_parser():
    parser = argparse.ArgumentParser()
    parser.add_argument("-r",
                        "--range",
                        dest="r",
                        nargs=8,
                        help="AddRange Parameters")
    parser.add_argument("-p",
                        "--parameters",
                        dest="p",
                        nargs=8,
                        help="SetDefaults as Parameters")
    parser.add_argument("-r",
                    "--range",
                    dest="r",
                    nargs=8,
                    help="AddRange Parameters")
    return parser

"""Create a Template for a Job"""
def create_Template(temp3_,temp_tournsize,temp_popsize,temp0_,temp1_,temp_ngen,temp_run,tmpverb):
    #single GA job
    logging.basicConfig(level=logging.DEBUG)
    template = job.JobTemplate(runGASimple)
    print tmpverb
    template.setDefaults(temp3=temp3_, tournsize=temp_tournsize, popSize=temp_popsize, temp0=temp0_, temp1=temp1_, ngen=temp_ngen, number_of_runs=temp_run, verbose=tmpverb)
    return template

"""Run a simple Job"""
def ajob_run(template):
    ajob = job.Job(template)
    ajob.run()
    pass

    """change Default params with AddRange"""
def add_Range(var_temp0,var_start,var_end,var_stepSize,var_temp1,var_start2,var_end2,var_stepSize2,tmp_template):
    jobCreator = job.JobCreator()
    #jobCreator.addRange('temp0', start=0.0, end=1.0, stepSize=0.1)
    jobCreator.addRange(var_temp0, start= var_start, end=var_end, stepSize=var_stepSize)
    #jobCreator.addRange('temp1', start=0.0, end=1.0, stepSize=0.1)
    jobCreator.addRange(var_temp1, start=var_start2, end=var_end2, stepSize=var_stepSize2)
    # all other params will take defaults
    jobs = jobCreator.generateJobs(tmp_template)

    return jobs

"""Create a Batchjob from Jobs"""
def batch_Job(tmp_jobs):
    batchJob = job.BatchJob(tmp_jobs, 5)

    return batchJob


if (__name__ == "__main__"):

    args = get_parser().parse_args()
    if (args.p and args.r):
        print 'AddRange with Parameters Input Start:'
        temp = create_Template(float(args.p[0]),int(args.p[1]),int(args.p[2]),float(args.p[3]),float(args.p[4]),int(args.p[5]),int(args.p[6]),ast.literal_eval(args.p[7]))
        tmpjobs = add_Range(args.r[0],float(args.r[1]),float(args.r[2]),float(args.r[3]),args.r[4],float(args.r[5]),float(args.r[6]),float(args.r[7]),temp)
        results = batch_Job(tmpjobs)


        print 'AddRange with Parameters Input Ende.'
    elif (args.p):
            print 'Parameters Input Start:'
        ajob_run(create_Template(
        float(args.p[0]),
        int(args.p[1]),
        int(args.p[2]),
        float(args.p[3]),
        float(args.p[4]),
        int(args.p[5]),
        int(args.p[6]),
        ast.literal_eval(args.p[7])))
        print 'Parameters Input Ende.'

CLI.py -p 0.8 20 20 0.5 0.5 20 1 False

Then came a long output with results from a framework.

My method expected this. The variable names can be change in the future.

template.setDefaults(mux=0.8, tournsize=20, rangeSize=20, temp0=0.5, temp1=0.5, ngen=20, number_of_runs=1, verbose=False)

jobCreator.addRange('temp0', start=0.0, end=1.0, tournStep=0.1)

jobCreator.addRange('temp1', start=0.0, end=1.0, turns=4)

An will change it like this:

setDefaults(**kwargs)

addRange(paraName,**kwargs)

I expect this:

CLI.py -p temp0=1 temp1=0.4 ....temp6=8 ... -r temp0 start=0 end=1 tournStep=0.1
or
CLI.py -p hn0=1 bn1=0.4 ....tp6=8 ... -r temp1 start=0 end=1 turns=4

then convert variablenames with the input to:

setDefaults()

and

addRange()

But I need the argparse, because i will build a command line interface.

I have forgot some details about a other method:

    """change Default params with AddSpecific"""
def add_Specific(tmp_template,paraName,*params):
    jobCreator = job.JobCreator()
    #jobCreator.addSpecific('temp0', 0.1,0.2,0.3,0.4,....,0.7,...)
    jobCreator.addRange(paraName, params)
    # all other params will take defaults
    jobs = jobCreator.generateJobs(tmp_template)
    return jobs

Is that the correct way?


回答1:


Here, parameters argument will have a list of 8 pairs, for example:

CLI.py -p argname1=v1 ... argname8=v8

(obviously argnameN should be the argument names of the desired function).

You can then easily turn args.p (which is ['argname1=v1', ... 'argname8=v8']) into a dictionary:

def convert_value(v):
    try:
        return float(v) if '.' in v else int(v)
    except ValueError:
        # v is not a number
        return v

params = dict([convert_value(n) for n in pair.split('=')] for pair in args.p)

and pass it to your function:

"""Create a Template for a Job"""
def create_Template(params):
    #single GA job
    logging.basicConfig(level=logging.DEBUG)
    template = job.JobTemplate(runGASimple)
    print tmpverb
    template.setDefaults(**params)
    return template

You can do the same with your range argument by creating two distinct range argument:

"""change Default params with AddRange"""
def add_Range(var_1, var_2, tmp_template):
    jobCreator = job.JobCreator()
    #jobCreator.addRange('temp0', start=0.0, end=1.0, stepSize=0.1)
    jobCreator.addRange(**var_1)
    #jobCreator.addRange('temp1', start=0.0, end=1.0, stepSize=0.1)
    jobCreator.addRange(**var_2)
    # all other params will take defaults
    jobs = jobCreator.generateJobs(tmp_template)



回答2:


PLease pay attention at the Gall's answer - it might simplify your code dramatically. And regarding dynamic "argparse" please try this:

#!/usr/bin/env python

from __future__ import print_function
from __future__ import unicode_literals
import argparse


args_d = {
  '-r': {
    'flags': ['-r', '--range'],
    'nargs': 8,
    'help': 'AddRange Parameters',
    'dest': 'r'
  },
  '-p': {
    'flags': ['-p', '--parameters'],
    'nargs': 8,
    'help': 'SetDefaults as Parameters',
    'dest': 'p'
  }
}

def setup_parser(args_d):
    parser = argparse.ArgumentParser()

    for k,v in args_d.items():
        if 'flags' in v:
            flags = v['flags']
            del v['flags']
        parser.add_argument(*flags, **v)

    return parser

if __name__ == "__main__":

    args = setup_parser(args_d).parse_args()

    print(args)

You would still have to generate the dictionary dynamically. You can try to use the "inspect" module for that...



来源:https://stackoverflow.com/questions/35129953/argparse-how-pass-to-a-method-with-kwargs-or-argv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!