Skip to content

Commit

Permalink
Merge pull request #422 from flairNLP/fix-a-bug-in-ld-bfsearch
Browse files Browse the repository at this point in the history
Fix a bug in bf_search regarding boolean values
  • Loading branch information
MaxDall authored Apr 19, 2024
2 parents 983e0b2 + 1a500bf commit 927f651
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/fundus/parser/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

LDMappingValue: TypeAlias = Union[List[Dict[str, Any]], Dict[str, Any]]

_sentinel = object()


class LinkedDataMapping:
"""
Expand Down Expand Up @@ -123,19 +125,28 @@ def bf_search(self, key: str, depth: Optional[int] = None, default: Any = None)

def search_recursive(nodes: Iterable[LDMappingValue], current_depth: int):
if current_depth == depth:
return None
return _sentinel
else:
new: List[Dict[str, Any]] = []
for node in nodes:
if isinstance(node, list):
new.extend(node)
continue
elif value := node.get(key):
elif (value := node.get(key, _sentinel)) is not _sentinel:
return value
new.extend(v for v in node.values() if isinstance(v, dict))
return search_recursive(new, current_depth + 1) if new else None

return search_recursive([self.__dict__], 0) or default
if not new:
return _sentinel

return search_recursive(new, current_depth + 1)

result = search_recursive([self.__dict__], 0)

if result == _sentinel:
return default

return result

def __repr__(self):
return f"LD containing '{', '.join(content)}'" if (content := self.__dict__.keys()) else "Empty LD"
Expand Down

0 comments on commit 927f651

Please sign in to comment.