ubii.framework.util.collections module¶
- class ubii.framework.util.collections.MatchMappingMixin¶
Bases:
Mapping[str,T_co],ABCMixin that implements glob pattern matches on mapping keys
- match_name(name) Tuple[T_co, ...]¶
Returns all values where
namematches the keys of contained values interpreted as a glob pattern.Example
>>> import collections >>> class Container(collections.OrderedDict, MatchMappingMixin): pass >>> container = Container({"foo": 1, "foo*": 2, "bar": 3}) >>> val = container.match_name('foo') >>> print(val) (1, 2)
See also
fnmatch– details about glob patterns
- match_pattern(pattern) Tuple[T_co, ...]¶
Returns all values where the keys of contained values match the glob
pattern.Example
>>> import collections >>> class Container(collections.OrderedDict, MatchMappingMixin): pass >>> container = MatchMappingMixin({"foo": 1, "foo*": 2, "bar": 3}) >>> val = container.match_pattern('foo') >>> print(val) (1)
See also
fnmatch– details about glob patterns
- class ubii.framework.util.collections.DefaultHookMap(base_factory: Callable[[T_Key], T_co])¶
Bases:
Generic[T_Key,T_co],Mapping[T_Key,T_co]Acts like a
defaultdictmapping but it’sdefault_factoryis always autil.hookso it can be adjusted after creation of the mapping- __init__(base_factory: Callable[[T_Key], T_co])¶
- Parameters:
base_factory – Saved as
base_factory
- base_factory: Callable[[T_Key], T_co]¶
The base factory callable, gets used in
default_factory()to actually create the item
- default_factory(key: T_Key) None¶
Called whenever a key is not present in the mapping. Creates new value using the
base_factoryExample
>>> from ubii.framework.util import DefaultHookMap >>> class Topic: ... def __init__(self, pattern): ... self.pattern = pattern ... >>> store = DefaultHookMap(default_factory=Topic) >>> topic = store['topic/glob/pattern'] >>> assert topic.pattern == 'topic/glob/pattern'
- Parameters:
key – glob pattern
This callable had the
hookdecorator applied. Original signature:def default_factory(self, key: 'T_Key') -> 'None'