In [2]:
def readlinebyline( filename ) :
    '''This function takes in a textfile, and reads line by line
    and prints to stdout.'''
    f = open(filename,'r')
    for line in f :
        print line
    f.close()

In [6]:
from print_line_by_line import readlinebyline

In [12]:
# The glob object 
from glob import glob

In [18]:
glob('data*.txt')


Out[18]:
['data1.txt', 'data2.txt']

In [1]:
import print_line_by_line as pll

In [2]:
reload(pll)
pll.printtonewtext('data1.txt')

In [3]:
import numpy as np

In [4]:
arr1 = np.loadtxt('data1.txt')

In [39]:
# Transpose is an attribute of the numpy array object
arr1.T


Out[39]:
array([[   1.,    2.,    3.,    4.,    5.,    6.,    7.,    8.,    9.,
          10.],
       [   4.,    9.,   16.,   25.,   36.,   49.,   64.,   81.,  100.,
         121.]])

In [7]:
data1 = pll.readasnumpy('data1.txt')

In [46]:
# numpy has methods that take in numpy arrays as arguments
np.sum(data1)
np.diff(data1)


Out[46]:
array([[  1.,   1.,   1.,   1.,   1.,   1.,   1.,   1.,   1.],
       [  5.,   7.,   9.,  11.,  13.,  15.,  17.,  19.,  21.]])

In [5]:
import pylab as plb
%pylab inline


Populating the interactive namespace from numpy and matplotlib

In [9]:
plb.plot(data1[0], data1[1],c='r',ls=':')


Out[9]:
[<matplotlib.lines.Line2D at 0x105df7bd0>]

In [14]:
# Can save the figure under many different extensions: pdf, eps, png
plb.savefig('fig1.pdf')


<matplotlib.figure.Figure at 0x105db8790>

In [18]:
import plot_my_data as pmd

In [19]:
# Links to this notebook and the modules it imports
# are in the etherpad
pmd.plot_in_line(data1,figname='myfigipynotebook')



In [20]:
ls


Functions Modules and Libraries.ipynb  generate_data.sh~
LICENSE                                myfigipynotebook.png
Python Introduction.ipynb              plot_my_data.py
README.md                              plot_my_data.pyc
data1.txt                              plot_my_data.py~
data2.txt                              print_line_by_line.py
data3.txt                              print_line_by_line.pyc
fig1.eps                               print_line_by_line.py~
fig1.pdf                               simple_for_loop.sh*
fig1.png                               simple_for_loop.sh~*
generate_data.sh

In [21]:
__name__


Out[21]:
'__main__'

In [22]:
reload(pmd)


plot_my_data
Out[22]:
<module 'plot_my_data' from 'plot_my_data.py'>

In [23]:
reload(pmd)


Out[23]:
<module 'plot_my_data' from 'plot_my_data.py'>

In [24]:
reload(pmd)


plot_my_data
Out[24]:
<module 'plot_my_data' from 'plot_my_data.py'>

In [ ]: