Multi-Mapping Dictionary¶
pcapkit.corekit.multidict contains dictionaries that store multiple
values per key, based on the Werkzeug project.
- class pcapkit.corekit.multidict.MultiDict[_KT, _VT](mapping=None)[source]¶
Bases:
dict,Generic[_KT,_VT]A
MultiDictis a dictionary subclass customized to deal with multiple values for the same key which is for example used by the parsing functions in the wrappers. This is necessary because some HTML form elements pass multiple values for the same key.MultiDictimplements all standard dictionary methods. Internally, it saves all values for a key as a list, but the standard dict access methods will only return the first value for a key. If you want to gain access to the other values, too, you have to use the list methods as explained below.Basic Usage:
>>> d = MultiDict([('a', 'b'), ('a', 'c')]) >>> d MultiDict([('a', 'b'), ('a', 'c')]) >>> d['a'] 'b' >>> d.getlist('a') ['b', 'c'] >>> 'a' in d True
It behaves like a normal dict thus all dict functions will only return the first value when multiple values for one key are found.
A
MultiDictcan be constructed from an iterable of(key, value)tuples, a dict, aMultiDictor from Werkzeug 0.2 onwards some keyword parameters.- Parameters:
mapping (
MultiDict[_KT,_VT] |Mapping[_KT,_VT|list[_VT] |tuple[_VT,...] |set[_VT]] |Iterable[tuple[_KT,_VT]] |None) – the initial value for theMultiDict. Either a regular dict, an iterable of(key, value)tuples or None.
- add(key, value)[source]¶
Adds a new value for the key.
- Parameters:
key (
_KT) – the key for the value.value (
_VT) – the value to add.
- getlist(key)[source]¶
Return the list of items for a given key. If that key is not in the MultiDict, the return value will be an empty list.
- setlist(key, new_list)[source]¶
Remove the old values for a key and add new ones. Note that the list you pass the values in will be shallow-copied before it is inserted in the dictionary.
>>> d = MultiDict() >>> d.setlist('foo', ['1', '2']) >>> d['foo'] '1' >>> d.getlist('foo') ['1', '2']
- Parameters:
key (
_KT) – The key for which the values are set.new_list (
Iterable[_VT]) – An iterable with the new values for the key. Old values are removed first.
- setdefault(key, default=None)[source]¶
Returns the value for the key if it is in the dict, otherwise it returns default and sets that value for key.
- setlistdefault(key, default_list=None)[source]¶
Like setdefault but sets multiple values. The list returned is not a copy, but the list that is actually used internally. This means that you can put new values into the dict by appending items to the list:
>>> d = MultiDict({"foo": 1}) >>> d.setlistdefault("foo").extend([2, 3]) >>> d.getlist("foo") [1, 2, 3]
- lists()[source]¶
Return a iterator of
(key, values)pairs, where values is the list of all values associated with the key.
- values()[source]¶
Returns an iterator of the first value on every key’s value list.
- Return type:
Iterable[_VT]
- listvalues()[source]¶
Return an iterator of all values associated with a key. Zipping
keys()and this is the same as callinglists():>>> d = MultiDict({"foo": [1, 2, 3]}) >>> zip(d.keys(), d.listvalues()) == d.lists() True
- to_dict(flat=True)[source]¶
Return the contents as regular dict. If flat is True the returned dict will only have the first item present, if flat is False all values will be returned as lists.
- update(mapping)[source]¶
update() extends rather than replaces existing key lists:
>>> a = MultiDict({'x': 1}) >>> b = MultiDict({'x': 2, 'y': 3}) >>> a.update(b) >>> a MultiDict([('y', 3), ('x', 1), ('x', 2)])
If the value list for a key in
other_dictis empty, no new values will be added to the dict and the key will not be created:>>> x = {'empty_list': []} >>> y = MultiDict() >>> y.update(x) >>> y MultiDict([])
- pop(key, default=_missing)[source]¶
Pop the first item for a list on the dict. Afterwards the key is removed from the dict, so additional values are discarded:
>>> d = MultiDict({"foo": [1, 2, 3]}) >>> d.pop("foo") 1 >>> "foo" in d False
- Parameters:
key (
_KT) – the key to pop.default (
_VT|_T) – if provided the value to return if the key was not in the dictionary.
- class pcapkit.corekit.multidict.OrderedMultiDict[_KT, _VT](mapping=None)[source]¶
Bases:
ReduceToItems,MultiDict[_KT,_VT]Works like a regular
MultiDictbut preserves the order of the fields. To convert the ordered multi dict into a list you can use theitems()method and pass itmulti=True.In general an
OrderedMultiDictis an order of magnitude slower than aMultiDict.note
Due to a limitation in Python you cannot convert an ordered multi dict into a regular dict by using
dict(multidict). Instead you have to use theto_dict()method, otherwise the internal bucket objects are exposed.- deepcopy(memo=None)[source]¶
Return a deep copy of this object.
Notes
MultiDict.deepcopy()rebuilds fromto_dict()withflat=False, which groups the values by key. This rebuilds fromitems(multi=True)instead, so the copy keeps the insertion order of interleaved keys.__deepcopy__()reaches it too.Changed in version 1.5.0: Keeps the insertion order of interleaved keys, where the copy grouped the values by key (#1295).
- Return type:
OrderedMultiDict[_KT,_VT]