-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathphase5_devtools.py
More file actions
231 lines (182 loc) · 5.78 KB
/
phase5_devtools.py
File metadata and controls
231 lines (182 loc) · 5.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/env python3
"""
Phase 5 Features Example - Developer Tools.
Demonstrates the new Phase 5 developer tools:
- Pretty exceptions with syntax highlighting
- Debug output for objects
- Object inspection
- Diff visualization
- Tree view
- Logging handler
Run: python examples/phase5_devtools.py
"""
import logging
import sys
from dataclasses import dataclass
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
import clickmd
@dataclass
class User:
"""Sample user class for debugging."""
name: str
email: str
age: int
active: bool = True
# Constants for demo values
DEBUG_NUMBER = 42
HTTP_STATUS_OK = 200
SAMPLE_AGE = 30
def demo_debug() -> None:
"""Demonstrate debug output."""
clickmd.md("# 🔍 Debug Output\n")
clickmd.md("## Simple Values\n")
clickmd.debug(DEBUG_NUMBER, name="integer")
clickmd.debug(3.14159, name="float")
clickmd.debug("Hello, World!", name="string")
clickmd.debug(True, name="boolean")
clickmd.debug(None, name="none")
clickmd.md("\n## Collections\n")
clickmd.debug([1, 2, 3, "four", 5.0], name="list")
clickmd.debug({"name": "Alice", "age": SAMPLE_AGE, "active": True}, name="dict")
clickmd.debug({1, 2, 3}, name="set")
clickmd.md("\n## Nested Structure\n")
data = {
"users": [
{"name": "Alice", "roles": ["admin", "user"]},
{"name": "Bob", "roles": ["user"]},
],
"config": {
"debug": True,
"version": "1.0.0",
}
}
clickmd.debug(data, name="nested_data")
clickmd.md("\n## Custom Object\n")
user = User(name="Alice", email="alice@example.com", age=SAMPLE_AGE)
clickmd.debug(user, name="user_object")
def demo_inspect() -> None:
"""Demonstrate object inspection."""
clickmd.md("\n# 🔬 Object Inspection\n")
clickmd.md("## Inspect String\n")
clickmd.inspect_obj("Hello")
clickmd.md("\n## Inspect List\n")
clickmd.inspect_obj([1, 2, 3])
clickmd.md("\n## Inspect Custom Object\n")
user = User(name="Bob", email="bob@example.com", age=25)
clickmd.inspect_obj(user)
def demo_tree() -> None:
"""Demonstrate tree view."""
clickmd.md("\n# 🌳 Tree View\n")
data = {
"src": {
"components": {
"Button.tsx": "component",
"Input.tsx": "component",
"Form.tsx": "component",
},
"pages": {
"Home.tsx": "page",
"About.tsx": "page",
},
"utils": {
"api.ts": "utility",
"helpers.ts": "utility",
},
},
"public": {
"index.html": "html",
"favicon.ico": "icon",
},
"package.json": "config",
}
clickmd.tree(data, name="project/")
def demo_diff():
"""Demonstrate diff visualization."""
clickmd.md("\n# 📊 Diff Visualization\n")
old_code = '''def greet(name):
print("Hello, " + name)
return None'''
new_code = '''def greet(name: str) -> str:
"""Generate a greeting."""
message = f"Hello, {name}!"
print(message)
return message'''
clickmd.md("## Code Changes\n")
clickmd.diff(old_code, new_code, old_name="greet_v1.py", new_name="greet_v2.py")
def demo_logging():
"""Demonstrate logging handler."""
clickmd.md("\n# 📝 Logging Handler\n")
# Create logger with clickmd handler
logger = logging.getLogger("demo")
logger.setLevel(logging.DEBUG)
logger.handlers = [] # Clear existing handlers
logger.addHandler(clickmd.ClickmdHandler())
clickmd.md("## Log Messages\n")
logger.debug("Debug message - detailed information")
logger.info("Info message - general information")
logger.warning("Warning message - something to watch")
logger.error("Error message - something went wrong")
logger.critical("Critical message - system failure")
def demo_pretty_exceptions():
"""Demonstrate pretty exceptions."""
clickmd.md("\n# 🛑 Pretty Exceptions\n")
clickmd.md("""
Pretty exceptions provide:
- Syntax-highlighted code snippets
- Shortened file paths
- Colored traceback
To enable globally:
```python
import clickmd
clickmd.install_excepthook()
```
""")
# Show a formatted exception without actually raising
clickmd.md("## Example Exception Format\n")
try:
# Simulate an error
result = {"users": []}["admins"][0]
except Exception as e:
formatter = clickmd.PrettyExceptionFormatter()
output = formatter.format_exception(type(e), e, e.__traceback__)
print(output)
def demo_combined():
"""Demonstrate combined usage."""
clickmd.md("\n# 🎨 Combined Example: API Response Debug\n")
# Simulate API response
response = {
"status": HTTP_STATUS_OK,
"data": {
"users": [
{"id": 1, "name": "Alice", "email": "alice@example.com"},
{"id": 2, "name": "Bob", "email": "bob@example.com"},
],
"pagination": {
"page": 1,
"per_page": 10,
"total": 2,
}
},
"meta": {
"request_id": "abc-123",
"timestamp": "2024-01-07T20:00:00Z",
}
}
clickmd.debug(response, name="api_response")
clickmd.hr()
clickmd.tree(response, name="Response Structure")
if __name__ == "__main__":
print(f"\n{'=' * 60}")
print("clickmd Phase 5 Developer Tools Demo")
print(f"{'=' * 60}\n")
demo_debug()
demo_inspect()
demo_tree()
demo_diff()
demo_logging()
demo_pretty_exceptions()
demo_combined()
print(f"\n{'=' * 60}")
print("Demo Complete!")
print(f"{'=' * 60}\n")