Skip to content

Recursion tools

robertkearns edited this page Jul 14, 2019 · 1 revision

Debugging

StackTracker():

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.

Debugger():

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:

    1. Class is instantiated and the methods are decorated with the Debugger.
    1. Stack() object is created and reference given to the StackTracker
    1. 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.
    1. If a match is found the decorator prints the key and value, as well as the path, or iterable(depending on the parent class)
    1. 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)

Sample output:

------->   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
Clone this wiki locally