I'm developing a toolbox containing several python scripts. For several of them some arguments may be numeric values. Depending of the script some may require a value v to be between -1 and 1, or 0 and 1 or 1 and 10 or ... An example could be a page width from an output diagram which should be always positive.
I can check all the time if v is in the required range. I could also for each of these range define an Action or a type using argparse. An example is given using a new type:
def positive_num(a_value):
"""Check a numeric positive."""
if not a_value > 0:
raise argparse.ArgumentTypeError("Should be positive.")
return a_value
And add it later to the parser:
parser_grp.add_argument('-pw', '--page-width',
help='Output pdf file width (e.g. 7 inches).',
type=positive_num,
default=None,
required=False)
Now, if the value is a correlation coefficient (or anything in a range) would it be possible using action or types to write something more general using:
def ranged_num(a_value, lowest=-1, highest=1):
"""Check a numeric is in expected range."""
if not (a_value >= lowest and a_value <= highest):
raise argparse.ArgumentTypeError("Not in range.")
return a_value
That could later be added like:
parser_grp.add_argument('-c', '--correlation',
help='A value for the correlation coefficient',
type=ranged_num(-1,1),
default=None,
required=False)
I have tried in several ways but whithout success.
Thank you
typeneeds to be a function that accepts a single (note: string) argument, so that's whatranged_num(-1, 1)needs to return.typefunction can also take parameters. You would writetype=ranged_num(-1, 1), and that function would return an appropriately-configures function that accepts the string argument.functools.partialcan be used to bind thelowandhighofranged_num.