-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogic.py
More file actions
34 lines (27 loc) · 1.01 KB
/
logic.py
File metadata and controls
34 lines (27 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
'''Provides the business logic'''
from operator import itemgetter
def flattenTree(rev, c=[]):
for child in rev.children:
c = c + flattenTree(child, c)
return c + [rev]
def scoreRev(rev):
'''Individual score for a single revision'''
up = rev.votesFor
down = rev.votesAgainst
return (up + 1.) / (up + down + 2.)
def rankRevisions(rev):
'''Takes in revision, hands back ordered list of that revision plus all its children'''
l = flattenTree(rev)
l = zip(map(scoreRev,l),l)
l = sorted(l)[::-1]
return map(itemgetter(1),l)
def scoreThread(revlist):
'''Takes a list of revisions (from one thread) and gives the whole list a score'''
up = sum(map(lambda x: x.votesFor, revlist))
down = sum(map(lambda x: x.votesAgainst, revlist))
return up-down
def rankThreads(threadlist):
'''Takes a list of list of revisions and ranks the outer list by scoreThread'''
l = zip(map(scoreThread, threadlist), threadlist)
l = sorted(l)[::-1]
return map(itemgetter(1),l)