eggbasket.odict.SequenceOrderedDict

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

## Type SequenceOrderedDict

    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)]

Method Summary
| ``__init__`(self, init_val, strict)` Create a new ordered dictionary.
| ``__setattr__`(self, name, value)` Protect keys, items, and values.
Inherited from OrderedDict
| ``__deepcopy__`(self, memo)` To allow deepcopy to work with OrderedDict.
| ``__delitem__`(self, key)`
| ``__eq__`(self, other)`
| ``__ge__`(self, other)`
| ``__getattr__`(self, name)` Implemented so that access to `sequence` raises a warning.
| ``__getitem__`(self, key)` Allows slicing.
| ``__gt__`(self, other)`
| ``__iter__`(self)`
| ``__le__`(self, other)`
| ``__lt__`(self, other)`
| ``__ne__`(self, other)`
| ``__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)`
| ``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)`
| ``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)` Delete and return an item specified by index, not a random one as in dict.
| ``rename`(self, old_key, new_key)` Rename the key for a given value, without modifying sequence order.
| ``reverse`(self)` Reverse the order of the OrderedDict.
| ``setdefault`(self, key, defval)`
| ``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)` Return a list of all the values in the OrderedDict.
Inherited from dict
| ``__cmp__`(x, y)` x.__cmp__(y) <==> cmp(x,y)
| ``__contains__`(D, k)` D.__contains__(k) -> True if D has a key k, else False
| ``__getattribute__`(...)` x.__getattribute__('name') <==> x.name
| ``__hash__`(x)` x.__hash__() <==> hash(x)
| ``__len__`(x)` x.__len__() <==> len(x)
| ``__new__`(T, S, ...)` T.__new__(S, ...) -> a new object with type S, a subtype of T
| ``get`(D, k, d)` D.get(k[,d]) -> D[k] if k in D, else d.
| ``has_key`(D, k)` D.has_key(k) -> True if D has a key k, else False
Inherited from object
| ``__delattr__`(...)` x.__delattr__('name') <==> del x.name
| ``__reduce__`(...)` helper for pickle
| ``__reduce_ex__`(...)` helper for pickle
Inherited from type
| ``fromkeys`(dict, S, v)` v defaults to None.

Method Details

### __init__(self, init_val=(), strict=True) (Constructor) 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
    >>> OrderedDict({1: 1}.items())
    OrderedDict([(1, 1)])
    >>> 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: `eggbasket.odict.OrderedDict.__init__` (inherited documentation)

### __setattr__(self, name, value) Protect keys, items, and values. Overrides: `eggbasket.odict.OrderedDict.__setattr__`

Generated by Epydoc 2.1 on Mon Apr 21 14:16:57 2008 | http://epydoc.sf.net