Module Bio.EUtils.MultiDict
Dictionary-like objects which allow multiple keys
Python dictionaries map a key to a value. Duplicate keys are not
allowed, and new entries replace old ones with the same key. Order is
not otherwise preserved, so there's no way to get the items in the
order they were added to a dictionary.
Some types of data is best stored in dictionary-like object which
allow multiple values per key. Some of these need the input order
strongly preserved, so the items can be retrieved in the same order as
they were added to the dictionary. That is the OrderedMultiDict.
Others need a weaker ordering guarantee where the order of values for
a given key is preserved but the order between the keys is not. That
is UnorderedMultiDict. (Because strong ordering isn't needed, it's
faster to delete from an UnorderedMultiDict.)
To create a MultiDict, pass in an object which implements the
'allitems' method and returns a list of (key, value) pairs, or
pass in the list of (key, value) pairs directly.
The two MultiDict classes implement the following dictionary methods
d["lookup"],
d["key"] = value
del d[key]
d.get("key", default = None)
d1 == d2, d1 != d2, len(d), iter(d), str(d)
d.keys(), d.values(), d.items()
The new methods are:
d.getall(key)
d.allkeys()
d.allvalues()
d.allitems()
>>> import MultiDict
>>> od = MultiDict.OrderedMultiDict()
>>> od["Name"] = "Andrew"
>>> od["Color"] = "BLUE"
>>> od["Name"] = "Dalke"
>>> od["Color"] = "Green"
>>> od[3] = 9
>>> len(od)
3
>>> od["Name"]
'Dalke'
>>> od.getall("Name")
['Andrew', 'Dalke']
>>> for k, v in od.allitems():
... print "%r == %r" % (k, v)
...
'Name' == 'Andrew'
'Color' == 'BLUE'
'Name' == 'Dalke'
'Color' == 'Green'
3 == 9
>>> del od["Name"]
>>> len(od)
2
>>> for k, v in od.allitems():
... print "%r == %r" % (k, v)
...
'Color' == 'BLUE'
'Color' == 'Green'
3 == 9
>>>
The latest version of this code can be found at
http://www.dalkescientific.com/Python/