Skip to content

Debugging Guide

This guide provides tips and techniques for debugging issues in the portfolio management toolkit.

Using ipdb

ipdb is a powerful interactive debugger for Python. To use it, you can set a breakpoint in the code like this:

import ipdb; ipdb.set_trace()

When the code execution reaches this line, you will be dropped into an interactive shell where you can inspect variables, execute code, and step through the program.

Common ipdb Commands

  • n (next): Execute the current line and move to the next one in the same function.
  • c (continue): Continue execution until the next breakpoint.
  • s (step): Step into a function call.
  • q (quit): Quit the debugger and exit the program.
  • p <variable> (print): Print the value of a variable.

Debugging Tests

You can use the --pdb flag with pytest to automatically enter the debugger on test failures:

pytest --pdb

This will drop you into an ipdb shell at the point where the test failed, allowing you to inspect the state of the program and understand the cause of the failure.

Visualizing Data

When working with pandas DataFrames, it can be helpful to visualize the data to understand what's going on. You can use the print() function to display the contents of a DataFrame, but for larger DataFrames, it's often better to write them to a CSV file and inspect them in a spreadsheet editor:

df.to_csv("debug.csv")

Logging

The project uses the standard Python logging module. You can increase the log level to get more detailed information about what the program is doing. For example, to set the log level to DEBUG, you can add the following to your script:

import logging
logging.basicConfig(level=logging.DEBUG)

Refer to docs/best_practices/LOGGING.md for more information on the project's logging conventions.