Thread (3 messages) 3 messages, 3 authors, 2024-01-30

Re: [PATCH net-next v1 02/12] tools/net/ynl: Support sub-messages in nested attribute spaces

From: Jakub Kicinski <kuba@kernel.org>
Date: 2024-01-30 01:33:01
Also in: linux-doc

On Sun, 28 Jan 2024 19:36:29 +0000 Donald Hunter wrote:
quoted
from collections import ChainMap

class LevelChainMap(ChainMap):
    def __getitem__(self, key):
        for mapping in self.maps:
            try:
                return mapping[key], self.maps[::-1].index(mapping)
            except KeyError:
                pass
        return self.__missing__(key)

    def get(self, key, default=None, level=None):
        val, lvl = self[key] if key in self else (default, None)
        if level:
            if lvl != level:
                raise Exception("Level mismatch")
        return val, lvl

# example usage
c = LevelChainMap({'a':1}, {'inner':{'a':1}}, {'outer': {'inner':{'a':1}}})
print(c.get('a', level=2))
print(c.get('a', level=1)) #raise err

This will leave the spec as it is and will require small changes.

What do you think?  
The more I think about it, the more I agree that using path-like syntax
in the selector is overkill. It makes sense to resolve the selector
level from the spec and then directly access the mappings from the
correct scope level.
Plus if we resolve from the spec that's easily reusable in C / C++ 
code gen :)
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help