Created
February 12, 2017 13:17
-
-
Save snowleung/7aad36f4338c94a137dfb576c5d0f7cd to your computer and use it in GitHub Desktop.
ranking
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# conding:uft-8 | |
class User(object): | |
def __init__(self, _id=None): | |
self._id = _id | |
def score(self, resource, score, frequency=1): | |
resource.scores = resource.scores + score | |
resource.frequency =resource.frequency + frequency | |
class Anonymous(User): | |
def __init__(self): | |
_id = 'Anonymous' | |
User.__init__(self, _id) | |
class Resource(object): | |
def __init__(self, _id, uids=None, types=None): | |
self._id = _id | |
self.uids = uids if uids else [] | |
self.types = types if types else [] | |
self.avg_score = 0 | |
self.scores = 0 | |
self.frequency = 0 | |
def average(self): | |
return self.scores*1.0/self.frequency | |
# Test code | |
# import functools | |
# import random | |
# def randint(x): | |
# return random.randint(0, 5) | |
# if __name__ == '__main__': | |
# anony = Anonymous() | |
# res = Resource('anonymous') | |
# import multiprocessing | |
# p = multiprocessing.Pool(4) | |
# fre = 10000000 | |
# randints = p.map(randint, range(fre)) | |
# for i in randints: | |
# anony.score(res, i) | |
# print(res.average()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment