From 30ed0e6096ab044d058dc2d20f050fe197657318 Mon Sep 17 00:00:00 2001 From: Dmitry Selyutin Date: Tue, 20 Jun 2023 23:34:33 +0300 Subject: [PATCH] walker: simplify path handling --- src/mdis/walker.py | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/src/mdis/walker.py b/src/mdis/walker.py index 578284e..4d1c33d 100644 --- a/src/mdis/walker.py +++ b/src/mdis/walker.py @@ -28,38 +28,32 @@ class PartId(enum.Enum): class Walker(dispatcher.Dispatcher, metaclass=WalkerMeta): @dispatcher.Hook(tuple, list) - def dispatch_ordered_sequence(self, instance, path=()): + def dispatch_ordered_sequence(self, instance): for (index, item) in enumerate(instance): - part = (PartId.Index, index) - parts = (path + (part,)) - yield (item, parts) - yield from self(item, path=parts) + yield (item, instance, index, PartId.Index) + yield from self(item) @dispatcher.Hook(set, frozenset) - def dispatch_unordered_sequence(self, instance, path=[]): + def dispatch_unordered_sequence(self, instance): for item in instance: - part = (PartId.Hash, item) - parts = (path + (part,)) - yield (item, parts) - yield from self(item, path=parts) + yield (item, instance, item, PartId.Hash) + yield from self(item) @dispatcher.Hook(dataclasses.is_dataclass) - def dispatch_dataclass(self, instance, path=[]): + def dispatch_dataclass(self, instance): for field in dataclasses.fields(instance): key = field.name value = getattr(instance, key) - part = (PartId.Attribute, key) - parts = (path + (part,)) - yield (value, parts) - yield from self(value, path=parts) + yield (value, instance, key, PartId.Attribute) + yield from self(value) @dispatcher.Hook(dict) - def dispatch_mapping(self, instance, path=[]): + def dispatch_mapping(self, instance): for (key, value) in instance.items(): - part = (PartId.Index, key) - parts = (path + (part,)) - yield (value, parts) - yield from self(value, path=parts) + yield (key, instance, key, PartId.Hash) + yield from self(key) + yield (value, instance, key, PartId.Index) + yield from self(value) @dispatcher.Hook(object) def dispatch_object(self, instance, path=()): -- 2.30.2