Added __iter__ to UsersDB for a cleaner interface.

This commit is contained in:
Jeremy Fincher 2003-12-01 23:24:25 +00:00
parent e34d9deecd
commit b39e1237fc
3 changed files with 8 additions and 4 deletions

View File

@ -73,8 +73,7 @@ class User(callbacks.Privmsg):
else:
def p(s):
return True
users = [u.name for u in ircdb.users.users[1:] if u is not None]
users = filter(p, users)
users = [u.name for u in ircdb.users if p(u.name)]
if users:
utils.sortBy(str.lower, users)
irc.reply(msg, utils.commaAndify(users))

View File

@ -458,8 +458,13 @@ class UsersDB(object):
except KeyError:
return False
def __iter__(self):
x = ifilter(None, self.users)
x.next() # Skip the bot user.
return x
def numUsers(self):
return ilen(ifilter(None, self.users))-1 # -1 is for the bot user.
return ilen(self)
def setUser(self, id, user):
"""Sets a user (given its id) to the IrcUser given it."""

View File

@ -311,7 +311,7 @@ class UsersDBTestCase(unittest.TestCase):
pass
self.users = ircdb.UsersDB(self.filename)
def testNumUsers(self):
def testIterAndNumUsers(self):
self.assertEqual(self.users.numUsers(), 0)
(id, u) = self.users.newUser()
hostmask = 'foo!bar@baz'