dantro.utils.ordereddict module¶
Definition of an OrderedDict-subclass that maintains the order key values rather than the insertion order. It can be specialized to use a specific comparison function for ordering keys.
-
class
dantro.utils.ordereddict.KeyOrderedDict(*args, key: Callable = None, **kwds)[source]¶ Bases:
dictThis dict maintains the order of keys not by their insertion but by their value. It is a re-implementation of collections.OrderedDict, because subclassing that class is really difficult.
Ordering is maintained by adjusting those methods of OrderedDict that take care of building and maintaining the doubly-linked list that provides the ordering. See OrderedDict for details, e.g. the weak-referencing.
In effect, this relates only to __setitem__; all other methods rely on this to add elements to the mapping.
For comparison, the
keycallable given at initialisation can be used to perform a operation on keys, the result of which is used in comparison. If this is not given, theDEFAULT_KEY_COMPARATORclass variable is used; note that this needs to be a binary function, where the first argument is equivalent toselfand the second is the actual key to perform the unary operation on.-
DEFAULT_KEY_COMPARATOR(k)¶
-
__init__(*args, key: Callable = None, **kwds)[source]¶ Initialize a KeyOrderedDict, which maintains key ordering. If no custom ordering function is given, orders by simple “smaller than” comparison.
Apart from that, the interface is the same as for regular dictionaries.
- Parameters
*args – a single sequence of (key, value) pairs to insert
key (Callable, optional) – The callable used to
**kwds – Passed on to
updatemethod
- Raises
TypeError – on len(args) > 1
-
_key_comp_lt(k1, k2) → bool[source]¶ The key comparator. Returns true for k1 < k2.
Before comparison, the unary
self._keymethod is invoked on both keys.- Parameters
k1 – lhs of comparison
k2 – rhs of comparison
- Returns
k1 < k2
- Return type
bool
-
__setitem__(key, value, *, dict_setitem=<slot wrapper '__setitem__' of 'dict' objects>, proxy=<built-in function proxy>, Link=<class 'collections._Link'>)[source]¶ Set the item with the provided key to the given value, maintaining the ordering specified by NEW_KEY_GT_NEXT_KEY
If the key is not available, this takes care of finding the right place to insert the new link in the doubly-linked list.
-
insert(key, value, *, hint_after=None, hint_before=None)[source]¶ Inserts a (key, value) pair using hint information to speed up key search.
-
__delitem__(key, dict_delitem=<slot wrapper '__delitem__' of 'dict' objects>)[source]¶ kod.__delitem__(y) <==> del kod[y]
Deleting an existing item uses self.__map to find the link which gets removed by updating the links in the predecessor and successor nodes.
-
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
-
pop(k[, d]) → v, remove specified key and return the[source]¶ corresponding value. If key is not found, default is returned if given, otherwise KeyError is raised.
-
classmethod
fromkeys(S[, v]) → New key-ordered dictionary with keys from S.[source]¶ If not specified, the value defaults to None.
-
__eq__(other)[source]¶ kod.__eq__(y) <==> kod==y. Comparison to another KeyOrderedDict or OrderedDict is order-sensitive while comparison to a regular mapping is order-insensitive.
-
_KeyOrderedDict__marker= <object object>¶
-
_KeyOrderedDict__update(**kwds)¶ D.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
-
get()¶ Return the value for key if key is in the dictionary, else default.
-
popitem() → (k, v), remove and return some (key, value) pair as a¶ 2-tuple; but raise KeyError if D is empty.
-
-
class
dantro.utils.ordereddict.IntOrderedDict(*args, key: Callable = None, **kwds)[source]¶ Bases:
dantro.utils.ordereddict.KeyOrderedDictA
KeyOrderedDictspecialization that assumes keys to be castable to integer and using the comparison of the resulting integer values for maintaining the order-
DEFAULT_KEY_COMPARATOR(k)¶
-
_KeyOrderedDict__marker= <object object>¶
-
_KeyOrderedDict__update(**kwds)¶ D.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
-
__delitem__(key, dict_delitem=<slot wrapper '__delitem__' of 'dict' objects>)¶ kod.__delitem__(y) <==> del kod[y]
Deleting an existing item uses self.__map to find the link which gets removed by updating the links in the predecessor and successor nodes.
-
__eq__(other)¶ kod.__eq__(y) <==> kod==y. Comparison to another KeyOrderedDict or OrderedDict is order-sensitive while comparison to a regular mapping is order-insensitive.
-
__init__(*args, key: Callable = None, **kwds)¶ Initialize a KeyOrderedDict, which maintains key ordering. If no custom ordering function is given, orders by simple “smaller than” comparison.
Apart from that, the interface is the same as for regular dictionaries.
- Parameters
*args – a single sequence of (key, value) pairs to insert
key (Callable, optional) – The callable used to
**kwds – Passed on to
updatemethod
- Raises
TypeError – on len(args) > 1
-
__iter__() <==> iter(kod)¶ Traverse the linked list in order.
-
__reduce__()¶ Return state information for pickling
-
__reversed__() <==> reversed(kod)¶ Traverse the linked list in reverse order.
-
__setitem__(key, value, *, dict_setitem=<slot wrapper '__setitem__' of 'dict' objects>, proxy=<built-in function proxy>, Link=<class 'collections._Link'>)¶ Set the item with the provided key to the given value, maintaining the ordering specified by NEW_KEY_GT_NEXT_KEY
If the key is not available, this takes care of finding the right place to insert the new link in the doubly-linked list.
-
__sizeof__()¶ Get the size of this object
-
_key_comp_lt(k1, k2) → bool¶ The key comparator. Returns true for k1 < k2.
Before comparison, the unary
self._keymethod is invoked on both keys.- Parameters
k1 – lhs of comparison
k2 – rhs of comparison
- Returns
k1 < k2
- Return type
bool
-
clear()¶ Remove all items
-
copy() → a shallow copy of kod, maintaining the key comparator¶
-
classmethod
fromkeys(S[, v]) → New key-ordered dictionary with keys from S.¶ If not specified, the value defaults to None.
-
get()¶ Return the value for key if key is in the dictionary, else default.
-
insert(key, value, *, hint_after=None, hint_before=None)¶ Inserts a (key, value) pair using hint information to speed up key search.
-
items() → a set-like object providing a view on D's items¶
-
keys() → a set-like object providing a view on D's keys¶
-
pop(k[, d]) → v, remove specified key and return the¶ corresponding value. If key is not found, default is returned if given, otherwise KeyError is raised.
-
popitem() → (k, v), remove and return some (key, value) pair as a¶ 2-tuple; but raise KeyError if D is empty.
-
setdefault(k[, d]) → kod.get(k,d), also set kod[k]=d if k not¶ in kod
-
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¶
-