From 6f3a9f2c6e39e86ae4cb4072e83887b57c398884 Mon Sep 17 00:00:00 2001 From: Dmitry Selyutin Date: Wed, 14 Jun 2023 01:20:39 +0300 Subject: [PATCH] dispatcher: simplify typeid traversal --- src/mdis/core.py | 15 +++++---------- src/mdis/dispatcher.py | 9 ++++----- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/mdis/core.py b/src/mdis/core.py index 708f8d2..61c4fd8 100644 --- a/src/mdis/core.py +++ b/src/mdis/core.py @@ -10,7 +10,7 @@ class TypeidHook(object): yield from self.__typeids def __repr__(self): - return f"{self.__class__.__name__}({self.__typeids})" + return f"{self.__class__.__name__}({self.__typeids!r})" def __call__(self, call): if not callable(call): @@ -28,16 +28,11 @@ class CallHook(object): self.__call = call return super().__init__() - def __repr__(self): - return f"{self.__class__.__name__}(call={self.call!r}, typeids={self.typeids!r})" - - @property - def typeids(self): - return self.__typeids + def __iter__(self): + yield from self.__typeids - @property - def call(self): - return self.__call + def __repr__(self): + return f"{self.__class__.__name__}(call={self.__call!r}, typeids={self.__typeids!r})" def __call__(self, dispatcher, instance): return self.__call(dispatcher, instance) diff --git a/src/mdis/dispatcher.py b/src/mdis/dispatcher.py index 3a5dc6c..009f946 100644 --- a/src/mdis/dispatcher.py +++ b/src/mdis/dispatcher.py @@ -11,13 +11,12 @@ class DispatcherMeta(type): for (key, value) in tuple(ns.items()): if not isinstance(value, _core.CallHook): continue - hook = ns.pop(key) - for typeid in hook.typeids: + hook = value + for typeid in hook: if typeid in hooks: raise ValueError(f"conflicting hook: {typeid!r}") hooks[typeid] = hook - site = hook.call.__name__ - ns[site] = hook + ns[key] = hook return super().__new__(metacls, name, bases, ns) @@ -25,7 +24,7 @@ class DispatcherMeta(type): hooks = {} for hook in map(_operator.itemgetter(1), _inspect.getmembers(cls, predicate=lambda member: isinstance(member, _core.CallHook))): - for typeid in hook.typeids: + for typeid in hook: hooks[typeid] = hook cls.__hooks = hooks return super().__init__(name, bases, ns) -- 2.30.2