From 4bf3bb1bb1e2d6d0ccf384ba7110978178cdfaf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Ne=C4=8Dada?= Date: Sun, 3 May 2020 21:29:10 +0300 Subject: [PATCH] "slice" argument type in argproc.py Former-commit-id: 35117c0a6c09063c41350bb6bc2e07ef2adf97c0 --- qpms/argproc.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/qpms/argproc.py b/qpms/argproc.py index bfd1fbf..21d445c 100644 --- a/qpms/argproc.py +++ b/qpms/argproc.py @@ -100,6 +100,52 @@ def float_range(string): else: 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): '''Tries to match a float, or a float with prepended 's'