all: deprecate core module
authorDmitry Selyutin <ghostmansd@gmail.com>
Wed, 14 Jun 2023 10:36:50 +0000 (13:36 +0300)
committerDmitry Selyutin <ghostmansd@gmail.com>
Wed, 14 Jun 2023 10:36:50 +0000 (13:36 +0300)
README.md
src/mdis/core.py [deleted file]
src/mdis/dispatcher.py
src/mdis/visitor.py
src/mdis/walker.py

index 7284b66815fb7185cfad01197cd00f9fa2dec5fb..b38f8fad13deca54a44efff8e2c4cc0fba888194 100644 (file)
--- a/README.md
+++ b/README.md
@@ -21,11 +21,11 @@ A walker is just a particular example of dispatcher, where the call yields some
 The default `mdis.Walker` already incorporates the logic to walk over some builtin Python objects.
 The example below shows how to override the way the dicts are traversed so that keys and values are swapped.
 
-    import mdis.core
+    import mdis.dispatcher
     import mdis.walker
 
     class CustomWalker(mdis.walker.Walker):
-        @mdis.core.hook(dict)
+        @mdis.dispatcher.hook(dict)
         def dispatch_dict(self, instance):
             for (key, value) in instance.items():
                 yield (value, key)
@@ -54,25 +54,25 @@ The example below shows how to execute some arbitrary code upon visiting an obje
 
     import contextlib
 
-    import mdis.core
+    import mdis.dispatcher
     import mdis.visitor
 
     class CustomVisitor(mdis.visitor.Visitor):
-        @mdis.core.hook(int)
+        @mdis.dispatcher.hook(int)
         @contextlib.contextmanager
         def dispatch_int(self, instance):
             print("entering int")
             yield (instance + 42)
             print("leaving int")
 
-        @mdis.core.hook(str)
+        @mdis.dispatcher.hook(str)
         @contextlib.contextmanager
         def dispatch_str(self, instance):
             print("entering str")
             yield f"!!!{instance}!!!"
             print("leaving str")
 
-        @mdis.core.hook(object)
+        @mdis.dispatcher.hook(object)
         @contextlib.contextmanager
         def dispatch_object(self, instance):
             print("entering object")
diff --git a/src/mdis/core.py b/src/mdis/core.py
deleted file mode 100644 (file)
index 9774cb9..0000000
+++ /dev/null
@@ -1,31 +0,0 @@
-class Hook(object):
-    def __init__(self, *typeids):
-        for typeid in typeids:
-            if not isinstance(typeid, type):
-                raise ValueError(typeid)
-        self.__typeids = typeids
-        return super().__init__()
-
-    def __iter__(self):
-        yield from self.__typeids
-
-    def __repr__(self):
-        names = []
-        for typeid in self.__typeids:
-            name = typeid.__qualname__
-            module = typeid.__module__
-            if module not in ("builtins",):
-                name = f"{module}.{name}"
-            names.append(name)
-        return f"<{', '.join(names)}>"
-
-    def __call__(self, call):
-        class ConcreteHook(Hook):
-            def __call__(self, dispatcher, instance):
-                return call(self=dispatcher, instance=instance)
-
-        return ConcreteHook(*tuple(self))
-
-
-def hook(*typeids):
-    return Hook(*typeids)
index f21a292006df30ea1991267b12ceb542215f3c1e..6f6b9cd1be5d36bf797837039524a30ace1494ad 100644 (file)
@@ -2,7 +2,34 @@ import collections as _collections
 import inspect as _inspect
 import types as _types
 
-from . import core as _core
+
+class Hook(object):
+    def __init__(self, *typeids):
+        for typeid in typeids:
+            if not isinstance(typeid, type):
+                raise ValueError(typeid)
+        self.__typeids = typeids
+        return super().__init__()
+
+    def __iter__(self):
+        yield from self.__typeids
+
+    def __repr__(self):
+        names = []
+        for typeid in self.__typeids:
+            name = typeid.__qualname__
+            module = typeid.__module__
+            if module not in ("builtins",):
+                name = f"{module}.{name}"
+            names.append(name)
+        return f"<{', '.join(names)}>"
+
+    def __call__(self, call):
+        class ConcreteHook(Hook):
+            def __call__(self, dispatcher, instance):
+                return call(self=dispatcher, instance=instance)
+
+        return ConcreteHook(*tuple(self))
 
 
 class DispatcherMeta(type):
@@ -10,7 +37,7 @@ class DispatcherMeta(type):
 
     def __new__(metacls, name, bases, ns):
         hooks = {}
-        ishook = lambda member: isinstance(member, _core.Hook)
+        ishook = lambda member: isinstance(member, Hook)
 
         for basecls in reversed(bases):
             members = _inspect.getmembers(basecls, predicate=ishook)
@@ -49,6 +76,6 @@ class Dispatcher(metaclass=DispatcherMeta):
             hook = self.__class__.dispatch()
         return hook(dispatcher=self, instance=instance)
 
-    @_core.hook(object)
+    @Hook(object)
     def dispatch_object(self, instance):
         raise NotImplementedError()
index 9b1e02bac39ff9288df8ed4ceec59aa9a3d67309..f70b5c52392a8f3fe4ee9bd0056af00cc04dca2f 100644 (file)
@@ -1,6 +1,5 @@
 import contextlib as _contextlib
 
-from . import core as _core
 from . import dispatcher as _dispatcher
 
 
@@ -9,7 +8,7 @@ class VisitorMeta(_dispatcher.DispatcherMeta):
 
 
 class Visitor(_dispatcher.Dispatcher, metaclass=VisitorMeta):
+    @_dispatcher.Hook(object)
     @_contextlib.contextmanager
-    @_core.hook(object)
     def dispatch_object(self, instance):
         yield instance
index 507d9531730d61eba680fcc3879b4457cb22e880..b8fe5eff76ea1ea4375e3025f51f25f168962576 100644 (file)
@@ -1,4 +1,3 @@
-from . import core as _core
 from . import dispatcher as _dispatcher
 
 
@@ -7,18 +6,18 @@ class WalkerMeta(_dispatcher.DispatcherMeta):
 
 
 class Walker(_dispatcher.Dispatcher, metaclass=WalkerMeta):
-    @_core.hook(tuple, list, set, frozenset)
+    @_dispatcher.Hook(tuple, list, set, frozenset)
     def dispatch_sequence(self, instance):
         for item in instance:
             yield item
             yield from self(item)
 
-    @_core.hook(dict)
+    @_dispatcher.Hook(dict)
     def dispatch_mapping(self, instance):
         for (key, value) in instance.items():
             yield (key, value)
             yield from self((key, value))
 
-    @_core.hook(object)
+    @_dispatcher.Hook(object)
     def dispatch_object(self, instance):
         yield from ()