Advertisement
ruanyf

reddit_algorithm

Mar 6th, 2012
5,239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.69 KB | None | 0 0
  1. #Rewritten code from /r2/r2/lib/db/_sorts.pyx
  2.  
  3. from datetime import datetime, timedelta
  4. from math import log
  5.  
  6. epoch = datetime(1970, 1, 1)
  7.  
  8. def epoch_seconds(date):
  9.     """Returns the number of seconds from the epoch to date."""
  10.     td = date - epoch
  11.     return td.days * 86400 + td.seconds + (float(td.microseconds) / 1000000)
  12.  
  13. def score(ups, downs):
  14.     return ups - downs
  15.  
  16. def hot(ups, downs, date):
  17.     """The hot formula. Should match the equivalent function in postgres."""
  18.     s = score(ups, downs)
  19.     order = log(max(abs(s), 1), 10)
  20.     sign = 1 if s > 0 else -1 if s < 0 else 0
  21.     seconds = epoch_seconds(date) - 1134028003
  22.     return round(order + sign * seconds / 45000, 7)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement