You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kibble.apache.org by tu...@apache.org on 2020/12/13 02:12:19 UTC

[kibble] branch main updated: Improve Code Quality (#113)

This is an automated email from the ASF dual-hosted git repository.

turbaszek pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/kibble.git


The following commit(s) were added to refs/heads/main by this push:
     new 3485535  Improve Code Quality (#113)
3485535 is described below

commit 3485535775309330b06f6d077c2764b397aeac2d
Author: Kaxil Naik <ka...@gmail.com>
AuthorDate: Sun Dec 13 02:12:10 2020 +0000

    Improve Code Quality (#113)
    
    - Comparison with None
    - Redundant Parenthesis
    - Remove unused variable
    - Fix mutable default arguments
    - Augmented assignment expressions
---
 kibble/scanners/brokers/kibbleES.py       |  4 ++--
 kibble/scanners/scanners/__init__.py      |  2 +-
 kibble/scanners/scanners/bugzilla.py      |  6 +++---
 kibble/scanners/scanners/gerrit.py        |  4 +++-
 kibble/scanners/scanners/git-census.py    |  4 ++--
 kibble/scanners/scanners/git-evolution.py |  2 +-
 kibble/scanners/scanners/jira.py          |  6 +++---
 kibble/scanners/scanners/pipermail.py     |  4 ++--
 kibble/scanners/scanners/ponymail-kpe.py  |  2 +-
 kibble/scanners/scanners/ponymail-tone.py |  2 +-
 kibble/scanners/scanners/ponymail.py      |  4 ++--
 kibble/scanners/scanners/travis.py        |  2 +-
 kibble/scanners/scanners/twitter.py       | 12 ++++++------
 kibble/scanners/utils/github.py           | 10 +++++++---
 kibble/scanners/utils/tone.py             | 16 +++++-----------
 kibble/scanners/utils/urlmisc.py          |  1 -
 16 files changed, 40 insertions(+), 41 deletions(-)

diff --git a/kibble/scanners/brokers/kibbleES.py b/kibble/scanners/brokers/kibbleES.py
index 0f3c909..16f999b 100644
--- a/kibble/scanners/brokers/kibbleES.py
+++ b/kibble/scanners/brokers/kibbleES.py
@@ -114,7 +114,7 @@ class KibbleESWrapperSeven:
 
 # This is redundant, refactor later?
 def pprint(string, err=False):
-    line = "[core]: %s" % (string)
+    line = "[core]: %s" % string
     if err:
         sys.stderr.write(line + "\n")
     else:
@@ -265,7 +265,7 @@ class KibbleOrganisation:
         )
 
         for hit in res["hits"]["hits"]:
-            if sourceType == None or hit["_source"]["type"] == sourceType:
+            if sourceType is None or hit["_source"]["type"] == sourceType:
                 s.append(hit["_source"])
         return s
 
diff --git a/kibble/scanners/scanners/__init__.py b/kibble/scanners/scanners/__init__.py
index 7b51436..771ba57 100644
--- a/kibble/scanners/scanners/__init__.py
+++ b/kibble/scanners/scanners/__init__.py
@@ -58,4 +58,4 @@ for p in __all__:
 def enumerate():
     """ Returns the scanners as a dictionary, sorted by run-order """
     for p in __all__:
-        yield (p, scanners[p])
+        yield p, scanners[p]
diff --git a/kibble/scanners/scanners/bugzilla.py b/kibble/scanners/scanners/bugzilla.py
index dd7ae57..e1f1bfb 100644
--- a/kibble/scanners/scanners/bugzilla.py
+++ b/kibble/scanners/scanners/bugzilla.py
@@ -90,13 +90,13 @@ def wasclosed(js):
                     if item["field"] == "status" and (
                         item["toString"] == "Closed" or item["toString"] == "Resolved"
                     ):
-                        return (True, citem["author"])
+                        return True, citem["author"]
     else:
         if "items" in js:
             for item in js["items"]:
                 if item["field"] == "status" and item["toString"] == "Closed":
-                    return (True, None)
-    return (False, None)
+                    return True, None
+    return False, None
 
 
 def resolved(js):
diff --git a/kibble/scanners/scanners/gerrit.py b/kibble/scanners/scanners/gerrit.py
index ebd15a9..b82dab6 100644
--- a/kibble/scanners/scanners/gerrit.py
+++ b/kibble/scanners/scanners/gerrit.py
@@ -70,7 +70,9 @@ def get_commit_id(commit_message):
     return None
 
 
-def get_all(base_url, f, params={}):
+def get_all(base_url, f, params=None):
+    if params is None:
+        params = {}
     acc = []
 
     while True:
diff --git a/kibble/scanners/scanners/git-census.py b/kibble/scanners/scanners/git-census.py
index d507426..aa19a03 100644
--- a/kibble/scanners/scanners/git-census.py
+++ b/kibble/scanners/scanners/git-census.py
@@ -173,12 +173,12 @@ def scan(KibbleBit, source):
                     alcseries[gname][ts] = {}
                 if not ce in lcseries[gname][ts]:
                     lcseries[gname][ts][ce] = [0, 0]
-                lcseries[gname][ts][ce][0] = lcseries[gname][ts][ce][0] + insert
+                lcseries[gname][ts][ce][0] += insert
                 lcseries[gname][ts][ce][1] = lcseries[gname][ts][ce][0] + delete
 
                 if not ae in alcseries[gname][ts]:
                     alcseries[gname][ts][ae] = [0, 0]
-                alcseries[gname][ts][ae][0] = alcseries[gname][ts][ae][0] + insert
+                alcseries[gname][ts][ae][0] += insert
                 alcseries[gname][ts][ae][1] = alcseries[gname][ts][ae][0] + delete
 
                 if not ts in ctseries[gname]:
diff --git a/kibble/scanners/scanners/git-evolution.py b/kibble/scanners/scanners/git-evolution.py
index 130eabd..07a7ada 100644
--- a/kibble/scanners/scanners/git-evolution.py
+++ b/kibble/scanners/scanners/git-evolution.py
@@ -149,7 +149,7 @@ def scan(KibbleBit, source):
     inp = get_first_ref(gpath)
     if inp:
         ts = int(inp.split()[0])
-        ts = ts - (ts % 86400)
+        ts -= ts % 86400
         date = time.strftime("%Y-%b-%d 0:00", time.gmtime(ts))
 
         # print("Starting from %s" % date)
diff --git a/kibble/scanners/scanners/jira.py b/kibble/scanners/scanners/jira.py
index efc22cf..41dafa6 100644
--- a/kibble/scanners/scanners/jira.py
+++ b/kibble/scanners/scanners/jira.py
@@ -91,15 +91,15 @@ def wasclosed(js):
                         item["toString"].lower().find("closed") != -1
                         or item["toString"].lower().find("resolved") != -1
                     ):
-                        return (True, citem.get("author", {}))
+                        return True, citem.get("author", {})
     else:
         if "items" in js:
             for item in js["items"]:
                 if item["field"] == "status" and (
                     item["toString"].find("Closed") != -1
                 ):
-                    return (True, None)
-    return (False, None)
+                    return True, None
+    return False, None
 
 
 def resolved(js):
diff --git a/kibble/scanners/scanners/pipermail.py b/kibble/scanners/scanners/pipermail.py
index e361ad2..606a97e 100644
--- a/kibble/scanners/scanners/pipermail.py
+++ b/kibble/scanners/scanners/pipermail.py
@@ -89,7 +89,7 @@ def scan(KibbleBit, source):
             gzurl = "%s/%04u-%s.txt.gz" % (url, year, monthNames[month])
             pd = datetime.date(year, month, 1).timetuple()
             dhash = hashlib.sha224(
-                (("%s %s") % (source["organisation"], gzurl)).encode(
+                ("%s %s" % (source["organisation"], gzurl)).encode(
                     "ascii", errors="replace"
                 )
             ).hexdigest()
@@ -218,7 +218,7 @@ def scan(KibbleBit, source):
                             md = time.strftime("%Y/%m/%d %H:%M:%S", pd)
                             mlhash = hashlib.sha224(
                                 (
-                                    ("%s%s%s%s")
+                                    "%s%s%s%s"
                                     % (
                                         key,
                                         source["sourceURL"],
diff --git a/kibble/scanners/scanners/ponymail-kpe.py b/kibble/scanners/scanners/ponymail-kpe.py
index 478dfba..50edac7 100644
--- a/kibble/scanners/scanners/ponymail-kpe.py
+++ b/kibble/scanners/scanners/ponymail-kpe.py
@@ -115,7 +115,7 @@ def scan(KibbleBit, source):
             KPEs = kpe.azureKPE(KibbleBit, bodies)
         elif "picoapi" in KibbleBit.config:
             KPEs = kpe.picoKPE(KibbleBit, bodies)
-        if KPEs == False:
+        if not KPEs:
             KibbleBit.pprint("Hit rate limit, not trying further emails for now.")
 
         a = 0
diff --git a/kibble/scanners/scanners/ponymail-tone.py b/kibble/scanners/scanners/ponymail-tone.py
index e5cc02b..e0dea32 100644
--- a/kibble/scanners/scanners/ponymail-tone.py
+++ b/kibble/scanners/scanners/ponymail-tone.py
@@ -116,7 +116,7 @@ def scan(KibbleBit, source):
             moods = tone.azureTone(KibbleBit, bodies)
         elif "picoapi" in KibbleBit.config:
             moods = tone.picoTone(KibbleBit, bodies)
-        if moods == False:
+        if not moods:
             KibbleBit.pprint("Hit rate limit, not trying further emails for now.")
 
         a = 0
diff --git a/kibble/scanners/scanners/ponymail.py b/kibble/scanners/scanners/ponymail.py
index 78f0a4a..92a5b0b 100644
--- a/kibble/scanners/scanners/ponymail.py
+++ b/kibble/scanners/scanners/ponymail.py
@@ -148,7 +148,7 @@ def scan(KibbleBit, source):
             "%04u-%02u" % (year, month),
         )
         dhash = hashlib.sha224(
-            (("%s %s") % (source["organisation"], statsurl)).encode(
+            ("%s %s" % (source["organisation"], statsurl)).encode(
                 "ascii", errors="replace"
             )
         ).hexdigest()
@@ -206,7 +206,7 @@ def scan(KibbleBit, source):
                 md = time.strftime("%Y/%m/%d %H:%M:%S", pd)
                 mlhash = hashlib.sha224(
                     (
-                        ("%s%s%s%s")
+                        "%s%s%s%s"
                         % (top[0], source["sourceURL"], source["organisation"], md)
                     ).encode("ascii", errors="replace")
                 ).hexdigest()  # one unique id per month per mail thread
diff --git a/kibble/scanners/scanners/travis.py b/kibble/scanners/scanners/travis.py
index c2157c6..22b4975 100644
--- a/kibble/scanners/scanners/travis.py
+++ b/kibble/scanners/scanners/travis.py
@@ -55,7 +55,7 @@ def scanJob(KibbleBit, source, bid, token, TLD):
     oURL = "https://api.travis-ci.%s/repo/%s/builds" % (TLD, bid)
 
     # For as long as pagination makes sense...
-    while last_page == False:
+    while not last_page:
         bURL = "https://api.travis-ci.%s/repo/%s/builds?limit=100&offset=%u" % (
             TLD,
             bid,
diff --git a/kibble/scanners/scanners/twitter.py b/kibble/scanners/scanners/twitter.py
index de1846f..fd6af5b 100644
--- a/kibble/scanners/scanners/twitter.py
+++ b/kibble/scanners/scanners/twitter.py
@@ -47,9 +47,9 @@ def getFollowers(KibbleBit, source, t):
     no_followers = tuser.followers_count
     d = time.strftime("%Y/%m/%d 0:00:00", time.gmtime())  # Today at midnight
     dhash = hashlib.sha224(
-        (
-            ("twitter:%s:%s:%s") % (source["organisation"], source["sourceURL"], d)
-        ).encode("ascii", errors="replace")
+        ("twitter:%s:%s:%s" % (source["organisation"], source["sourceURL"], d)).encode(
+            "ascii", errors="replace"
+        )
     ).hexdigest()
     jst = {
         "organisation": source["organisation"],
@@ -74,9 +74,9 @@ def getFollowers(KibbleBit, source, t):
 
         # Store twitter follower profile if not already logged
         dhash = hashlib.sha224(
-            (
-                ("twitter:%s:%s:%s") % (source["organisation"], handle, follower.id)
-            ).encode("ascii", errors="replace")
+            ("twitter:%s:%s:%s" % (source["organisation"], handle, follower.id)).encode(
+                "ascii", errors="replace"
+            )
         ).hexdigest()
         if not KibbleBit.exists("twitter_follow", dhash):
             jst = {
diff --git a/kibble/scanners/utils/github.py b/kibble/scanners/utils/github.py
index 1731fdb..251e980 100644
--- a/kibble/scanners/utils/github.py
+++ b/kibble/scanners/utils/github.py
@@ -52,7 +52,9 @@ def get_tokens_left(auth=None):
     return tokens_left
 
 
-def issues(source, params={}, auth=None):
+def issues(source, params=None, auth=None):
+    if params is None:
+        params = {}
     local_params = {"per_page": 100, "page": 1}
     local_params.update(params)
 
@@ -79,7 +81,9 @@ def user(user_url, auth=None):
     return get_limited(user_url, auth=auth)
 
 
-def get_all(source, f, params={}, auth=None):
+def get_all(source, f, params=None, auth=None):
+    if params is None:
+        params = {}
     acc = []
     page = params.get("page", 1)
 
@@ -91,7 +95,7 @@ def get_all(source, f, params={}, auth=None):
 
         acc.extend(items)
 
-        page = page + 1
+        page += 1
         params.update({"page": page})
 
     return acc
diff --git a/kibble/scanners/utils/tone.py b/kibble/scanners/utils/tone.py
index a41bc01..c920b5e 100644
--- a/kibble/scanners/utils/tone.py
+++ b/kibble/scanners/utils/tone.py
@@ -164,20 +164,14 @@ def picoTone(KibbleBit, bodies):
 
         if "results" in jsout and len(jsout["results"]) > 0:
             for doc in jsout["results"]:
-                mood = {}
+                mood = {
+                    "negative": doc["negativity"],
+                    "positive": doc["positivity"],
+                    "neutral": doc["neutrality"],
+                }
 
                 # Sentiment is the overall score, and we use that for the neutrality of a text
 
-                mood["negative"] = doc[
-                    "negativity"
-                ]  # Use the direct Bayesian score from picoAPI
-                mood["positive"] = doc[
-                    "positivity"
-                ]  # Use the direct Bayesian score from picoAPI
-                mood["neutral"] = doc[
-                    "neutrality"
-                ]  # Calc neutrality to favor a middle sentiment score, ignore high/low
-
                 # Additional (optional) emotion weighting
                 if "emotions" in doc:
                     for k, v in doc["emotions"].items():
diff --git a/kibble/scanners/utils/urlmisc.py b/kibble/scanners/utils/urlmisc.py
index e80b9f6..ab0db4f 100644
--- a/kibble/scanners/utils/urlmisc.py
+++ b/kibble/scanners/utils/urlmisc.py
@@ -58,7 +58,6 @@ def unzip(url, creds=None, cookie=None):
             subprocess.check_call(("/usr/bin/wget", "-O", tmpfile.name, url))
 
             try:
-                te
                 compressedFile = open("/tmp/kibbletmp.gz", "rb")
                 if compressedFile.read(2) == "\x1f\x8b":
                     compressedFile.seek(0)