graviti.utility.collections#

Basic concepts of user-defined objects.

Module Contents#

Classes#

UserSequence

UserSequence is a user-defined wrapper around sequence objects.

UserMutableSequence

UserMutableSequence is a user-defined wrapper around mutable sequence objects.

UserMapping

UserMapping is a user-defined wrapper around mapping objects.

UserMutableMapping

UserMutableMapping is a user-defined wrapper around mutable mapping objects.

FrozenNameOrderedDict

This class is an immutable dict of ordered elements, supports searching the element by index.

NameOrderedDict

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.

index(self, value, start=0, stop=maxsize)[source]#

Return the first index of the value.

Parameters
  • value (_T) – The value to be found.

  • start (int) – The start index of the subsequence.

  • stop (int) – The end index of the subsequence.

Returns

The First index of value.

Return type

int

count(self, value)[source]#

Return the number of occurrences of value.

Parameters

value (_T) – The value to be counted the number of occurrences.

Returns

The number of occurrences of value.

Return type

int

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

clear(self)[source]#

Remove all items from 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

reverse(self)[source]#

Reverse the items of the mutable sequence in place.

Return type

None

pop(self, index=- 1)[source]#

Return the item at index (default last) and remove it from the mutable sequence.

Parameters

index (int) – Position of the mutable sequence.

Returns

Element to be removed from the mutable sequence.

Return type

_T

remove(self, value)[source]#

Remove the first occurrence of value.

Parameters

value (_T) – Element to be removed from 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]

keys(self)[source]#

Return a new view of the keys in dict.

Returns

The keys in dict.

Return type

KeysView[_K]

values(self)[source]#

Return a new view of the values in dict.

Returns

The values in dict.

Return type

ValuesView[_V]

class graviti.utility.collections.UserMutableMapping[source]#

Bases: MutableMapping[_K, _V], UserMapping[_K, _V]

UserMutableMapping is a user-defined wrapper around mutable mapping objects.

clear(self)[source]#

Remove all items from the mutable mapping object.

Return type

None

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.

popitem(self)[source]#

Remove and return a (key, value) pair as a tuple.

Pairs are returned in LIFO (last-in, first-out) order.

Raises

KeyError – When the dict is empty.

Returns

A (key, value) pair as a tuple.

Return type

Tuple[str, _V]