-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
87 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import argparse, os | ||
|
||
|
||
def positive_int(value: str) -> int: | ||
""" | ||
Validates if the provided string represents a positive integer. | ||
Parameters | ||
---------- | ||
value : str | ||
The value to validate. | ||
Returns | ||
------- | ||
int | ||
The validated positive integer. | ||
Raises | ||
------ | ||
argparse.ArgumentTypeError | ||
If the value is not a valid integer or positive integer. | ||
""" | ||
if value is not None: | ||
try: | ||
ivalue = int(value) | ||
except ValueError: | ||
raise argparse.ArgumentTypeError(f"{value} is not a valid integer") | ||
if ivalue <= 0: | ||
raise argparse.ArgumentTypeError(f"{value} is not a positive integer") | ||
return ivalue | ||
|
||
|
||
def positive_number(value: str) -> float: | ||
""" | ||
Validates if the provided string represents a positive number. | ||
Parameters | ||
---------- | ||
value : str | ||
The value to validate. | ||
Returns | ||
------- | ||
float | ||
The validated positive number. | ||
Raises | ||
------ | ||
argparse.ArgumentTypeError | ||
If the value is not a valid number or positive number. | ||
""" | ||
if value is not None: | ||
try: | ||
fvalue = float(value) | ||
except ValueError: | ||
raise argparse.ArgumentTypeError(f"{value} is not a valid number") | ||
if fvalue <= 0: | ||
raise argparse.ArgumentTypeError(f"{value} is not a positive number") | ||
return fvalue | ||
|
||
|
||
def existed_file(value: str) -> str: | ||
""" | ||
Validates if the provided string is a path to an existing file. | ||
Parameters | ||
---------- | ||
value : str | ||
The path to validate. | ||
Returns | ||
------- | ||
str | ||
The validated file path. | ||
Raises | ||
------ | ||
argparse.ArgumentTypeError | ||
If the file does not exist. | ||
""" | ||
if value is not None: | ||
if not os.path.isfile(value): | ||
raise argparse.ArgumentTypeError(f"{value} is not found") | ||
return value |