Skip to content

Commit

Permalink
Add resolving string to NestedChainMap
Browse files Browse the repository at this point in the history
This is in preparation for solving AstarVienna/ScopeSim#387

A resolving string is a string key ending in `"!"`. An instance of `NestedChainMap` will only continue to follow references if the key is a resolving key, otherwise it will return the value a the bang-string that is stored.
  • Loading branch information
teutoburg committed Oct 17, 2024
1 parent f049cd5 commit e038ce2
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions astar_utils/nested_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ class NestedChainMap(ChainMap):

def __getitem__(self, key):
"""x.__getitem__(y) <==> x[y]."""
value = super().__getitem__(key)
value = super().__getitem__(key.removesuffix("!"))

if isinstance(value, abc.Mapping):
submaps = tuple(RecursiveNestedMapping.from_maps(self.maps, key))
Expand All @@ -285,7 +285,7 @@ def __getitem__(self, key):
return submaps[0]
return NestedChainMap(*submaps)

if is_bangkey(value):
if is_bangkey(value) and is_resolving_key(key):
value = self[value]
return value

Expand All @@ -306,6 +306,11 @@ def is_bangkey(key) -> bool:
return isinstance(key, str) and key.startswith("!")


def is_resolving_key(key) -> bool:
"""Return ``True`` if the key is a ``str`` and ends with a "!"."""
return isinstance(key, str) and key.endswith("!")


def is_nested_mapping(mapping) -> bool:
"""Return ``True`` if `mapping` contains any further map as a value."""
if not isinstance(mapping, abc.Mapping):
Expand Down

0 comments on commit e038ce2

Please sign in to comment.