← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~lxer/launchpad:refactor into launchpad:master

 

alex has proposed merging ~lxer/launchpad:refactor into launchpad:master.

Commit message:
refactoring proposal 

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~lxer/launchpad/+git/launchpad/+merge/429892

While reading the code I found some things that could be simplified.

None of these changes should have any impact.
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~lxer/launchpad:refactor into launchpad:master.
diff --git a/lib/lp/app/browser/root.py b/lib/lp/app/browser/root.py
index 5dc4181..cdd55d3 100644
--- a/lib/lp/app/browser/root.py
+++ b/lib/lp/app/browser/root.py
@@ -2,6 +2,8 @@
 # GNU Affero General Public License version 3 (see the file LICENSE).
 """Browser code for the Launchpad root page."""
 
+
+import contextlib
 __all__ = [
     "LaunchpadRootIndexView",
     "LaunchpadSearchView",
@@ -175,18 +177,18 @@ class LaunchpadRootIndexView(HasAnnouncementsView, LaunchpadView):
         except requests.RequestException:
             return []
         feed = feedparser.parse(response.content)
-        posts = []
+
         max_count = config.launchpad.homepage_recent_posts_count
         # FeedParser takes care of HTML sanitisation.
-        for entry in feed.entries[:max_count]:
-            posts.append(
-                {
+        posts = [{
                     "title": entry.title,
                     "description": entry.description,
                     "link": entry.link,
                     "date": time.strftime("%d %b %Y", entry.published_parsed),
-                }
-            )
+                } for entry in feed.entries[:max_count]
+        ]
+
+
         # The cache of posts expires after an hour.
         getUtility(IMemcacheClient).set(key, posts, expire=3600)
         return posts
@@ -248,9 +250,7 @@ class LaunchpadPrimarySearchFormView(LaunchpadSearchFormView):
     @property
     def error_class(self):
         """Return the 'error' if there is an error, or None."""
-        if self.error:
-            return "error"
-        return None
+        return "error" if self.error else None
 
 
 class ILaunchpadSearch(Interface):
@@ -321,10 +321,7 @@ class LaunchpadSearchView(LaunchpadFormView):
         """Sanitize the search_params and add the BatchNavigator params."""
         if self.search_params["text"] is not None:
             text = self.search_params["text"].strip()
-            if text == "":
-                self.search_params["text"] = None
-            else:
-                self.search_params["text"] = text
+            self.search_params["text"] = None if text == "" else text
         request_start = self.request.get("start", self.search_params["start"])
         try:
             start = int(request_start)
@@ -372,9 +369,7 @@ class LaunchpadSearchView(LaunchpadFormView):
     @property
     def focusedElementScript(self):
         """Focus the first widget when there are no matches."""
-        if self.has_matches:
-            return None
-        return super().focusedElementScript()
+        return None if self.has_matches else super().focusedElementScript()
 
     @property
     def bug(self):
@@ -454,10 +449,7 @@ class LaunchpadSearchView(LaunchpadFormView):
 
     def containsMatchingKind(self, kinds):
         """Return True if one of the items in kinds is not None, or False."""
-        for kind in kinds:
-            if kind is not None and kind is not False:
-                return True
-        return False
+        return any(kind is not None and kind is not False for kind in kinds)
 
     def validate(self, data):
         """See `LaunchpadFormView`"""
@@ -512,9 +504,7 @@ class LaunchpadSearchView(LaunchpadFormView):
         """Return the first group of numbers in the search text, or None."""
         numeric_pattern = re.compile(r"(\d+)")
         match = numeric_pattern.search(text)
-        if match is None:
-            return None
-        return match.group(1)
+        return None if match is None else match[1]
 
     def _getNameToken(self, text):
         r"""Return the search text as a Launchpad name.
@@ -540,15 +530,11 @@ class LaunchpadSearchView(LaunchpadFormView):
     def _getDistributionOrProductOrProjectGroup(self, name):
         """Return the matching distribution, product or project, or None."""
         vocabulary_registry = getVocabularyRegistry()
-        vocab = vocabulary_registry.get(
-            None, "DistributionOrProductOrProjectGroup"
-        )
-        try:
+        vocab = vocabulary_registry.get(None, "DistributionOrProductOrProjectGroup")
+        with contextlib.suppress(LookupError):
             pillar = vocab.getTermByToken(name).value
             if check_permission("launchpad.View", pillar):
                 return pillar
-        except LookupError:
-            pass
         return None
 
     def searchPages(self, query_terms, start=0):
@@ -614,7 +600,7 @@ class WindowedList:
 
     def __iter__(self):
         """Yield each item, or None if the index is virtual."""
-        for index in range(0, self._total):
+        for index in range(self._total):
             yield self[index]