### Install dowhen Source: https://dowhen.readthedocs.io Install the dowhen package using pip. ```bash pip install dowhen ``` -------------------------------- ### Basic dowhen Usage: do and when Source: https://dowhen.readthedocs.io Execute a simple assignment callback before a function's return statement. The handler can be removed to revert to default behavior. ```python from dowhen import bp, do, goto, when def f(x): x += 100 # Let's change the value of x before return return x # do("x = 1") is the callback # when(f, "return x") is the trigger # This is equivalent to: # handler = when(f, "return x").do("x = 1") handler = do("x = 1").when(f, "return x") # x = 1 is executed before "return x" assert f(0) == 1 # You can remove the handler handler.remove() assert f(0) == 100 ``` -------------------------------- ### Chaining Callbacks with dowhen Source: https://dowhen.readthedocs.io Chain multiple callbacks like goto() and do() to execute in sequence at a trigger. Handlers do not need to be stored if not explicitly managed. ```python # You can chain callbacks and they'll run in order # You don't need to store the handler if you don't use it when(f, "x += 100").goto("return x").do("x = 42") assert f(0) == 42 ``` -------------------------------- ### Using bp() for Debugging Source: https://dowhen.readthedocs.io Use the bp() callback to enter pdb at a specific trigger point. Handlers can be temporarily disabled. ```python # bp() is another callback that brings up pdb handler = bp().when(f, "return x") # This will enter pdb f(0) # You can temporarily disable the handler # handler.enable() will enable it again handler.disable() ``` -------------------------------- ### Using goto() to Skip Code Source: https://dowhen.readthedocs.io Employ the goto() callback within a context manager to skip specific lines of code. The handler is automatically removed after the context. ```python # goto() is a callback too # This will skip the line of `x += 100` # The handler will be removed after the context with goto("return x").when(f, "x += 100"): assert f(0) == 0 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.