mode.utils.collections
¶
Custom data structures.
- class mode.utils.collections.AttributeDict¶
Dict subclass with attribute access.
- class mode.utils.collections.AttributeDictMixin¶
Mixin for Mapping interface that adds attribute access.
I.e., d.key -> d[key]).
- class mode.utils.collections.DictAttribute(obj: Any)¶
Dict interface to attributes.
obj[k] -> obj.k obj[k] = val -> obj.k = val
- get(k[, d]) D[k] if k in D, else d. d defaults to None. ¶
- obj: Any = None¶
- setdefault(k[, d]) D.get(k,d), also set D[k]=d if k not in D ¶
- class mode.utils.collections.FastUserDict¶
Proxy to dict.
Like
collection.UserDict
but reimplements some methods for better performance when the underlying dictionary is a real dict.- clear() None. Remove all items from D. ¶
- copy() dict ¶
- data: MutableMapping[KT, VT]¶
- classmethod fromkeys(iterable: Iterable[KT], value: Optional[VT] = None) FastUserDict ¶
- items() a set-like object providing a view on D's items ¶
- keys() a set-like object providing a view on D's keys ¶
- update([E, ]**F) None. Update D from mapping/iterable E and F. ¶
If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v
- values() an object providing a view on D's values ¶
- class mode.utils.collections.FastUserList(initlist=None)¶
Proxy to list.
- class mode.utils.collections.FastUserSet¶
Proxy to set.
- add(element: T) None ¶
Add an element.
- clear() None ¶
This is slow (creates N new iterators!) but effective.
- copy() MutableSet[T] ¶
- data: MutableSet[T]¶
- difference(other: Union[AbstractSet[T], Iterable[T]]) MutableSet[T] ¶
- difference_update(other: Union[AbstractSet[T], Iterable[T]]) None ¶
- discard(element: T) None ¶
Remove an element. Do not raise an exception if absent.
- intersection(other: Union[AbstractSet[T], Iterable[T]]) MutableSet[T] ¶
- intersection_update(other: Union[AbstractSet[T], Iterable[T]]) None ¶
- isdisjoint(other: Iterable[T]) bool ¶
Return True if two sets have a null intersection.
- issubset(other: AbstractSet[T]) bool ¶
- issuperset(other: AbstractSet[T]) bool ¶
- pop() T ¶
Return the popped value. Raise KeyError if empty.
- remove(element: T) None ¶
Remove an element. If not a member, raise a KeyError.
- symmetric_difference(other: Union[AbstractSet[T], Iterable[T]]) MutableSet[T] ¶
- symmetric_difference_update(other: Union[AbstractSet[T], Iterable[T]]) None ¶
- union(other: Union[AbstractSet[T], Iterable[T]]) MutableSet[T] ¶
- update(other: Union[AbstractSet[T], Iterable[T]]) None ¶
- class mode.utils.collections.Heap(data: Optional[Sequence[T]] = None)¶
Generic interface to
heapq
.- insert(index: int, object: T) None ¶
S.insert(index, value) – insert value before index
- nlargest(n: int, key: Optional[Callable] = None) list[T] ¶
Find the n largest elements in the dataset.
- nsmallest(n: int, key: Optional[Callable] = None) list[T] ¶
Find the n smallest elements in the dataset.
- pop(index: int = 0) T ¶
Pop the smallest item off the heap.
Maintains the heap invariant.
- push(item: T) None ¶
Push item onto heap, maintaining the heap invariant.
- pushpop(item: T) T ¶
Push item on the heap, then pop and return from the heap.
The combined action runs more efficiently than
push()
followed by a separate call topop()
.
- replace(item: T) T ¶
Pop and return the current smallest value, and add the new item.
This is more efficient than :meth`pop` followed by
push()
, and can be more appropriate when using a fixed-size heap.Note that the value returned may be larger than item! That constrains reasonable uses of this routine unless written as part of a conditional replacement:
if item > heap[0]: item = heap.replace(item)
- class mode.utils.collections.LRUCache(limit: Optional[int] = None, *, thread_safety: bool = False)¶
LRU Cache implementation using a doubly linked list to track access.
- Parameters:
limit (int) – The maximum number of keys to keep in the cache. When a new key is inserted and the limit has been exceeded, the Least Recently Used key will be discarded from the cache.
thread_safety (bool) – Enable if multiple OS threads are going to access/mutate the cache.
- data: OrderedDict¶
- incr(key: KT, delta: int = 1) int ¶
- items() a set-like object providing a view on D's items ¶
- keys() a set-like object providing a view on D's keys ¶
- limit: Optional[int]¶
- popitem() (k, v), remove and return some (key, value) pair ¶
as a 2-tuple; but raise KeyError if D is empty.
- thread_safety: bool¶
- update([E, ]**F) None. Update D from mapping/iterable E and F. ¶
If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v
- values() an object providing a view on D's values ¶
- class mode.utils.collections.ManagedUserDict¶
A UserDict that adds callbacks for when keys are get/set/deleted.
- clear() None. Remove all items from D. ¶
- on_clear() None ¶
Handle that the mapping is being cleared.
- on_key_del(key: KT) None ¶
Handle that a key is deleted.
- on_key_get(key: KT) None ¶
Handle that key is being retrieved.
- on_key_set(key: KT, value: VT) None ¶
Handle that value for a key is being set.
- raw_update(*args: Any, **kwargs: Any) None ¶
- update([E, ]**F) None. Update D from mapping/iterable E and F. ¶
If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v
- class mode.utils.collections.ManagedUserSet¶
A MutableSet that adds callbacks for when keys are get/set/deleted.
- add(element: T) None ¶
Add an element.
- clear() None ¶
This is slow (creates N new iterators!) but effective.
- difference_update(other: Union[AbstractSet[T], Iterable[T]]) None ¶
- discard(element: T) None ¶
Remove an element. Do not raise an exception if absent.
- intersection_update(other: Union[AbstractSet[T], Iterable[T]]) None ¶
- on_add(value: T) None ¶
- on_change(added: set[T], removed: set[T]) None ¶
- on_clear() None ¶
- on_discard(value: T) None ¶
- pop() T ¶
Return the popped value. Raise KeyError if empty.
- raw_update(*args: Any, **kwargs: Any) None ¶
- symmetric_difference_update(other: Union[AbstractSet[T], Iterable[T]]) None ¶
- update(other: Union[AbstractSet[T], Iterable[T]]) None ¶
- mode.utils.collections.force_mapping(m: Any) Mapping ¶
Wrap object into supporting the mapping interface if necessary.