Refactor cast function in utils.py (#1340)

- less indentation, more readable code
- early return
This commit is contained in:
Dr.Blank 2024-02-04 14:22:37 -05:00 committed by GitHub
parent d9539a357d
commit ba384e0be5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -144,22 +144,21 @@ def cast(func, value):
func (func): Callback function to used cast to type (int, bool, float).
value (any): value to be cast and returned.
"""
if value is not None:
if value is None:
return value
if func == bool:
if value in (1, True, "1", "true"):
return True
elif value in (0, False, "0", "false"):
if value in (0, False, "0", "false"):
return False
else:
raise ValueError(value)
elif func in (int, float):
if func in (int, float):
try:
return func(value)
except ValueError:
return float('nan')
return func(value)
return value
def joinArgs(args):