generated from Pseudo-Lab/Jupyter-Book-Template
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
## (Chapter 14) pdb 사용해보기 | ||
#### (참고) 주요 PDB 명령어 | ||
- n (next): 다음 라인으로 이동 | ||
- s (step): 함수 내부로 들어감 | ||
- c (continue): 다음 중단점까지 실행 | ||
- p (print): 변수 값 출력 | ||
- l (list): 현재 위치의 코드 표시 | ||
- q (quit): 디버거 종료 | ||
|
||
#### test.py | ||
```python | ||
import pdb | ||
|
||
def add(n1, n2): | ||
result = n1 + n2 | ||
pdb.set_trace() | ||
return result | ||
|
||
def subtract(n1, n2): | ||
result = n2 - n1 | ||
pdb.set_trace() | ||
return result | ||
|
||
a = 1 | ||
b = 2 | ||
c = 3 | ||
d = add(a, b) | ||
e = subtract(c, b) | ||
``` | ||
|
||
#### Result | ||
``` | ||
✗ python3 -m pdb test.py | ||
> /Users/user/Documents/CPython-Guide/ch14/test.py(1)<module>() | ||
-> import pdb | ||
(Pdb) n | ||
> /Users/user/Documents/CPython-Guide/ch14/test.py(3)<module>() | ||
-> def add(n1, n2): | ||
(Pdb) n | ||
> /Users/user/Documents/CPython-Guide/ch14/test.py(8)<module>() | ||
-> def subtract(n1, n2): | ||
(Pdb) n | ||
> /Users/user/Documents/CPython-Guide/ch14/test.py(13)<module>() | ||
-> a = 1 | ||
(Pdb) n | ||
> /Users/user/Documents/CPython-Guide/ch14/test.py(14)<module>() | ||
-> b = 2 | ||
(Pdb) l | ||
9 result = n2 - n1 | ||
10 pdb.set_trace() | ||
11 return result | ||
12 | ||
13 a = 1 | ||
14 -> b = 2 | ||
15 c = 3 | ||
16 d = add(a, b) | ||
17 e = subtract(c, b) | ||
18 | ||
[EOF] | ||
(Pdb) n | ||
> /Users/user/Documents/CPython-Guide/ch14/test.py(15)<module>() | ||
-> c = 3 | ||
(Pdb) n | ||
> /Users/user/Documents/CPython-Guide/ch14/test.py(16)<module>() | ||
-> d = add(a, b) | ||
(Pdb) s | ||
--Call-- | ||
> /Users/user/Documents/CPython-Guide/ch14/test.py(3)add() | ||
-> def add(n1, n2): | ||
(Pdb) n | ||
> /Users/user/Documents/CPython-Guide/ch14/test.py(4)add() | ||
-> result = n1 + n2 | ||
(Pdb) n | ||
> /Users/user/Documents/CPython-Guide/ch14/test.py(5)add() | ||
-> pdb.set_trace() | ||
(Pdb) n | ||
> /Users/user/Documents/CPython-Guide/ch14/test.py(6)add() | ||
-> return result | ||
(Pdb) n | ||
--Return-- | ||
> /Users/user/Documents/CPython-Guide/ch14/test.py(6)add()->3 | ||
-> return result | ||
(Pdb) n | ||
> /Users/user/Documents/CPython-Guide/ch14/test.py(17)<module>() | ||
-> e = subtract(c, b) | ||
(Pdb) s | ||
--Call-- | ||
> /Users/user/Documents/CPython-Guide/ch14/test.py(8)subtract() | ||
-> def subtract(n1, n2): | ||
(Pdb) c | ||
> /Users/user/Documents/CPython-Guide/ch14/test.py(11)subtract() | ||
-> return result | ||
(Pdb) l | ||
6 return result | ||
7 | ||
8 def subtract(n1, n2): | ||
9 result = n2 - n1 | ||
10 pdb.set_trace() | ||
11 -> return result | ||
12 | ||
13 a = 1 | ||
14 b = 2 | ||
15 c = 3 | ||
16 d = add(a, b) | ||
(Pdb) n | ||
--Return-- | ||
> /Users/user/Documents/CPython-Guide/ch14/test.py(11)subtract()->-1 | ||
-> return result | ||
(Pdb) n | ||
--Return-- | ||
> /Users/user/Documents/CPython-Guide/ch14/test.py(17)<module>()->None | ||
-> e = subtract(c, b) | ||
(Pdb) n | ||
--Return-- | ||
> <string>(1)<module>()->None | ||
(Pdb) n | ||
> /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/bdb.py(584)run() | ||
-> self.quitting = True | ||
(Pdb) n | ||
> /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/bdb.py(585)run() | ||
-> sys.settrace(None) | ||
(Pdb) n | ||
The program finished and will be restarted | ||
> /Users/user/Documents/CPython-Guide/ch14/test.py(1)<module>() | ||
-> import pdb | ||
(Pdb) q | ||
``` | ||
|
||
## (Chapter 15) cProfile 사용해보기 | ||
|
||
(위 test.py에서 pdb 관련 부분만 주석 처리) | ||
``` | ||
✗ python3 -m cProfile test.py | ||
5 function calls in 0.000 seconds | ||
Ordered by: standard name | ||
ncalls tottime percall cumtime percall filename:lineno(function) | ||
1 0.000 0.000 0.000 0.000 test.py:3(<module>) | ||
1 0.000 0.000 0.000 0.000 test.py:3(add) | ||
1 0.000 0.000 0.000 0.000 test.py:8(subtract) | ||
1 0.000 0.000 0.000 0.000 {built-in method builtins.exec} | ||
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} | ||
``` | ||
![image](https://github.com/heehehe/CPython-Guide/assets/41580746/6fa7b923-d139-487d-9f2f-6fb9b55dd677) |