Skip to content

Commit

Permalink
Merge pull request #146 from singnet/bugfix/issue-145-refactor-traver…
Browse files Browse the repository at this point in the history
…se-neighbors-iterator

[#145] Refactor TraverseNeighborsIterator
  • Loading branch information
marcocapozzoli authored Feb 26, 2024
2 parents 65f885e + f5036f2 commit 4cd79ec
Show file tree
Hide file tree
Showing 3 changed files with 1,115 additions and 1,097 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
[#134] Update package version
[#138] Implement iterators to incoming_links
[#139] Update TraveseEngine to use IncomingLinksIterators
[#145] Refactor TraverseNeighborsIterator
37 changes: 20 additions & 17 deletions hyperon_das/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def __init__(self, source: IncomingLinksIterator, **kwargs) -> None:
self.custom_filter = kwargs.get('filter')
self.targets_only = kwargs.get('targets_only', False)
self.buffer = None
if not self.is_empty():
if not self.source.is_empty():
self.iterator = self.source
self.current_value = self._find_first_valid_element()
self.buffer = self.current_value
Expand All @@ -287,7 +287,6 @@ def __next__(self):
) or self._filter(link, targets):
self.current_value = targets if self.targets_only else link
break

return self.current_value

def _find_first_valid_element(self):
Expand Down Expand Up @@ -325,7 +324,7 @@ def _filter(self, link: Dict[str, Any], targets: Dict[str, Any]) -> bool:
return True

def is_empty(self) -> bool:
return not self.current_value and self.source.is_empty()
return not self.current_value


class TraverseNeighborsIterator(QueryAnswerIterator):
Expand All @@ -335,9 +334,9 @@ def __init__(self, source: TraverseLinksIterator, **kwargs) -> None:
self.cursor = self.source.cursor
self.target_type = self.source.target_type
self.visited_neighbors = []
self.current_value = self._find_first_valid_element()
if not self.is_empty():
if not self.source.is_empty():
self.iterator = source
self.current_value = self._find_first_valid_element()

def __next__(self):
if self.buffered_answer:
Expand All @@ -348,24 +347,28 @@ def __next__(self):

while True:
targets = super().__next__()
_new_neighbors = []
match_found = False
for target in targets:
if self._filter(target):
match_found = True
self.visited_neighbors.append(target['handle'])
_new_neighbors.append(target)

_new_neighbors, match_found = self._process_targets(targets)
if match_found:
self.buffered_answer = ListIterator(_new_neighbors)
self.current_value = self.buffered_answer.__next__()
return self.current_value

def _find_first_valid_element(self):
if self.source.current_value:
for target in self.source.current_value:
if self._filter(target):
return target
for targets in self.iterator:
_new_neighbors, match_found = self._process_targets(targets)
if match_found:
self.buffered_answer = ListIterator(_new_neighbors)
return self.buffered_answer.get()

def _process_targets(self, targets: list) -> tuple:
answer = []
match_found = False
for target in targets:
if self._filter(target):
match_found = True
self.visited_neighbors.append(target['handle'])
answer.append(target)
return (answer, match_found)

def _filter(self, target: Dict[str, Any]) -> bool:
handle = target['handle']
Expand Down
Loading

0 comments on commit 4cd79ec

Please sign in to comment.