Added TwoWayDictionary.

This commit is contained in:
Jeremy Fincher 2003-10-16 19:36:33 +00:00
parent b2dd349235
commit f392696d98
2 changed files with 42 additions and 6 deletions

View File

@ -39,8 +39,6 @@ import fix
import types
__all__ = ['RingBuffer', 'queue', 'smallqueue', 'MaxLengthQueue']
class RingBuffer(object):
"""Class to represent a fixed-size ring buffer."""
__slots__ = ('L', 'i', 'full', 'maxSize')
@ -319,7 +317,22 @@ class MaxLengthQueue(queue):
if len(self) > self.length:
self.dequeue()
## class MaxLengthQueue(RingBuffer):
## enqueue = RingBuffer.append
## def peek(self):
## return self[0]
class TwoWayDictionary(dict):
__slots__ = ()
def __init__(self, seq=(), **kwargs):
for (key, value) in seq:
self[key] = value
self[value] = key
for (key, value) in kwargs.iteritems():
self[key] = value
self[value] = key
def __setitem__(self, key, value):
dict.__setitem__(self, key, value)
dict.__setitem__(self, value, key)
def __delitem__(self, key):
value = self[key]
dict.__delitem__(self, key)
dict.__delitem__(self, value)

View File

@ -549,5 +549,28 @@ class MaxLengthQueueTestCase(unittest.TestCase):
self.assertEqual(q[0], 3)
class TwoWayDictionaryTestCase(unittest.TestCase):
def testInit(self):
d = TwoWayDictionary(foo='bar')
self.failUnless('foo' in d)
self.failUnless('bar' in d)
def testSetitem(self):
d = TwoWayDictionary()
d['foo'] = 'bar'
self.failUnless('foo' in d)
self.failUnless('bar' in d)
def testDelitem(self):
d = TwoWayDictionary(foo='bar')
del d['foo']
self.failIf('foo' in d)
self.failIf('bar' in d)
d = TwoWayDictionary(foo='bar')
del d['bar']
self.failIf('bar' in d)
self.failIf('foo' in d)
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: