Debugging Tips
Quickly Locating and Solving Problems
Debugging Levels
- Print Debugging (Simplest)
- Assertions (Assert)
- Logging
- Debugger
Print Debugging
Basic Usage
python
def calculate_tax(income, rate):
print(f"Debug: income={income}, rate={rate}") # Debug output
tax = income * rate
print(f"Debug: tax={tax}")
return tax
result = calculate_tax(100000, 0.25)Variable Inspection
python
data = [1, 2, 3, 4, 5]
print(f"Type: {type(data)}")
print(f"Length: {len(data)}")
print(f"Content: {data}")Assertions (Assert)
python
def calculate_mean(data):
assert len(data) > 0, "Data cannot be empty"
assert all(isinstance(x, (int, float)) for x in data), "Must be numbers"
return sum(data) / len(data)
# Normal
result = calculate_mean([1, 2, 3])
# Triggers assertion
result = calculate_mean([]) # AssertionError: Data cannot be emptyLogging
python
import logging
# Configure logging
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s',
filename='analysis.log'
)
# Use logging
logging.debug("Starting data processing")
logging.info("Loaded 1000 rows of data")
logging.warning("Found 15 missing values")
logging.error("File read failed")
logging.critical("System crash")Logging Levels:
DEBUG: Detailed informationINFO: General informationWARNING: WarningsERROR: ErrorsCRITICAL: Critical errors
Practical Techniques
Technique 1: Conditional Breakpoints
python
for i, resp in enumerate(respondents):
if resp['id'] == 1001: # Only debug specific data
print(f"Debugging ID 1001: {resp}")Technique 2: Check Data Types
python
def process_income(income):
print(f"income type: {type(income)}")
print(f"income value: {repr(income)}")
if not isinstance(income, (int, float)):
raise TypeError(f"Expected number, got {type(income)}")Technique 3: Using pdb (Python Debugger)
python
import pdb
def analyze_data(df):
print("Starting analysis")
pdb.set_trace() # Program pauses here
result = df.mean()
return result
# Debugging commands:
# n (next): Next line
# s (step): Step into function
# c (continue): Continue execution
# p variable: Print variable
# q (quit): QuitCommon Problem Debugging
Problem 1: Data Type Errors
python
# Hidden type issue
ages = ['25', '30', '35'] # String list
mean = sum(ages) / len(ages) # TypeError
# Debug
print(f"ages type: {type(ages)}")
print(f"First element type: {type(ages[0])}")
# Fix
ages = [int(x) for x in ages]Problem 2: DataFrame Column Name Errors
python
import pandas as pd
df = pd.DataFrame({'age': [25, 30], 'income': [50000, 75000]})
# KeyError
mean_incom = df['incom'].mean() # Spelling error
# Debug
print("Available columns:", df.columns.tolist())
print("Column types:", [type(col) for col in df.columns])Problem 3: Loop Index Issues
python
data = [10, 20, 30]
# IndexError
for i in range(len(data) + 1): # Range error
print(data[i])
# Debug
print(f"Data length: {len(data)}")
print(f"Loop range: {list(range(len(data) + 1))}")Debugging Checklist
Data Issues
- [ ] Check data types
- [ ] Check data length/shape
- [ ] Check missing values
- [ ] Check anomalous values
Logic Issues
- [ ] Print intermediate results
- [ ] Check conditional statements
- [ ] Verify loop ranges
- [ ] Confirm function parameters
Performance Issues
- [ ] Use time measurement
- [ ] Check algorithm complexity
- [ ] Look for repeated calculations
Practical Debugging Template
python
import logging
import traceback
# Configuration
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s'
)
def safe_process(data):
"""Processing function with complete debugging"""
try:
# Input validation
logging.debug(f"Input data type: {type(data)}")
logging.debug(f"Input data length: {len(data)}")
# Main processing
result = process_logic(data)
# Output validation
logging.debug(f"Output result: {result}")
return result
except Exception as e:
logging.error(f"Processing failed: {e}")
logging.error(traceback.format_exc())
return NoneSummary
Debugging Order:
- Read error message (Most important!)
- Add print statements
- Check data types and values
- Use logging
- Use debugger (pdb)
Module 8 Complete!
Next module is a Core Focus: Module 9 Libraries (NumPy/Pandas/Visualization, etc.)
Ready?