yapping-debugrithm 0.1.0__py3-none-any.whl
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.
- debugrithm/__init__.py +0 -0
- debugrithm/debugrithm.py +121 -0
- yapping_debugrithm-0.1.0.dist-info/METADATA +406 -0
- yapping_debugrithm-0.1.0.dist-info/RECORD +7 -0
- yapping_debugrithm-0.1.0.dist-info/WHEEL +5 -0
- yapping_debugrithm-0.1.0.dist-info/licenses/LICENSE +21 -0
- yapping_debugrithm-0.1.0.dist-info/top_level.txt +1 -0
debugrithm/__init__.py
ADDED
|
File without changes
|
debugrithm/debugrithm.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
from functools import wraps
|
|
2
|
+
from pprint import pformat
|
|
3
|
+
from sys import exit
|
|
4
|
+
from time import strftime
|
|
5
|
+
from typing import Callable, NoReturn
|
|
6
|
+
|
|
7
|
+
type _Func = Callable
|
|
8
|
+
type _Void = NoReturn
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _indentprint(func: str, clas: str, text: str, *args, **kwargs) -> None:
|
|
12
|
+
time = strftime("%H%M%S")
|
|
13
|
+
comm = f"{func}@{time}"
|
|
14
|
+
head = ""
|
|
15
|
+
if clas:
|
|
16
|
+
head = f"{clas}.{comm}"
|
|
17
|
+
if text:
|
|
18
|
+
head = f"{head} '{text}'"
|
|
19
|
+
print(head)
|
|
20
|
+
|
|
21
|
+
idx = 0
|
|
22
|
+
for arg in args:
|
|
23
|
+
print(f"|{idx}| {arg!r}")
|
|
24
|
+
idx += 1
|
|
25
|
+
|
|
26
|
+
for key, val in kwargs.items():
|
|
27
|
+
if isinstance(val, (dict, list)):
|
|
28
|
+
print(f"|{idx}| {key}=")
|
|
29
|
+
|
|
30
|
+
form = pformat(val, width=60)
|
|
31
|
+
lins = form.splitlines()
|
|
32
|
+
for lin in lins:
|
|
33
|
+
print(f"| {lin}")
|
|
34
|
+
else:
|
|
35
|
+
print(f"|{idx}| {key}={val}")
|
|
36
|
+
idx += 1
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _quitprogram() -> _Void:
|
|
40
|
+
exit(0)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class Debugger:
|
|
44
|
+
def __init__(self, name: str = "", used: bool = False) -> None:
|
|
45
|
+
self._name = name
|
|
46
|
+
self._used = used
|
|
47
|
+
|
|
48
|
+
def stopped(self) -> bool:
|
|
49
|
+
return self._used is False
|
|
50
|
+
|
|
51
|
+
def running(self) -> bool:
|
|
52
|
+
return self._used is True
|
|
53
|
+
|
|
54
|
+
def pauseit(self) -> None:
|
|
55
|
+
self._used = False
|
|
56
|
+
|
|
57
|
+
def startit(self) -> None:
|
|
58
|
+
self._used = True
|
|
59
|
+
|
|
60
|
+
def expectthat(self, cond: object, yes: str = "", no: str = "") -> _Void | None:
|
|
61
|
+
if self.stopped():
|
|
62
|
+
return
|
|
63
|
+
if bool(cond):
|
|
64
|
+
_indentprint("expectthat", self._name, yes)
|
|
65
|
+
return
|
|
66
|
+
_indentprint("expectthat", self._name, no)
|
|
67
|
+
_quitprogram()
|
|
68
|
+
|
|
69
|
+
def forbidthat(self, cond: object, yes: str = "", no: str = "") -> _Void | None:
|
|
70
|
+
if self.stopped():
|
|
71
|
+
return
|
|
72
|
+
if not bool(cond):
|
|
73
|
+
_indentprint("forbidthat", self._name, yes)
|
|
74
|
+
return
|
|
75
|
+
_indentprint("forbidthat", self._name, no)
|
|
76
|
+
_quitprogram()
|
|
77
|
+
|
|
78
|
+
def breakpoint(self, text: str = "", *args, **kwargs) -> _Void | None:
|
|
79
|
+
if self.stopped():
|
|
80
|
+
return
|
|
81
|
+
_indentprint("breakpoint", self._name, text, *args, **kwargs)
|
|
82
|
+
_quitprogram()
|
|
83
|
+
|
|
84
|
+
def checkpoint(self, text: str = "", *args, **kwargs) -> None:
|
|
85
|
+
if self.stopped():
|
|
86
|
+
return
|
|
87
|
+
_indentprint("checkpoint", self._name, text, *args, **kwargs)
|
|
88
|
+
|
|
89
|
+
def bypasswith(self, curr: object, prev: object, text: str = "") -> object:
|
|
90
|
+
if self.stopped():
|
|
91
|
+
return prev
|
|
92
|
+
_indentprint("breakpoint", self._name, text)
|
|
93
|
+
return curr
|
|
94
|
+
|
|
95
|
+
def pingcalled(self, text: str = "", dump: bool = True) -> _Func:
|
|
96
|
+
def decorator(func):
|
|
97
|
+
@wraps(func)
|
|
98
|
+
def wrapper(*args, **kwargs):
|
|
99
|
+
if self.running():
|
|
100
|
+
if dump:
|
|
101
|
+
_indentprint("pingcalled", self._name, text, *args, **kwargs)
|
|
102
|
+
else:
|
|
103
|
+
_indentprint("pingcalled", self._name, text)
|
|
104
|
+
return func(*args, **kwargs)
|
|
105
|
+
return wrapper
|
|
106
|
+
return decorator
|
|
107
|
+
|
|
108
|
+
def whenproved(self, cond: object, falb: object, yes: str = "", no: str = "") -> _Func:
|
|
109
|
+
def decorator(func):
|
|
110
|
+
@wraps(func)
|
|
111
|
+
def wrapper(*args, **kwargs):
|
|
112
|
+
if self.stopped():
|
|
113
|
+
return func(*args, **kwargs)
|
|
114
|
+
if bool(cond):
|
|
115
|
+
_indentprint("whenproved", self._name, yes)
|
|
116
|
+
return func(*args, **kwargs)
|
|
117
|
+
else:
|
|
118
|
+
_indentprint("whenproved", self._name, no)
|
|
119
|
+
return falb
|
|
120
|
+
return wrapper
|
|
121
|
+
return decorator
|
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: yapping-debugrithm
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Author-email: Patrick Ghetea <ghetea_patrick@outlook.com>
|
|
5
|
+
Classifier: Programming Language :: Python :: 3
|
|
6
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
7
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
8
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
9
|
+
Requires-Python: >=3.12
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Dynamic: license-file
|
|
13
|
+
|
|
14
|
+
# Debugrithm
|
|
15
|
+
|
|
16
|
+
**Print-debugging, with a name tag and an off switch.**
|
|
17
|
+
|
|
18
|
+
Debugrithm is a lightweight Python utility for explicit assertions, execution tracing, runtime checkpoints, value overrides, and conditional decorators. You put the debugging operations directly in your code and decide when they are active.
|
|
19
|
+
|
|
20
|
+
If your current debugging strategy is `print(f"HERE {x}")` followed by `exit()`, this gives that workflow a little structure. The detective work is still yours; at least the evidence gets a timestamp.
|
|
21
|
+
|
|
22
|
+
> **IMPORTANT**
|
|
23
|
+
> **Now on PyPI!** You no longer need to clone the repository, sacrifice a weekend, and manually summon the source code. Just install it:
|
|
24
|
+
>
|
|
25
|
+
> ```bash
|
|
26
|
+
> pip install yapping-debugrithm
|
|
27
|
+
> ```
|
|
28
|
+
>
|
|
29
|
+
> Then import it in Python using the module name, minus the `yapping-` prefix:
|
|
30
|
+
>
|
|
31
|
+
> ```python
|
|
32
|
+
> import debugrithm
|
|
33
|
+
> ```
|
|
34
|
+
>
|
|
35
|
+
> Remember: `yapping-debugrithm` is the package name; `debugrithm` is the import name. Two names, one project. Don't overthink it. Python already does enough of that.
|
|
36
|
+
>
|
|
37
|
+
> Happy debugging!
|
|
38
|
+
|
|
39
|
+
[What it does](#what-it-does) · [Get started](#get-started) · [A quick example](#a-quick-example) · [How it fits together](#how-it-fits-together) · [Reference](#reference) · [Examples](#examples) · [Good to know](#good-to-know) · [Design notes](#design-notes)
|
|
40
|
+
|
|
41
|
+
## What it does
|
|
42
|
+
|
|
43
|
+
- Creates named or anonymous `Debugger` instances for tagging diagnostic output.
|
|
44
|
+
- Switches debugging on and off with explicit running and paused states.
|
|
45
|
+
- Checks expected and forbidden conditions through `expectthat()` and `forbidthat()`.
|
|
46
|
+
- Prints diagnostics and stops execution through `breakpoint()`.
|
|
47
|
+
- Prints the same style of diagnostics while continuing execution through `checkpoint()`.
|
|
48
|
+
- Substitutes temporary values with `bypasswith()` while debugging is active.
|
|
49
|
+
- Traces function calls with `pingcalled()` and optionally dumps their arguments.
|
|
50
|
+
- Gates decorated functions with `whenproved()`, including a fallback return value.
|
|
51
|
+
- Formats output with method names, timestamps, zero-based argument indices, and pretty-printed collections.
|
|
52
|
+
|
|
53
|
+
## Get started
|
|
54
|
+
|
|
55
|
+
> **NOTE**
|
|
56
|
+
> Debugrithm uses only Python's standard library and works with ordinary Python objects and control flow. Replace the module path below if your installation uses a different one.
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
from debugrithm import Debugger
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Create a debugger and turn it on explicitly:
|
|
63
|
+
|
|
64
|
+
```python
|
|
65
|
+
dbg = Debugger(name="App", used=True)
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
The constructor accepts two optional arguments:
|
|
69
|
+
|
|
70
|
+
| Argument | Default | Purpose |
|
|
71
|
+
| --- | --- | --- |
|
|
72
|
+
| `name` | `""` | Label included in formatted trace headers. |
|
|
73
|
+
| `used` | `False` | Whether the debugging features start active. |
|
|
74
|
+
|
|
75
|
+
With no arguments, the debugger starts stopped:
|
|
76
|
+
|
|
77
|
+
```python
|
|
78
|
+
dbg = Debugger()
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
> **IMPORTANT**
|
|
82
|
+
> `Debugger()` is inactive by default. Use `used=True` or call `startit()` when you want debugging output and checks. Pausing disables the debugging behavior; decorated application functions still follow the paused behavior documented below.
|
|
83
|
+
|
|
84
|
+
## A quick example
|
|
85
|
+
|
|
86
|
+
An anonymous debugger is useful for a quick inspection:
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
dbg = Debugger(used=True)
|
|
90
|
+
dbg.checkpoint("Quick check")
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Illustrative output:
|
|
94
|
+
|
|
95
|
+
```text
|
|
96
|
+
checkpoint@143000 'Quick check'
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
You still get the method name and timestamp. Leaving `name` empty only removes the `Name.` prefix from the header; it does not remove tracking or timestamping.
|
|
100
|
+
|
|
101
|
+
> **NOTE**
|
|
102
|
+
> The examples use `143000` as a sample timestamp. Your output reflects the time of the actual call.
|
|
103
|
+
|
|
104
|
+
## How it fits together
|
|
105
|
+
|
|
106
|
+
### Query the current state
|
|
107
|
+
|
|
108
|
+
Use these methods whenever you need to know whether an instance is active:
|
|
109
|
+
|
|
110
|
+
| Method | Description | Return Type |
|
|
111
|
+
|---|---|---|
|
|
112
|
+
| `running()` | Returns `True` if the debugger is active. | `bool` |
|
|
113
|
+
| `stopped()` | Returns `True` if the debugger is paused or inactive. | `bool` |
|
|
114
|
+
|
|
115
|
+
### Start and pause debugging
|
|
116
|
+
|
|
117
|
+
You can change the state at runtime:
|
|
118
|
+
|
|
119
|
+
```python
|
|
120
|
+
dbg = Debugger(used=True)
|
|
121
|
+
dbg.pauseit()
|
|
122
|
+
print(dbg.running())
|
|
123
|
+
|
|
124
|
+
dbg.startit()
|
|
125
|
+
print(dbg.running())
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
The two `print()` calls produce `False` and then `True`. While paused, assertion checks, checkpoints, breakpoints, and trace output are ignored.
|
|
129
|
+
|
|
130
|
+
### Choose what should happen at a debugging point
|
|
131
|
+
|
|
132
|
+
| Operation | Running behavior | Stopped behavior |
|
|
133
|
+
| --- | --- | --- |
|
|
134
|
+
| `expectthat(cond, ...)` | Log success if `bool(cond)` is true; log failure and call `exit(0)` otherwise. | Ignore the check. |
|
|
135
|
+
| `forbidthat(cond, ...)` | Log success if `bool(cond)` is false; log failure and call `exit(0)` otherwise. | Ignore the check. |
|
|
136
|
+
| `breakpoint(...)` | Print the diagnostics and call `exit(0)`. | Ignore the debugging operation. |
|
|
137
|
+
| `checkpoint(...)` | Print the diagnostics and continue. | Ignore the debugging operation. |
|
|
138
|
+
| `bypasswith(curr, prev, ...)` | Log the override and return `curr`. | Return `prev`. |
|
|
139
|
+
| `pingcalled(...)` | Log the call, optionally dump arguments, and invoke the function. | Invoke the function normally without tracing. |
|
|
140
|
+
| `whenproved(cond, falb, ...)` | Run the function when the condition is true; otherwise return `falb`. | Run the original function normally. |
|
|
141
|
+
|
|
142
|
+
The distinction matters most for the decorators: turning debugging off restores normal execution rather than making the decorated function disappear.
|
|
143
|
+
|
|
144
|
+
## Reference
|
|
145
|
+
|
|
146
|
+
### Require a true condition: `expectthat()`
|
|
147
|
+
|
|
148
|
+
Use `expectthat()` when a condition must be truthy:
|
|
149
|
+
|
|
150
|
+
```python
|
|
151
|
+
dbg.expectthat(cond, yes="Condition met", no="Condition failed")
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
- If the debugger is stopped, it ignores the check.
|
|
155
|
+
- If `bool(cond)` is `True`, it prints `yes` and continues.
|
|
156
|
+
- If `bool(cond)` is `False`, it prints `no` and immediately calls `exit(0)`.
|
|
157
|
+
|
|
158
|
+
For example:
|
|
159
|
+
|
|
160
|
+
```python
|
|
161
|
+
dbg = Debugger("Auth", used=True)
|
|
162
|
+
|
|
163
|
+
user_id = 42
|
|
164
|
+
dbg.expectthat(user_id > 0, yes="Valid user ID", no="Invalid user ID")
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
Output:
|
|
168
|
+
|
|
169
|
+
```text
|
|
170
|
+
Auth.expectthat@143000 'Valid user ID'
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
### Require a false condition: `forbidthat()`
|
|
174
|
+
|
|
175
|
+
Use `forbidthat()` when a condition must be falsy:
|
|
176
|
+
|
|
177
|
+
```python
|
|
178
|
+
dbg.forbidthat(cond, yes="Not forbidden", no="Forbidden condition occurred")
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
- If the debugger is stopped, it ignores the check.
|
|
182
|
+
- If `not bool(cond)` is `True`, it prints `yes` and continues.
|
|
183
|
+
- If `bool(cond)` is `True`, it prints `no` and calls `exit(0)`.
|
|
184
|
+
|
|
185
|
+
Here, `yes` means the check passed: the forbidden condition did **not** occur. `no` reports that it did occur.
|
|
186
|
+
|
|
187
|
+
> **CAUTION**
|
|
188
|
+
> These are invasive checks. A failed active check ends execution through `exit(0)`, and the exit status is **zero**, even on failure. Treat that as debugging behavior when interpreting a script's result in a shell or automation system.
|
|
189
|
+
|
|
190
|
+
### Print and stop: `breakpoint()`
|
|
191
|
+
|
|
192
|
+
An active `breakpoint()` prints a diagnostic header, positional arguments, and keyword arguments, then halts execution:
|
|
193
|
+
|
|
194
|
+
```python
|
|
195
|
+
dbg = Debugger("Pipeline", used=True)
|
|
196
|
+
dbg.breakpoint("Fatal pipeline error", code=500, retries=3)
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
Output:
|
|
200
|
+
|
|
201
|
+
```text
|
|
202
|
+
Pipeline.breakpoint@143000 'Fatal pipeline error'
|
|
203
|
+
|0| code=500
|
|
204
|
+
|1| retries=3
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
After printing, it calls `exit(0)`. This method is Debugrithm's print-and-stop operation; it does not open an interactive debugging session.
|
|
208
|
+
|
|
209
|
+
### Print and continue: `checkpoint()`
|
|
210
|
+
|
|
211
|
+
`checkpoint()` uses the same output formatting while allowing the program to continue:
|
|
212
|
+
|
|
213
|
+
```python
|
|
214
|
+
dbg = Debugger("Worker", used=True)
|
|
215
|
+
dbg.checkpoint("Processing items", count=2, items=["a", "b"])
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
Output:
|
|
219
|
+
|
|
220
|
+
```text
|
|
221
|
+
Worker.checkpoint@143000 'Processing items'
|
|
222
|
+
|0| count=2
|
|
223
|
+
|1| items=
|
|
224
|
+
| ['a', 'b']
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
Execution continues immediately after the checkpoint. Sometimes you need a breadcrumb, not a shutdown sequence.
|
|
228
|
+
|
|
229
|
+
### Substitute a value: `bypasswith()`
|
|
230
|
+
|
|
231
|
+
Use a temporary override when debugging is running and retain the original value when it is stopped:
|
|
232
|
+
|
|
233
|
+
```python
|
|
234
|
+
url = dbg.bypasswith("http://localhost:8080", "https://api.prod.com", "Overriding API")
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
| State | Returned value | Diagnostic output |
|
|
238
|
+
| --- | --- | --- |
|
|
239
|
+
| Stopped | Previous/original value: `"https://api.prod.com"`. | No override trace. |
|
|
240
|
+
| Running | Current override: `"http://localhost:8080"`. | Logs `"Overriding API"`. |
|
|
241
|
+
|
|
242
|
+
The argument order is `curr`, then `prev`, then optional text. The first value is the debugging substitute; the second is the value you use during normal execution.
|
|
243
|
+
|
|
244
|
+
### Trace a function call: `pingcalled()`
|
|
245
|
+
|
|
246
|
+
The decorator logs each invocation of the decorated function. This example uses a debugger named `Service`:
|
|
247
|
+
|
|
248
|
+
```python
|
|
249
|
+
dbg = Debugger("Service", used=True)
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
```python
|
|
253
|
+
@dbg.pingcalled("Calculating total", dump=True)
|
|
254
|
+
def calculate(price, tax=0.05):
|
|
255
|
+
return price * (1 + tax)
|
|
256
|
+
|
|
257
|
+
calculate(100, tax=0.1)
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
Output:
|
|
261
|
+
|
|
262
|
+
```text
|
|
263
|
+
Service.pingcalled@143000 'Calculating total'
|
|
264
|
+
|0| 100
|
|
265
|
+
|1| tax=0.1
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
With `dump=True`, the trace includes parameter values. Set `dump=False` to keep the invocation header while omitting the argument dump.
|
|
269
|
+
|
|
270
|
+
### Gate a function: `whenproved()`
|
|
271
|
+
|
|
272
|
+
This decorator selects between calling the function and returning a fallback:
|
|
273
|
+
|
|
274
|
+
```python
|
|
275
|
+
@dbg.whenproved(is_auth, falb="Denied", yes="Auth OK", no="Auth Failed")
|
|
276
|
+
def get_data():
|
|
277
|
+
return "Secret payload"
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
`is_auth` is the condition supplied by your application. The state and condition determine the result:
|
|
281
|
+
|
|
282
|
+
| State | Condition | Result |
|
|
283
|
+
| --- | --- | --- |
|
|
284
|
+
| Stopped | Either value. | Execute the original function normally. |
|
|
285
|
+
| Running | True. | Print `yes`, then execute the function. |
|
|
286
|
+
| Running | False. | Print `no`, then return `falb` without executing the function. |
|
|
287
|
+
|
|
288
|
+
In this example, the active false branch returns `"Denied"`; the true branch runs `get_data()` and returns `"Secret payload"`.
|
|
289
|
+
|
|
290
|
+
### Understand the diagnostic format
|
|
291
|
+
|
|
292
|
+
The internal `_indentprint()` helper assembles diagnostic headers. The debugger name, method, timestamp, and optional text are combined like this:
|
|
293
|
+
|
|
294
|
+
| Debugger Name | Text | Generated Header Pattern |
|
|
295
|
+
|---|---|---|
|
|
296
|
+
| `"App"` | `"Loaded"` | `App.checkpoint@143000 'Loaded'` |
|
|
297
|
+
| `"App"` | `""` | `App.checkpoint@143000` |
|
|
298
|
+
| `""` | `"Loaded"` | `checkpoint@143000 'Loaded'` |
|
|
299
|
+
|
|
300
|
+
An empty message omits the quoted text. An empty debugger name omits the name and its trailing dot.
|
|
301
|
+
|
|
302
|
+
Positional arguments and simple keyword arguments use zero-based indices:
|
|
303
|
+
|
|
304
|
+
```text
|
|
305
|
+
|0| 'first_arg'
|
|
306
|
+
|1| status=True
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
Dictionary and list values passed as keyword arguments use multiple lines, formatted with Python's `pprint.pformat()` using `width=60`:
|
|
310
|
+
|
|
311
|
+
```text
|
|
312
|
+
|0| data=
|
|
313
|
+
| {'items': ['a', 'b', 'c'], 'status': 200}
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
That gives larger values some breathing room without making you format each collection by hand.
|
|
317
|
+
|
|
318
|
+
### API at a glance
|
|
319
|
+
|
|
320
|
+
The signatures below collect the constructor, state methods, inspection methods, and decorators in one place. Return annotations describe the interface.
|
|
321
|
+
|
|
322
|
+
#### Instantiation & State
|
|
323
|
+
|
|
324
|
+
```text
|
|
325
|
+
Debugger(name="", used=False)
|
|
326
|
+
dbg.stopped() -> bool
|
|
327
|
+
dbg.running() -> bool
|
|
328
|
+
dbg.pauseit() -> None
|
|
329
|
+
dbg.startit() -> None
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
#### Assertions & Inspection
|
|
333
|
+
|
|
334
|
+
```text
|
|
335
|
+
dbg.expectthat(cond, yes="", no="")
|
|
336
|
+
dbg.forbidthat(cond, yes="", no="")
|
|
337
|
+
dbg.breakpoint(text="", *args, **kwargs)
|
|
338
|
+
dbg.checkpoint(text="", *args, **kwargs)
|
|
339
|
+
dbg.bypasswith(curr, prev, text="")
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
#### Decorators
|
|
343
|
+
|
|
344
|
+
```text
|
|
345
|
+
@dbg.pingcalled(text="", dump=True)
|
|
346
|
+
@dbg.whenproved(cond, falb, yes="", no="")
|
|
347
|
+
```
|
|
348
|
+
|
|
349
|
+
## Examples
|
|
350
|
+
|
|
351
|
+
### Trace a request from entry to result
|
|
352
|
+
|
|
353
|
+
This combines invocation tracing, a structured checkpoint, an assertion, and a debugging override:
|
|
354
|
+
|
|
355
|
+
```python
|
|
356
|
+
from debugrithm import Debugger
|
|
357
|
+
|
|
358
|
+
dbg = Debugger("App", used=True)
|
|
359
|
+
|
|
360
|
+
@dbg.pingcalled("Processing incoming request")
|
|
361
|
+
def handle_request(payload):
|
|
362
|
+
dbg.checkpoint("Inspecting payload structure", data=payload)
|
|
363
|
+
dbg.expectthat(len(payload) > 0, yes="Payload is valid", no="Empty payload")
|
|
364
|
+
|
|
365
|
+
return dbg.bypasswith("mock_response_200", "real_db_response", "Bypassing DB")
|
|
366
|
+
|
|
367
|
+
result = handle_request({"user_id": 42, "roles": ["admin", "editor"]})
|
|
368
|
+
print(f"Final Result: {result}")
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
Expected output, using the illustrative timestamp:
|
|
372
|
+
|
|
373
|
+
```text
|
|
374
|
+
App.pingcalled@143000 'Processing incoming request'
|
|
375
|
+
|0| {'roles': ['admin', 'editor'], 'user_id': 42}
|
|
376
|
+
App.checkpoint@143000 'Inspecting payload structure'
|
|
377
|
+
|0| data=
|
|
378
|
+
| {'roles': ['admin', 'editor'], 'user_id': 42}
|
|
379
|
+
App.expectthat@143000 'Payload is valid'
|
|
380
|
+
App.bypasswith@143000 'Bypassing DB'
|
|
381
|
+
Final Result: mock_response_200
|
|
382
|
+
```
|
|
383
|
+
|
|
384
|
+
The call is traced first, the payload is inspected next, and the nonempty-payload check passes. Because the debugger is active, `bypasswith()` returns `"mock_response_200"` instead of `"real_db_response"`.
|
|
385
|
+
|
|
386
|
+
## Good to know
|
|
387
|
+
|
|
388
|
+
Debugrithm stays focused on explicit inspection inside your own code. The current implementation does not provide:
|
|
389
|
+
|
|
390
|
+
- An interactive REPL or step-by-step execution.
|
|
391
|
+
- Variable inspection beyond the arguments you pass in.
|
|
392
|
+
- Stack-trace capture or frame inspection.
|
|
393
|
+
- File logging or log-level filtering.
|
|
394
|
+
- Thread-safe state synchronization.
|
|
395
|
+
- Asynchronous (`asyncio`) decorator support.
|
|
396
|
+
|
|
397
|
+
> **IMPORTANT**
|
|
398
|
+
> In ordinary Python execution, `exit(0)` raises `SystemExit`. It can terminate the process if it propagates, and it is not caught by `except Exception` because `SystemExit` derives from `BaseException`. Catching `SystemExit` explicitly is possible. The stop operations are still intended to end execution.
|
|
399
|
+
|
|
400
|
+
Use the running/stopped state deliberately: a paused debugger skips its assertions, returns original values from `bypasswith()`, and lets decorated functions execute normally. Those choices are part of the interface, not a replacement for your application's own validation or control flow.
|
|
401
|
+
|
|
402
|
+
## Design notes
|
|
403
|
+
|
|
404
|
+
Debugrithm embeds small assertion and inspection primitives directly into code paths. State control, assertions, fatal and non-fatal inspection, value substitution, and decorators each have a clear role, built with standard-library utilities and little overhead.
|
|
405
|
+
|
|
406
|
+
It stays compact instead of attaching an interactive debugger through a subshell or socket. The aim is to make print-debugging deliberate, readable, and easy to turn off. Your future self deserves better than twelve identical lines saying `HERE`.
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
debugrithm/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
debugrithm/debugrithm.py,sha256=Cx6qoIK-C9ce6Vfy5OXKUk2PO-iLjpTBEfF21sBCIuw,3725
|
|
3
|
+
yapping_debugrithm-0.1.0.dist-info/licenses/LICENSE,sha256=PRYv9Y8uqHFOEixDS2z_uFQtw1aVEE1mZX6fcvlVM7w,1090
|
|
4
|
+
yapping_debugrithm-0.1.0.dist-info/METADATA,sha256=ODqwGxc3rQbmUGi8GYG9lJu5RqayLo2gxs8CYQpO6Qg,14781
|
|
5
|
+
yapping_debugrithm-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
6
|
+
yapping_debugrithm-0.1.0.dist-info/top_level.txt,sha256=rxY9wKMgMFBDwOFF2BH-NznobmUswI3n8cHXPPqQWLQ,11
|
|
7
|
+
yapping_debugrithm-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ghetea-patrick
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
debugrithm
|