graviti.utility.collections
#
Basic concepts of user-defined objects.
Module Contents#
Classes#
UserSequence is a user-defined wrapper around sequence objects. |
|
UserMutableSequence is a user-defined wrapper around mutable sequence objects. |
|
UserMapping is a user-defined wrapper around mapping objects. |
|
UserMutableMapping is a user-defined wrapper around mutable mapping objects. |
|
This class is an immutable dict of ordered elements, supports searching the element by index. |
|
This class is a dict of ordered elements, supports searching the element by its index. |
- class graviti.utility.collections.UserSequence[source]#
Bases:
Sequence
[_T
],graviti.utility.repr.ReprMixin
UserSequence is a user-defined wrapper around sequence objects.
- class graviti.utility.collections.UserMutableSequence[source]#
Bases:
MutableSequence
[_T
],UserSequence
[_T
]UserMutableSequence is a user-defined wrapper around mutable sequence objects.
- insert(self, index, value)[source]#
Insert object before index.
- Parameters
index (int) – Position of the mutable sequence.
value (_T) – Element to be inserted into the mutable sequence.
- Return type
None
- append(self, value)[source]#
Append object to the end of the mutable sequence.
- Parameters
value (_T) – Element to be appended to the mutable sequence.
- Return type
None
- extend(self, values)[source]#
Extend mutable sequence by appending elements from the iterable.
- Parameters
values (Iterable[_T]) – Elements to be Extended into the mutable sequence.
- Return type
None
- class graviti.utility.collections.UserMapping[source]#
Bases:
Mapping
[_K
,_V
],graviti.utility.repr.ReprMixin
UserMapping is a user-defined wrapper around mapping objects.
- get(self, key: _K) Optional[_V] [source]#
- get(self, key: _K, default: Union[_V, _T] = ...) Union[_V, _T]
Return the value for the key if it is in the dict, else default.
- Parameters
key – The key for dict, which can be any immutable type.
default – The value to be returned if key is not in the dict.
- Returns
The value for the key if it is in the dict, else default.
- items(self)[source]#
Return a new view of the (key, value) pairs in dict.
- Returns
The (key, value) pairs in dict.
- Return type
ItemsView[_K, _V]
- class graviti.utility.collections.UserMutableMapping[source]#
Bases:
MutableMapping
[_K
,_V
],UserMapping
[_K
,_V
]UserMutableMapping is a user-defined wrapper around mutable mapping objects.
- pop(self, key: _K) _V [source]#
- pop(self, key: _K, default: Union[_V, _T] = ...) Union[_V, _T]
Remove specified item and return the corresponding value.
- Parameters
key – The key for dict, which can be any immutable type.
default – The value to be returned if the key is not in the dict and it is given.
- Returns
Value to be removed from the mutable mapping object.
- popitem(self)[source]#
Remove and return a (key, value) pair as a tuple.
Pairs are returned in LIFO (last-in, first-out) order.
- Returns
A (key, value) pair as a tuple.
- Return type
Tuple[_K, _V]
- setdefault(self, key, default=None)[source]#
Set the value of the item with the specified key.
If the key is in the dict, return the corresponding value. If not, insert the key with a value of default and return default.
- Parameters
key (_K) – The key for dict, which can be any immutable type.
default (_V) – The value to be set if the key is not in the dict.
- Returns
The value for key if it is in the dict, else default.
- Return type
_V
- update(self, __m: _SupportsKeysAndGetItem[_K, _V], **kwargs: _V) None [source]#
- update(self, __m: Iterable[Tuple[_K, _V]], **kwargs: _V) None
- update(self, **kwargs: _V) None
Update the dict.
- Parameters
__m – A dict object, a generator object yielding a (key, value) pair or other object which has a .keys() method.
**kwargs – The value to be added to the mutable mapping.
- class graviti.utility.collections.FrozenNameOrderedDict(items=None)[source]#
Bases:
Mapping
[str
,_V
],graviti.utility.repr.ReprMixin
This class is an immutable dict of ordered elements, supports searching the element by index.
- Parameters
items (Union[Iterable[Tuple[str, _V]], Mapping[str, _V], None]) – The items need to be stored into the FrozenNameOrderedDict.
- class graviti.utility.collections.NameOrderedDict(items=None)[source]#
Bases:
MutableMapping
[str
,_V
],FrozenNameOrderedDict
[_V
]This class is a dict of ordered elements, supports searching the element by its index.
- Parameters
items (Union[Iterable[Tuple[str, _V]], Mapping[str, _V], None]) – The items need to be stored into the NameOrderedDict.