Skip to content

Instantly share code, notes, and snippets.

@BBarbosa
Last active June 26, 2019 16:36
Show Gist options
  • Save BBarbosa/9d7df82b61ec6021da587addd7e94253 to your computer and use it in GitHub Desktop.
Save BBarbosa/9d7df82b61ec6021da587addd7e94253 to your computer and use it in GitHub Desktop.
Python custom type arguments parser
def input_parser(obj_type=None, min_value=None, max_value=None, clip=False):
if obj_type in (int, float) and min_value is not None and max_value is not None and min_value > max_value:
raise ValueError("Minimum value (" + str(min_value) + ") is greater than maximum value (" + str(max_value) + ")")
def input_parser_value(value):
if obj_type is not None:
value = obj_type(value)
if obj_type in (int, float):
if min_value is not None and value < min_value:
if clip:
value = min_value
else:
raise ValueError("Minimum value allowed is " + str(min_value))
if max_value is not None and value > max_value:
if clip:
value = max_value
else:
raise ValueError("Maximum value allowed is " + str(max_value))
if obj_type in (tuple, list):
pass
return value
return input_parser_value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment