"slice" argument type in argproc.py
Former-commit-id: 35117c0a6c09063c41350bb6bc2e07ef2adf97c0
This commit is contained in:
parent
1221012c7b
commit
4bf3bb1bb1
|
@ -100,6 +100,52 @@ def float_range(string):
|
||||||
else:
|
else:
|
||||||
return np.arange(first, last, increment)
|
return np.arange(first, last, increment)
|
||||||
|
|
||||||
|
def int_or_None(string):
|
||||||
|
"""Tries to parse a string either as an int or None (if it contains only whitespaces)"""
|
||||||
|
try:
|
||||||
|
return int(string)
|
||||||
|
except ValueError as ve:
|
||||||
|
if string.strip() == '':
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
raise ve
|
||||||
|
|
||||||
|
def sslice(string):
|
||||||
|
"""Tries to parse a string either as one individual int value
|
||||||
|
or one of the following patterns:
|
||||||
|
|
||||||
|
first:last:increment
|
||||||
|
first:last
|
||||||
|
|
||||||
|
first, last and increment must be parseable as ints
|
||||||
|
or be empty (then
|
||||||
|
|
||||||
|
In each case, 's' letter can be prepended to the whole string to avoid
|
||||||
|
argparse interpreting this as a new option (if the argument contains
|
||||||
|
'-' or '+').
|
||||||
|
|
||||||
|
Returns either int or slice containing ints or Nones.
|
||||||
|
"""
|
||||||
|
if string[0] == 's':
|
||||||
|
string = string[1:]
|
||||||
|
try:
|
||||||
|
res = int(string)
|
||||||
|
return res
|
||||||
|
except ValueError:
|
||||||
|
import re
|
||||||
|
match = re.match(r'([^:]*):([^:]*):(.*)', string)
|
||||||
|
if match:
|
||||||
|
step = int_or_None(match.group(3))
|
||||||
|
else:
|
||||||
|
match = re.match(r'([^:]*):(.*)', string)
|
||||||
|
if match:
|
||||||
|
step = None
|
||||||
|
else:
|
||||||
|
argparse.ArgumentTypeError('Invalid int/slice format: "%s"' % string)
|
||||||
|
start = int_or_None(match.group(1))
|
||||||
|
stop = int_or_None(match.group(2))
|
||||||
|
return slice(start, stop, step)
|
||||||
|
|
||||||
def sfloat(string):
|
def sfloat(string):
|
||||||
'''Tries to match a float, or a float with prepended 's'
|
'''Tries to match a float, or a float with prepended 's'
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue