A collection of Mapping types.¶
This module provides specialized mapping classes built on top of Python’s
collections.abc.Mapping and collections.OrderedDict.
Module Contents¶
A |
|
A read-only |
|
Dynamically filtered read-only view on a… |
- |
-
class OrderableDict(other: _SupportsKeysAndGetItem[_KT, _VT] | collections.abc.Iterable[tuple[_KT, _VT]] =
(), /, **kwds: _VT)[source]¶ Extends:
collections.OrderedDict[_KT,_VT]A
OrderedDictsubclass with positional key manipulation.Extends
OrderedDictwithafter()andbefore()methods for inserting, moving, or querying entries relative to other keys andfirst/lastproperties for O(1) access to boundary keys.Initialize self. See help(type(self)) for accurate signature.
- property first : _KT¶
Return the first key.
- property last : _KT¶
Return the last key.
-
after(key: _KT, other: _KT | None =
None, value: _VT | None =None) tuple[_KT, _VT] | None[source]¶ Insert, move, or return a (key, value) pair after another key.
If
otheris not provided, returns the(key, value)pair found afterkeyin the ordering.If
valueisNone, movekeyafterotherin the dict (the entry must already exist).If
valueis provided, insert the(key, value)pair afterother. If the key already exists in the dict, the value is updated but no move occurs.
-
before(key: _KT, other: _KT | None =
None, value: _VT | None =None) tuple[_KT, _VT] | None[source]¶ Insert, move, or return a (key, value) pair before another key.
If
otheris not provided, returns the(key, value)pair found beforekeyin the ordering.If
valueisNone, movekeybeforeotherin the dict (the entry must already exist).If
valueis provided, insert the(key, value)pair beforeother. If the key already exists in the dict, the value is updated but no move occurs.
-
class FrozenMap(other: collections.abc.Mapping[_KT, _VT] | None =
None, /, **kwds: _VT)[source]¶ Extends:
collections.abc.Mapping[_KT,_VT]A read-only
Mappingthat cannot be modified.Items are provided at instantiation and cannot be added, changed, or removed afterward. Note that
FrozenMapdoes not enforce the immutability of the values themselves; use immutable containers as values if that is required.Note
This class exists primarily because
types.MappingProxyType, which offers similar behavior cannot be subclassed.The following dunder methods are implemented to satisfy the
Mappingprotocol:__repr__()produces aFrozenMap({...})string.
- Parameters:¶
- other: collections.abc.Mapping[_KT, _VT] | None =
None¶ The source mapping to freeze. If
None, keyword arguments are used instead. Defaults toNone.- **kwds: _VT¶
Additional key-value pairs merged into the mapping.
- other: collections.abc.Mapping[_KT, _VT] | None =
- keys() collections.abc.KeysView[_KT][source]¶
Return a view of the keys in the mapping.
- Returns:¶
KeysView– A view of the keys in the mapping.
- values() collections.abc.ValuesView[_VT][source]¶
Return a view of the values in the mapping.
- Returns:¶
ValuesView– A view of the values in the mapping.
- items() collections.abc.ItemsView[_KT, _VT][source]¶
Return a view of the (key, value) pairs in the mapping.
- Returns:¶
ItemsView– A view of the (key, value) pairs in the mapping.
- view_filter¶
-
class FilteredView(source: collections.abc.Mapping[_KT, _VT], filter: view_filter | None =
None)[source]¶ Extends:
FrozenMap[_KT,_VT]Dynamically filtered read-only view on a
Mapping.Provides a live, filtered view of a source mapping: when the source mapping is modified, this view reflects those changes automatically.
Dunder methods
__contains__(),__getitem__(),__len__(), and__iter__()all respect the filter predicate, only entries for which the filter returnsTrueare visible.Examples:
>>> d = {'a': 1, 'b': 2, 'c': 3} >>> FilteredView(d, lambda k, v: v > 1) FilteredView({'b': 2, 'c': 3})- Parameters:¶
- source: collections.abc.Mapping[_KT, _VT]¶
The source mapping for this view.
- filter : callable, optional¶
A callable
(key, value) -> boolthat determines whether an entry is included. IfNone, an identity-like filter is used: entries whose value evaluates toFalseare excluded. Defaults toNone.
inherited Members¶ keys()Return a view of the keys in the mapping.
values()Return a view of the values in the mapping.
items()Return a view of the (key, value) pairs in the…