1. 탭 자동 완성

쉘에서 입력을 하는 동안 <Tab>을 누르면 네임스페이스에서 그 시점까지 입력한 내용과 맞아떨어지는 변수를 자동으로 찾아준다.

2. 자기 관찰

<?> : 변수 이름 앞이나 뒤에 ? 기호를 붙이면 그 객체에 대한 일반 정보를 출력

<??> : 가능한 경우 함수의 소스코드를 보여줌

<*>도 사용 가능

3. %run 명령어

%run ipython_script_test.py : python ipython_script_test.py 와 동일하게 실행

%run -i 대화형 네임스페이스에 미리 선언된 변수에 접근해야 할 때


4. %debug

에러가 났을 때 예외가 발생한 시점의 스택 프레임 정보를 보여줌

%run -d : 스크립트를 실행하기 전에 디버거를 먼저 실행함

디버거 명령과 같은 변수가 있다면 !를 변수 이름 앞에 붙여서 내용 확인 가능


5. 쉽게 디버깅 하는 법

(1) set_trace()

def set_trace():

from IPython.core.debugger import Pdb

Pdb(color_scheme='Linux').set_trace(sys._getframe().f_back)

 함수를 정의 해 둔 후 문자가 생기는 곳 바로 위에 set_trace()코드를 넣으면 멈추고 살펴볼 수 있음


(2) debug()

def debug(f, *args, **kwargs):

from IPython.core.debugger import Pdb

pdb = Pdb(color_scheme = 'Linux')

return pdb.runcall(f, *args, **kwargs)

def f(x, y, z=1):

tmp = x + y

return tmp / z


debug(f, 1, 2, z=3)

과 같은 방식으로 코딩하면 됨


6. 코드 시간 측정

% time method1 = [x for x in strings if x.startswith('foo')]

%timeit method2 = [x for x in strings if x[:3] == 'foo']

%timeit은 여러번 실행 후 평균 시간 값을 return해줌


7. 기본적인 프로파일링 : 각 함수에서 소모된 시간을 기록

cProfile은 프로그램이나 임의의 코드 블록을 실행하면서 각 함수에서 소모된 시간을 계속 기록한다.

cprof_example.py

import numpy as np

from numpy.linalg import eigvals


def run_experiment(niter=100):

    K = 100

    results = []

    for _ in xrange(niter):

        mat = np.random.randn(K, K)

        max_eigenvalue = np.abs(eigvals(mat)).max()

        results.append(max_eigenvalue)

    return results

some_results = run_experiment()

print 'Largest one we saw: %s' % np.max(some_results)

python -m cProfile -s cumulative cprof_example.py

주의 : ipython notebook에서 실행할 때는 !를 맨 앞에 붙이고 실행해야 함!!

%run -p -s cumulative cprof_exaple.py


8. IPython HTML 노트북

ipython notebook --pylab=inline

실행 시 UTF-8 에러 날 경우 아래 사이트 참조

http://stackoverflow.com/questions/15526996/ipython-notebook-locale-error 


Posted by 공놀이나하여보세
,