Traverser interface for tasks. Class for storing the state while performing a preorder-traversal of a task. Parameters ---------- term : task The task to be traversed Attributes ---------- term The current element in the traversal current
| 28 | |
| 29 | |
| 30 | class Traverser: |
| 31 | """Traverser interface for tasks. |
| 32 | |
| 33 | Class for storing the state while performing a preorder-traversal of a |
| 34 | task. |
| 35 | |
| 36 | Parameters |
| 37 | ---------- |
| 38 | term : task |
| 39 | The task to be traversed |
| 40 | |
| 41 | Attributes |
| 42 | ---------- |
| 43 | term |
| 44 | The current element in the traversal |
| 45 | current |
| 46 | The head of the current element in the traversal. This is simply `head` |
| 47 | applied to the attribute `term`. |
| 48 | """ |
| 49 | |
| 50 | def __init__(self, term, stack=None): |
| 51 | self.term = term |
| 52 | if not stack: |
| 53 | self._stack = deque([END]) |
| 54 | else: |
| 55 | self._stack = stack |
| 56 | |
| 57 | def __iter__(self): |
| 58 | while self.current is not END: |
| 59 | yield self.current |
| 60 | self.next() |
| 61 | |
| 62 | def copy(self): |
| 63 | """Copy the traverser in its current state. |
| 64 | |
| 65 | This allows the traversal to be pushed onto a stack, for easy |
| 66 | backtracking.""" |
| 67 | |
| 68 | return Traverser(self.term, deque(self._stack)) |
| 69 | |
| 70 | def next(self): |
| 71 | """Proceed to the next term in the preorder traversal.""" |
| 72 | |
| 73 | subterms = args(self.term) |
| 74 | if not subterms: |
| 75 | # No subterms, pop off stack |
| 76 | self.term = self._stack.pop() |
| 77 | else: |
| 78 | self.term = subterms[0] |
| 79 | self._stack.extend(reversed(subterms[1:])) |
| 80 | |
| 81 | @property |
| 82 | def current(self): |
| 83 | return head(self.term) |
| 84 | |
| 85 | def skip(self): |
| 86 | """Skip over all subterms of the current level in the traversal""" |
| 87 | self.term = self._stack.pop() |
no outgoing calls
searching dependent graphs…