Spaces:
Running
Running
File size: 1,460 Bytes
b20af9f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
import argparse
from typing import List, Optional, Union, Tuple
def int_or_int_list_or_none(value: Optional[Union[int, str]]) -> List[Optional[int]]:
"""
Parse an input value into a list of integers or a single integer, or None.
Args:
value (Optional[Union[int, str]]): The input value to parse.
Returns:
List[Optional[int]]: A list containing either a single integer, a list of integers,
or a single None value.
Raises:
argparse.ArgumentTypeError: If the input value cannot be parsed into the specified formats.
"""
if value in ['None', 'null']:
return [None]
try:
# If the value contains commas, parse it as a comma-separated list of integers
if ',' in value:
return [int(x) for x in value.split(',')]
# If it's a single integer, pack it into a list
else:
return [int(value)]
except ValueError:
raise argparse.ArgumentTypeError("Invalid format. Use an integer, a comma-separated list of integers, or None.")
def int_or_float(value):
if '.' in value:
try:
return float(value)
except ValueError:
raise argparse.ArgumentTypeError("Quality level must be an integer or a float")
else:
try:
return int(value)
except ValueError:
raise argparse.ArgumentTypeError("Quality level must be an integer or a float")
|