Skip to content

Commit d6933d6

Browse files
committed
Day 29 - Debugging
1 parent 138e260 commit d6933d6

File tree

6 files changed

+120
-0
lines changed

6 files changed

+120
-0
lines changed

Day 29 - Debugging/Commands.mdx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
```sh
2+
pdb Commands:
3+
n(ext) - Execute next line
4+
s(tep) - Step into function
5+
r(eturn) - Run until function returns
6+
c(ontinue) - Continue execution
7+
l(ist) - Show current code context
8+
p <var> - Print variable value
9+
q(uit) - Quit debugger
10+
11+
Logging Levels:
12+
DEBUG - Detailed information
13+
INFO - Confirmation of operation
14+
WARNING - Potential issues
15+
ERROR - Serious problems
16+
CRITICAL - Fatal errors
17+
```

Day 29 - Debugging/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Day 29: Debugging
2+
3+
## What You'll Learn:
4+
- Using Python Debugger (pdb)
5+
- Setting breakpoints and stepping through code
6+
- Configuring logging module
7+
- Logging levels and handlers
8+
- Crash investigation techniques
9+
10+
## Files:
11+
1. `pdb_example.py` - Debugger usage
12+
2. `logging_config.py` - Logging setup
13+
3. `advanced_debugging.py` - Post-mortem debugging
14+
4. `error_handling.py` - Structured logging
15+
16+
## Exercises:
17+
1. Debug a faulty function that:
18+
- Miscalculates factorial values
19+
- Fails on specific inputs
20+
- Use pdb to identify logic errors
21+
22+
2. Implement logging in a file processor:
23+
- Track file operations with different log levels
24+
- Capture exceptions with tracebacks
25+
- Rotate logs when they reach 1MB
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import pdb
2+
3+
def analyze_data(data):
4+
# Post-mortem debugging
5+
results = []
6+
for item in data:
7+
pdb.set_trace()
8+
if item < 0: # Break when negative values appear
9+
results.append(0)
10+
else:
11+
results.append(item * 2)
12+
return results
13+
14+
# Test with problematic data
15+
data = [1, 3, -5, 8]
16+
analyze_data(data)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import logging
2+
from logging.handlers import RotatingFileHandler
3+
4+
logger = logging.getLogger("data_processor")
5+
logger.setLevel(logging.DEBUG)
6+
7+
handler = RotatingFileHandler(
8+
"data.log",
9+
maxBytes=1024*1024, # 1MB
10+
backupCount=3
11+
)
12+
formatter = logging.Formatter(
13+
"%(asctime)s - %(levelname)s - %(message)s"
14+
)
15+
handler.setFormatter(formatter)
16+
logger.addHandler(handler)
17+
18+
def process_record(record):
19+
try:
20+
logger.debug(f"Processing: {record}")
21+
if not record.is_valid():
22+
logger.warning("Invalid record format")
23+
# Processing logic here
24+
except Exception as e:
25+
logger.error(f"Failed to process record: {str(e)}", exc_info=True)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import logging
2+
from pathlib import Path
3+
4+
# Create logs directory
5+
Path("logs").mkdir(exist_ok=True)
6+
7+
# Configure root logger
8+
logging.basicConfig(
9+
level=logging.DEBUG,
10+
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
11+
handlers=[
12+
logging.FileHandler("logs/app.log"),
13+
logging.StreamHandler()
14+
]
15+
)
16+
17+
logger = logging.getLogger(__name__)
18+
19+
def main():
20+
logger.info("Application started")
21+
try:
22+
1 / 0
23+
except Exception:
24+
logger.exception("Critical error occurred")
25+
26+
if __name__ == "__main__":
27+
main()

Day 29 - Debugging/pdf_example.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import pdb
2+
3+
def calculate_discount(price, discount):
4+
pdb.set_trace() # Breakpoint
5+
if discount > 0.5:
6+
print("Warning: Large discount applied")
7+
return price - (price * discount)
8+
9+
# Debug this calculation
10+
print(calculate_discount(100, 0.6))

0 commit comments

Comments
 (0)