some enhancements Factoids.rank:

output options: plain key output, and alpha sorting for plain output.
allow an optional argument for how many ranked facts to show.
This commit is contained in:
Daniel Folkinshteyn 2010-04-25 02:58:43 -04:00 committed by Valentin Lorentz
parent 7c3bc67c86
commit 8ed94257ba
2 changed files with 41 additions and 8 deletions

View File

@ -385,28 +385,56 @@ class Factoids(callbacks.Plugin, plugins.ChannelDBHandler):
alias = wrap(alias, ['channel', 'something', 'something', optional('int')])
@internationalizeDocstring
def rank(self, irc, msg, args, channel):
"""[<channel>]
def rank(self, irc, msg, args, channel, optlist, number):
"""[<channel>] [--plain] [--alpha] [<number>]
Returns a list of top-ranked factoid keys, sorted by usage count
(rank). The number of factoid keys returned is set by the
rankListLength registry value. <channel> is only necessary if the
message isn't sent in the channel itself.
(rank). If <number> is not provided, the default number of factoid keys
returned is set by the rankListLength registry value.
If --plain option is given, rank numbers and usage counts are not
included in output.
If --alpha option is given in addition to --plain, keys are sorted
alphabetically, instead of by rank.
<channel> is only necessary if the message isn't sent in the channel
itself.
"""
numfacts = self.registryValue('rankListLength', channel)
if not number:
number = self.registryValue('rankListLength', channel)
db = self.getDb(channel)
cursor = db.cursor()
cursor.execute("""SELECT keys.key, relations.usage_count
FROM keys, relations
WHERE relations.key_id=keys.id
ORDER BY relations.usage_count DESC
LIMIT ?""", (numfacts,))
LIMIT ?""", (number,))
factkeys = cursor.fetchall()
s = [ "#%d %s (%d)" % (i+1, key[0], key[1]) for i, key in enumerate(factkeys) ]
plain=False
alpha=False
for (option, arg) in optlist:
if option == 'plain':
plain = True
elif option =='alpha':
alpha = True
if plain:
s = [ "%s" % (key[0],) for i, key in enumerate(factkeys) ]
if alpha:
s.sort()
else:
s = [ "#%d %s (%d)" % (i+1, key[0], key[1]) for i, key in enumerate(factkeys) ]
irc.reply(", ".join(s))
<<<<<<< HEAD
rank = wrap(rank, ['channel'])
@internationalizeDocstring
=======
rank = wrap(rank, ['channel',
getopts({'plain': '', 'alpha': '',}),
optional('int')])
>>>>>>> f1517a7... some enhancements Factoids.rank:
def lock(self, irc, msg, args, channel, key):
"""[<channel>] <key>

View File

@ -189,6 +189,11 @@ class FactoidsTestCase(ChannelPluginTestCase):
self.assertRegexp('factoids rank', '#1 foo \(0\), #2 moo \(0\)')
self.assertRegexp('whatis moo', '.*cow.*')
self.assertRegexp('factoids rank', '#1 moo \(1\), #2 foo \(0\)')
self.assertRegexp('factoids rank 1', '#1 moo \(1\)')
self.assertNotRegexp('factoids rank 1', 'foo')
self.assertRegexp('factoids rank --plain', 'moo, foo')
self.assertRegexp('factoids rank --plain --alpha', 'foo, moo')
self.assertResponse('factoids rank --plain 1', 'moo')
def testQuoteHandling(self):
self.assertNotError('learn foo as "\\"bar\\""')