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 :)