-
Notifications
You must be signed in to change notification settings - Fork 0
Recursion tools
Is a context manager that counts the depth of the current stack. Its purpose is to let the user define a max recursion depth, which is very useful for huge data sets. When debugging is enabled it also prints the levels of the stack in realtime. When a loop is entered its __enter__
method is called; __exit__
on departure. If at any time the stack reaches the max depth a StackMaxReached
error is raised and excepted by the __exit__
method which kills that particular call to the recursive searcher.
All functions and classes in the Aphid library have a debug mode. This project is still in the early stages, and when dealing with many loops; with different types, things can become a bit unpredictable. To make diagnosing problems easier debug=True
can be added to any command. When the Debugger is enabled the on_match
, recursive
, recursive_path
, and recursive_class
methods are decorated with the recursion_tools.Debugger
. This prints in real time what is happening. The order or calls is:
-
- Class is instantiated and the methods are decorated with the Debugger.
-
-
Stack()
object is created and reference given to theStackTracker
-
-
- Every time a new loop is started the tracker prints
'\n-------> Entering stack level {}\n'.format(self.stack.stacksize)
- The loop function prints out the iterable it just received under
'Current Iterable: {}'
, and if it is given a path prints a dictionary{'current_path':[0,2,etc.]}
underneath.
- The loop function prints out the iterable it just received under
- Every time a new loop is started the tracker prints
-
- If a match is found the decorator prints the key and value, as well as the path, or iterable(depending on the parent class)
-
- Every time a loop is complete the stack has a layer removed and the tracker prints
'\n<------- Leaving stack level. Entering {}\n'.format(self.stack.stacksize)
- Every time a loop is complete the stack has a layer removed and the tracker prints
-------> Entering stack level 1
-------> Entering stack level 2
-------> Entering stack level 3
key 1
__________MATCH__________
{'key': 1}
<------- Leaving stack level. Entering 2
Current Iterable: {'key': 1}
-------> Entering stack level 3
-------> Entering stack level 4
key 1
__________MATCH__________
{'key': 1, 3:4,'random':'thing'}
<------- Leaving stack level. Entering 3
Current Iterable: {'key': 1, 3:4,'random':'thing'}
-------> Entering stack level 4
__Max Stack Reached At 4__
Current Iterable: {6: {'key': 1}}
<------- Leaving stack level. Entering 3
Current Iterable: {4: {'key': 1}, 5: {6: {'key': 1}}}
<------- Leaving stack level. Entering 2