Update usage of utils.iter functions.

This commit is contained in:
James Vega 2005-01-31 15:22:48 +00:00
parent 4d32d36982
commit 0fde2393e6
13 changed files with 31 additions and 33 deletions

View File

@ -27,8 +27,6 @@
# POSSIBILITY OF SUCH DAMAGE. # POSSIBILITY OF SUCH DAMAGE.
### ###
import random
import babelfish import babelfish
import supybot.conf as conf import supybot.conf as conf
@ -152,9 +150,9 @@ class Babelfish(callbacks.Privmsg):
languages = self.registryValue('languages', msg.args[0]) languages = self.registryValue('languages', msg.args[0])
if not languages: if not languages:
irc.error('I can\'t speak any other languages.', Raise=True) irc.error('I can\'t speak any other languages.', Raise=True)
language = random.choice(languages) language = utils.iter.choice(languages)
while not allowEnglish and language == 'English': while not allowEnglish and language == 'English':
language = random.choice(languages) language = utils.iter.choice(languages)
irc.reply(language) irc.reply(language)
randomlanguage = wrap(randomlanguage, [getopts({'allow-english': ''})]) randomlanguage = wrap(randomlanguage, [getopts({'allow-english': ''})])

View File

@ -36,6 +36,7 @@ import supybot.utils as utils
import supybot.world as world import supybot.world as world
import supybot.ircdb as ircdb import supybot.ircdb as ircdb
from supybot.commands import * from supybot.commands import *
from supybot.utils.iter import all
import supybot.ircutils as ircutils import supybot.ircutils as ircutils
import supybot.registry as registry import supybot.registry as registry
import supybot.callbacks as callbacks import supybot.callbacks as callbacks

View File

@ -27,7 +27,6 @@
# POSSIBILITY OF SUCH DAMAGE. # POSSIBILITY OF SUCH DAMAGE.
### ###
import random
import socket import socket
import dictclient import dictclient
@ -64,7 +63,7 @@ class Dict(callbacks.Privmsg):
server = conf.supybot.plugins.Dict.server() server = conf.supybot.plugins.Dict.server()
conn = dictclient.Connection(server) conn = dictclient.Connection(server)
dbs = conn.getdbdescs().keys() dbs = conn.getdbdescs().keys()
irc.reply(random.choice(dbs)) irc.reply(utils.iter.choice(dbs))
except socket.error, e: except socket.error, e:
irc.error(utils.web.strError(e)) irc.error(utils.web.strError(e))
random = wrap(random) random = wrap(random)

View File

@ -407,7 +407,7 @@ class Filter(callbacks.Privmsg):
text = text.replace(',', ' ') text = text.replace(',', ' ')
text = text.replace("'", '') text = text.replace("'", '')
text = text.replace('one', '1') text = text.replace('one', '1')
smiley = random.choice(['<3', ':)', ':-)', ':D', ':-D']) smiley = utils.iter.choice(['<3', ':)', ':-)', ':D', ':-D'])
text += smiley*3 text += smiley*3
irc.reply(text) irc.reply(text)
aol = wrap(aol, ['text']) aol = wrap(aol, ['text'])
@ -418,7 +418,7 @@ class Filter(callbacks.Privmsg):
Returns <text> as if JeffK had said it himself. Returns <text> as if JeffK had said it himself.
""" """
def randomlyPick(L): def randomlyPick(L):
return random.choice(L) return utils.iter.choice(L)
def quoteOrNothing(m): def quoteOrNothing(m):
return randomlyPick(['"', '']).join(m.groups()) return randomlyPick(['"', '']).join(m.groups())
def randomlyReplace(s, probability=0.5): def randomlyReplace(s, probability=0.5):
@ -445,23 +445,22 @@ class Filter(callbacks.Privmsg):
def randomlyLaugh(text, probability=.3): def randomlyLaugh(text, probability=.3):
if random.random() < probability: if random.random() < probability:
if random.random() < .5: if random.random() < .5:
insult = random.choice([' fagot1', ' fagorts', ' jerks', insult = utils.iter.choice([' fagot1', ' fagorts',
'fagot' ' jerk', ' dumbshoes', ' jerks', 'fagot' ' jerk',
' dumbshoe']) 'dumbshoes', ' dumbshoe'])
else: else:
insult = '' insult = ''
laugh1 = random.choice(['ha', 'hah', 'lol', 'l0l', 'ahh']) laugh1 = utils.iter.choice(['ha', 'hah', 'lol', 'l0l', 'ahh'])
laugh2 = random.choice(['ha', 'hah', 'lol', 'l0l', 'ahh']) laugh2 = utils.iter.choice(['ha', 'hah', 'lol', 'l0l', 'ahh'])
laugh1 = laugh1 * random.randrange(1, 5) laugh1 = laugh1 * random.randrange(1, 5)
laugh2 = laugh2 * random.randrange(1, 5) laugh2 = laugh2 * random.randrange(1, 5)
exclaim = random.choice(['!', '~', '!~', '~!!~~', exclaim = utils.iter.choice(['!', '~', '!~', '~!!~~',
'!!~', '~~~!']) '!!~', '~~~!'])
exclaim += random.choice(['!', '~', '!~', '~!!~~', exclaim += utils.iter.choice(['!', '~', '!~', '~!!~~',
'!!~', '~~~!'])
if random.random() < 0.5:
exclaim += random.choice(['!', '~', '!~', '~!!~~',
'!!~', '~~~!']) '!!~', '~~~!'])
if random.random() < 0.5:
exclaim += utils.iter.choice(['!', '~', '!~', '~!!~~',
'!!~', '~~~!'])
laugh = ''.join([' ', laugh1, laugh2, insult, exclaim]) laugh = ''.join([' ', laugh1, laugh2, insult, exclaim])
text += laugh text += laugh
return text return text

View File

@ -51,9 +51,9 @@ import pprint
import socket import socket
import logging import logging
import optparse import optparse
from itertools import imap
import supybot.ansi as ansi import supybot.ansi as ansi
from utils.iter import imap
import supybot.utils as utils import supybot.utils as utils
import supybot.ircutils as ircutils import supybot.ircutils as ircutils
import supybot.registry as registry import supybot.registry as registry

View File

@ -48,7 +48,6 @@ import string
import inspect import inspect
import operator import operator
from cStringIO import StringIO from cStringIO import StringIO
from itertools import imap, ifilter
import supybot.log as log import supybot.log as log
import supybot.conf as conf import supybot.conf as conf
@ -59,6 +58,7 @@ import supybot.irclib as irclib
import supybot.ircmsgs as ircmsgs import supybot.ircmsgs as ircmsgs
import supybot.ircutils as ircutils import supybot.ircutils as ircutils
import supybot.registry as registry import supybot.registry as registry
from supybot.utils.iter import any, imap, ifilter
def _addressed(nick, msg, prefixChars=None, nicks=None, def _addressed(nick, msg, prefixChars=None, nicks=None,
prefixStrings=None, whenAddressedByNick=None, prefixStrings=None, whenAddressedByNick=None,

View File

@ -364,7 +364,7 @@ class DB(object):
def random(self): def random(self):
try: try:
return self._newRecord(*random.choice(self.map)) return self._newRecord(*utils.iter.choice(self.map))
except IndexError: except IndexError:
return None return None

View File

@ -38,7 +38,6 @@ from __future__ import division
import time import time
import select import select
import socket import socket
from itertools import imap
import supybot.log as log import supybot.log as log
import supybot.conf as conf import supybot.conf as conf
@ -46,6 +45,7 @@ import supybot.utils as utils
import supybot.world as world import supybot.world as world
import supybot.drivers as drivers import supybot.drivers as drivers
import supybot.schedule as schedule import supybot.schedule as schedule
from supybot.utils.iter import imap
# XXX Shouldn't the reconnect wait (at least the last one) be configurable? # XXX Shouldn't the reconnect wait (at least the last one) be configurable?
reconnectWaits = [0, 60, 300] reconnectWaits = [0, 60, 300]

View File

@ -72,9 +72,9 @@ class ServersMixin(object):
self.networkGroup = conf.supybot.networks.get(irc.network) self.networkGroup = conf.supybot.networks.get(irc.network)
self.servers = servers self.servers = servers
super(ServersMixin, self).__init__(irc) super(ServersMixin, self).__init__(irc)
def _getServers(self): def _getServers(self):
# We do this, rather than itertools.cycle the servers in __init__, # We do this, rather than utils.iter.cycle the servers in __init__,
# because otherwise registry updates given as setValues or sets # because otherwise registry updates given as setValues or sets
# wouldn't be visible until a restart. # wouldn't be visible until a restart.
return self.networkGroup.servers()[:] # Be sure to copy! return self.networkGroup.servers()[:] # Be sure to copy!
@ -87,7 +87,7 @@ class ServersMixin(object):
server = self.servers.pop(0) server = self.servers.pop(0)
self.currentServer = '%s:%s' % server self.currentServer = '%s:%s' % server
return server return server
def empty(): def empty():
"""Returns whether or not the driver loop is empty.""" """Returns whether or not the driver loop is empty."""
@ -178,7 +178,7 @@ class Log(object):
stat = staticmethod(supylog.stat) stat = staticmethod(supylog.stat)
log = Log() log = Log()
def newDriver(irc, moduleName=None): def newDriver(irc, moduleName=None):
"""Returns a new driver for the given server using the irc given and using """Returns a new driver for the given server using the irc given and using
conf.supybot.driverModule to determine what driver to pick.""" conf.supybot.driverModule to determine what driver to pick."""

View File

@ -856,7 +856,7 @@ class Irc(IrcCommandDispatcher):
while len(L) <= 3: while len(L) <= 3:
L.append('`') L.append('`')
while ircutils.strEqual(ret, nick): while ircutils.strEqual(ret, nick):
L[random.randrange(len(L))] = random.choice('0123456789') L[random.randrange(len(L))] = utils.iter.choice('0123456789')
ret = ''.join(L) ret = ''.join(L)
return ret return ret

View File

@ -625,7 +625,7 @@ def standardSubstitute(irc, msg, text, env=None):
if len(L) > 1: if len(L) > 1:
n = msg.nick n = msg.nick
while n == msg.nick: while n == msg.nick:
n = random.choice(L) n = utils.iter.choice(L)
return n return n
else: else:
return msg.nick return msg.nick

View File

@ -314,7 +314,7 @@ class TimeoutQueue(object):
return self.timeout() return self.timeout()
else: else:
return self.timeout return self.timeout
def _clearOldElements(self): def _clearOldElements(self):
now = time.time() now = time.time()
while now - self.queue.peek()[0] > self._getTimeout(): while now - self.queue.peek()[0] > self._getTimeout():
@ -341,7 +341,7 @@ class TimeoutQueue(object):
yield elt yield elt
def __len__(self): def __len__(self):
# No dependency on utils. # No dependency on utils.iter
# return ilen(self) # return ilen(self)
i = 0 i = 0
for _ in self: for _ in self:
@ -413,7 +413,7 @@ class MultiSet(object):
def __contains__(self, elt): def __contains__(self, elt):
return elt in self.d return elt in self.d
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:

View File

@ -32,6 +32,7 @@ from supybot.test import *
import sets import sets
import supybot.irclib as irclib import supybot.irclib as irclib
from supybot.utils.iter import all
import supybot.ircutils as ircutils import supybot.ircutils as ircutils
class holder: class holder: