Skip to content

Commit f008689

Browse files
authored
Merge pull request CloudBotIRC#176 from edillingham/karma_cleanup
fixed karma plugin docstrings & most pep8 violations
2 parents 8b5d435 + ed8ab78 commit f008689

1 file changed

Lines changed: 32 additions & 25 deletions

File tree

plugins/karma.py

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
karmaminus_re = re.compile('^.*\-\-$')
99
db_ready = []
1010

11+
1112
def db_init(db, conn_name):
1213
"""Check to see if the DB has the herald table. Connection name is for caching the result per connection.
1314
:type db: sqlalchemy.orm.Session
@@ -21,10 +22,10 @@ def db_init(db, conn_name):
2122

2223
@hook.command("pp", "addpoint")
2324
def addpoint(text, nick, chan, db, conn):
24-
""".addpoint or (.pp) <thing> adds a point to the <thing>"""
25+
"""<thing> - adds a point to the <thing>"""
2526
text = text.strip()
2627
db_init(db, conn.name)
27-
karma = db.execute("select score from karma where name = :name and chan = :chan and thing = :thing", {'name':nick, 'chan': chan, 'thing': text.lower()}).fetchone()
28+
karma = db.execute("select score from karma where name = :name and chan = :chan and thing = :thing", {'name': nick, 'chan': chan, 'thing': text.lower()}).fetchone()
2829
if karma:
2930
score = int(karma[0])
3031
score = score + 1
@@ -34,70 +35,74 @@ def addpoint(text, nick, chan, db, conn):
3435
else:
3536
db.execute("insert or replace into karma(name, chan, thing, score) values (:name, :chan, :thing, :score)", {'name': nick, 'chan': chan, 'thing': text.lower(), 'score': 1})
3637
db.commit()
37-
#return "{} is now worth 1 in {}'s eyes.".format(text, nick)
38+
# return "{} is now worth 1 in {}'s eyes.".format(text, nick)
39+
3840

3941
@hook.regex(karmaplus_re)
4042
def re_addpt(match, nick, chan, db, conn, notice):
4143
"""no useful help txt"""
4244
thing = match.group().split('++')[0]
4345
if thing:
4446
addpoint(thing, nick, chan, db, conn)
45-
#return out
47+
# return out
4648
else:
4749
notice(pluspts(nick, chan, db, conn))
4850

51+
4952
@hook.command("mm", "rmpoint")
5053
def rmpoint(text, nick, chan, db, conn):
51-
""".rmpoint or (.mm) <thing> subtracts a point from the <thing>"""
54+
"""<thing> - subtracts a point from the <thing>"""
5255
text = text.strip()
5356
db_init(db, conn.name)
54-
karma = db.execute("select score from karma where name = :name and chan = :chan and thing = :thing", {'name':nick, 'chan': chan, 'thing': text.lower()}).fetchone()
57+
karma = db.execute("select score from karma where name = :name and chan = :chan and thing = :thing", {'name': nick, 'chan': chan, 'thing': text.lower()}).fetchone()
5558
if karma:
5659
score = int(karma[0])
5760
score = score - 1
5861
db.execute("insert or replace into karma(name, chan, thing, score) values (:name, :chan, :thing, :score)", {'name': nick, 'chan': chan, 'thing': text.lower(), 'score': score})
5962
db.commit()
60-
#return "{} is now worth {} in {}'s eyes.".format(text, score, nick)
63+
# return "{} is now worth {} in {}'s eyes.".format(text, score, nick)
6164
else:
6265
db.execute("insert or replace into karma(name, chan, thing, score) values (:name, :chan, :thing, :score)", {'name': nick, 'chan': chan, 'thing': text.lower(), 'score': -1})
6366
db.commit()
64-
#return "{} is now worth -1 in {}'s eyes.".format(text, nick)
67+
# return "{} is now worth -1 in {}'s eyes.".format(text, nick)
6568

6669

6770
@hook.command("pluspts", autohelp=False)
6871
def pluspts(nick, chan, db, conn):
69-
"""prints the things you have liked"""
72+
"""- prints the things you have liked and their scores"""
7073
db_init(db, conn.name)
7174
output = ""
72-
likes = db.execute("select thing, score from karma where name = :name and chan = :chan and score >= 0 order by score desc", { 'name': nick, 'chan': chan }).fetchall()
75+
likes = db.execute("select thing, score from karma where name = :name and chan = :chan and score >= 0 order by score desc", {'name': nick, 'chan': chan}).fetchall()
7376
for like in likes:
74-
output = output + str(like[0]) + " has " + str(like[1]) + " points "
77+
output = output + str(like[0]) + " has " + str(like[1]) + " points "
7578
return output
7679

80+
7781
@hook.command("minuspts", autohelp=False)
7882
def minuspts(nick, chan, db, conn):
79-
"""prints the things you have liked"""
83+
"""- prints the things you have disliked and their scores"""
8084
db_init(db, conn.name)
8185
output = ""
82-
likes = db.execute("select thing, score from karma where name = :name and chan = :chan and score <= 0 order by score", { 'name': nick, 'chan': chan }).fetchall()
86+
likes = db.execute("select thing, score from karma where name = :name and chan = :chan and score <= 0 order by score", {'name': nick, 'chan': chan}).fetchall()
8387
for like in likes:
84-
output = output + str(like[0]) + " has " + str(like[1]) + " points "
88+
output = output + str(like[0]) + " has " + str(like[1]) + " points "
8589
return output
8690

91+
8792
@hook.regex(karmaminus_re)
8893
def re_rmpt(match, nick, chan, db, conn, notice):
8994
"""no useful help txt"""
9095
thing = match.group().split('--')[0]
9196
if thing:
9297
rmpoint(thing, nick, chan, db, conn)
93-
#return out
98+
# return out
9499
else:
95100
notice(minuspts(nick, chan, db, conn))
96101

97102

98103
@hook.command("points", autohelp=False)
99104
def points(text, chan, db, conn):
100-
""".points <thing> will print the total points for <thing> in the channel."""
105+
"""<thing> - will print the total points for <thing> in the channel."""
101106
db_init(db, conn.name)
102107
score = 0
103108
karma = ""
@@ -107,7 +112,7 @@ def points(text, chan, db, conn):
107112
karma = db.execute("select score from karma where thing = :thing", {'thing': thing.lower()}).fetchall()
108113
else:
109114
text = text.strip()
110-
karma = db.execute("select score from karma where thing = :thing and chan = :chan", {'thing': text.lower(), 'chan': chan }).fetchall()
115+
karma = db.execute("select score from karma where thing = :thing and chan = :chan", {'thing': text.lower(), 'chan': chan}).fetchall()
111116
if karma:
112117
pos = 0
113118
neg = 0
@@ -118,14 +123,15 @@ def points(text, chan, db, conn):
118123
pos += int(k[0])
119124
score += int(k[0])
120125
if thing:
121-
return "{} has a total score of {} (+{}/{}) across all channels I know about.".format(thing, score, pos, neg)
126+
return "{} has a total score of {} (+{}/{}) across all channels I know about.".format(thing, score, pos, neg)
122127
return "{} has a total score of {} (+{}/{}) in {}.".format(text, score, pos, neg, chan)
123128
else:
124129
return "I couldn't find {} in the database.".format(text)
125130

131+
126132
@hook.command("topten", "pointstop", "loved", autohelp=False)
127133
def pointstop(text, chan, db, message, conn, notice):
128-
""".topten or .pointstop prints the top 10 things with the highest points in the channel. To see the top 10 items in all of the channels the bot sits in use .topten global."""
134+
"""- prints the top 10 things with the highest points in the channel. To see the top 10 items in all of the channels the bot sits in use .topten global."""
129135
db_init(db, conn.name)
130136
scores = []
131137
points = defaultdict(int)
@@ -135,27 +141,28 @@ def pointstop(text, chan, db, message, conn, notice):
135141
items = db.execute("select thing, score from karma").fetchall()
136142
out = "The top {} favorite things in all channels are: "
137143
else:
138-
items = db.execute("select thing, score from karma where chan = :chan", {'chan':chan}).fetchall()
144+
items = db.execute("select thing, score from karma where chan = :chan", {'chan': chan}).fetchall()
139145
out = "The top {} favorite things in {} are: "
140146
if items:
141147
for item in items:
142148
thing = item[0]
143149
score = int(item[1])
144150
points[thing] += score
145151
scores = points.items()
146-
sorts = sorted(scores, key=operator.itemgetter(1), reverse = True)
152+
sorts = sorted(scores, key=operator.itemgetter(1), reverse=True)
147153
ten = str(len(sorts))
148154
if int(ten) > 10:
149155
ten = "10"
150156
out = out.format(ten, chan)
151-
for i in range(0,int(ten)):
157+
for i in range(0, int(ten)):
152158
out += "{} with {} points \u2022 ".format(sorts[i][0], sorts[i][1])
153159
out = out[:-2]
154160
return out
155161

162+
156163
@hook.command("bottomten", "pointsbottom", "hated", autohelp=False)
157164
def pointsbottom(text, chan, db, message, conn, notice):
158-
""".bottomten or .pointsbottom prints the top 10 things with the highest points in the channel. To see the top 10 items in all of the channels the bot sits in use .topten global."""
165+
"""- prints the top 10 things with the lowest points in the channel. To see the bottom 10 items in all of the channels the bot sits in use .bottomten global."""
159166
db_init(db, conn.name)
160167
scores = []
161168
points = defaultdict(int)
@@ -165,7 +172,7 @@ def pointsbottom(text, chan, db, message, conn, notice):
165172
items = db.execute("select thing, score from karma").fetchall()
166173
out = "The {} most hated things in all channels are: "
167174
else:
168-
items = db.execute("select thing, score from karma where chan = :chan", {'chan':chan}).fetchall()
175+
items = db.execute("select thing, score from karma where chan = :chan", {'chan': chan}).fetchall()
169176
out = "The {} most hated things in {} are: "
170177
if items:
171178
for item in items:
@@ -178,7 +185,7 @@ def pointsbottom(text, chan, db, message, conn, notice):
178185
if int(ten) > 10:
179186
ten = "10"
180187
out = out.format(ten, chan)
181-
for i in range(0,int(ten)):
188+
for i in range(0, int(ten)):
182189
out += "{} with {} points \u2022 ".format(sorts[i][0], sorts[i][1])
183190
out = out[:-2]
184191
return out

0 commit comments

Comments
 (0)