Move 'top 10' logic to utils, add !wordiest to modules/words.py
This commit is contained in:
parent
17caaa08dd
commit
75f25db4cd
3 changed files with 47 additions and 13 deletions
|
@ -96,12 +96,11 @@ class Module(object):
|
|||
items = [(coin[0], decimal.Decimal(coin[1])) for coin in all_coins]
|
||||
all_coins = dict(items)
|
||||
|
||||
top_10 = sorted(all_coins.keys())
|
||||
top_10 = sorted(top_10, key=all_coins.get, reverse=True)[:10]
|
||||
top_10 = ", ".join("%s (%s)" % (utils.prevent_highlight(
|
||||
event["server"].get_user(nickname).nickname), "{0:.2f}".format(
|
||||
all_coins[nickname])) for nickname in top_10)
|
||||
event["stdout"].write("Richest users: %s" % top_10)
|
||||
top_10 = utils.top_10(all_coins,
|
||||
convert_key=lambda nickname: utils.prevent_highlight(
|
||||
event["server"].get_user(nickname).nickname),
|
||||
value_format=lambda value: "{0:.2f}".format(value))
|
||||
event["stdout"].write("Richest users: %s" % ", ".join(top_10))
|
||||
|
||||
def _redeem_cache(self, server, user):
|
||||
return "redeem|%s|%s@%s" % (server.id, user.username, user.hostname)
|
||||
|
|
|
@ -82,12 +82,36 @@ class Module(ModuleManager.BaseModule):
|
|||
"word-%s" % word, [])
|
||||
items = [(word_user[0], word_user[1]) for word_user in word_users]
|
||||
word_users = dict(items)
|
||||
|
||||
top_10 = sorted(word_users.keys())
|
||||
top_10 = sorted(top_10, key=word_users.get, reverse=True)[:10]
|
||||
top_10 = ", ".join("%s (%d)" % (utils.prevent_highlight(event[
|
||||
"server"].get_user(nickname).nickname), word_users[nickname]
|
||||
) for nickname in top_10)
|
||||
event["stdout"].write("Top '%s' users: %s" % (word, top_10))
|
||||
top_10 = utils.top_10(word_users,
|
||||
convert_key=lambda nickname: utils.prevent_highlight(
|
||||
event["server"].get_user(nickname).nickname))
|
||||
event["stdout"].write("Top '%s' users: %s" % (word,
|
||||
", ".join(top_10)))
|
||||
else:
|
||||
event["stderr"].write("That word is not being tracked")
|
||||
|
||||
@utils.hook("received.command.wordiest")
|
||||
def wordiest(self, event):
|
||||
"""
|
||||
:help: Show wordiest users
|
||||
:usage: [channel]
|
||||
"""
|
||||
channel_query = None
|
||||
word_prefix = ""
|
||||
if event["args_split"]:
|
||||
channel_query = event["args_split"][0].lower()
|
||||
word_prefix = " (%s)" % channel_query
|
||||
|
||||
words = event["server"].find_all_user_channel_settings("words")
|
||||
user_words = {}
|
||||
for channel_name, nickname, word_count in words:
|
||||
if not channel_query or channel_name == channel_query:
|
||||
if not nickname in user_words:
|
||||
user_words[nickname] = 0
|
||||
user_words[nickname] += word_count
|
||||
|
||||
top_10 = utils.top_10(user_words,
|
||||
convert_key=lambda nickname: utils.prevent_highlight(
|
||||
event["server"].get_user(nickname).nickname))
|
||||
event["stdout"].write("wordiest%s: %s" % (
|
||||
word_prefix, ", ".join(top_10)))
|
||||
|
|
|
@ -171,3 +171,14 @@ def parse_docstring(s):
|
|||
description += " "
|
||||
description += line
|
||||
return Docstring(description, items)
|
||||
|
||||
def top_10(items, convert_key=lambda x: x, value_format=lambda x: x):
|
||||
top_10 = sorted(items.keys())
|
||||
top_10 = sorted(top_10, key=items.get, reverse=True)[:10]
|
||||
|
||||
top_10_items = []
|
||||
for key in top_10:
|
||||
top_10_items.append("%s (%s)" % (convert_key(key),
|
||||
value_format(items[key])))
|
||||
|
||||
return top_10_items
|
||||
|
|
Loading…
Reference in a new issue