Added __len__, __repr__, and a useful __init__ to IrcMsgQueue.

This commit is contained in:
Jeremy Fincher 2003-11-19 16:22:46 +00:00
parent 9a7de79467
commit 429c90ac2a
2 changed files with 47 additions and 2 deletions

View File

@ -30,11 +30,11 @@
###
import fix
from structures import queue, smallqueue, RingBuffer
import copy
import sets
import time
from itertools import imap, chain
import conf
import debug
@ -43,6 +43,7 @@ import world
import ircdb
import ircmsgs
import ircutils
from structures import queue, smallqueue, RingBuffer
###
# The base class for a callback to be registered with an Irc object. Shows
@ -132,8 +133,10 @@ class IrcMsgQueue(object):
ones.
"""
__slots__ = ('msgs', 'highpriority', 'normal', 'lowpriority')
def __init__(self):
def __init__(self, iterable=()):
self.reset()
for msg in iterable:
self.enqueue(msg)
def reset(self):
"""Clears the queue."""
@ -175,6 +178,16 @@ class IrcMsgQueue(object):
def __nonzero__(self):
return bool(self.highpriority or self.normal or self.lowpriority)
def __len__(self):
return sum(imap(len,[self.highpriority,self.lowpriority,self.normal]))
def __repr__(self):
name = self.__class__.__name__
return '%s(%r)' % (name, list(chain(self.highpriority,
self.normal,
self.lowpriority)))
__str__ = __repr__
###
# Maintains the state of IRC connection -- the most recent messages, the

View File

@ -51,6 +51,38 @@ class IrcMsgQueueTestCase(unittest.TestCase):
join = ircmsgs.join('#foo')
who = ircmsgs.who('#foo')
def testInit(self):
q = irclib.IrcMsgQueue([self.msg, self.topic, self.ping])
self.assertEqual(len(q), 3)
def testLen(self):
q = irclib.IrcMsgQueue()
q.enqueue(self.msg)
self.assertEqual(len(q), 1)
q.enqueue(self.mode)
self.assertEqual(len(q), 2)
q.enqueue(self.kick)
self.assertEqual(len(q), 3)
q.enqueue(self.topic)
self.assertEqual(len(q), 4)
q.dequeue()
self.assertEqual(len(q), 3)
q.dequeue()
self.assertEqual(len(q), 2)
q.dequeue()
self.assertEqual(len(q), 1)
q.dequeue()
self.assertEqual(len(q), 0)
def testRepr(self):
q = irclib.IrcMsgQueue()
self.assertEqual(repr(q), 'IrcMsgQueue([])')
q.enqueue(self.msg)
try:
repr(q)
except Exception, e:
self.fail('repr(q) raised an exception: %s' % debug.exnToString(e))
def testEmpty(self):
q = irclib.IrcMsgQueue()
self.failIf(q)