Fix ValueError if casting to int/float
This commit is contained in:
parent
563e86c8db
commit
e08e9b0d32
2 changed files with 43 additions and 26 deletions
|
@ -21,32 +21,47 @@ WINDOW_COMMAND = 'plexkodiconnect.command'.encode('utf-8')
|
||||||
|
|
||||||
def cast(func, value):
|
def cast(func, value):
|
||||||
"""
|
"""
|
||||||
Cast the specified value to the specified type (returned by func). Currently this
|
Cast the specified value to the specified type (returned by func). Currently
|
||||||
only support int, float, bool. Should be extended if needed.
|
this only support int, float, bool. Should be extended if needed.
|
||||||
Parameters:
|
Parameters:
|
||||||
func (func): Calback function to used cast to type (int, bool, float).
|
func (func): Calback function to used cast to type (int, bool, float).
|
||||||
value (any): value to be cast and returned.
|
value (any): value to be cast and returned.
|
||||||
|
|
||||||
|
Returns None if something goes wrong
|
||||||
"""
|
"""
|
||||||
if value is not None:
|
if value is None:
|
||||||
if func == bool:
|
return value
|
||||||
return bool(int(value))
|
elif func == bool:
|
||||||
elif func == unicode:
|
return bool(int(value))
|
||||||
if isinstance(value, (int, long, float)):
|
elif func == unicode:
|
||||||
return unicode(value)
|
if isinstance(value, (int, long, float)):
|
||||||
else:
|
return unicode(value)
|
||||||
return value.decode('utf-8')
|
elif isinstance(value, unicode):
|
||||||
elif func == str:
|
return value
|
||||||
if isinstance(value, (int, long, float)):
|
else:
|
||||||
return str(value)
|
return value.decode('utf-8')
|
||||||
else:
|
elif func == str:
|
||||||
return value.encode('utf-8')
|
if isinstance(value, (int, long, float)):
|
||||||
elif func in (int, float):
|
return str(value)
|
||||||
|
elif isinstance(value, str):
|
||||||
|
return value
|
||||||
|
else:
|
||||||
|
return value.encode('utf-8')
|
||||||
|
elif func == int:
|
||||||
|
try:
|
||||||
|
return int(value)
|
||||||
|
except ValueError:
|
||||||
try:
|
try:
|
||||||
return func(value)
|
# Converting e.g. '8.0' fails; need to convert to float first
|
||||||
|
return int(float(value))
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return float('nan')
|
return
|
||||||
return func(value)
|
elif func == float:
|
||||||
return value
|
try:
|
||||||
|
return float(value)
|
||||||
|
except ValueError:
|
||||||
|
return
|
||||||
|
return func(value)
|
||||||
|
|
||||||
|
|
||||||
def kodi_window(property, value=None, clear=False):
|
def kodi_window(property, value=None, clear=False):
|
||||||
|
|
|
@ -268,11 +268,13 @@ class AttributeDict(dict):
|
||||||
|
|
||||||
def cast(func, value):
|
def cast(func, value):
|
||||||
"""
|
"""
|
||||||
Cast the specified value to the specified type (returned by func). Currently this
|
Cast the specified value to the specified type (returned by func). Currently
|
||||||
only support int, float, bool. Should be extended if needed.
|
this only support int, float, bool. Should be extended if needed.
|
||||||
Parameters:
|
Parameters:
|
||||||
func (func): Calback function to used cast to type (int, bool, float).
|
func (func): Calback function to used cast to type (int, bool, float).
|
||||||
value (any): value to be cast and returned.
|
value (any): value to be cast and returned.
|
||||||
|
|
||||||
|
Returns None if something goes wrong
|
||||||
"""
|
"""
|
||||||
if value is None:
|
if value is None:
|
||||||
return value
|
return value
|
||||||
|
@ -294,18 +296,18 @@ def cast(func, value):
|
||||||
return value.encode('utf-8')
|
return value.encode('utf-8')
|
||||||
elif func == int:
|
elif func == int:
|
||||||
try:
|
try:
|
||||||
return func(value)
|
return int(value)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
try:
|
try:
|
||||||
# Converting e.g. '8.0' fails; need to convert to float first
|
# Converting e.g. '8.0' fails; need to convert to float first
|
||||||
return int(float(value))
|
return int(float(value))
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return float('nan')
|
return
|
||||||
elif func == float:
|
elif func == float:
|
||||||
try:
|
try:
|
||||||
return func(value)
|
return float(value)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return float('nan')
|
return
|
||||||
return func(value)
|
return func(value)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue