From a3baa39e1f445a5461fbf8f40f7303558730a19d Mon Sep 17 00:00:00 2001
From: Tim Jaacks <tim.jaacks@seco.com>
Date: Mon, 31 Jul 2023 11:55:27 +0200
Subject: [PATCH] Refactoring: use list comprehensions

A list comprehension can create the list on one line, cutting out the
clutter of declaring an empty list and then appending values.

See for reference:
https://docs.sourcery.ai/Reference/Python/Default-Rules/list-comprehension/
---
 scripts/.sourcery.yaml                 |  1 -
 scripts/changelog_generator.py         | 12 ++++--------
 scripts/generate_alphaplan_fwr_file.py | 22 ++++++++++------------
 scripts/get_pipeline_jobs.py           |  4 +---
 scripts/gitlab_backup.py               |  8 ++++----
 scripts/lava_create_testreport.py      | 18 ++++--------------
 6 files changed, 23 insertions(+), 42 deletions(-)

diff --git a/scripts/.sourcery.yaml b/scripts/.sourcery.yaml
index 924b8c82..a03b9ac8 100644
--- a/scripts/.sourcery.yaml
+++ b/scripts/.sourcery.yaml
@@ -19,7 +19,6 @@ rule_settings:
   - assign-if-exp
   - inline-immediately-returned-variable
   - remove-redundant-if
-  - list-comprehension
   - switch
   - dict-literal
   - low-code-quality
diff --git a/scripts/changelog_generator.py b/scripts/changelog_generator.py
index 23a386da..72bf8f3e 100755
--- a/scripts/changelog_generator.py
+++ b/scripts/changelog_generator.py
@@ -240,14 +240,10 @@ def main(args):
     logging.debug(options)
     gitlab = gl.Gitlab(options.gitlab_url, private_token=options.token)
 
-    projects = []
-    for project in options.project:
-        projects.append(Project(gitlab.projects.get(project)))
-
-    releases = []
-    for t in projects[0].project.tags.list(search=options.branch):
-        releases.append(Release(Tag(t)))
-
+    projects = [Project(gitlab.projects.get(project)) for project in options.project]
+    releases = [
+        Release(Tag(t)) for t in projects[0].project.tags.list(search=options.branch)
+    ]
     # Add dummy release with date today for new unstaged commits
     releases.append(
         Release(
diff --git a/scripts/generate_alphaplan_fwr_file.py b/scripts/generate_alphaplan_fwr_file.py
index 301383c0..96af50c0 100755
--- a/scripts/generate_alphaplan_fwr_file.py
+++ b/scripts/generate_alphaplan_fwr_file.py
@@ -160,19 +160,17 @@ def generate_fwr_articles(
             ]
 
         if subarticles_uboot:
-            stueckliste_uboot = []
-            for key in subarticles_uboot:
-                stueckliste_uboot.append(
-                    generate_ap_subarticle(
-                        files,
-                        key,
-                        machine,
-                        machine_ap,
-                        release_name_ap,
-                        md5sums,
-                    )
+            stueckliste_uboot = [
+                generate_ap_subarticle(
+                    files,
+                    key,
+                    machine,
+                    machine_ap,
+                    release_name_ap,
+                    md5sums,
                 )
-
+                for key in subarticles_uboot
+            ]
             # At the moment there are no attributes specified for uboot FWR
             attribute_uboot = [None, None, "UBOOT"]
             data_uboot = new_ap_article(
diff --git a/scripts/get_pipeline_jobs.py b/scripts/get_pipeline_jobs.py
index 05a03659..acc3518a 100755
--- a/scripts/get_pipeline_jobs.py
+++ b/scripts/get_pipeline_jobs.py
@@ -44,10 +44,8 @@ def get_pipeline_jobs(gitlab, project, pipeline, name=None, stage=None):
         gl_project = gitlab.projects.get(project)
 
     pipeline_jobs = get_pipeline_pipelinejobs(gitlab, project, pipeline, name, stage)
-    jobs = []
     # Project Jobs from Pipeline Job
-    for job in pipeline_jobs:
-        jobs.append(gl_project.jobs.get(job.id))
+    jobs = [gl_project.jobs.get(job.id) for job in pipeline_jobs]
     return jobs
 
 
diff --git a/scripts/gitlab_backup.py b/scripts/gitlab_backup.py
index bb102927..0c148d71 100755
--- a/scripts/gitlab_backup.py
+++ b/scripts/gitlab_backup.py
@@ -190,10 +190,10 @@ class MergeRequest:
 def get_projects_from_group(gitlab, base_group):
     """Recurse through all subgroups and create a flat list of all projects"""
 
-    p_list = []
-    for group_project in base_group.projects.list(retry_transient_errors=True):
-        p_list.append(gitlab.projects.get(group_project.id))
-
+    p_list = [
+        gitlab.projects.get(group_project.id)
+        for group_project in base_group.projects.list(retry_transient_errors=True)
+    ]
     for subgroup in base_group.subgroups.list(retry_transient_errors=True):
         group = gitlab.groups.get(subgroup.id)
         p_list += get_projects_from_group(gitlab, group)
diff --git a/scripts/lava_create_testreport.py b/scripts/lava_create_testreport.py
index 7995889d..ad7f0e6f 100755
--- a/scripts/lava_create_testreport.py
+++ b/scripts/lava_create_testreport.py
@@ -35,9 +35,7 @@ class TestCollection:
         if machine not in self.tests.keys():
             return ""
         tests = self.tests[machine]
-        out = []
-        for t in tests:
-            out.append("{:.2f}".format(float(t.measurement)))
+        out = ["{:.2f}".format(float(t.measurement)) for t in tests]
         if not out:
             return None
         return "/".join(out)
@@ -80,13 +78,11 @@ class TestCollection:
             all_tests += suite.tests
 
         tests = {}
-        test_collections = []
         for test in all_tests:
             if test.test_case_id not in tests.keys():
                 tests[test.test_case_id] = []
             tests[test.test_case_id].append(test)
-        for k, v in tests.items():
-            test_collections.append(TestCollection(k, v))
+        test_collections = [TestCollection(k, v) for k, v in tests.items()]
         return test_collections
 
     def __str__(self):
@@ -162,14 +158,11 @@ class SuiteCollection:
                 continue
             all_suites += job.suites
         suites = {}
-        suite_collections = []
         for suite in all_suites:
             if suite.name not in suites.keys():
                 suites[suite.name] = []
             suites[suite.name].append(suite)
-        for k, v in suites.items():
-            suite_collections.append(SuiteCollection(k, v))
-
+        suite_collections = [SuiteCollection(k, v) for k, v in suites.items()]
         suite_collections.sort()
         return suite_collections
 
@@ -206,10 +199,7 @@ def lava_create_testreport(jobids, lava=None):
     lava_tags = LavaTag.taglist_from_lava(lava)
     lava_devices = LavaDevice.devicelist_from_lava(lava, lava_tags)
 
-    lava_jobs = []
-    for jobid in jobids:
-        lava_jobs.append(LavaJob(lava, jobid, lava_devices))
-
+    lava_jobs = [LavaJob(lava, jobid, lava_devices) for jobid in jobids]
     suite_collections = SuiteCollection.collections_from_jobs(lava_jobs)
 
     all_machines = SuiteCollection.all_machines(suite_collections)
-- 
GitLab