mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-12-02 16:09:24 +01:00
Added TwoWayDictionary.
This commit is contained in:
parent
b2dd349235
commit
f392696d98
@ -39,8 +39,6 @@ import fix
|
|||||||
|
|
||||||
import types
|
import types
|
||||||
|
|
||||||
__all__ = ['RingBuffer', 'queue', 'smallqueue', 'MaxLengthQueue']
|
|
||||||
|
|
||||||
class RingBuffer(object):
|
class RingBuffer(object):
|
||||||
"""Class to represent a fixed-size ring buffer."""
|
"""Class to represent a fixed-size ring buffer."""
|
||||||
__slots__ = ('L', 'i', 'full', 'maxSize')
|
__slots__ = ('L', 'i', 'full', 'maxSize')
|
||||||
@ -319,7 +317,22 @@ class MaxLengthQueue(queue):
|
|||||||
if len(self) > self.length:
|
if len(self) > self.length:
|
||||||
self.dequeue()
|
self.dequeue()
|
||||||
|
|
||||||
## class MaxLengthQueue(RingBuffer):
|
|
||||||
## enqueue = RingBuffer.append
|
class TwoWayDictionary(dict):
|
||||||
## def peek(self):
|
__slots__ = ()
|
||||||
## return self[0]
|
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)
|
||||||
|
@ -549,5 +549,28 @@ class MaxLengthQueueTestCase(unittest.TestCase):
|
|||||||
self.assertEqual(q[0], 3)
|
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:
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user