Package pythonutils :: Module odict :: Class SequenceOrderedDict
[hide private]
[frames] | no frames]

Class SequenceOrderedDict
source code

object --+        
         |        
      dict --+    
             |    
   OrderedDict --+
                 |
                SequenceOrderedDict

Experimental version of OrderedDict that has a custom object for keys, values, and items.

These are callable sequence objects that work as methods, or can be manipulated directly as sequences.

Test for keys, items and values.

>>> d = SequenceOrderedDict(((1, 2), (2, 3), (3, 4)))
>>> d
SequenceOrderedDict([(1, 2), (2, 3), (3, 4)])
>>> d.keys
[1, 2, 3]
>>> d.keys()
[1, 2, 3]
>>> d.setkeys((3, 2, 1))
>>> d
SequenceOrderedDict([(3, 4), (2, 3), (1, 2)])
>>> d.setkeys((1, 2, 3))
>>> d.keys[0]
1
>>> d.keys[:]
[1, 2, 3]
>>> d.keys[-1]
3
>>> d.keys[-2]
2
>>> d.keys[0:2] = [2, 1]
>>> d
SequenceOrderedDict([(2, 3), (1, 2), (3, 4)])
>>> d.keys.reverse()
>>> d.keys
[3, 1, 2]
>>> d.keys = [1, 2, 3]
>>> d
SequenceOrderedDict([(1, 2), (2, 3), (3, 4)])
>>> d.keys = [3, 1, 2]
>>> d
SequenceOrderedDict([(3, 4), (1, 2), (2, 3)])
>>> a = SequenceOrderedDict()
>>> b = SequenceOrderedDict()
>>> a.keys == b.keys
1
>>> a['a'] = 3
>>> a.keys == b.keys
0
>>> b['a'] = 3
>>> a.keys == b.keys
1
>>> b['b'] = 3
>>> a.keys == b.keys
0
>>> a.keys > b.keys
0
>>> a.keys < b.keys
1
>>> 'a' in a.keys
1
>>> len(b.keys)
2
>>> 'c' in d.keys
0
>>> 1 in d.keys
1
>>> [v for v in d.keys]
[3, 1, 2]
>>> d.keys.sort()
>>> d.keys
[1, 2, 3]
>>> d = SequenceOrderedDict(((1, 2), (2, 3), (3, 4)), strict=True)
>>> d.keys[::-1] = [1, 2, 3]
>>> d
SequenceOrderedDict([(3, 4), (2, 3), (1, 2)])
>>> d.keys[:2]
[3, 2]
>>> d.keys[:2] = [1, 3]
Traceback (most recent call last):
KeyError: 'Keylist is not the same as current keylist.'
>>> d = SequenceOrderedDict(((1, 2), (2, 3), (3, 4)))
>>> d
SequenceOrderedDict([(1, 2), (2, 3), (3, 4)])
>>> d.values
[2, 3, 4]
>>> d.values()
[2, 3, 4]
>>> d.setvalues((4, 3, 2))
>>> d
SequenceOrderedDict([(1, 4), (2, 3), (3, 2)])
>>> d.values[::-1]
[2, 3, 4]
>>> d.values[0]
4
>>> d.values[-2]
3
>>> del d.values[0]
Traceback (most recent call last):
TypeError: Can't delete items from values
>>> d.values[::2] = [2, 4]
>>> d
SequenceOrderedDict([(1, 2), (2, 3), (3, 4)])
>>> 7 in d.values
0
>>> len(d.values)
3
>>> [val for val in d.values]
[2, 3, 4]
>>> d.values[-1] = 2
>>> d.values.count(2)
2
>>> d.values.index(2)
0
>>> d.values[-1] = 7
>>> d.values
[2, 3, 7]
>>> d.values.reverse()
>>> d.values
[7, 3, 2]
>>> d.values.sort()
>>> d.values
[2, 3, 7]
>>> d.values.append('anything')
Traceback (most recent call last):
TypeError: Can't append items to values
>>> d.values = (1, 2, 3)
>>> d
SequenceOrderedDict([(1, 1), (2, 2), (3, 3)])
>>> d = SequenceOrderedDict(((1, 2), (2, 3), (3, 4)))
>>> d
SequenceOrderedDict([(1, 2), (2, 3), (3, 4)])
>>> d.items()
[(1, 2), (2, 3), (3, 4)]
>>> d.setitems([(3, 4), (2 ,3), (1, 2)])
>>> d
SequenceOrderedDict([(3, 4), (2, 3), (1, 2)])
>>> d.items[0]
(3, 4)
>>> d.items[:-1]
[(3, 4), (2, 3)]
>>> d.items[1] = (6, 3)
>>> d.items
[(3, 4), (6, 3), (1, 2)]
>>> d.items[1:2] = [(9, 9)]
>>> d
SequenceOrderedDict([(3, 4), (9, 9), (1, 2)])
>>> del d.items[1:2]
>>> d
SequenceOrderedDict([(3, 4), (1, 2)])
>>> (3, 4) in d.items
1
>>> (4, 3) in d.items
0
>>> len(d.items)
2
>>> [v for v in d.items]
[(3, 4), (1, 2)]
>>> d.items.count((3, 4))
1
>>> d.items.index((1, 2))
1
>>> d.items.index((2, 1))
Traceback (most recent call last):
ValueError: list.index(x): x not in list
>>> d.items.reverse()
>>> d.items
[(1, 2), (3, 4)]
>>> d.items.reverse()
>>> d.items.sort()
>>> d.items
[(1, 2), (3, 4)]
>>> d.items.append((5, 6))
>>> d.items
[(1, 2), (3, 4), (5, 6)]
>>> d.items.insert(0, (0, 0))
>>> d.items
[(0, 0), (1, 2), (3, 4), (5, 6)]
>>> d.items.insert(-1, (7, 8))
>>> d.items
[(0, 0), (1, 2), (3, 4), (7, 8), (5, 6)]
>>> d.items.pop()
(5, 6)
>>> d.items
[(0, 0), (1, 2), (3, 4), (7, 8)]
>>> d.items.remove((1, 2))
>>> d.items
[(0, 0), (3, 4), (7, 8)]
>>> d.items.extend([(1, 2), (5, 6)])
>>> d.items
[(0, 0), (3, 4), (7, 8), (1, 2), (5, 6)]


Instance Methods [hide private]
  __init__(self, init_val=(), strict=True)
Create a new ordered dictionary.
  __setattr__(self, name, value)
Protect keys, items, and values.

Inherited from dict: __cmp__, __contains__, __getattribute__, __hash__, __len__, __new__, fromkeys, get, has_key

Inherited from object: __delattr__, __reduce__, __reduce_ex__

    Inherited from OrderedDict
  __deepcopy__(self, memo)
To allow deepcopy to work with OrderedDict.
  __delitem__(self, key)
del x[y]
  __eq__(self, other)
x==y
  __ge__(self, other)
x>=y
  __getattr__(self, name)
Implemented so that access to sequence raises a warning.
  __getitem__(self, key)
Allows slicing.
  __gt__(self, other)
x>y
  __iter__(self)
iter(x)
  __le__(self, other)
x<=y
  __lt__(self, other)
x<y
  __ne__(self, other)
x!=y
  __repr__(self)
Used for __repr__ and __str__
  __setitem__(self, key, val)
Allows slice assignment, so long as the slice is an OrderedDict >>> d = OrderedDict() >>> d['a'] = 'b' >>> d['b'] = 'a' >>> d[3] = 12 >>> d OrderedDict([('a', 'b'), ('b', 'a'), (3, 12)]) >>> d[:] = OrderedDict(((1, 2), (2, 3), (3, 4))) >>> d OrderedDict([(1, 2), (2, 3), (3, 4)]) >>> d[::2] = OrderedDict(((7, 8), (9, 10))) >>> d OrderedDict([(7, 8), (2, 3), (9, 10)]) >>> d = OrderedDict(((0, 1), (1, 2), (2, 3), (3, 4))) >>> d[1:3] = OrderedDict(((1, 2), (5, 6), (7, 8))) >>> d OrderedDict([(0, 1), (1, 2), (5, 6), (7, 8), (3, 4)]) >>> d = OrderedDict(((0, 1), (1, 2), (2, 3), (3, 4)), strict=True) >>> d[1:3] = OrderedDict(((1, 2), (5, 6), (7, 8))) >>> d OrderedDict([(0, 1), (1, 2), (5, 6), (7, 8), (3, 4)])
  __str__(self)
Used for __repr__ and __str__
  clear(self)
Remove all items from D.
  copy(self)
  index(self, key)
Return the position of the specified key in the OrderedDict.
  insert(self, index, key, value)
Takes index, key, and value as arguments.
  items(self)
items returns a list of tuples representing all the (key, value) pairs in the dictionary.
  iteritems(self)
  iterkeys(self)
iter(x)
  itervalues(self)
  keys(self)
Return a list of keys in the OrderedDict.
  pop(self, key, *args)
No dict.pop in Python 2.2, gotta reimplement it
  popitem(self, i=-1)
Delete and return an item specified by index, not a random one as in dict.
  reverse(self)
Reverse the order of the OrderedDict.
  setdefault(self, key, defval=None)
  setitems(self, items)
This method allows you to set the items in the dict.
  setkeys(self, keys)
setkeys all ows you to pass in a new list of keys which will replace the current set.
  setvalues(self, values)
You can pass in a list of values, which will replace the current list.
  sort(self, *args, **kwargs)
Sort the key order in the OrderedDict.
  update(self, from_od)
Update from another OrderedDict or sequence of (key, value) pairs
  values(self, values=None)
Return a list of all the values in the OrderedDict.

Class Variables [hide private]

Inherited from object: __class__


Method Details [hide private]

__init__(self, init_val=(), strict=True)
(Constructor)

source code 

Create a new ordered dictionary. Cannot init from a normal dict, nor from kwargs, since items order is undefined in those cases.

If the strict keyword argument is True (False is the default) then when doing slice assignment - the OrderedDict you are assigning from must not contain any keys in the remaining dict.

>>> OrderedDict()
OrderedDict([])
>>> OrderedDict({1: 1})
Traceback (most recent call last):
TypeError: undefined order, cannot get items from dict
>>> d = OrderedDict(((1, 3), (3, 2), (2, 1)))
>>> d
OrderedDict([(1, 3), (3, 2), (2, 1)])
>>> OrderedDict(d)
OrderedDict([(1, 3), (3, 2), (2, 1)])
Overrides: OrderedDict.__init__
(inherited documentation)

__setattr__(self, name, value)

source code 
Protect keys, items, and values.
Overrides: OrderedDict.__setattr__