Skip to content

Commit

Permalink
Merge pull request #234 from klauer/fix_match_missing_key
Browse files Browse the repository at this point in the history
FIX: missing keys should not cause failure in backend
  • Loading branch information
klauer authored Mar 4, 2022
2 parents f8c55b2 + f9f8e97 commit e3f92f2
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions happi/backends/json_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
fcntl = None


# A sentinel for keys that are missing for comparisons below.
_MISSING = object()


@contextlib.contextmanager
def _load_and_store_context(backend):
"""Context manager used to load, and optionally store the JSON database."""
Expand Down Expand Up @@ -164,8 +168,10 @@ def find(self, to_match):
"""

def comparison(name, doc):
return all(value == doc[key]
for key, value in to_match.items())
return all(
value == doc.get(key, _MISSING)
for key, value in to_match.items()
)

yield from self._iterative_compare(comparison)

Expand All @@ -187,12 +193,13 @@ def find_range(self, key, *, start, stop=None, to_match):
"""

def comparison(name, doc):
if all(value == doc[k] for k, value in to_match.items()):
value = doc.get(key)
if all(value == doc.get(k, _MISSING)
for k, value in to_match.items()):
try:
return start <= value < stop
return start <= doc[key] < stop
except Exception:
...
return False

if key in to_match:
raise ValueError('Cannot specify the same key in `to_match` as '
Expand Down

0 comments on commit e3f92f2

Please sign in to comment.