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
MultiDict
is a dictionary subclass customized to deal with multiple values for the same key.MultiDict
implements all standard dictionary methods. Internally, it saves all values for a key as a list, but the standarddict
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 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
dict
thus alldict
functions 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
dict
by appending items to the list.
- lists()[source]¶
Return an iterator of
(key, values)
pairs, wherevalues
is thelist
of 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
flat
isTrue
the returneddict
will only have the first item present, ifflat
isFalse
all values will be returned as lists.
- update(mapping)[source]¶
update()
extends rather than replaces existing key lists.If the value
list
for a key inother_dict
is empty, no new values will be added to thedict
and the key will not be created.
- pop(key, default=no value)[source]¶
Pop the first item for a
list
on thedict
.Afterwards the
key
is removed from thedict
, so additional values are discarded.
- class pcapkit.corekit.multidict.OrderedMultiDict(mapping=None)[source]¶
-
Works like a regular
MultiDict
but 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
list
you can us theitems()
method and pass itmulti=True
.In general an
OrderedMultiDict
is an order of magnitude slower than aMultiDict
.Notes
Due to a limitation in Python you cannot convert an ordered multi dict into a regular
dict
by usingdict(multidict)
. Instead you have to use theto_dict()
method, otherwise the internal bucket objects are exposed.