eventstreamr2.utils — Utilities¶
Contents
eventstreamr2.utils.collections¶
Concrete Classes¶
- class eventstreamr2.utils.collections.PrioritySubDictionary(parent_dict, key)[source]¶
An implementation of AbstractPriorityDictionary.
This priority dictionary get’s it contents from the values of key in the parent dictionary provided.
Abstract Classes¶
- class eventstreamr2.utils.collections.AbstractPriorityDictionary[source]¶
An immutable dictionary that combines the contents of multiple other dictionaries together; with the value for a key taken from the dictionary that has the highest priority.
Subclasses simply need to provide an implementation for _ordered_configs().
This is an example to show how this method resolves keys:
>>> class APD(AbstractPriorityDictionary): ... def _ordered_configs(self): ... return [ ... {"a": 1, "b": 1.2}, ... { "b": 2.2, "d": 2.4, "z", {"a": 1}}, ... {"a": 3, "e": 3.4, "z", {"a": 2}}, ... ] ... >>> c = APD() >>> c["a"] 1 >>> c.get("d") 3.5 >>> c["f"] KeyError >>> c.get("f", "default") "default" >>> c.all("a") [1, 3] >>> c.all("f") [] >>> c.get_subdictionary("z").all("a") [1, 2]
Note
This class does no caching; so each call will recalculate the values. This can lead to values changing between calls. Be careful with it.
See also
This module includes collections.Mapping and this provides additional functionality.
- _ordered_configs()[source]¶
This should return an list(dict)(A list containing dictionaries) from which the key/value pairs for this dictionary are taken.
The earlier in the list, the higher priority.
Note
Only this method needs to be implemented.
- get(k[, d]) → D[k] if k in D, else d. d defaults to None.¶
- all(key)[source]¶
Returns a list of all the values associated with the key.
The order of the elements in the returned list is from highest priority(first) to lowest.
If the key is missing then an empty dictionary is returned.
- keys()[source]¶
Creates a set of all the keys across all the dictionaries.
Note
Each key will appear exactly once and in an undefined order.
- items() → list of D's (key, value) pairs, as 2-tuples¶
- values() → list of D's values¶
eventstreamr2.utils.events¶
- class eventstreamr2.utils.events.Observable(*args, **kwargs)[source]¶
An mixin object that provides an event listener style interface.
Note
It is reccomended that add_weak_observer() is used whenever possible as it handles cleanup better than it’s strong counterpart.
Note
remove_weak_observer is missing intentionally; Primarily because it is unlikely to be needed; and secondly because it is hard to implement a remove method inside WeakFunctionCollection.
- add_weak_observer(fn)[source]¶
Add an observer function that is stored weakly. This means that when the given function leaves scope, it will no longer be called. Special handling is done on bound methods to ensure that they are not removed until the object they are bound on leaves scope.
- _notify_observers(*args, **kwargs)[source]¶
Notify all existing observers, or more specifically call each observer with any arguments(both positional and keyword) passed in.
All listeners defined at the start of this method’s execution will be called and any changes afterwards will not affect this method call.