Multi-Mapping Dictionary¶
pcapkit.corekit.multidict contains multi-mapping dictionary classes,
which are used to store multiple mappings of the same key. The implementation
is inspired and based on the Werkzeug project.
- class pcapkit.corekit.multidict.MultiDict(mapping=None)[source]¶
Bases:
dict,Generic[_KT,_VT]A
MultiDictis a dictionary subclass customized to deal with multiple values for the same key.MultiDictimplements all standard dictionary methods. Internally, it saves all values for a key as a list, but the standarddictaccess 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 thegetlist()and similar methods.- Parameters:
mapping (
Union[dict[TypeVar(_KT),TypeVar(_VT)],Iterable[tuple[TypeVar(_KT),TypeVar(_VT)]],None]) – The initial value for theMultiDict. Either a regulardict, an iterable of(key, value)tuples, orNone.
It behaves like a normal
dictthus alldictfunctions will only return the first value when multiple values for one key are found.See also
The class is inspired from and based on the Werkzeug project (c.f.
werkzeug.datastructures.MultiDict).- 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.
Notes
The list you pass the values in will be shallow-copied before it is inserted in the dictionary.
- setdefault(key, default=None)[source]¶
If key is in the dictionary, returns its value.
If not, set it to default and return default.
- 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
dictby appending items to the list.
- lists()[source]¶
Return an iterator of
(key, values)pairs, wherevaluesis thelistof all values associated with the key.
- listvalues()[source]¶
Return an iterator of all values associated with a key. Zipping
keys()and this is the same as callinglists().
- to_dict(flat=True)[source]¶
Return the contents as regular
dict.If
flatisTruethe returneddictwill only have the first item present, ifflatisFalseall values will be returned as lists.
- update(mapping)[source]¶
update()extends rather than replaces existing key lists.If the value
listfor a key inother_dictis empty, no new values will be added to thedictand the key will not be created.
- pop(key, default=no value)[source]¶
Pop the first item for a
liston thedict.Afterwards the
keyis removed from thedict, so additional values are discarded.
- class pcapkit.corekit.multidict.OrderedMultiDict(mapping=None)[source]¶
-
Works like a regular
MultiDictbut preserves the order of the fields.- Parameters:
mapping (
Union[dict[TypeVar(_KT),TypeVar(_VT)],Iterable[tuple[TypeVar(_KT),TypeVar(_VT)]],None]) – The initial value for theMultiDict. Either a regulardict, an iterable of(key, value)tuples, orNone.
To convert the ordered multi dict into a
listyou can us theitems()method and pass itmulti=True.In general an
OrderedMultiDictis an order of magnitude slower than aMultiDict.Notes
Due to a limitation in Python you cannot convert an ordered multi dict into a regular
dictby usingdict(multidict). Instead you have to use theto_dict()method, otherwise the internal bucket objects are exposed.