Created
May 3, 2021 14:43
-
-
Save osharaki/6f32efd15390055069d0e1b7806e724f to your computer and use it in GitHub Desktop.
Unogs API miner: CLI type functions (snippet 5/6)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import re | |
# Allows for more control over the error message in the case of invalid arguments | |
def buffer_type(value, min=0, max=100): | |
value = int(value) | |
if min <= value <= max: | |
return value | |
else: | |
raise argparse.ArgumentTypeError( | |
f"Buffer must be in the inclusive range {min} to {max}" | |
) | |
def time_type(value): | |
match = re.match("^[0-9]{2}:[0-9]{2}$|^now$", value) | |
if match is None: | |
raise argparse.ArgumentTypeError(f"Execution time must be either now or HH:mm") | |
if match[0] != "now": | |
# Check for correct time values | |
hrs, mins = match[0].split(":") | |
if not (0 <= int(hrs) < 24 and 0 <= int(mins) < 60): | |
raise argparse.ArgumentTypeError( | |
f"Time must follow HH:mm format. Hours and minutes must be in the range [0-24) and [0-60), respectively" | |
) | |
return match[0] | |
def offset_type(value): | |
value = int(value) | |
if value >= 0 and value % 100 == 0: | |
return value | |
raise argparse.ArgumentTypeError("Offset must be non-negative multiple of 100") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment