<jupyter_start><jupyter_text>Using a debugger Stepping through the codeDebuggers are programs that can be used to test other programs. They allow programmers to suspend execution of the target program and inspect variables at that point.* Mac - compiled languages:  [Xcode](https://developer.apple.com/library/ios/documentation/ToolsLanguages/Conceptual/Xcode_Overview/DebugYourApp/DebugYourApp.html)* Windows - compiled languages:  [Visual Studio](http://msdn.microsoft.com/en-us/library/bb483011.aspx)* Linux: [DDD](https://www.gnu.org/software/ddd/)* all platforms: [eclipse](http://www.eclipse.org), [gdb](http://www.sourceware.org/gdb/) (DDD and  eclipse are GUIs for gdb)* python: [spyder](http://pythonhosted.org/spyder/index.html),*          [pdb](https://docs.python.org/3.8/library/pdb.html)* R: [RStudio](http://www.rstudio.com/ide/docs/debugging/overview),  [debug](http://stat.ethz.ch/R-manual/R-devel/library/base/html/debug.html),  [browser](http://stat.ethz.ch/R-manual/R-devel/library/base/html/browser.html)  Using the python debugger Unfortunately this doesn't work nicely in the notebook. But from the command line, you can run a python program with: ``` bashpython -m pdb my_program.py```  Basic navigation:Basic command to navigate the code and the python debugger:* `help`: prints the help* `help n`: prints help about command `n`* `n`(ext): executes one line of code. Executes and steps **over** functions.* `s`(tep): step into current function in line of code* `l`(ist): list program around current position* `w`(where): prints current stack (where we are in code)* `[enter]`: repeats last command* `anypythonvariable`: print the value of that variableThe python debugger is **a python shell**: it can print and compute values, and even change the valuesof the variables at that point in the program. BreakpointsBreak points tell debugger where and when to stopWe say* `b somefunctionname`<jupyter_code>%%writefile energy_example.py
from diffusion.model import energy

print(energy([5, 6, 7, 8, 0, 1]))<jupyter_output>Overwriting energy_example.py<jupyter_text>The debugger is, of course, most used interactively, but here I'm showing a prewritten debugger script:<jupyter_code>%%writefile commands
restart        # restart session
n
b energy       # program will stop when entering energy
c              # continue program until break point is reached
print(density) # We are now "inside" the energy function and can print any variable.
%%bash
#%%cmd #(windows)
python -m pdb energy_example.py < commands<jupyter_output>> /Users/jrobinson/Projects/rsd-engineering/rsd-engineeringcourse/ch03tests/energy_example.py(1)<module>()
-> from diffusion.model import energy
(Pdb) Restarting energy_example.py with arguments:
	energy_example.py
> /Users/jrobinson/Projects/rsd-engineering/rsd-engineeringcourse/ch03tests/energy_example.py(1)<module>()
-> from diffusion.model import energy
(Pdb) > /Users/jrobinson/Projects/rsd-engineering/rsd-engineeringcourse/ch03tests/energy_example.py(3)<module>()
-> print(energy([5, 6, 7, 8, 0, 1]))
(Pdb) Breakpoint 1 at /Users/jrobinson/Projects/rsd-engineering/rsd-engineeringcourse/ch03tests/diffusion/model.py:4
(Pdb) > /Users/jrobinson/Projects/rsd-engineering/rsd-engineeringcourse/ch03tests/diffusion/model.py(10)energy()
-> from numpy import array, any, sum
(Pdb) [5, 6, 7, 8, 0, 1]
(Pdb)
{'fraction_non_alphanumeric': 0.12466447957053385, 'fraction_numerical': 0.012824336415150612, 'mean_word_length': 5.304511278195489, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 18, 'https://': 3, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '79180', 'license': 'CC-BY-3.0', 'n_tokens_mistral': 1085, 'n_tokens_neox': 1018, 'n_words': 309, 'path': 'ch03tests/06Debugger.ipynb', 'repo_name': 'jack89roberts/rsd-engineeringcourse', 'size': '3353'}