dispatcher: simplify typeid traversal
authorDmitry Selyutin <ghostmansd@gmail.com>
Tue, 13 Jun 2023 22:20:39 +0000 (01:20 +0300)
committerDmitry Selyutin <ghostmansd@gmail.com>
Tue, 13 Jun 2023 22:23:28 +0000 (01:23 +0300)
src/mdis/core.py
src/mdis/dispatcher.py

index 708f8d26b41ed2a37c862943d1287422ecf2ce00..61c4fd831526837183140c8e3928c3f2f8acd092 100644 (file)
@@ -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)
index 3a5dc6ce8419cac76654c9b71593e89a014bcd3c..009f946bc6888749c55b1f46e8acd2064af6d018 100644 (file)
@@ -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)