From 5d09b9de95ce4ed6c27df4d147571b0afcd6adb3 Mon Sep 17 00:00:00 2001 From: Tim Jaacks <tim.jaacks@seco.com> Date: Mon, 31 Jul 2023 10:44:25 +0200 Subject: [PATCH] Refactoring: simplify length comparisons Something we often do is check whether a list or sequence has elements before we try and do something with it. A Pythonic way of doing this is just to use the fact that Python lists and sequences evaluate to True if they have elements, and False otherwise. Doing it this way is a convention, set out in Python's PEP8 style guide. See for reference: https://docs.sourcery.ai/Reference/Python/Default-Rules/simplify-len-comparison/ --- scripts/.sourcery.yaml | 1 - scripts/common.py | 2 +- scripts/lava_create_testreport.py | 4 ++-- scripts/submit_test.py | 2 +- 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/scripts/.sourcery.yaml b/scripts/.sourcery.yaml index 0b514de6..85688643 100644 --- a/scripts/.sourcery.yaml +++ b/scripts/.sourcery.yaml @@ -26,7 +26,6 @@ rule_settings: - dict-comprehension - for-append-to-extend - raise-specific-error - - simplify-len-comparison - use-named-expression - use-next diff --git a/scripts/common.py b/scripts/common.py index b87a426a..4a6b98b6 100755 --- a/scripts/common.py +++ b/scripts/common.py @@ -279,7 +279,7 @@ def get_repository_file_obj(project: Project, filename, ref=None): # logging.debug(repository_tree) fileobj = [f for f in repository_tree if f["name"] == filename] - if len(fileobj) == 0: + if not fileobj: logging.error("Could not find file %s", filename) for f in repository_tree: logging.debug(f["name"]) diff --git a/scripts/lava_create_testreport.py b/scripts/lava_create_testreport.py index f9038cf2..7995889d 100755 --- a/scripts/lava_create_testreport.py +++ b/scripts/lava_create_testreport.py @@ -38,7 +38,7 @@ class TestCollection: out = [] for t in tests: out.append("{:.2f}".format(float(t.measurement))) - if len(out) == 0: + if not out: return None return "/".join(out) @@ -55,7 +55,7 @@ class TestCollection: def get_test(self, index=0): keys = list(self.tests.keys()) - if len(keys) < 1: + if not keys: return None if len(self.tests[keys[index]]) < 1: return None diff --git a/scripts/submit_test.py b/scripts/submit_test.py index 29cd32e8..9e3a5046 100755 --- a/scripts/submit_test.py +++ b/scripts/submit_test.py @@ -208,7 +208,7 @@ def main(): jobs[jobid] = [platform, ""] logging.debug("Queued jobs: %s", jobs) - if len(jobs) == 0: + if not jobs: logging.info("No jobs queued.") return 0 -- GitLab