Skip to content

Instantly share code, notes, and snippets.

@csytan
Created November 17, 2010 18:30
Show Gist options
  • Save csytan/703780 to your computer and use it in GitHub Desktop.
Save csytan/703780 to your computer and use it in GitHub Desktop.
class Votable(BaseModel):
points = db.IntegerProperty(default=1)
score = db.FloatProperty()
up_votes = db.StringListProperty() # contains user key_names
down_votes = db.StringListProperty() # contains user key_names
def update_score(self):
"""Adapted from reddit's algorithm
http://code.reddit.com/browser/r2/r2/lib/db/sorts.py?rev=4778b17e939e119417cc5ec25b82c4e9a65621b2
"""
td = self.created - datetime.datetime(1970, 1, 1)
epoch_seconds = td.days * 86400 + td.seconds + (float(td.microseconds) / 1000000)
order = math.log(max(abs(self.points), 1), 10)
sign = 1 if self.points > 0 else -1 if self.points < 0 else 0
seconds = epoch_seconds - 1134028003
self.score = round(order + sign * seconds / 45000, 7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment