-
-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fs/jquinn (Sourcery refactored) #42
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Due to GitHub API limits, only the first 60 comments can be shown.
Sourcery Code Quality Report✅ Merging this PR will increase code quality in the affected files by 0.32%.
Here are some functions in these files that still need a tune-up:
Legend and ExplanationThe emojis denote the absolute quality of the code:
The 👍 and 👎 indicate whether the quality has improved or gotten worse with this pull request. Please see our documentation here for details on how these metrics are calculated. We are actively working on this report - lots more documentation and extra metrics to come! Help us improve this quality report! |
5b9b687
to
d8b1513
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Due to GitHub API limits, only the first 60 comments can be shown.
for i in range(niter): | ||
for _ in range(niter): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function stratStats
refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore
)
pollErrors = [random.gauss(0, pollingError/2) for i in range(numCands)] | ||
pollErrors = [random.gauss(0, pollingError/2) for _ in range(numCands)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function influentialBlocs
refactored with the following changes:
- Replace unused for index with underscore [×2] (
for-index-underscore
)
self.mNames = [m[0].__name__ + ':' + m[1].__name__ + str(m[2]) for m in ms] | ||
self.mNames = [f'{m[0].__name__}:{m[1].__name__}{str(m[2])}' for m in ms] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function CID.__init__
refactored with the following changes:
- Use f-string instead of string concatenation [×3] (
use-fstring-for-concatenation
)
incentFracts = {re.match(".*(?=:)", name).group(0): data for name, data in incentFracts.items()} | ||
incentFracts = { | ||
re.match(".*(?=:)", name)[0]: data | ||
for name, data in incentFracts.items() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function CID.chart
refactored with the following changes:
- Replace m.group(x) with m[x] for re.Match objects (
use-getitem-for-re-match-groups
)
myFile = open(baseName + (str(i) if newFile else "") + ".csv", "w") | ||
dw = csv.DictWriter(myFile, fields, restval="data missing") | ||
dw.writeheader() | ||
for outcome, index in resultTypeIndices.items(): | ||
for name, results in self.summarize()[index].items(): | ||
row = {'name': name, 'baseResult': outcome} | ||
row.update({i: result for i, result in enumerate(results)}) | ||
row.update(universalInfo) | ||
dw.writerow(row) | ||
myFile.close() | ||
with open(baseName + (str(i) if newFile else "") + ".csv", "w") as myFile: | ||
dw = csv.DictWriter(myFile, fields, restval="data missing") | ||
dw.writeheader() | ||
for outcome, index in resultTypeIndices.items(): | ||
for name, results in self.summarize()[index].items(): | ||
row = ( | ||
{'name': name, 'baseResult': outcome} | ||
| enumerate(results) | ||
| universalInfo | ||
) | ||
dw.writerow(row) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function CID.saveFile
refactored with the following changes:
- Use
with
when opening file to ensure closure (ensure-file-closed
) - Merge dictionary updates via the union operator [×4] (
dict-assign-update-to-union
) - Replace identity comprehension with call to collection constructor (
identity-comprehension
) - Remove a redundant constructor in a dictionary union (
remove-redundant-constructor-in-dict-union
)
results = {c: kw.get(c, None) for c in resultColumns} | ||
results.update(kw) | ||
return results | ||
return {c: kw.get(c, None) for c in resultColumns} | kw |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function makeResults
refactored with the following changes:
- Merge dictionary updates via the union operator [×2] (
dict-assign-update-to-union
) - Inline variable that is immediately returned (
inline-immediately-returned-variable
)
if False and len(polls) < 6: | ||
return tieFor2Probs(polls, uncertainty) | ||
else: | ||
return tieFor2Estimate(tuple(pollsToProbs(polls, uncertainty))) | ||
return tieFor2Estimate(tuple(pollsToProbs(polls, uncertainty))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function adaptiveTieFor2
refactored with the following changes:
- Remove redundant conditional (
remove-redundant-if
)
dataClasses.py
Outdated
print("# " + str(globalComment), | ||
#dict( | ||
#media=self.media.__name__, | ||
# version=self.repo_version, | ||
# seed=self.seed, | ||
## model=self.model, | ||
# methods=self.methods, | ||
# nvot=self.nvot, | ||
# ncand=self.ncand, | ||
# niter=self.niter)), | ||
file=myFile) | ||
print(f"# {str(globalComment)}", file=myFile) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function appendResults
refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation
)
This removes the following comments ( why? ):
#media=self.media.__name__,
# niter=self.niter)),
#dict(
## model=self.model,
# nvot=self.nvot,
# version=self.repo_version,
# seed=self.seed,
# methods=self.methods,
# ncand=self.ncand,
Top2Version.__name__ = noRunoffMethod.__name__ + "Top2" | ||
Top2Version.__name__ = f"{noRunoffMethod.__name__}Top2" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function top2
refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation
)
methods.py
Outdated
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function makeScoreMethod
refactored with the following changes:
- Simplify generator expression (
simplify-generator
)
methods.py
Outdated
expectedUtility = sum(u for u in utils)/len(utils) | ||
expectedUtility = sum(utils) / len(utils) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Approval.zeroInfoBallot
refactored with the following changes:
- Simplify generator expression (
simplify-generator
)
methods.py
Outdated
#minF, maxF = min(winner, runnerUp), max(winner, runnerUp) | ||
#thirdProbs = pollsToProbs( | ||
#electabilities[:minF] + electabilities[minF+1:maxF] + electabilities[maxF+1:], pollingUncertainty) | ||
for third in range(nCands): | ||
if third == winner or third == runnerUp: | ||
if third in [winner, runnerUp]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function getCoefficients
refactored with the following changes:
- Replace multiple comparisons of same variable with
in
operator (merge-comparisons
)
This removes the following comments ( why? ):
#thirdProbs = pollsToProbs(
#electabilities[:minF] + electabilities[minF+1:maxF] + electabilities[maxF+1:], pollingUncertainty)
#minF, maxF = min(winner, runnerUp), max(winner, runnerUp)
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function makeSTARMethod
refactored with the following changes:
- Replace if statement with if expression [×5] (
assign-if-exp
) - Remove redundant conditional [×2] (
remove-redundant-if
) - Lift code into else after jump in control flow (
reintroduce-else
) - Use f-string instead of string concatenation (
use-fstring-for-concatenation
)
def candScore(self, scores): | ||
def candScore(cls, scores): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Mav.candScore
refactored with the following changes:
- The first argument to class methods should be
cls
(class-method-first-arg-name
)
methods.py
Outdated
((frontId,frontResult), (targId, targResult)) = places[0:2] | ||
((frontId,frontResult), (targId, targResult)) = places[:2] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Mav.stratBallot
refactored with the following changes:
- Replace a[0:x] with a[:x] and a[x:len(a)] with a[x:] (
remove-redundant-slice-index
) - Remove unnecessary else after guard condition (
remove-unnecessary-else
)
matchupWins = [sum(1 for marg in row if marg > 0) for row in cmat] | ||
matchupWins = [sum(marg > 0 for marg in row) for row in cmat] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function RankedRobin.results
refactored with the following changes:
- Simplify constant sum() call (
simplify-constant-sum
) - Remove unnecessary casts to int, str, float or bool (
remove-unnecessary-cast
)
for i, b in enumerate(ballots): | ||
for b in ballots: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function ITRV.results
refactored with the following changes:
- Remove unnecessary calls to
enumerate
when the index is not used (remove-unused-enumerate
)
@@ -2,6 +2,7 @@ | |||
from methods import * | |||
|
|||
def makeBlock(method): | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function makeBlock
refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation
)
scoreSupport = 0 | ||
for ballot in ballots: | ||
if ballot[winner] == score: | ||
scoreSupport += ballot.weight | ||
scoreSupport = sum( | ||
ballot.weight for ballot in ballots if ballot[winner] == score | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function AllocatedScore.reweight
refactored with the following changes:
- Convert for loop into call to sum() (
sum-comprehension
)
if upset > 0: | ||
return runnerUp | ||
else: return top | ||
return runnerUp if upset > 0 else top |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function ASFinalRunoff.pickWinner
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
)
if upset > 0: | ||
return runnerUp | ||
else: return top | ||
return runnerUp if upset > 0 else top |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function ASRunoffs.pickWinner
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
)
else: | ||
scores = [sum(ballot[c]*ballot.weight for ballot in ballots) if c in bestCands else -1 for c in range(ncand)] | ||
return scores.index(max(scores)) | ||
scores = [sum(ballot[c]*ballot.weight for ballot in ballots) if c in bestCands else -1 for c in range(ncand)] | ||
return scores.index(max(scores)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function SequentialMonroe.pickWinner
refactored with the following changes:
- Remove unnecessary else after guard condition (
remove-unnecessary-else
)
scoreSupport = 0 | ||
for ballot in ballots: | ||
if ballot[winner] == score: | ||
scoreSupport += score*ballot.weight | ||
scoreSupport = sum( | ||
score * ballot.weight | ||
for ballot in ballots | ||
if ballot[winner] == score | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function S5H.reweight
refactored with the following changes:
- Convert for loop into call to sum() (
sum-comprehension
)
piles = [[] for i in range(nCands)] | ||
piles = [[] for _ in range(nCands)] | ||
while len(winners) + len(candsLeft) > numWinners: | ||
cls.resort(ballotsToSort, candsLeft, piles) | ||
newWinners = [c for c in candsLeft if sum(b.weight for b in piles[c]) >= quota] | ||
if newWinners: | ||
if newWinners := [ | ||
c for c in candsLeft if sum(b.weight for b in piles[c]) >= quota | ||
]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function STV.winnerSet
refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore
) - Use named expression to simplify assignment and conditional (
use-named-expression
) - Replace a for append loop with list extend (
for-append-to-extend
) - Simplify generator expression (
simplify-generator
)
piles = [[] for i in range(nCands)] | ||
piles = [[] for _ in range(nCands)] | ||
while len(winners) < numWinners - 1 and len(winners) + len(candsLeft) > numWinners - 1: | ||
cls.resort(ballotsToSort, candsLeft, piles) | ||
newWinners = [c for c in candsLeft if sum(b.weight for b in piles[c]) >= quota][:max(0, numWinners-len(winners)-1)] | ||
if newWinners: | ||
if newWinners := [ | ||
c for c in candsLeft if sum(b.weight for b in piles[c]) >= quota | ||
][: max(0, numWinners - len(winners) - 1)]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function MinimaxSTV.winnerSet
refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore
) - Use named expression to simplify assignment and conditional (
use-named-expression
) - Replace a for append loop with list extend (
for-append-to-extend
) - Simplify generator expression (
simplify-generator
)
if callable(biaser): | ||
bias = biaser(standings) | ||
else: | ||
bias = biaser | ||
result= (standings[0:2] + | ||
[(standing - bias + numerator * (bias / max(i+2, 1))) | ||
for i, standing in enumerate(standings[2:])]) | ||
bias = biaser(standings) if callable(biaser) else biaser | ||
result = standings[:2] + [ | ||
(standing - bias + numerator * (bias / max(i + 2, 1))) | ||
for i, standing in enumerate(standings[2:]) | ||
] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function biasedMediaFor
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
) - Replace a[0:x] with a[:x] and a[x:len(a)] with a[x:] (
remove-redundant-slice-index
)
if callable(biaser): | ||
bias = biaser(standings) | ||
else: | ||
bias = biaser | ||
bias = biaser(standings) if callable(biaser) else biaser |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function skewedMediaFor
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
)
if caring==None: | ||
if caring is None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function DimVoter.fromDims
refactored with the following changes:
- Use x is None rather than x == None (
none-compare
)
subclusterMeans.append([random.gauss(0,sqrt(cares)) for i in range(self.dimsPerView[c])]) | ||
subclusterMeans.append( | ||
[ | ||
random.gauss(0, sqrt(cares)) | ||
for _ in range(self.dimsPerView[c]) | ||
] | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function KSElectorate.chooseClusters
refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore
)
fgHelped = [] | ||
fgHarmed = [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function threeRoundResults
refactored with the following changes:
- Hoist statements out of for/while loops (
hoist-statement-from-loop
) - Replace if statement with if expression (
assign-if-exp
) - Merge dictionary updates via the union operator (
dict-assign-update-to-union
)
This removes the following comments ( why? ):
#If not I should be quitting earlier than this but easier to just fake it.
#zero-sized foreground
d8b1513
to
278cea6
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Due to GitHub API limits, only the first 60 comments can be shown.
for i in range(niter): | ||
for _ in range(niter): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function stratStats
refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore
)
pollErrors = [random.gauss(0, pollingError/2) for i in range(numCands)] | ||
pollErrors = [random.gauss(0, pollingError/2) for _ in range(numCands)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function influentialBlocs
refactored with the following changes:
- Replace unused for index with underscore [×2] (
for-index-underscore
)
self.mNames = [m[0].__name__ + ':' + m[1].__name__ + str(m[2]) for m in ms] | ||
self.mNames = [f'{m[0].__name__}:{m[1].__name__}{str(m[2])}' for m in ms] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function CID.__init__
refactored with the following changes:
- Use f-string instead of string concatenation [×3] (
use-fstring-for-concatenation
)
incentFracts = {re.match(".*(?=:)", name).group(0): data for name, data in incentFracts.items()} | ||
incentFracts = { | ||
re.match(".*(?=:)", name)[0]: data | ||
for name, data in incentFracts.items() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function CID.chart
refactored with the following changes:
- Replace m.group(x) with m[x] for re.Match objects (
use-getitem-for-re-match-groups
)
myFile = open(baseName + (str(i) if newFile else "") + ".csv", "w") | ||
dw = csv.DictWriter(myFile, fields, restval="data missing") | ||
dw.writeheader() | ||
for outcome, index in resultTypeIndices.items(): | ||
for name, results in self.summarize()[index].items(): | ||
row = {'name': name, 'baseResult': outcome} | ||
row.update({i: result for i, result in enumerate(results)}) | ||
row.update(universalInfo) | ||
dw.writerow(row) | ||
myFile.close() | ||
with open(baseName + (str(i) if newFile else "") + ".csv", "w") as myFile: | ||
dw = csv.DictWriter(myFile, fields, restval="data missing") | ||
dw.writeheader() | ||
for outcome, index in resultTypeIndices.items(): | ||
for name, results in self.summarize()[index].items(): | ||
row = ( | ||
{'name': name, 'baseResult': outcome} | ||
| enumerate(results) | ||
| universalInfo | ||
) | ||
dw.writerow(row) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function CID.saveFile
refactored with the following changes:
- Use
with
when opening file to ensure closure (ensure-file-closed
) - Merge dictionary updates via the union operator [×4] (
dict-assign-update-to-union
) - Replace identity comprehension with call to collection constructor (
identity-comprehension
) - Remove a redundant constructor in a dictionary union (
remove-redundant-constructor-in-dict-union
)
results = {c: kw.get(c, None) for c in resultColumns} | ||
results.update(kw) | ||
return results | ||
return {c: kw.get(c, None) for c in resultColumns} | kw |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function makeResults
refactored with the following changes:
- Merge dictionary updates via the union operator [×2] (
dict-assign-update-to-union
) - Inline variable that is immediately returned (
inline-immediately-returned-variable
)
if False and len(polls) < 6: | ||
return tieFor2Probs(polls, uncertainty) | ||
else: | ||
return tieFor2Estimate(tuple(pollsToProbs(polls, uncertainty))) | ||
return tieFor2Estimate(tuple(pollsToProbs(polls, uncertainty))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function adaptiveTieFor2
refactored with the following changes:
- Remove redundant conditional (
remove-redundant-if
)
print( | ||
"# " + str(globalComment), | ||
# dict( | ||
# media=self.media.__name__, | ||
# version=self.repo_version, | ||
# seed=self.seed, | ||
## model=self.model, | ||
# methods=self.methods, | ||
# nvot=self.nvot, | ||
# ncand=self.ncand, | ||
# niter=self.niter)), | ||
file=myFile, | ||
) | ||
print(f"# {str(globalComment)}", file=myFile) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function appendResults
refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation
)
This removes the following comments ( why? ):
# version=self.repo_version,
# seed=self.seed,
# media=self.media.__name__,
# niter=self.niter)),
# dict(
# nvot=self.nvot,
# ncand=self.ncand,
# methods=self.methods,
## model=self.model,
Top2Version.__name__ = noRunoffMethod.__name__ + "Top2" | ||
Top2Version.__name__ = f"{noRunoffMethod.__name__}Top2" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function top2
refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation
)
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function makeScoreMethod
refactored with the following changes:
- Simplify generator expression (
simplify-generator
)
expectedUtility = sum(u for u in utils) / len(utils) | ||
expectedUtility = sum(utils) / len(utils) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Approval.zeroInfoBallot
refactored with the following changes:
- Simplify generator expression (
simplify-generator
)
if third == winner or third == runnerUp: | ||
if third in [winner, runnerUp]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function getCoefficients
refactored with the following changes:
- Replace multiple comparisons of same variable with
in
operator (merge-comparisons
)
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function makeSTARMethod
refactored with the following changes:
- Replace if statement with if expression [×5] (
assign-if-exp
) - Remove redundant conditional [×2] (
remove-redundant-if
) - Lift code into else after jump in control flow (
reintroduce-else
) - Use f-string instead of string concatenation (
use-fstring-for-concatenation
)
def candScore(self, scores): | ||
def candScore(cls, scores): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Mav.candScore
refactored with the following changes:
- The first argument to class methods should be
cls
(class-method-first-arg-name
)
((frontId, frontResult), (targId, targResult)) = places[0:2] | ||
((frontId, frontResult), (targId, targResult)) = places[:2] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Mav.stratBallot
refactored with the following changes:
- Replace a[0:x] with a[:x] and a[x:len(a)] with a[x:] (
remove-redundant-slice-index
) - Remove unnecessary else after guard condition (
remove-unnecessary-else
)
matchupWins = [sum(1 for marg in row if marg > 0) for row in cmat] | ||
matchupWins = [sum(marg > 0 for marg in row) for row in cmat] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function RankedRobin.results
refactored with the following changes:
- Simplify constant sum() call (
simplify-constant-sum
) - Remove unnecessary casts to int, str, float or bool (
remove-unnecessary-cast
)
for i, b in enumerate(ballots): | ||
for b in ballots: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function ITRV.results
refactored with the following changes:
- Remove unnecessary calls to
enumerate
when the index is not used (remove-unused-enumerate
)
@@ -2,6 +2,7 @@ | |||
from methods import * | |||
|
|||
def makeBlock(method): | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function makeBlock
refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation
)
scoreSupport = 0 | ||
for ballot in ballots: | ||
if ballot[winner] == score: | ||
scoreSupport += ballot.weight | ||
scoreSupport = sum( | ||
ballot.weight for ballot in ballots if ballot[winner] == score | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function AllocatedScore.reweight
refactored with the following changes:
- Convert for loop into call to sum() (
sum-comprehension
)
if upset > 0: | ||
return runnerUp | ||
else: return top | ||
return runnerUp if upset > 0 else top |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function ASFinalRunoff.pickWinner
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
)
if upset > 0: | ||
return runnerUp | ||
else: return top | ||
return runnerUp if upset > 0 else top |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function ASRunoffs.pickWinner
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
)
else: | ||
scores = [sum(ballot[c]*ballot.weight for ballot in ballots) if c in bestCands else -1 for c in range(ncand)] | ||
return scores.index(max(scores)) | ||
scores = [sum(ballot[c]*ballot.weight for ballot in ballots) if c in bestCands else -1 for c in range(ncand)] | ||
return scores.index(max(scores)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function SequentialMonroe.pickWinner
refactored with the following changes:
- Remove unnecessary else after guard condition (
remove-unnecessary-else
)
scoreSupport = 0 | ||
for ballot in ballots: | ||
if ballot[winner] == score: | ||
scoreSupport += score*ballot.weight | ||
scoreSupport = sum( | ||
score * ballot.weight | ||
for ballot in ballots | ||
if ballot[winner] == score | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function S5H.reweight
refactored with the following changes:
- Convert for loop into call to sum() (
sum-comprehension
)
piles = [[] for i in range(nCands)] | ||
piles = [[] for _ in range(nCands)] | ||
while len(winners) + len(candsLeft) > numWinners: | ||
cls.resort(ballotsToSort, candsLeft, piles) | ||
newWinners = [c for c in candsLeft if sum(b.weight for b in piles[c]) >= quota] | ||
if newWinners: | ||
if newWinners := [ | ||
c for c in candsLeft if sum(b.weight for b in piles[c]) >= quota | ||
]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function STV.winnerSet
refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore
) - Use named expression to simplify assignment and conditional (
use-named-expression
) - Replace a for append loop with list extend (
for-append-to-extend
) - Simplify generator expression (
simplify-generator
)
piles = [[] for i in range(nCands)] | ||
piles = [[] for _ in range(nCands)] | ||
while len(winners) < numWinners - 1 and len(winners) + len(candsLeft) > numWinners - 1: | ||
cls.resort(ballotsToSort, candsLeft, piles) | ||
newWinners = [c for c in candsLeft if sum(b.weight for b in piles[c]) >= quota][:max(0, numWinners-len(winners)-1)] | ||
if newWinners: | ||
if newWinners := [ | ||
c for c in candsLeft if sum(b.weight for b in piles[c]) >= quota | ||
][: max(0, numWinners - len(winners) - 1)]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function MinimaxSTV.winnerSet
refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore
) - Use named expression to simplify assignment and conditional (
use-named-expression
) - Replace a for append loop with list extend (
for-append-to-extend
) - Simplify generator expression (
simplify-generator
)
if callable(biaser): | ||
bias = biaser(standings) | ||
else: | ||
bias = biaser | ||
result= (standings[0:2] + | ||
[(standing - bias + numerator * (bias / max(i+2, 1))) | ||
for i, standing in enumerate(standings[2:])]) | ||
bias = biaser(standings) if callable(biaser) else biaser | ||
result = standings[:2] + [ | ||
(standing - bias + numerator * (bias / max(i + 2, 1))) | ||
for i, standing in enumerate(standings[2:]) | ||
] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function biasedMediaFor
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
) - Replace a[0:x] with a[:x] and a[x:len(a)] with a[x:] (
remove-redundant-slice-index
)
if callable(biaser): | ||
bias = biaser(standings) | ||
else: | ||
bias = biaser | ||
bias = biaser(standings) if callable(biaser) else biaser |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function skewedMediaFor
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
)
if caring==None: | ||
if caring is None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function DimVoter.fromDims
refactored with the following changes:
- Use x is None rather than x == None (
none-compare
)
subclusterMeans.append([random.gauss(0,sqrt(cares)) for i in range(self.dimsPerView[c])]) | ||
subclusterMeans.append( | ||
[ | ||
random.gauss(0, sqrt(cares)) | ||
for _ in range(self.dimsPerView[c]) | ||
] | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function KSElectorate.chooseClusters
refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore
)
fgHelped = [] | ||
fgHarmed = [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function threeRoundResults
refactored with the following changes:
- Hoist statements out of for/while loops (
hoist-statement-from-loop
) - Replace if statement with if expression (
assign-if-exp
) - Merge dictionary updates via the union operator (
dict-assign-update-to-union
)
This removes the following comments ( why? ):
#If not I should be quitting earlier than this but easier to just fake it.
#zero-sized foreground
Pull Request #41 refactored by Sourcery.
If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.
NOTE: As code is pushed to the original Pull Request, Sourcery will
re-run and update (force-push) this Pull Request with new refactorings as
necessary. If Sourcery finds no refactorings at any point, this Pull Request
will be closed automatically.
See our documentation here.
Run Sourcery locally
Reduce the feedback loop during development by using the Sourcery editor plugin:
Review changes via command line
To manually merge these changes, make sure you're on the
fs/jquinn
branch, then run:Help us improve this pull request!