mirror of
https://github.com/pkkid/python-plexapi
synced 2024-11-10 06:04:15 +00:00
Refactor cast function in utils.py (#1340)
- less indentation, more readable code - early return
This commit is contained in:
parent
d9539a357d
commit
ba384e0be5
1 changed files with 14 additions and 15 deletions
|
@ -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):
|
||||
|
|
Loading…
Reference in a new issue