|
22 | 22 | "\n", |
23 | 23 | "import mimetypes,bz2,pickle,random,json,urllib,subprocess,shlex\n", |
24 | 24 | "from contextlib import contextmanager\n", |
| 25 | + "from pdb import set_trace\n", |
25 | 26 | "from urllib.request import Request,urlopen\n", |
26 | 27 | "from urllib.error import HTTPError\n", |
27 | 28 | "from urllib.parse import urlencode" |
|
294 | 295 | { |
295 | 296 | "data": { |
296 | 297 | "text/plain": [ |
297 | | - "<__main__._t at 0x7f4a534e8730>" |
| 298 | + "<__main__._t at 0x7fe83401cca0>" |
298 | 299 | ] |
299 | 300 | }, |
300 | 301 | "execution_count": null, |
|
2169 | 2170 | { |
2170 | 2171 | "data": { |
2171 | 2172 | "text/plain": [ |
2172 | | - "['b', 'f', 'c', 'a', 'g', 'h', 'd', 'e']" |
| 2173 | + "['h', 'c', 'e', 'a', 'g', 'b', 'f', 'd']" |
2173 | 2174 | ] |
2174 | 2175 | }, |
2175 | 2176 | "execution_count": null, |
|
2521 | 2522 | "#export\n", |
2522 | 2523 | "def trace(f):\n", |
2523 | 2524 | " \"Add `set_trace` to an existing function `f`\"\n", |
| 2525 | + " if getattr(f, '_traced', False): return f\n", |
2524 | 2526 | " def _inner(*args,**kwargs):\n", |
2525 | 2527 | " set_trace()\n", |
2526 | 2528 | " return f(*args,**kwargs)\n", |
| 2529 | + " _inner._traced = True\n", |
2527 | 2530 | " return _inner" |
2528 | 2531 | ] |
2529 | 2532 | }, |
2530 | 2533 | { |
2531 | 2534 | "cell_type": "markdown", |
2532 | 2535 | "metadata": {}, |
2533 | 2536 | "source": [ |
2534 | | - "We can faciltate debugging with [the python debugger (pdb)](https://docs.python.org/3/library/pdb.html) by annotating your function with the `@trace` decorator:" |
2535 | | - ] |
2536 | | - }, |
2537 | | - { |
2538 | | - "cell_type": "code", |
2539 | | - "execution_count": null, |
2540 | | - "metadata": {}, |
2541 | | - "outputs": [], |
2542 | | - "source": [ |
2543 | | - "@trace\n", |
2544 | | - "def myfunc(x):return x +1" |
2545 | | - ] |
2546 | | - }, |
2547 | | - { |
2548 | | - "cell_type": "markdown", |
2549 | | - "metadata": {}, |
2550 | | - "source": [ |
2551 | | - "Now, when the function is called it will drop you into the debugger. Note, you must issue the `s` command when you begin to step into the function that is being traced:\n", |
| 2537 | + "You can add a breakpoint to an existing function, e.g:\n", |
2552 | 2538 | "\n", |
2553 | | - "```bash\n", |
2554 | | - "> myfunc(3)\n", |
2555 | | - "> <ipython-input-257-13ccd7456a8e>(6)_inner()\n", |
2556 | | - " 3 \"Add `set_trace` to an existing function `f`\"\n", |
2557 | | - " 4 def _inner(*args,**kwargs):\n", |
2558 | | - " 5 set_trace()\n", |
2559 | | - "----> 6 return f(*args,**kwargs)\n", |
2560 | | - " 7 return _inner\n", |
| 2539 | + "```python\n", |
| 2540 | + "Path.cwd = trace(Path.cwd)\n", |
| 2541 | + "Path.cwd()\n", |
| 2542 | + "```\n", |
2561 | 2543 | "\n", |
2562 | | - "ipdb> s\n", |
2563 | | - "--Call--\n", |
2564 | | - "> <ipython-input-260-b3d1b2a13afb>(1)myfunc()\n", |
2565 | | - "----> 1 @trace\n", |
2566 | | - " 2 def myfunc(x):return x +1\n", |
2567 | | - "```" |
| 2544 | + "Now, when the function is called it will drop you into the debugger. Note, you must issue the `s` command when you begin to step into the function that is being traced." |
2568 | 2545 | ] |
2569 | 2546 | }, |
2570 | 2547 | { |
|
0 commit comments