Interactive Python provides some Built-in magic commands that can help up when developing code.
In [ ]:
?
In [ ]:
%quickref
In [ ]:
help
In [ ]:
a = 1
a?
In [ ]:
!cd /
In [ ]:
!pwd
In [ ]:
%cd /
In [ ]:
!pwd
In [ ]:
%cd ~/YAPT
In [ ]:
!pwd
In [ ]:
%whos
In [ ]:
a
In [ ]:
%time a=3
In [ ]:
a
In [ ]:
a
In [ ]:
%timeit a=4
In [ ]:
a
In [ ]:
%run hello_world.py
In [ ]:
# %load module.py
a = 1
In [ ]:
%run module.py
In [ ]:
%whos
In [ ]:
a
In [ ]:
!cat substract_curves.py
In [ ]:
!paste dataset1.txt dataset2.txt
In [ ]:
%run -t substract_curves.py dataset1.txt dataset2.txt
A longer example (for the sake of the correctness of substract_curves.py):
In [ ]:
# Generate two "random" similar signals.
import io
import numpy as np
x = 0
base = []
with io.open('dataset3.txt', 'w') as file:
for i in range(100):
x += np.random.randint(10) - 4.5
file.write('{}\t{}\n'.format(i*2,x))
base.append(x)
with io.open('dataset4.txt', 'w') as file:
for i in range(99):
x = base[i] + np.random.randint(10) - 4.5
file.write('{}\t{}\n'.format(i*2+1,x))
In [ ]:
# Run "substract_curves.py" and its stdout is written in "differences.txt".
import IPython.utils
with IPython.utils.io.capture_output() as captured:
!python substract_curves.py dataset3.txt dataset4.txt
import io
with io.open('differences.txt', 'w') as file:
file.write(captured.stdout)
In [ ]:
# Plot the signals.
import matplotlib.pyplot as plt
plt.plot(*np.loadtxt("dataset3.txt",unpack=True), linewidth=2.0)
plt.plot(*np.loadtxt("dataset4.txt",unpack=True), linewidth=2.0)
plt.plot(*np.loadtxt("differences.txt",unpack=True), linewidth=2.0)
plt.show()
Provides line-by-line running information of decorated methods.
@profile those methods you want to profile.
@profile
def profile_me(...):
...
kernprof -l -v <your_module>.py
In [ ]:
!cat profile_quicksort.py
In [ ]:
!kernprof -l -v profile_quicksort.py
<!-- !cat quicksort.c ->
In [ ]:
<!-- !python -m cProfile quicksort.py -->
In [1]:
%load_ext Cython
In [2]:
def f_slow(i):
return i**4 + 3*i**2 + 10
In [3]:
%timeit f_slow(100)
In [4]:
%%cython --annotate
def f_cython(int i):
return i**4 + 3*i**2 + 10
Out[4]:
In [5]:
%timeit f_cython(100)