Skip to content

Commit bea38fa

Browse files
committed
Fix pylint trivial bug for sdk and example code.
1. Unify indent to 2 spaces, except test dir. 2. Add module structure to example code, can use repo code directly.
1 parent 35f11fb commit bea38fa

File tree

10 files changed

+1601
-1586
lines changed

10 files changed

+1601
-1586
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
.project
33
.pydevproject
44
.settings
5+
*.swp
56
build/
67
dist/
78
MANIFEST

examples/__init__.py

Whitespace-only changes.

examples/itemrec/__init__.py

Whitespace-only changes.

examples/itemrec/movies/__init__.py

Whitespace-only changes.

examples/itemrec/movies/app_config.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
2-
APP_KEY = 'wWKeyokhpwEgis9BHCHkwqB7Hcop0OEF6KzkZWXfz2uMJYjl2QDwNlFVGl5hhHfy'
1+
APP_KEY = 'uJKTKyUAFNZYQQO5yxkdrSo3XIlaf9LXejI63CWE0mtZVEYF89hyVtOwpMKfXXXX'
32
API_URL = 'http://localhost:8000'
4-

examples/itemrec/movies/appdata.py

Lines changed: 120 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -15,134 +15,134 @@
1515

1616

1717
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
2121

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)
2424

2525
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
2929

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)
3232

3333
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
3939

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)
4242

4343

4444
class AppData:
4545

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
Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
from appdata import AppData
32
import predictionio
43
import sys
@@ -7,60 +6,60 @@
76

87
def batch_import_task(app_data, client, all_info=False):
98

10-
print "[Info] Importing users to PredictionIO..."
11-
count = 0
12-
for k, v in app_data.get_users().iteritems():
13-
count += 1
14-
if all_info:
15-
print "[Info] Importing %s..." % v
16-
else:
17-
if (count % 32 == 0):
18-
sys.stdout.write('\r[Info] %s' % count)
19-
sys.stdout.flush()
9+
print "[Info] Importing users to PredictionIO..."
10+
count = 0
11+
for k, v in app_data.get_users().iteritems():
12+
count += 1
13+
if all_info:
14+
print "[Info] Importing %s..." % v
15+
else:
16+
if (count % 32 == 0):
17+
sys.stdout.write('\r[Info] %s' % count)
18+
sys.stdout.flush()
2019

21-
client.create_user(v.uid)
22-
23-
sys.stdout.write('\r[Info] %s users were imported.\n' % count)
24-
sys.stdout.flush()
20+
client.create_user(v.uid)
21+
22+
sys.stdout.write('\r[Info] %s users were imported.\n' % count)
23+
sys.stdout.flush()
2524

26-
print "[Info] Importing items to PredictionIO..."
27-
count = 0
28-
for k, v in app_data.get_items().iteritems():
29-
count += 1
30-
if all_info:
31-
print "[Info] Importing %s..." % v
32-
else:
33-
if (count % 32 == 0):
34-
sys.stdout.write('\r[Info] %s' % count)
35-
sys.stdout.flush()
25+
print "[Info] Importing items to PredictionIO..."
26+
count = 0
27+
for k, v in app_data.get_items().iteritems():
28+
count += 1
29+
if all_info:
30+
print "[Info] Importing %s..." % v
31+
else:
32+
if (count % 32 == 0):
33+
sys.stdout.write('\r[Info] %s' % count)
34+
sys.stdout.flush()
3635

37-
client.create_item(v.iid, ("movie",))
38-
39-
sys.stdout.write('\r[Info] %s items were imported.\n' % count)
40-
sys.stdout.flush()
36+
client.create_item(v.iid, ("movie",))
37+
38+
sys.stdout.write('\r[Info] %s items were imported.\n' % count)
39+
sys.stdout.flush()
4140

42-
print "[Info] Importing rate actions to PredictionIO..."
43-
count = 0
44-
for v in app_data.get_rate_actions():
45-
count += 1
46-
if all_info:
47-
print "[Info] Importing %s..." % v
48-
else:
49-
if (count % 32 == 0):
50-
sys.stdout.write('\r[Info] %s' % count)
51-
sys.stdout.flush()
41+
print "[Info] Importing rate actions to PredictionIO..."
42+
count = 0
43+
for v in app_data.get_rate_actions():
44+
count += 1
45+
if all_info:
46+
print "[Info] Importing %s..." % v
47+
else:
48+
if (count % 32 == 0):
49+
sys.stdout.write('\r[Info] %s' % count)
50+
sys.stdout.flush()
5251

53-
client.identify(v.uid)
54-
client.record_action_on_item("rate", v.iid, { "pio_rate": v.rating, "pio_t": v.t })
52+
client.identify(v.uid)
53+
client.record_action_on_item("rate", v.iid, { "pio_rate": v.rating, "pio_t": v.t })
5554

56-
sys.stdout.write('\r[Info] %s rate actions were imported.\n' % count)
57-
sys.stdout.flush()
55+
sys.stdout.write('\r[Info] %s rate actions were imported.\n' % count)
56+
sys.stdout.flush()
5857

5958

6059
if __name__ == '__main__':
6160

62-
app_data = AppData()
63-
client = predictionio.Client(APP_KEY, 1, API_URL)
64-
batch_import_task(app_data, client)
65-
client.close()
61+
app_data = AppData()
62+
client = predictionio.Client(APP_KEY, 1, API_URL)
63+
batch_import_task(app_data, client)
64+
client.close()
6665

0 commit comments

Comments
 (0)