mirror of
https://github.com/Mikaela/Limnoria.git
synced 2025-02-17 06:00:42 +01:00
Added utils.pluralize.
This commit is contained in:
parent
1461535b5d
commit
30d3747f1d
@ -127,7 +127,10 @@ def makeNewAlias(name, alias):
|
|||||||
return args[idx-1]
|
return args[idx-1]
|
||||||
alias_ = dollarRe.sub(replace, alias_)
|
alias_ = dollarRe.sub(replace, alias_)
|
||||||
self.Proxy(irc, msg, callbacks.tokenize(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)
|
#f = new.function(f.func_code, f.func_globals, name)
|
||||||
return f
|
return f
|
||||||
|
|
||||||
|
@ -461,13 +461,17 @@ class FunCommands(callbacks.Privmsg):
|
|||||||
childUser, childSystem, childUser + childSystem,
|
childUser, childSystem, childUser + childSystem,
|
||||||
(user+system+childUser+childSystem)/timeRunning,
|
(user+system+childUser+childSystem)/timeRunning,
|
||||||
world.threadsSpawned,
|
world.threadsSpawned,
|
||||||
world.threadsSpawned == 1 and 'thread' or 'threads',
|
utils.pluralize(world.threadsSpawned, 'thread'),
|
||||||
activeThreads, world.commandsProcessed,
|
activeThreads,
|
||||||
world.commandsProcessed == 1 and 'command' or 'commands')
|
world.commandsProcessed,
|
||||||
|
utils.pluralize(world.commandsProcessed, 'command'))
|
||||||
irc.reply(msg, response)
|
irc.reply(msg, response)
|
||||||
|
|
||||||
def uptime(self, irc, msg, args):
|
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.' % \
|
response = 'I have been running for %s.' % \
|
||||||
utils.timeElapsed(time.time() - world.startedAt)
|
utils.timeElapsed(time.time() - world.startedAt)
|
||||||
irc.reply(msg, response)
|
irc.reply(msg, response)
|
||||||
|
@ -81,7 +81,7 @@ def tableExists(db, table):
|
|||||||
try:
|
try:
|
||||||
cursor.execute("""SELECT * from %s LIMIT 1""" % table)
|
cursor.execute("""SELECT * from %s LIMIT 1""" % table)
|
||||||
return True
|
return True
|
||||||
except DatabaseError:
|
except sqlite.DatabaseError:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
tableDict = {
|
tableDict = {
|
||||||
@ -153,9 +153,13 @@ class FunDB(callbacks.Privmsg):
|
|||||||
cursor = self.db.cursor()
|
cursor = self.db.cursor()
|
||||||
started = int(world.startedAt)
|
started = int(world.startedAt)
|
||||||
cursor.execute("""INSERT INTO uptime VALUES (%s, NULL)""", started)
|
cursor.execute("""INSERT INTO uptime VALUES (%s, NULL)""", started)
|
||||||
|
self.db.commit()
|
||||||
def f():
|
def f():
|
||||||
|
db = makeDb(dbFilename)
|
||||||
|
cursor = db.cursor()
|
||||||
cursor.execute("""UPDATE uptime SET ended=%s WHERE started=%s""",
|
cursor.execute("""UPDATE uptime SET ended=%s WHERE started=%s""",
|
||||||
int(time.time()), started)
|
int(time.time()), started)
|
||||||
|
db.commit()
|
||||||
atexit.register(f)
|
atexit.register(f)
|
||||||
|
|
||||||
def die(self):
|
def die(self):
|
||||||
@ -163,18 +167,6 @@ class FunDB(callbacks.Privmsg):
|
|||||||
self.db.close()
|
self.db.close()
|
||||||
del self.db
|
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):
|
def bestuptime(self, irc, msg, args):
|
||||||
"""takes no arguments.
|
"""takes no arguments.
|
||||||
|
|
||||||
@ -352,9 +344,8 @@ class FunDB(callbacks.Privmsg):
|
|||||||
total = int(cursor.fetchone()[0])
|
total = int(cursor.fetchone()[0])
|
||||||
except ValueError:
|
except ValueError:
|
||||||
irc.error(msg, 'Unexpected response from database')
|
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' % \
|
||||||
irc.reply(msg, 'There %s currently %s %s in my database' %\
|
(utils.be(total), total, utils.pluralize(total, table)))
|
||||||
(verb, total, table))
|
|
||||||
|
|
||||||
def dbget(self, irc, msg, args):
|
def dbget(self, irc, msg, args):
|
||||||
"""<lart|excuse|insult|praise> <id>
|
"""<lart|excuse|insult|praise> <id>
|
||||||
@ -406,8 +397,9 @@ class FunDB(callbacks.Privmsg):
|
|||||||
else:
|
else:
|
||||||
(add,req,count) = cursor.fetchone()
|
(add,req,count) = cursor.fetchone()
|
||||||
reply = '%s #%s: Created by %s. last requested by %s, requested '\
|
reply = '%s #%s: Created by %s. last requested by %s, requested '\
|
||||||
' a total of %s %s' % (table, id, add, req, count,
|
' a total of %s %s' % \
|
||||||
self._pluralize('time',count))
|
(table, id, add, req,
|
||||||
|
count, utils.pluralize(count, 'time'))
|
||||||
irc.reply(msg, reply)
|
irc.reply(msg, reply)
|
||||||
|
|
||||||
def lart(self, irc, msg, args):
|
def lart(self, irc, msg, args):
|
||||||
|
@ -157,9 +157,9 @@ class Notes(callbacks.Privmsg):
|
|||||||
notes.to_id=users.id AND
|
notes.to_id=users.id AND
|
||||||
read=0""", name)
|
read=0""", name)
|
||||||
unread = int(cursor.fetchone()[0])
|
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..' % \
|
'%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))
|
irc.queueMsg(ircmsgs.privmsg(msg.nick, s))
|
||||||
cursor.execute("""UPDATE notes
|
cursor.execute("""UPDATE notes
|
||||||
SET notified=1
|
SET notified=1
|
||||||
|
16
src/utils.py
16
src/utils.py
@ -286,4 +286,20 @@ def wrapLines(s):
|
|||||||
L.append(textwrap.fill(line))
|
L.append(textwrap.fill(line))
|
||||||
return '\n'.join(L)
|
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:
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user