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

gstein pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/steve.git

commit 8de31df41d9bb247953491cdd26451dc74db8bad
Author: Greg Stein <[email protected]>
AuthorDate: Wed Jun 1 20:50:29 2022 -0500

    use sum() to replace a couple loops
---
 monitoring/stv_tool.py | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)

diff --git a/monitoring/stv_tool.py b/monitoring/stv_tool.py
index 22389c1..a946a5b 100755
--- a/monitoring/stv_tool.py
+++ b/monitoring/stv_tool.py
@@ -165,11 +165,7 @@ class CandidateList(object):
       self.l.append(c)
 
   def count(self, state):
-    count = 0
-    for c in self.l:
-      if (c.status & state) != 0:
-        count += 1
-    return count
+    return sum(int((c.status & state) != 0) for c in self.l)
 
   def change_state(self, from_state, to_state):
     any_changed = False
@@ -216,6 +212,7 @@ class CandidateList(object):
       return 1
     return sorted(self.l, key=functools.cmp_to_key(compare))
 
+
 class Candidate(object):
   def __init__(self, name, rand, ahead):
     self.name = name
@@ -238,6 +235,7 @@ class Candidate(object):
     assert quota is not None
     self.weight = (self.weight * quota) / self.vote
 
+
 def iterate_one(quota, votes, candidates, num_seats):
   # assume that: count(ELECTED) < num_seats
   if candidates.count(ELECTED + HOPEFUL) <= num_seats:
@@ -326,10 +324,7 @@ def elect(quota, candidates, num_seats):
 
 
 def calc_surplus(quota, candidates):
-  surplus = 0.0
-  for c in candidates.l:
-    if c.status == ELECTED:
-      surplus += c.vote - quota
+  surplus = sum(c.vote - quota for c in candidates.l if c.status == ELECTED)
   dbg('Total Surplus = %.9f', surplus)
   return surplus
 

Reply via email to