[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 12 21:37:42 BST 2023


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

--- Comment #132 from Dmitry Selyutin <ghostmansd at gmail.com> ---
I liked the idea so much that I decided to play more with it. I think I finally
found the way to handle everything in a graceful way. Perhaps eventually I'll
provide a standalone module, maybe even on PyPI.

As this is a demo and as I remember these debates about underscores and other
extremely important stuff, I took a liberty to write this code as I like. Since
our project does not welcome "as I like" approach, I posted it to anonymous
repository at repo.or.cz. Keep in mind that's just a concept; it's unlikely
this will satisfy personal preferences unless modified.

https://repo.or.cz/pydispatch.git

Example:

from pydispatch import visitor
from pydispatch import walker

class SwappedDict(dict):
    pass

class CustomWalker(walker.Walker):
    @walker.hook(dict)
    def dict(self, instance):
        for (key, value) in instance.items():
            yield (value, key)
            yield from self((value, key))

class CustomVisitor(visitor.Visitor):
    @visitor.hook(object)
    def object(self, instance):
        print("enter")
        yield instance
        print("leave")

visitor = CustomVisitor()
walker = CustomWalker()
for item in walker(SwappedDict({"a": 1, "b": 2})):
    with visitor(item) as node:
        print("hook", repr(item))

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


More information about the libre-soc-bugs mailing list