|
15 | 15 |
|
16 | 16 |
|
17 | 17 | class User:
|
18 |
| - def __init__(self, uid): |
19 |
| - self.uid = uid |
20 |
| - self.rec = [] # recommendations, list of iid |
| 18 | + def __init__(self, uid): |
| 19 | + self.uid = uid |
| 20 | + self.rec = [] # recommendations, list of iid |
21 | 21 |
|
22 |
| - def __str__(self): |
23 |
| - return "User[uid=%s,rec=%s]" % (self.uid, self.rec) |
| 22 | + def __str__(self): |
| 23 | + return "User[uid=%s,rec=%s]" % (self.uid, self.rec) |
24 | 24 |
|
25 | 25 | class Item:
|
26 |
| - def __init__(self, iid, name): |
27 |
| - self.iid = iid |
28 |
| - self.name = name |
| 26 | + def __init__(self, iid, name): |
| 27 | + self.iid = iid |
| 28 | + self.name = name |
29 | 29 |
|
30 |
| - def __str__(self): |
31 |
| - return "Item[iid=%s,name=%s]" % (self.iid, self.name) |
| 30 | + def __str__(self): |
| 31 | + return "Item[iid=%s,name=%s]" % (self.iid, self.name) |
32 | 32 |
|
33 | 33 | class RateAction:
|
34 |
| - def __init__(self, uid, iid, rating, t): |
35 |
| - self.uid = uid |
36 |
| - self.iid = iid |
37 |
| - self.rating = rating |
38 |
| - self.t = t |
| 34 | + def __init__(self, uid, iid, rating, t): |
| 35 | + self.uid = uid |
| 36 | + self.iid = iid |
| 37 | + self.rating = rating |
| 38 | + self.t = t |
39 | 39 |
|
40 |
| - def __str__(self): |
41 |
| - return "RateAction[uid=%s,iid=%s,rating=%s,t=%s]" % (self.uid, self.iid, self.rating, self.t) |
| 40 | + def __str__(self): |
| 41 | + return "RateAction[uid=%s,iid=%s,rating=%s,t=%s]" % (self.uid, self.iid, self.rating, self.t) |
42 | 42 |
|
43 | 43 |
|
44 | 44 | class AppData:
|
45 | 45 |
|
46 |
| - def __init__(self): |
47 |
| - self._users = {} # dict of User obj |
48 |
| - self._items = {} # dict of Item obj |
49 |
| - self._rate_actions = [] # list of RateAction obj |
50 |
| - |
51 |
| - self._users_file = "%s/%s" % (APPDATA_DIRNAME, USERS_FILENAME) |
52 |
| - self._items_file = "%s/%s" % (APPDATA_DIRNAME, ITEMS_FILENAME) |
53 |
| - self._rate_actions_file = "%s/%s" % (APPDATA_DIRNAME, RATE_ACTIONS_FILENAME) |
54 |
| - self.__init_users() |
55 |
| - self.__init_items() |
56 |
| - self.__init_rate_actions() |
57 |
| - |
58 |
| - def __init_users(self): |
59 |
| - """ |
60 |
| - uid| |
61 |
| - """ |
62 |
| - print "[Info] Initializing users..." |
63 |
| - f = open(self._users_file, 'r') |
64 |
| - for line in f: |
65 |
| - data = line.rstrip('\r\n').split(USERS_FILE_DELIMITER) |
66 |
| - self.add_user(User(data[0])) |
67 |
| - f.close() |
68 |
| - print "[Info] %s users were initialized." % len(self._users) |
69 |
| - |
70 |
| - def __init_items(self): |
71 |
| - """ |
72 |
| - iid|name |
73 |
| - """ |
74 |
| - print "[Info] Initializing items..." |
75 |
| - f = open(self._items_file, 'r') |
76 |
| - for line in f: |
77 |
| - data = line.rstrip('\r\n').split(ITEMS_FILE_DELIMITER) |
78 |
| - self.add_item(Item(data[0], data[1])) |
79 |
| - f.close() |
80 |
| - print "[Info] %s items were initialized." % len(self._items) |
81 |
| - |
82 |
| - def __init_rate_actions(self): |
83 |
| - """ |
84 |
| - uid|iid|rating|timestamp |
85 |
| - """ |
86 |
| - print "[Info] Initializing rate actions..." |
87 |
| - f = open(self._rate_actions_file, 'r') |
88 |
| - for line in f: |
89 |
| - data = line.rstrip('\r\n').split(RATE_ACTIONS_DELIMITER) |
90 |
| - t = datetime.datetime.utcfromtimestamp(int(data[3])).isoformat() |
91 |
| - self.add_rate_action(RateAction(data[0], data[1], data[2], t)) |
92 |
| - f.close() |
93 |
| - print "[Info] %s rate actions were initialized." % len(self._rate_actions) |
94 |
| - |
95 |
| - def add_user(self, user): |
96 |
| - self._users[user.uid] = user |
97 |
| - |
98 |
| - def add_item(self, item): |
99 |
| - self._items[item.iid] = item |
100 |
| - |
101 |
| - def add_rate_action(self, action): |
102 |
| - self._rate_actions.append(action) |
103 |
| - |
104 |
| - def get_users(self): |
105 |
| - return self._users |
106 |
| - |
107 |
| - def get_items(self): |
108 |
| - return self._items |
109 |
| - |
110 |
| - def get_rate_actions(self): |
111 |
| - return self._rate_actions |
112 |
| - |
113 |
| - def get_user(self, uid): |
114 |
| - """return single user |
115 |
| - """ |
116 |
| - if uid in self._users: |
117 |
| - return self._users[uid] |
118 |
| - else: |
119 |
| - return None |
120 |
| - |
121 |
| - def get_item(self, iid): |
122 |
| - """return single item |
123 |
| - """ |
124 |
| - if iid in self._items: |
125 |
| - return self._items[iid] |
126 |
| - else: |
127 |
| - return None |
128 |
| - |
129 |
| - def get_top_rated_items(self, uid, n): |
130 |
| - """get top n rated iids by this uid |
131 |
| - """ |
132 |
| - if uid in self._users: |
133 |
| - actions = filter(lambda u: u.uid==uid, self._rate_actions) |
134 |
| - top = sorted(actions, key=attrgetter('rating'), reverse=True) |
135 |
| - topn_iids = map(lambda a: a.iid, top[:n]) |
136 |
| - return topn_iids |
137 |
| - else: |
138 |
| - return None |
139 |
| - |
140 |
| - def get_top_rate_actions(self, uid, n): |
141 |
| - """get top n rated actions by this uid |
142 |
| - """ |
143 |
| - if uid in self._users: |
144 |
| - actions = filter(lambda u: u.uid==uid, self._rate_actions) |
145 |
| - top = sorted(actions, key=attrgetter('rating'), reverse=True) |
146 |
| - return top[:n] |
147 |
| - else: |
148 |
| - return None |
| 46 | + def __init__(self): |
| 47 | + self._users = {} # dict of User obj |
| 48 | + self._items = {} # dict of Item obj |
| 49 | + self._rate_actions = [] # list of RateAction obj |
| 50 | + |
| 51 | + self._users_file = "%s/%s" % (APPDATA_DIRNAME, USERS_FILENAME) |
| 52 | + self._items_file = "%s/%s" % (APPDATA_DIRNAME, ITEMS_FILENAME) |
| 53 | + self._rate_actions_file = "%s/%s" % (APPDATA_DIRNAME, RATE_ACTIONS_FILENAME) |
| 54 | + self.__init_users() |
| 55 | + self.__init_items() |
| 56 | + self.__init_rate_actions() |
| 57 | + |
| 58 | + def __init_users(self): |
| 59 | + """ |
| 60 | + uid| |
| 61 | + """ |
| 62 | + print "[Info] Initializing users..." |
| 63 | + f = open(self._users_file, 'r') |
| 64 | + for line in f: |
| 65 | + data = line.rstrip('\r\n').split(USERS_FILE_DELIMITER) |
| 66 | + self.add_user(User(data[0])) |
| 67 | + f.close() |
| 68 | + print "[Info] %s users were initialized." % len(self._users) |
| 69 | + |
| 70 | + def __init_items(self): |
| 71 | + """ |
| 72 | + iid|name |
| 73 | + """ |
| 74 | + print "[Info] Initializing items..." |
| 75 | + f = open(self._items_file, 'r') |
| 76 | + for line in f: |
| 77 | + data = line.rstrip('\r\n').split(ITEMS_FILE_DELIMITER) |
| 78 | + self.add_item(Item(data[0], data[1])) |
| 79 | + f.close() |
| 80 | + print "[Info] %s items were initialized." % len(self._items) |
| 81 | + |
| 82 | + def __init_rate_actions(self): |
| 83 | + """ |
| 84 | + uid|iid|rating|timestamp |
| 85 | + """ |
| 86 | + print "[Info] Initializing rate actions..." |
| 87 | + f = open(self._rate_actions_file, 'r') |
| 88 | + for line in f: |
| 89 | + data = line.rstrip('\r\n').split(RATE_ACTIONS_DELIMITER) |
| 90 | + t = datetime.datetime.utcfromtimestamp(int(data[3])).isoformat() |
| 91 | + self.add_rate_action(RateAction(data[0], data[1], data[2], t)) |
| 92 | + f.close() |
| 93 | + print "[Info] %s rate actions were initialized." % len(self._rate_actions) |
| 94 | + |
| 95 | + def add_user(self, user): |
| 96 | + self._users[user.uid] = user |
| 97 | + |
| 98 | + def add_item(self, item): |
| 99 | + self._items[item.iid] = item |
| 100 | + |
| 101 | + def add_rate_action(self, action): |
| 102 | + self._rate_actions.append(action) |
| 103 | + |
| 104 | + def get_users(self): |
| 105 | + return self._users |
| 106 | + |
| 107 | + def get_items(self): |
| 108 | + return self._items |
| 109 | + |
| 110 | + def get_rate_actions(self): |
| 111 | + return self._rate_actions |
| 112 | + |
| 113 | + def get_user(self, uid): |
| 114 | + """return single user |
| 115 | + """ |
| 116 | + if uid in self._users: |
| 117 | + return self._users[uid] |
| 118 | + else: |
| 119 | + return None |
| 120 | + |
| 121 | + def get_item(self, iid): |
| 122 | + """return single item |
| 123 | + """ |
| 124 | + if iid in self._items: |
| 125 | + return self._items[iid] |
| 126 | + else: |
| 127 | + return None |
| 128 | + |
| 129 | + def get_top_rated_items(self, uid, n): |
| 130 | + """get top n rated iids by this uid |
| 131 | + """ |
| 132 | + if uid in self._users: |
| 133 | + actions = filter(lambda u: u.uid==uid, self._rate_actions) |
| 134 | + top = sorted(actions, key=attrgetter('rating'), reverse=True) |
| 135 | + topn_iids = map(lambda a: a.iid, top[:n]) |
| 136 | + return topn_iids |
| 137 | + else: |
| 138 | + return None |
| 139 | + |
| 140 | + def get_top_rate_actions(self, uid, n): |
| 141 | + """get top n rated actions by this uid |
| 142 | + """ |
| 143 | + if uid in self._users: |
| 144 | + actions = filter(lambda u: u.uid==uid, self._rate_actions) |
| 145 | + top = sorted(actions, key=attrgetter('rating'), reverse=True) |
| 146 | + return top[:n] |
| 147 | + else: |
| 148 | + return None |
0 commit comments