def _dispatch(cls, vcls): global _cache_token if _cache_token is not None: current_token = get_cache_token() if _cache_token != current_token: _dispatch_cache.clear() _cache_token = current_token try: func = _dispatch_cache[(cls, vcls)] except KeyError: try: vreg = _registry[cls] except KeyError: vreg = _find_impl(cls, _registry) if not vreg: raise NotImplementedError( f"No implementation found for '{cls.__qualname__}'") try: return vreg[vcls] except KeyError: func = _find_impl(vcls, vreg) if not func: raise TypeError( f"No implementation found for '{cls.__qualname__}' from {vcls.__qualname__}") _dispatch_cache[(cls, vcls)] = func return func
def dispatch(event_type): try: return cache[event_type] except KeyError: handler = _find_impl(event_type, handlers) or func cache[event_type] = handler return handler
def plot(self, cga_objs, **kwargs) -> Iterator[Artist]: all_os = [classify.classify(cga_obj) for cga_obj in cga_objs] for handler, os in _groupby( all_os, lambda o: functools._find_impl(type(o), self._handlers)): if not handler: unsupported_msg = (\ "Unable to plot any of the following objects:\n{}" .format("\n".join(" * {!r}".format(o) for o in os)) ) warnings.warn(unsupported_msg) else: yield from handler(self, os, **kwargs)
def _dispatch(self, instance, instance_type: type) -> Optional[Callable]: """ Dispatches a function by its type. How do we dispatch a function? 1. By direct ``instance`` types 2. By matching protocols 3. By its ``mro`` """ implementation = self._instances.get(instance_type, None) if implementation is not None: return implementation for protocol, callback in self._protocols.items(): if isinstance(instance, protocol): return callback return _find_impl(instance_type, self._instances)
def dispatch(cls): """generic_func.dispatch(cls) -> <function implementation> Runs the dispatch algorithm to return the best available implementation for the given *cls* registered on *generic_func*. """ nonlocal cache_token if cache_token is not None: current_token = get_cache_token() if cache_token != current_token: dispatch_cache.clear() cache_token = current_token try: impl = dispatch_cache[cls] except KeyError: try: impl = registry[cls] except KeyError: impl = _find_impl(cls, registry) dispatch_cache[cls] = impl return impl
def _write_method(cls: Type[T]) -> Callable[[zarr.Group, str, T], None]: return _find_impl(cls, ZARR_WRITE_REGISTRY)
def _write_method(cls: Type[T]) -> Callable[[H5Group, str, T], None]: return _find_impl(cls, H5AD_WRITE_REGISTRY)
def test_cache_invalidation(self): from collections import UserDict class TracingDict(UserDict): def __init__(self, *args, **kwargs): super(TracingDict, self).__init__(*args, **kwargs) self.set_ops = [] self.get_ops = [] def __getitem__(self, key): result = self.data[key] self.get_ops.append(key) return result def __setitem__(self, key, value): self.set_ops.append(key) self.data[key] = value def clear(self): self.data.clear() _orig_wkd = functools.WeakKeyDictionary td = TracingDict() functools.WeakKeyDictionary = lambda: td c = collections @functools.singledispatch def g(arg): return "base" d = {} l = [] self.assertEqual(len(td), 0) self.assertEqual(g(d), "base") self.assertEqual(len(td), 1) self.assertEqual(td.get_ops, []) self.assertEqual(td.set_ops, [dict]) self.assertEqual(td.data[dict], g.registry[object]) self.assertEqual(g(l), "base") self.assertEqual(len(td), 2) self.assertEqual(td.get_ops, []) self.assertEqual(td.set_ops, [dict, list]) self.assertEqual(td.data[dict], g.registry[object]) self.assertEqual(td.data[list], g.registry[object]) self.assertEqual(td.data[dict], td.data[list]) self.assertEqual(g(l), "base") self.assertEqual(g(d), "base") self.assertEqual(td.get_ops, [list, dict]) self.assertEqual(td.set_ops, [dict, list]) g.register(list, lambda arg: "list") self.assertEqual(td.get_ops, [list, dict]) self.assertEqual(len(td), 0) self.assertEqual(g(d), "base") self.assertEqual(len(td), 1) self.assertEqual(td.get_ops, [list, dict]) self.assertEqual(td.set_ops, [dict, list, dict]) self.assertEqual(td.data[dict], functools._find_impl(dict, g.registry)) self.assertEqual(g(l), "list") self.assertEqual(len(td), 2) self.assertEqual(td.get_ops, [list, dict]) self.assertEqual(td.set_ops, [dict, list, dict, list]) self.assertEqual(td.data[list], functools._find_impl(list, g.registry)) class X: pass c.MutableMapping.register(X) # Will not invalidate the cache, # not using ABCs yet. self.assertEqual(g(d), "base") self.assertEqual(g(l), "list") self.assertEqual(td.get_ops, [list, dict, dict, list]) self.assertEqual(td.set_ops, [dict, list, dict, list]) g.register(c.Sized, lambda arg: "sized") self.assertEqual(len(td), 0) self.assertEqual(g(d), "sized") self.assertEqual(len(td), 1) self.assertEqual(td.get_ops, [list, dict, dict, list]) self.assertEqual(td.set_ops, [dict, list, dict, list, dict]) self.assertEqual(g(l), "list") self.assertEqual(len(td), 2) self.assertEqual(td.get_ops, [list, dict, dict, list]) self.assertEqual(td.set_ops, [dict, list, dict, list, dict, list]) self.assertEqual(g(l), "list") self.assertEqual(g(d), "sized") self.assertEqual(td.get_ops, [list, dict, dict, list, list, dict]) self.assertEqual(td.set_ops, [dict, list, dict, list, dict, list]) g.dispatch(list) g.dispatch(dict) self.assertEqual(td.get_ops, [list, dict, dict, list, list, dict, list, dict]) self.assertEqual(td.set_ops, [dict, list, dict, list, dict, list]) c.MutableSet.register(X) # Will invalidate the cache. self.assertEqual(len(td), 2) # Stale cache. self.assertEqual(g(l), "list") self.assertEqual(len(td), 1) g.register(c.MutableMapping, lambda arg: "mutablemapping") self.assertEqual(len(td), 0) self.assertEqual(g(d), "mutablemapping") self.assertEqual(len(td), 1) self.assertEqual(g(l), "list") self.assertEqual(len(td), 2) g.register(dict, lambda arg: "dict") self.assertEqual(g(d), "dict") self.assertEqual(g(l), "list") g._clear_cache() self.assertEqual(len(td), 0) functools.WeakKeyDictionary = _orig_wkd
def update_event(self, inp=-1): self.set_output_val(0, functools._find_impl(self.input(0), self.input(1)))