From ba384e0be5fb01d8dd96330d1cdc6319d48182a9 Mon Sep 17 00:00:00 2001 From: "Dr.Blank" <64108942+Dr-Blank@users.noreply.github.com> Date: Sun, 4 Feb 2024 14:22:37 -0500 Subject: [PATCH] Refactor cast function in utils.py (#1340) - less indentation, more readable code - early return --- plexapi/utils.py | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/plexapi/utils.py b/plexapi/utils.py index ad28a5cb..467ccf05 100644 --- a/plexapi/utils.py +++ b/plexapi/utils.py @@ -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) + 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) - elif func in (int, float): - try: - return func(value) - except ValueError: - return float('nan') - return func(value) - return value + if func in (int, float): + try: + return func(value) + except ValueError: + return float('nan') + return func(value) def joinArgs(args):