mode.utils.collections
¶
Custom data structures.
-
class
mode.utils.collections.
Heap
(data: Sequence[T] = None)¶ Generic interface to
heapq
.-
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)
-
nlargest
(n: int, key: Callable = None) → List[T]¶ Find the n largest elements in the dataset.
-
nsmallest
(n: int, key: Callable = None) → List[T]¶ Find the n smallest elements in the dataset.
-
insert
(index: int, object: T) → None¶ S.insert(index, value) – insert value before index
-
-
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.-
data
: MutableMapping[KT, VT] = None¶
-
classmethod
fromkeys
(iterable: Iterable[KT], value: VT = None) → mode.utils.collections.FastUserDict¶
-
copy
() → dict¶
-
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
-
clear
() → None. Remove all items from D.¶
-
items
() → a set-like object providing a view on D's items¶
-
keys
() → a set-like object providing a view on D's keys¶
-
values
() → an object providing a view on D's values¶
-
-
class
mode.utils.collections.
FastUserSet
¶ Proxy to set.
-
data
: MutableSet[T] = None¶
-
copy
() → MutableSet[T]¶
-
difference
(other: Union[AbstractSet[T], Iterable[T]]) → MutableSet[T]¶
-
intersection
(other: Union[AbstractSet[T], Iterable[T]]) → MutableSet[T]¶
-
isdisjoint
(other: Iterable[T]) → bool¶ Return True if two sets have a null intersection.
-
issubset
(other: AbstractSet[T]) → bool¶
-
issuperset
(other: AbstractSet[T]) → bool¶
-
symmetric_difference
(other: Union[AbstractSet[T], Iterable[T]]) → MutableSet[T]¶
-
union
(other: Union[AbstractSet[T], Iterable[T]]) → MutableSet[T]¶
-
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¶
-
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_update
(other: Union[AbstractSet[T], Iterable[T]]) → None¶
-
update
(other: Union[AbstractSet[T], Iterable[T]]) → None¶
-
-
class
mode.utils.collections.
FastUserList
(initlist=None)¶ Proxy to list.
-
class
mode.utils.collections.
LRUCache
(limit: int = None, *, thread_safety: bool = False)¶ LRU Cache implementation using a doubly linked list to track access.
- Parameters
-
limit
: Optional[int] = None¶
-
thread_safety
: bool = None¶
-
data
: OrderedDict = 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
-
popitem
() → (k, v), remove and return some (key, value) pair¶ as a 2-tuple; but raise KeyError if D is empty.
-
keys
() → a set-like object providing a view on D's keys¶
-
values
() → an object providing a view on D's values¶
-
items
() → a set-like object providing a view on D's items¶
-
incr
(key: KT, delta: int = 1) → int¶
-
class
mode.utils.collections.
ManagedUserSet
¶ A MutableSet that adds callbacks for when keys are get/set/deleted.
-
on_add
(value: T) → None¶
-
on_discard
(value: T) → None¶
-
on_clear
() → None¶
-
on_change
(added: Set[T], removed: Set[T]) → None¶
-
add
(element: T) → None¶ Add an element.
-
clear
() → None¶ This is slow (creates N new iterators!) but effective.
-
discard
(element: T) → None¶ Remove an element. Do not raise an exception if absent.
-
pop
() → T¶ Return the popped value. Raise KeyError if empty.
-
raw_update
(*args: Any, **kwargs: Any) → None¶
-
difference_update
(other: Union[AbstractSet[T], Iterable[T]]) → None¶
-
intersection_update
(other: Union[AbstractSet[T], Iterable[T]]) → None¶
-
symmetric_difference_update
(other: Union[AbstractSet[T], Iterable[T]]) → None¶
-
update
(other: Union[AbstractSet[T], Iterable[T]]) → None¶
-
data
= None¶
-
-
class
mode.utils.collections.
ManagedUserDict
¶ A UserDict that adds callbacks for when keys are get/set/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.
-
on_key_del
(key: KT) → None¶ Handle that a key is deleted.
-
on_clear
() → None¶ Handle that the mapping is being cleared.
-
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
-
clear
() → None. Remove all items from D.¶
-
raw_update
(*args: Any, **kwargs: Any) → None¶
-
data
= None¶
-
-
class
mode.utils.collections.
AttributeDictMixin
¶ Mixin for Mapping interface that adds attribute access.
I.e., d.key -> d[key]).
-
class
mode.utils.collections.
AttributeDict
¶ Dict subclass with attribute access.
-
class
mode.utils.collections.
DictAttribute
(obj: Any)¶ Dict interface to attributes.
obj[k] -> obj.k obj[k] = val -> obj.k = val
-
obj
: Any = None¶
-
get
(k[, d]) → D[k] if k in D, else d. d defaults to None.¶
-
setdefault
(k[, d]) → D.get(k,d), also set D[k]=d if k not in D¶
-
-
mode.utils.collections.
force_mapping
(m: Any) → Mapping¶ Wrap object into supporting the mapping interface if necessary.