[Libre-soc-bugs] [Bug 1094] insndb instruction database visitor-walker is needed

bugzilla-daemon at libre-soc.org bugzilla-daemon at libre-soc.org
Mon Jun 19 21:26:57 BST 2023


https://bugs.libre-soc.org/show_bug.cgi?id=1094

--- Comment #189 from Dmitry Selyutin <ghostmansd at gmail.com> ---
With a minor tweak in the visitor which accepts an additional argument:

class Visitor(mdis.visitor.ContextVisitor):
    @mdis.dispatcher.Hook(object)
    @contextlib.contextmanager
    def dispatch_object(self, instance, *, path=[]):
        indent = (" " * (len(path) * 4))
        path = "".join(map(lambda part: f"[{part}]", path))
        print(indent, path, "=>", instance)
        yield instance

items = (
    {("a", 1): 3.4, 2: {5, 6}},
)
walker = Walker()
visitor = Visitor()
for (item, path) in walker(items):
    with visitor(item, path=path):
        pass


The output is:
     [0] => {('a', 1): 3.4, 2: {5, 6}}
         [0][('a', 1)] => 3.4
         [0][2] => {5, 6}
             [0][2][5] => 5
             [0][2][6] => 6


Notice that path is just an additional argument to visitor __call__. Anything
can be here, it's up to caller. Paths are obtained from walker, not visitor.

-- 
You are receiving this mail because:
You are on the CC list for the bug.


More information about the libre-soc-bugs mailing list