mode.utils.collections

Custom data structures.

class mode.utils.collections.FastUserDict[source]

Proxy to dict.

Like collection.UserDict but reimplements some methods for better performance when the underlying dictionary is a real dict.

classmethod fromkeys(iterable: Iterable[KT], value: VT = None) → mode.utils.collections.FastUserSet[source]
copy() → dict[source]
update([E, ]**F) → None. Update D from mapping/iterable E and F.[source]

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.[source]
items() → a set-like object providing a view on D's items[source]
keys() → a set-like object providing a view on D's keys[source]
values() → an object providing a view on D's values[source]
class mode.utils.collections.FastUserSet[source]

Proxy to set.

copy() → Set[T][source]
difference(other: Union[AbstractSet[T], Iterable[T]]) → Set[T][source]
intersection(other: Union[AbstractSet[T], Iterable[T]]) → Set[T][source]
isdisjoint(other: AbstractSet[T]) → bool[source]

Return True if two sets have a null intersection.

issubset(other: AbstractSet[T]) → bool[source]
issuperset(other: AbstractSet[T]) → bool[source]
symmetric_difference(other: Union[AbstractSet[T], Iterable[T]]) → Set[T][source]
union(other: Union[AbstractSet[T], Iterable[T]]) → Set[T][source]
add(element: T) → None[source]

Add an element.

clear() → None[source]

This is slow (creates N new iterators!) but effective.

difference_update(other: Union[AbstractSet[T], Iterable[T]]) → None[source]
discard(element: T) → None[source]

Remove an element. Do not raise an exception if absent.

intersection_update(other: Union[AbstractSet[T], Iterable[T]]) → None[source]
pop() → T[source]

Return the popped value. Raise KeyError if empty.

remove(element: T) → None[source]

Remove an element. If not a member, raise a KeyError.

symmetric_difference_update(other: Union[AbstractSet[T], Iterable[T]]) → None[source]
update(other: Union[AbstractSet[T], Iterable[T]]) → None[source]
class mode.utils.collections.FastUserList(initlist=None)[source]

Proxy to list.

class mode.utils.collections.LRUCache(limit: int = None, *, thread_safety: bool = False)[source]

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.

update([E, ]**F) → None. Update D from mapping/iterable E and F.[source]

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[source]

as a 2-tuple; but raise KeyError if D is empty.

keys() → a set-like object providing a view on D's keys[source]
values() → an object providing a view on D's values[source]
items() → a set-like object providing a view on D's items[source]
incr(key: KT, delta: int = 1) → int[source]
class mode.utils.collections.ManagedUserSet[source]

A MutableSet that adds callbacks for when keys are get/set/deleted.

on_add(value: T) → None[source]
on_discard(value: T) → None[source]
on_clear() → None[source]
on_change(added: Set[T], removed: Set[T]) → None[source]
add(element: T) → None[source]

Add an element.

clear() → None[source]

This is slow (creates N new iterators!) but effective.

discard(element: T) → None[source]

Remove an element. Do not raise an exception if absent.

pop() → T[source]

Return the popped value. Raise KeyError if empty.

raw_update(*args: Any, **kwargs: Any) → None[source]
difference_update(other: Union[AbstractSet[T], Iterable[T]]) → None[source]
intersection_update(other: Union[AbstractSet[T], Iterable[T]]) → None[source]
symmetric_difference_update(other: Union[AbstractSet[T], Iterable[T]]) → None[source]
update(other: Union[AbstractSet[T], Iterable[T]]) → None[source]
class mode.utils.collections.ManagedUserDict[source]

A UserDict that adds callbacks for when keys are get/set/deleted.

on_key_get(key: KT) → None[source]

Handle that key is being retrieved.

on_key_set(key: KT, value: VT) → None[source]

Handle that value for a key is being set.

on_key_del(key: KT) → None[source]

Handle that a key is deleted.

on_clear() → None[source]

Handle that the mapping is being cleared.

update([E, ]**F) → None. Update D from mapping/iterable E and F.[source]

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.[source]
raw_update(*args: Any, **kwargs: Any) → None[source]
class mode.utils.collections.AttributeDictMixin[source]

Mixin for Mapping interface that adds attribute access.

I.e., d.key -> d[key]).

class mode.utils.collections.AttributeDict[source]

Dict subclass with attribute access.

class mode.utils.collections.DictAttribute(obj: Any)[source]

Dict interface to attributes.

obj[k] -> obj.k obj[k] = val -> obj.k = val

obj = None
get(k[, d]) → D[k] if k in D, else d. d defaults to None.[source]
setdefault(k[, d]) → D.get(k,d), also set D[k]=d if k not in D[source]
mode.utils.collections.force_mapping(m: Any) → Mapping[source]

Wrap object into supporting the mapping interface if necessary.