Added utils.pluralize.

This commit is contained in:
Jeremy Fincher 2003-09-01 05:42:35 +00:00
parent 1461535b5d
commit 30d3747f1d
5 changed files with 40 additions and 25 deletions

View File

@ -127,7 +127,10 @@ def makeNewAlias(name, alias):
return args[idx-1]
alias_ = dollarRe.sub(replace, alias_)
self.Proxy(irc, msg, callbacks.tokenize(alias_))
f.__doc__ ='<an alias, %s arguments>\n\nAlias for %r'%(biggestDollar,alias)
f.__doc__ ='<an alias, %s %s>\n\nAlias for %r' % \
(biggestDollar,
utils.pluralize(biggestDollar, 'argument'),
alias)
#f = new.function(f.func_code, f.func_globals, name)
return f

View File

@ -461,13 +461,17 @@ class FunCommands(callbacks.Privmsg):
childUser, childSystem, childUser + childSystem,
(user+system+childUser+childSystem)/timeRunning,
world.threadsSpawned,
world.threadsSpawned == 1 and 'thread' or 'threads',
activeThreads, world.commandsProcessed,
world.commandsProcessed == 1 and 'command' or 'commands')
utils.pluralize(world.threadsSpawned, 'thread'),
activeThreads,
world.commandsProcessed,
utils.pluralize(world.commandsProcessed, 'command'))
irc.reply(msg, response)
def uptime(self, irc, msg, args):
"takes no arguments"
"""takes no arguments.
Returns the amount of time the bot has been running.
"""
response = 'I have been running for %s.' % \
utils.timeElapsed(time.time() - world.startedAt)
irc.reply(msg, response)

View File

@ -81,7 +81,7 @@ def tableExists(db, table):
try:
cursor.execute("""SELECT * from %s LIMIT 1""" % table)
return True
except DatabaseError:
except sqlite.DatabaseError:
return False
tableDict = {
@ -153,9 +153,13 @@ class FunDB(callbacks.Privmsg):
cursor = self.db.cursor()
started = int(world.startedAt)
cursor.execute("""INSERT INTO uptime VALUES (%s, NULL)""", started)
self.db.commit()
def f():
db = makeDb(dbFilename)
cursor = db.cursor()
cursor.execute("""UPDATE uptime SET ended=%s WHERE started=%s""",
int(time.time()), started)
db.commit()
atexit.register(f)
def die(self):
@ -163,18 +167,6 @@ class FunDB(callbacks.Privmsg):
self.db.close()
del self.db
def _pluralize(self, string, count, verb=None):
if verb is None:
if count == 1:
return string
else:
return '%ss' % string
else:
if count == 1:
return ('is', string)
else:
return ('are', '%ss' % string)
def bestuptime(self, irc, msg, args):
"""takes no arguments.
@ -352,9 +344,8 @@ class FunDB(callbacks.Privmsg):
total = int(cursor.fetchone()[0])
except ValueError:
irc.error(msg, 'Unexpected response from database')
(verb, table) = self._pluralize(table, total, 1)
irc.reply(msg, 'There %s currently %s %s in my database' %\
(verb, total, table))
irc.reply(msg, 'There %s currently %s %s in my database' % \
(utils.be(total), total, utils.pluralize(total, table)))
def dbget(self, irc, msg, args):
"""<lart|excuse|insult|praise> <id>
@ -406,8 +397,9 @@ class FunDB(callbacks.Privmsg):
else:
(add,req,count) = cursor.fetchone()
reply = '%s #%s: Created by %s. last requested by %s, requested '\
' a total of %s %s' % (table, id, add, req, count,
self._pluralize('time',count))
' a total of %s %s' % \
(table, id, add, req,
count, utils.pluralize(count, 'time'))
irc.reply(msg, reply)
def lart(self, irc, msg, args):

View File

@ -157,9 +157,9 @@ class Notes(callbacks.Privmsg):
notes.to_id=users.id AND
read=0""", name)
unread = int(cursor.fetchone()[0])
s = 'You have %s unread note%s ' \
s = 'You have %s unread %s; ' \
'%s that I haven\'t told you about before now..' % \
(unread, unread == 1 and ';' or 's;', unnotified)
(unread, utils.pluralize(unread, 'note'), unnotified)
irc.queueMsg(ircmsgs.privmsg(msg.nick, s))
cursor.execute("""UPDATE notes
SET notified=1

View File

@ -286,4 +286,20 @@ def wrapLines(s):
L.append(textwrap.fill(line))
return '\n'.join(L)
plurals = {}
def pluralize(i, s):
if i == 1:
return s
else:
if s in plurals:
return plurals[s]
else:
return s + 's'
def be(i):
if i == 1:
return 'is'
else:
return 'are'
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: