chiron 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,222 @@
1
+ # Python Debugging Workflow
2
+
3
+ When debugging Python code, follow this systematic approach to identify and resolve issues efficiently.
4
+
5
+ ## Debugging Strategy
6
+
7
+ ### 1. Understand the Error
8
+ - Read the full traceback from bottom to top
9
+ - Identify the exact line where the error occurred
10
+ - Note the exception type and message
11
+ - Check if it's a syntax error, runtime error, or logic error
12
+
13
+ ### 2. Use Print Debugging
14
+ ```python
15
+ # Quick debugging with print
16
+ print(f"DEBUG: variable_name = {variable_name}")
17
+ print(f"DEBUG: type = {type(variable_name)}")
18
+ print(f"DEBUG: {locals()=}") # Python 3.8+
19
+
20
+ # Pretty print complex objects
21
+ from pprint import pprint
22
+ pprint(complex_object)
23
+ ```
24
+
25
+ ### 3. Python Debugger (pdb)
26
+ ```python
27
+ # Insert breakpoint in code
28
+ import pdb; pdb.set_trace() # Traditional way
29
+ breakpoint() # Python 3.7+
30
+
31
+ # Common pdb commands:
32
+ # n - next line
33
+ # s - step into function
34
+ # c - continue
35
+ # l - list code
36
+ # p variable - print variable
37
+ # pp variable - pretty print
38
+ # h - help
39
+ ```
40
+
41
+ ### 4. IDE Debugging
42
+ - Set breakpoints by clicking on line numbers
43
+ - Use conditional breakpoints for specific cases
44
+ - Watch variables in the debugger panel
45
+ - Step through code line by line
46
+
47
+ ### 5. Logging for Production
48
+ ```python
49
+ import logging
50
+
51
+ logging.basicConfig(
52
+ level=logging.DEBUG,
53
+ format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
54
+ )
55
+ logger = logging.getLogger(__name__)
56
+
57
+ # Use different log levels
58
+ logger.debug("Detailed information for debugging")
59
+ logger.info("General information")
60
+ logger.warning("Warning messages")
61
+ logger.error("Error messages")
62
+ logger.exception("Error with traceback")
63
+ ```
64
+
65
+ ## Common Python Issues
66
+
67
+ ### 1. Import Errors
68
+ ```python
69
+ # Check Python path
70
+ import sys
71
+ print(sys.path)
72
+
73
+ # Check module location
74
+ import module_name
75
+ print(module_name.__file__)
76
+ ```
77
+
78
+ ### 2. Type Errors
79
+ ```python
80
+ # Check types before operations
81
+ if isinstance(variable, expected_type):
82
+ # Safe to proceed
83
+
84
+ # Use type hints for clarity
85
+ from typing import List, Dict, Optional
86
+
87
+ def process_data(items: List[str]) -> Dict[str, int]:
88
+ return {item: len(item) for item in items}
89
+ ```
90
+
91
+ ### 3. Async Debugging
92
+ ```python
93
+ # Debug async code
94
+ import asyncio
95
+
96
+ async def debug_async():
97
+ print(f"Current task: {asyncio.current_task()}")
98
+ print(f"All tasks: {asyncio.all_tasks()}")
99
+
100
+ # Enable asyncio debug mode
101
+ asyncio.run(main(), debug=True)
102
+ ```
103
+
104
+ ### 4. Memory Issues
105
+ ```python
106
+ # Check memory usage
107
+ import sys
108
+ print(f"Size of object: {sys.getsizeof(object)} bytes")
109
+
110
+ # Profile memory
111
+ from memory_profiler import profile
112
+
113
+ @profile
114
+ def memory_intensive_function():
115
+ # Your code here
116
+ ```
117
+
118
+ ## Testing for Debugging
119
+
120
+ ### 1. Write Minimal Test Case
121
+ ```python
122
+ def test_specific_issue():
123
+ # Isolate the problem
124
+ result = function_under_test(specific_input)
125
+ assert result == expected_output
126
+ ```
127
+
128
+ ### 2. Use pytest with verbose output
129
+ ```bash
130
+ pytest -vv test_file.py::test_specific_issue
131
+ pytest --pdb # Drop into debugger on failure
132
+ pytest --pdb-trace # Drop into debugger at start of test
133
+ ```
134
+
135
+ ### 3. Mock External Dependencies
136
+ ```python
137
+ from unittest.mock import Mock, patch
138
+
139
+ @patch('module.external_service')
140
+ def test_with_mock(mock_service):
141
+ mock_service.return_value = "expected_response"
142
+ # Test your code without external dependencies
143
+ ```
144
+
145
+ ## Performance Debugging
146
+
147
+ ### 1. Time Execution
148
+ ```python
149
+ import time
150
+
151
+ start = time.time()
152
+ # Code to measure
153
+ end = time.time()
154
+ print(f"Execution time: {end - start:.4f} seconds")
155
+
156
+ # Or use context manager
157
+ from contextlib import contextmanager
158
+
159
+ @contextmanager
160
+ def timer():
161
+ start = time.time()
162
+ yield
163
+ print(f"Execution time: {time.time() - start:.4f} seconds")
164
+
165
+ with timer():
166
+ # Code to measure
167
+ ```
168
+
169
+ ### 2. Profile Code
170
+ ```python
171
+ import cProfile
172
+ import pstats
173
+
174
+ # Profile function
175
+ cProfile.run('function_to_profile()', 'profile_stats')
176
+
177
+ # Analyze results
178
+ stats = pstats.Stats('profile_stats')
179
+ stats.sort_stats('cumulative')
180
+ stats.print_stats(10) # Top 10 functions
181
+ ```
182
+
183
+ ## Framework-Specific Debugging
184
+
185
+ ### Django
186
+ ```python
187
+ # Django shell for interactive debugging
188
+ python manage.py shell
189
+
190
+ # Django debug toolbar
191
+ # Install: pip install django-debug-toolbar
192
+ # Add to INSTALLED_APPS and MIDDLEWARE
193
+
194
+ # SQL query debugging
195
+ from django.db import connection
196
+ print(connection.queries)
197
+ ```
198
+
199
+ ### FastAPI
200
+ ```python
201
+ # Enable debug mode
202
+ app = FastAPI(debug=True)
203
+
204
+ # Log requests
205
+ @app.middleware("http")
206
+ async def log_requests(request: Request, call_next):
207
+ logger.info(f"Request: {request.method} {request.url}")
208
+ response = await call_next(request)
209
+ return response
210
+ ```
211
+
212
+ ## Debug Checklist
213
+
214
+ - [ ] Read the full error message and traceback
215
+ - [ ] Identify the exact line causing the issue
216
+ - [ ] Check variable types and values at the error point
217
+ - [ ] Verify function inputs and outputs
218
+ - [ ] Test with minimal reproducible example
219
+ - [ ] Check for common issues (None values, empty lists, type mismatches)
220
+ - [ ] Use appropriate debugging tools (print, pdb, logging)
221
+ - [ ] Write a test to prevent regression
222
+ - [ ] Document the solution for future reference