diff --git a/happi/backends/json_db.py b/happi/backends/json_db.py index d5cfb177..efc29f25 100644 --- a/happi/backends/json_db.py +++ b/happi/backends/json_db.py @@ -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.""" @@ -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) @@ -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 '