Skip to content

Instantly share code, notes, and snippets.

@hosaka
Created March 12, 2015 19:03
Show Gist options
  • Save hosaka/ff833063e12d52246a17 to your computer and use it in GitHub Desktop.
Save hosaka/ff833063e12d52246a17 to your computer and use it in GitHub Desktop.
Using this method with argparse parameter type=delay_check. Returns a value in seconds according to units specified. For example 51m or 120s or 2d, or defaults to seconds.
def delay_check(self, delay):
# get the units and the delay value
unit = delay.lstrip("0123456789")
# TODO: this raises exception, still caught by the parser, but incorrect
# for example input node 1 timer on aksldjhf will crash here
if unit not in "":
value = int(delay[:-len(unit)])
else:
return int(delay)
# manipulate the returned value
if unit in "s" or "":
pass
elif unit in "m":
value *= 60
elif unit in "h":
value *= 3600
elif unit in "d":
value *= 86400
else:
# raise the argparse error
msg = "'{}': {}".format(delay, "is not in valid time format")
raise argparse.ArgumentTypeError(msg)
return value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment