Small efficiency/readability fixes.

This commit is contained in:
Jeremy Fincher 2003-12-01 12:04:02 +00:00
parent efa81e0ac2
commit 24f95e6a75

View File

@ -413,7 +413,10 @@ class UsersDB(object):
if len(ids) == 1: if len(ids) == 1:
id = ids[0] id = ids[0]
self._hostmaskCache[s] = id self._hostmaskCache[s] = id
self._hostmaskCache.setdefault(id, sets.Set()).add(s) try:
self._hostmaskCache[id].add(s)
except KeyError:
self._hostmaskCache[id] = sets.Set([s])
return id return id
elif len(ids) == 0: elif len(ids) == 0:
raise KeyError, s raise KeyError, s
@ -458,19 +461,19 @@ class UsersDB(object):
def setUser(self, id, user): def setUser(self, id, user):
"""Sets a user (given its id) to the IrcUser given it.""" """Sets a user (given its id) to the IrcUser given it."""
assert isinstance(id, int), 'setUser takes an integer userId.' assert isinstance(id, int), 'setUser takes an integer userId.'
if not 0 <= id < len(self.users) or self.users[id] is None: if (not 0 <= id < len(self.users)) or self.users[id] is None:
raise KeyError, id raise KeyError, id
try: try:
if self.getUserId(user.name) != id: if self.getUserId(user.name) != id:
raise ValueError, \ s = '%s is already registered to someone else.' % user.name
'%s is already registered to someone else.' % user.name raise ValueError, s
except KeyError: except KeyError:
pass pass
for hostmask in user.hostmasks: for hostmask in user.hostmasks:
try: try:
if self.getUserId(hostmask) != id: if self.getUserId(hostmask) != id:
raise ValueError, \ s = '%s is already registered to someone else.'% hostmask
'%s is already registered to someone else.'% hostmask raise ValueError, s
except KeyError: except KeyError:
continue continue
if id in self._nameCache: if id in self._nameCache:
@ -480,7 +483,6 @@ class UsersDB(object):
for hostmask in self._hostmaskCache[id]: for hostmask in self._hostmaskCache[id]:
del self._hostmaskCache[hostmask] del self._hostmaskCache[hostmask]
del self._hostmaskCache[id] del self._hostmaskCache[id]
### FIXME: what if the new hostmasks overlap with another hostmask?
self.users[id] = user self.users[id] = user
self.flush() self.flush()