Skip to content

Commit

Permalink
Refactor cast function in utils.py (#1340)
Browse files Browse the repository at this point in the history
- less indentation, more readable code
- early return
  • Loading branch information
Dr-Blank authored Feb 4, 2024
1 parent d9539a3 commit ba384e0
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions plexapi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 func == bool:
if value in (1, True, "1", "true"):
return True
elif value in (0, False, "0", "false"):
return False
else:
raise ValueError(value)

elif func in (int, float):
try:
return func(value)
except ValueError:
return float('nan')
return func(value)
return value
if value is None:
return value
if func == bool:
if value in (1, True, "1", "true"):
return True
if value in (0, False, "0", "false"):
return False
raise ValueError(value)

if func in (int, float):
try:
return func(value)
except ValueError:
return float('nan')
return func(value)


def joinArgs(args):
Expand Down

0 comments on commit ba384e0

Please sign in to comment.