From a07d691fbbddde5e2ff0ba30e229ef01362a23c2 Mon Sep 17 00:00:00 2001 From: Dmitry Selyutin Date: Wed, 21 Jun 2023 23:48:42 +0300 Subject: [PATCH] all: rename instance to node --- src/mdis/dispatcher.py | 13 ++++++------- src/mdis/visitor.py | 8 ++++---- src/mdis/walker.py | 30 +++++++++++++++--------------- 3 files changed, 25 insertions(+), 26 deletions(-) diff --git a/src/mdis/dispatcher.py b/src/mdis/dispatcher.py index 7add027..c956e0c 100644 --- a/src/mdis/dispatcher.py +++ b/src/mdis/dispatcher.py @@ -33,9 +33,8 @@ class Hook(object): def __call__(self, call): class ConcreteHook(Hook): - def __call__(self, dispatcher, instance, *args, **kwargs): - return call(self=dispatcher, instance=instance, - *args, **kwargs) + def __call__(self, dispatcher, node, *args, **kwargs): + return call(dispatcher, node, *args, **kwargs) return ConcreteHook(*tuple(self)) @@ -82,15 +81,15 @@ class DispatcherMeta(type): class Dispatcher(metaclass=DispatcherMeta): - def __call__(self, instance, *args, **kwargs): - for typeid in instance.__class__.__mro__: + def __call__(self, node, *args, **kwargs): + for typeid in node.__class__.__mro__: hook = self.__class__.dispatch(typeid=typeid) if hook is not None: break if hook is None: hook = self.__class__.dispatch() - return hook(dispatcher=self, instance=instance, *args, **kwargs) + return hook(self, node, *args, **kwargs) @Hook(object) - def dispatch_object(self, instance, *args, **kwargs): + def dispatch_object(self, node, *args, **kwargs): raise NotImplementedError() diff --git a/src/mdis/visitor.py b/src/mdis/visitor.py index ca6f809..ff26172 100644 --- a/src/mdis/visitor.py +++ b/src/mdis/visitor.py @@ -15,12 +15,12 @@ class VisitorMeta(dispatcher.DispatcherMeta): class Visitor(dispatcher.Dispatcher, metaclass=VisitorMeta): @dispatcher.Hook(object) - def dispatch_object(self, instance): - return instance + def dispatch_object(self, node): + return node class ContextVisitor(Visitor): @dispatcher.Hook(object) @contextlib.contextmanager - def dispatch_object(self, instance): - yield super().__call__(instance=instance) + def dispatch_object(self, node): + yield super().__call__(node) diff --git a/src/mdis/walker.py b/src/mdis/walker.py index 5509355..3605d0a 100644 --- a/src/mdis/walker.py +++ b/src/mdis/walker.py @@ -46,33 +46,33 @@ class HashPath(GenericPath): class Walker(dispatcher.Dispatcher, metaclass=WalkerMeta): @dispatcher.Hook(tuple, list) - def dispatch_ordered_sequence(self, instance): - for (index, item) in enumerate(instance): - yield (item, instance, index, IndexPath) + def dispatch_ordered_sequence(self, node): + for (index, item) in enumerate(node): + yield (item, node, index, IndexPath) yield from self(item) @dispatcher.Hook(set, frozenset) - def dispatch_unordered_sequence(self, instance): - for item in instance: - yield (item, instance, item, HashPath) + def dispatch_unordered_sequence(self, node): + for item in node: + yield (item, node, item, HashPath) yield from self(item) @dispatcher.Hook(dataclasses.is_dataclass) - def dispatch_dataclass(self, instance): - for field in dataclasses.fields(instance): + def dispatch_dataclass(self, node): + for field in dataclasses.fields(node): key = field.name - value = getattr(instance, key) - yield (value, instance, key, AttributePath) + value = getattr(node, key) + yield (value, node, key, AttributePath) yield from self(value) @dispatcher.Hook(dict) - def dispatch_mapping(self, instance): - for (key, value) in instance.items(): - yield (key, instance, key, HashPath) + def dispatch_mapping(self, node): + for (key, value) in node.items(): + yield (key, node, key, HashPath) yield from self(key) - yield (value, instance, key, IndexPath) + yield (value, node, key, IndexPath) yield from self(value) @dispatcher.Hook(object) - def dispatch_object(self, instance, path=()): + def dispatch_object(self, node, path=()): yield from () -- 2.30.2