DRAFT version 1.6
Prepared for:
Clemson University Cyberinfrastructure Technology Integration (CITI)
Mark Smotherman
June 2011
| Table of Contents |
| Introduction |
Python programming language
| Interpreter Example (user enters the blue text and the control character) |
[cmd-line-prompt] python Python 2.4.3 (#1, Nov 11 2010, 13:30:19) [GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> for i in range(0,5): ... print i, ... 0 1 2 3 4 >>> cntl-D [cmd-line-prompt] |
| Command Line Example (user enters the blue text and the control character) |
[cmd-line-prompt] cat > test.py
for i in range(0,5):
print i,
cntl-D
[cmd-line-prompt] python test.py
0 1 2 3 4
[cmd-line-prompt]
|
| Executable Script Example (user enters the blue text and the control character) |
[cmd-line-prompt] cat > test
#!/usr/bin/env python
cntl-D
[cmd-line-prompt] cat test.py >> test
[cmd-line-prompt] cat test
#!/usr/bin/env python
for i in range(0,5):
print i,
[cmd-line-prompt] chmod u+x test
[cmd-line-prompt] ./test
0 1 2 3 4
[cmd-line-prompt]
|
| Dynamic Typing Example |
>>> a = 1 >>> type(a) <type 'int'> >>> a = 'a' >>> type(a) <type 'str'> |
| Undefined Object Example |
>>> z Traceback (most recent call last): File "<stdin>", line 1, in ? NameError: name 'z' is not defined |
| Data Types |
Overview
| Ordered Compound Types |
Overview
| Strings |
"""Mr. Praline: Look, I took the liberty of examining that parrot when I got it home, and I discovered the only reason that it had been sitting on its perch in the first place was that it had been NAILED there."""
| String Indexing and Slicing Example |
>>> s = 'this is a test' >>> s[3] 's' >>> s[0:4] 'this' >>> s[:5] 'this ' >>> s[5:] 'is a test' |
| String Negative Indexing Example |
>>> s = 'this is a test' >>> s[-3] 'e' |
| String Infix Operators Example |
>>> 'abc' + 'def'
'abcdef'
>>> 'abc'*2
'abcabc'
>>> 'abc','def'
('abc', 'def')
|
| String Length Example |
>>> s='abcde' >>> len(s) 5 |
| String Methods Example |
>>> s = 'this is a test'
>>> s.count('is')
2
>>> s.find('a') # 0-origin
8
>>> s.replace('a','the')
'this is the test'
|
| Lists |
| List Indexing and Slicing Example |
>>> a = [1,2,3,4] >>> a[1] [2] >>> a[1:1] [] >>> a[1:2] [2] >>> a[1:3] [2, 3] >>> a[:3] [1, 2, 3] |
| List Negative Indexing Example |
>>> a = [1,2,3,4] >>> a[-2] 3 |
| List Infix Operators Example |
>>> a = [1,2] >>> b = [3,4] >>> a+b [1, 2, 3, 4] >>> a*2 [1, 2, 1, 2] >>> a,b ([1, 2], [3, 4]) |
| List Length Example |
>>> a = [1,2,3,4] >>> len(a) 4 |
| List Slice Assignment Example |
>>> a = [1,2,3,4] >>> a[2] = [5,6,7] # single element >>> a [1, 2, [5, 6, 7], 4] >>> len(a) 4 >>> b = [1,2,3,4] >>> b[2:3] = [5,6,7] # slice >>> b [1, 2, 5, 6, 7, 4] >>> len(b) 6 >>> b[1:4] = [] >>> b [1, 7, 4] |
| List Delete Element Example |
>>> a = [1,2,3,4] >>> del a[2] >>> a [1, 2, 4] >>> del a[0:2] >>> a [4] |
| List Methods Example |
>>> a = [1,'x',3.0]
>>> a.append('y')
>>> a
[1, 'x', 3.0, 'y']
>>> a.pop()
'y'
>>> a
[1, 'x', 3.0]
>>> a.reverse()
>>> a
[3.0, 'x', 1]
|
| Split and Join Example |
>>> a = 'this is a test of split and join' >>> b = a.split() >>> b ['this', 'is', 'a', 'test', 'of', 'split', 'and', 'join'] >>> c = '-'.join(b) >>> c 'this-is-a-test-of-split-and-join' >>> '; '.join(['1','2','3']) '1; 2; 3' |
| Unordered Compound Types |
Overview
| Sets |
| Set Operations Example |
>>> s1 = set(['a','b']) >>> s2 = set(['b','c']) >>> s1 | s2 set(['a', 'c', 'b']) >>> s1 & s2 set(['b']) >>> s1 ^ s2 set(['a', 'c']) |
| Dictionaries |
| Dictionary Example |
>>> assoc_array_names = { 'Snobol4':'table', 'Perl':'hash', 'C++':'map' }
>>> assoc_array_names['Snobol4']
'table'
>>> assoc_array_names['Python']
Traceback (most recent call last):
File "
|
| Statements |
Overview
| Deep Versus Shallow Copies |
| Shallow Copy Example |
>>> a = 1 >>> b = a >>> a 1 >>> b 1 >>> a = 2 >>> a # b is unchanged 2 >>> b 1 >>> a = [1,2] >>> b = a >>> a[0] = 3 # changes b[0] also! >>> a [3, 2] >>> b [3, 2] |
| Deep Copy Example |
>>> a = [1,2] >>> b = a >>> from copy import deepcopy >>> c = deepcopy(a) >>> a[0] = 3 >>> a [3, 2] >>> b # points to same place as a [3, 2] >>> c # unchanged; different memory [1, 2] |
| Conditional Statements |
| Conditional Statement Example |
>>> a = 55 >>> if a < 0: ... sign = -1 ... elif a == 0: ... sign = 0 ... else: ... sign = 1 ... >>> sign 1 |
| Looping |
| Loop Iterating Through List Example |
>>> a = [1,2,3] >>> for x in a: ... print x, ... 1 2 3 |
| Range Generation Example |
>>> range(5) [0, 1, 2, 3, 4] >>> range(5, 10) [5, 6, 7, 8, 9] >>> range(0, 10, 3) [0, 3, 6, 9] |
| For Loop with Else Example |
>>> for i in range(3,6): ... print 'in loop with i = ',i ... else: ... print 'outside loop with i = ',i ... in loop with i = 3 in loop with i = 4 in loop with i = 5 outside loop with i = 5 |
| List Comprehension Example [from Rossum tutorial] |
>>> [str(round(355/113.0, i)) for i in range(1,6)] ['3.1', '3.14', '3.142', '3.1416', '3.14159'] |
| Enumerate Example [from Rossum tutorial] |
>>> for i,v in enumerate(['tic', 'tac', 'toe']): ... print i,v ... 0 tic 1 tac 2 toe |
| Zip Example [from Rossum tutorial] |
>>> questions = ['name', 'quest', 'favorite color'] >>> answers = ['lancelot', 'the holy grail', 'blue'] >>> for q, a in zip(questions, answers): ... print 'What is your %s? It is %s.' % (q,a) ... What is your name? It is lancelot. What is your quest? It is the holy grail. What is your favorite color? It is blue. |
| Dictionary Traversal Example [from Rossum tutorial] |
>>> knights = {'gallahad':'the pure', 'robin':'the brave'}
>>> for k,v in knights.iteritems():
... print k,v
...
gallahad the pure
robin the brave
|
| Functions |
Overview
| Fibonacci Procedure and Function Examples [from Rossum tutorial] |
>>> def fib(n): # write Fibonacci series up to n ... """Print a Fibonacci series up to n.""" ... a,b = 0,1 ... while b < n: ... print b, ... a,b = b,a+b ... >>> fib(2000) 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 >>> def fib2(n): # return Fibonacci series up to n ... """Return a list containing the Fibonacci series up to n.""" ... result = [] ... a,b = 0,1 ... while b < n: ... result.append(b) ... a,b = b,a+b ... return result ... >>> f100 = fib2(100) >>> f100 [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] |
| Argument Passing Example |
>>> def f(x): ... x = 3 ... >>> a = 1 >>> f(a) >>> a 1 |
| Global Variable Example |
>>> a = 55 >>> def f2(x): ... a = x ... >>> def f3(x): ... global a ... a = x ... >>> a 55 >>> f2(22) >>> a 55 >>> f3(33) >>> a 33 |
| Default Arguments Example |
>>> a = 1 >>> b = 2 >>> def f(arg1=20,arg2=b): ... print arg1,arg2 ... >>> f(a,2) 1 2 >>> f(3) 3 2 >>> b = 4 >>> f(3) 3 2 >>> f <function f at 0x2ac232a94938> >>> f() 20 2 |
| Generator Function Example [from Scullin tutorial] |
>>> def squares(lastterm): ... for n in range(lastterm): ... yield n**2 ... >>> for i in squares(4): ... print i, ... 0 1 4 9 |
| Input/Output |
Overview
| File Input Example |
[cmd-line-prompt] cat numbers.txt
1
2.3
4.0
[cmd-line-prompt] cat numbers.py
# for loop over lines in file
data = open('numbers.txt','r')
for d in data.xreadlines():
# remove trailing newline from d
e = d.rstrip()
print e
data.close()
# while loop version with line-at-a-time reads
data = open('numbers.txt','r')
d = data.readline()
while d:
print d.rstrip()
d = data.readline()
data.close()
[cmd-line-prompt] python numbers.py
1
2.3
4.0
1
2.3
4.0
|
| File Output Example |
[cmd-line-prompt] cat numbers2.py
# write one string at a time to the file
data = open('numbers2.txt','w')
for i in range(3):
s = 'record ' + str(i) + '\n'
data.write(s)
data.close()
# create a list and then write to file
data = open('numbers3.txt','w')
lines = []
for i in range(3):
s = 'record ' + str(i) + '\n'
lines.append(s)
data.writelines(lines)
data.close()
[cmd-line-prompt] python numbers2.py
[cmd-line-prompt] cat numbers2.txt
record 0
record 1
record 2
[cmd-line-prompt] cat numbers3.txt
record 0
record 1
record 2
|
| Modules |
Overview
| Module Import Example |
>>> import my_functions >>> dir(my_functions) ['__builtins__', '__doc__', '__file__', '__name__', 'fn1', 'fn2'] >>> my_functions.fn1(1) call to fn1 with value 1 |
| Functional Style Programming |
Overview
| Reduction Example [from Rossum tutorial] |
>>> def sum(seq): ... def add(x,y): return x+y ... return reduce(add, seq, 0) ... >>> sum(range(1, 11)) 55 >>> sum([]) 0 |
| List Sorting Example with Key Function Defined Using Lamda Form |
>>> a = [[1,5],[2,3],[3,6]] >>> sorted(a,key = lambda x:x[1]) [[2, 3], [1, 5], [3, 6]] |
| OO Style Programming |
Overview
| Simple Class Example |
>>> class stack_example: ... def __init__(self): ... self._list = [] ... def push(self,item): ... self._list.append(item) ... def pop(self): ... if len(self._list) > 0: ... return self._list.pop() ... else: ... return 0 ... def empty(self): ... return len(self._list) == 0 ... >>> x = stack_example() >>> x.empty() True >>> x.push(1) >>> x.empty() False >>> x.pop() 1 |
| Class Example with + Operator Overloading |
[cmd-line-prompt] cat verbose_class.py
object_counter = 1
class verbose_example:
def __init__(self,data=0):
global object_counter
self._value = data
self._object_id = object_counter
object_counter += 1
print ' constructor for object',self._object_id,
print 'with value',self._value
def __repr__(self):
print ' string representation for object',self._object_id
return repr(self._value)
def __add__(self,other):
print ' addition for object',self._object_id
try:
print ' (1) first assume addition is to another object'
return verbose_example(self._value + other._value)
except:
print ' (2) if not, assume addition is to constant'
return verbose_example(self._value + other)
# assignment operator cannot be overloaded
def __del__(self):
print ' destructor for object',self._object_id
print '-- instantiate y and z --'
y = verbose_example()
z = verbose_example(10)
print '-- add y and z and assign to x: x = y + z --'
x = y + z
print '-- print x'
print x
print '-- add x and 20 and assign to x: x = x + 20 --'
x = x + 20
print '-- print x --'
print x
print '-- assign x to a: a = x (creates alias to same object) --'
a = x
print '-- print a --'
print a
print '-- end program --'
[cmd-line-prompt] python verbose_class.py
-- instantiate y and z --
constructor for object 1 with value 0
constructor for object 2 with value 10
-- add y and z and assign to x: x = y + z --
addition for object 1
(1) first assume addition is to another object
constructor for object 3 with value 10
-- print x
string representation for object 3
10
-- add x and 20 and assign to x: x = x + 20 --
addition for object 3
(1) first assume addition is to another object
(2) if not, assume addition is to constant
constructor for object 4 with value 30
destructor for object 3
-- print x --
string representation for object 4
30
-- assign x to a: a = x (creates alias to same object) --
-- print a --
string representation for object 4
30
-- end program --
destructor for object 4
destructor for object 1
destructor for object 2
|
| Inheritance Example |
[cmd-line-prompt] cat inherit.py
class parent:
def __init__(self,data=0):
self._value = data
def __repr__(self):
return repr(self._value)
def reset(self):
self._value = 0
class child(parent):
def update(self,data):
self._value += data
x = child(10)
print x
x.update(1)
print x
x.reset()
print x
[cmd-line-prompt] python inherit.py
10
11
0
|
| Regular Expressions |
Overview
| Regular Expression Example |
>>> import re >>> re.sub(r'(\b[a-z]+) \1', r'\1', "It's a fair cop") "It's a fair cop" >>> re.sub(r'(\b[a-z]+) \1', r'\1', "It's a a fair cop") "It's a fair cop" >>> re.sub(r'(\b[a-z]+) \1', r'\1', "It's a a a fair cop") "It's a a fair cop" >>> re.sub(r'(\b[a-z]+) \1', r'\1', "It's a a a a fair cop") "It's a a fair cop" |
| Compiled Regular Expression Example |
>>> import re >>> repeated_word_pattern = re.compile(r'(\b[a-z]+) \1') >>> repeated_word_pattern.sub(r'\1',"It's a a fair cop") "It's a fair cop" >>> repeated_word_pattern.sub(r'\1',"It's a a a fair cop") "It's a a fair cop" |
| OS Module |
Overview
| Shell Command Example |
>>> import os
>>> os.name
'posix'
>>> os.system('echo "this is a test"')
this is a test
0
>>> status = os.system('echo "this is a test"')
this is a test
>>> status
0
|
| Bioinformatics Examples |
| GC Calculation Example [from Xie tutorial] |
>>> dna = "gcatgacgttattacgactctg"
>>> len(dna)
22
>>> dna.count("a")
5
>>> gc = (100 * (dna.count("c")+dna.count("g"))) / float(len(dna))
>>> "%.2f" % gc
'45.45'
|
| Reverse Complement Function Example [from Xie tutorial] |
>>> from string import *
>>> dna = "gcatgacgttattacgactctg"
>>> def revcomp(dna):
... """ reverse complement of a DNA sequence """
... comp = dna.translate(maketrans("AGCTagct","TCGAtcga"))
... lcomp = list(comp)
... lcomp.reverse()
... return join(lcomp,"")
...
>>> dna
'gcatgacgttattacgactctg'
>>> revcomp(dna)
'cagagtcgtaataacgtcatgc'
|
| String Find and Replace Example [from Schuerer tutorial] |
>>> dna = """tgaattctatgaatggactgtccccaaagaagtaggacccactaatgcagatcctgga
tccctagctaagatgtattattctgctgtgaattcgatcccactaaagat"""
>>> count(dna,'a') # call of unqualified function
Traceback (most recent call last):
File "<stdin> line 1, in ?
NameError: name 'count' is not defined
>>> dna.count('a')
33
>>> from string import count, find, replace
>>> dna
'tgaattctatgaatggactgtccccaaagaagtaggacccactaatgcagatcctgga\ntccctagctaagatgtattattctgctgtgaattcgatcccactaaagat'
>>> replace(dna,'\n','')
'tgaattctatgaatggactgtccccaaagaagtaggacccactaatgcagatcctggatccctagctaagatgtattattctgctgtgaattcgatcccactaaagat'
>>> dna = replace(dna,'\n','')
>>> dna
'tgaattctatgaatggactgtccccaaagaagtaggacccactaatgcagatcctggatccctagctaagatgtattattctgctgtgaattcgatcccactaaagat'
>>> EcoRI = 'gaattc'
>>> count(dna,EcoRI) # now defined from import statement
2
>>> find(dna,EcoRI) # find position of first match
1
>>> find(dna,EcoRI,2) # find position of second match
88
|
| Long String Example [from Schuerer tutorial] |
>>> from string import replace
>>> dna = """tgaattctatgaatggactgtccccaaagaagtaggacccactaatgcagatcctgga
tccctagctaagatgtattattctgctgtgaattcgatcccactaaagat""".replace('\n','')
|
| Codon Count Example [from Schuerer tutorial] |
>>> cds = "atgagtgaacgtctgagcattaccccgctggggccgtatatcggcgcacaataa"
>>> def count_codons(cds):
... usage = {}
... for i in range(0,len(cds),3):
... codon = cds[i:i+3]
... if usage.has_key(codon):
... usage[codon] += 1
... else:
... usage[codon] = 1
... return usage
...
>>> count_codons(cds)
{'acc': 1, 'atg': 1, 'atc': 1, 'gca': 1, 'agc': 1, 'ggg': 1, 'att': 1, 'ctg': 2,
'taa': 1, 'ggc': 1, 'tat': 1, 'ccg': 2, 'agt': 1, 'caa': 1, 'cgt': 1, 'gaa': 1}
|
| Codon Translation Example [from Schuerer tutorial] |
>>> code = {
... 'ttt': 'F', 'tct': 'S', 'tat': 'Y', 'tgt': 'C',
... 'ttc': 'F', 'tcc': 'S', 'tac': 'Y', 'tgc': 'C',
... 'tta': 'L', 'tca': 'S', 'taa': '*', 'tga': '*',
... 'ttg': 'L', 'tcg': 'S', 'tag': '*', 'tgg': 'W',
... 'ctt': 'L', 'cct': 'P', 'cat': 'H', 'cgt': 'R',
... 'ctc': 'L', 'ccc': 'P', 'cac': 'H', 'cgc': 'R',
... 'cta': 'L', 'cca': 'P', 'caa': 'Q', 'cga': 'R',
... 'ctg': 'L', 'ccg': 'P', 'cag': 'Q', 'cgg': 'R',
... 'att': 'I', 'act': 'T', 'aat': 'N', 'agt': 'S',
... 'atc': 'I', 'acc': 'T', 'aac': 'N', 'agc': 'S',
... 'ata': 'I', 'aca': 'T', 'aaa': 'K', 'aga': 'R',
... 'atg': 'M', 'acg': 'T', 'aag': 'K', 'agg': 'R',
... 'gtt': 'V', 'gct': 'A', 'gat': 'D', 'ggt': 'G',
... 'gtc': 'V', 'gcc': 'A', 'gac': 'D', 'ggc': 'G',
... 'gta': 'V', 'gca': 'A', 'gaa': 'E', 'gga': 'G',
... 'gtg': 'V', 'gcg': 'A', 'gag': 'E', 'ggg': 'G'
... }
>>> def rectranslate(cds, code):
... if cds == "":
... return ""
... else:
... codon = cds[:3]
... return code[codon] + rectranslate(cds[3:], code)
...
>>> cds = "atgagtgaacgtctgagcattaccccgctggggccgtatatcggcgcacaataa"
>>> rectranslate(cds,code)
MSERLSITPLGPYIGAQ*
|
| Biopython Example |
| Sorting a Sequence by Length from a FASTA File Using Biopython [from Biopython tutorial] |
# Suppose you wanted to sort a sequence file by length (e.g., a set
# of contigs from an assembly), and you are working with a file format
# like FASTA or FASTQ which Bio.SeqIO can read, write (and index).
#
# If the file is small enough, you can load it all into memory at
# once as a list of SeqRecord objects, sort the list, and save it:
from Bio import SeqIO
records = list(SeqIO.parse("ls_orchid.fasta","fasta"))
records.sort(cmp=lambda x,y: cmp(len(x),len(y)))
SeqIO.write(records, "sorted_orchids.fasta", "fasta")
|
| Molecular Modeling Took Kit Example |
| Molecular Dynamics Example from MMTK [MD integrator example] |
# A Velocity-Verlet integrator implemented in Python.
# Use this as a starting point for modified integrators.
#
from MMTK import *
from MMTK.Proteins import Protein
from MMTK.ForceFields import Amber99ForceField
from MMTK.Trajectory import Trajectory, TrajectoryOutput, SnapshotGenerator
# Velocity Verlet integrator in Python
def doVelocityVerletSteps(delta_t, nsteps,
equilibration_temperature = None,
equilibration_frequency = 1):
configuration = universe.configuration()
velocities = universe.velocities()
gradients = ParticleVector(universe)
inv_masses = 1./universe.masses()
evaluator = universe.energyEvaluator()
energy, gradients = evaluator(gradients)
dv = -0.5*delta_t*gradients*inv_masses
time = 0.
snapshot(data={'time': time,
'potential_energy': energy})
for step in range(nsteps):
velocities += dv
configuration += delta_t*velocities
universe.setConfiguration(configuration)
energy, gradients = evaluator(gradients)
dv = -0.5*delta_t*gradients*inv_masses
velocities += dv
universe.setVelocities(velocities)
time += delta_t
snapshot(data={'time': time,
'potential_energy': energy})
if equilibration_temperature is not None \
and step % equilibration_frequency == 0:
universe.scaleVelocitiesToTemperature(equilibration_temperature)
# Define system
universe = InfiniteUniverse(Amber99ForceField())
universe.protein = Protein('bala1')
# Create trajectory and snapshot generator
trajectory = Trajectory(universe, "md_trajectory.nc", "w",
"Generated by a Python integrator")
snapshot = SnapshotGenerator(universe,
actions = [TrajectoryOutput(trajectory,
["all"], 0, None, 1)])
# Initialize velocities
universe.initializeVelocitiesToTemperature(50.*Units.K)
# Heat and equilibrate
for temperature in [50., 100., 200., 300.]:
doVelocityVerletSteps(delta_t = 1.*Units.fs, nsteps = 500,
equilibration_temperature = temperature*Units.K,
equilibration_frequency = 1)
doVelocityVerletSteps(delta_t = 1.*Units.fs, nsteps = 500,
equilibration_temperature = 300*Units.K,
equilibration_frequency = 10)
# Production run
doVelocityVerletSteps(delta_t = 1.*Units.fs, nsteps = 5000)
trajectory.close()
|
| Scientific Computation Examples |
| Math Module Example |
>>> import math >>> math.exp(2) 7.3890560989306504 |
| Integer and Floating-Point Example |
>>> a = 21 >>> a/7 3 >>> a/8 2 >>> a/8. 2.625 |
| Imaginary Number Example |
>>> z = 2+3j >>> z (2+3j) >>> z.real 2.0 >>> z.imag 3.0 >>> z.conjugate() (2-3j) |
| Large Integer Example |
>>> a = 2**100 >>> a 1267650600228229401496703205376L >>> a / 5 253530120045645880299340641075L |
| Factoring Example [from Carlson] |
>>> def factor3(n): ... d = 2 ... factors = [ ] ... while n % d == 0: ... factors.append(d) ... n = n/d ... d = 3 ... while n > 1 and d*d <= n: ... if n % d == 0: ... factors.append(d) ... n = n/d ... else: ... d = d + 2 ... if n > 1: ... factors.append(n) ... return factors ... >>> factor3(99) [3, 3, 11] >>> factor3(100) [2, 2, 5, 5] >>> factor3(1234) [2, 617] >>> a = 2**10 >>> factor3(a) [2, 2, 2, 2, 2, 2, 2, 2, 2, 2] >>> factor3(a+1) [5, 5, 41] |
| Floating Point Numbers Example |
>>> 0.1 0.10000000000000001 >>> step = 0.1 >>> sum = 0.0 >>> for i in range(10): ... sum += step ... >>> sum 0.99999999999999989 >>> 1.0 - sum 1.1102230246251565e-16 |
| NumPy Example |
| NumPy Examples [from NumPy Example List] |
>>> from numpy import *
>>> x = array([[1,2,3],[4,5,6]])
>>> x.shape
(2, 3)
>>> y = array([[1,2],[3,4],[5,6]])
>>> y.shape
(3, 2)
>>> dot(x,y) # matrix multiplication (2,3) x (3,2) -> (2,2)
array([[22, 28],
[49, 64]])
>>> x = array([1,2,3,4,5])
>>> y = array([6, 11, 18, 27, 38])
>>> polyfit(x,y,2) # fit a 2nd degree polynomial to the data, result is x**2 + 2x + 3
array([ 1., 2., 3.])
>>> polyfit(x,y,1) # fit a 1st degree polynomial (straight line), result is 8x-4
array([ 8., -4.])
>>> a = arange(30)
>>> a = a.reshape(2,3,5)
>>> a
array([[[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]],
[[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24],
[25, 26, 27, 28, 29]]])
>>> b = a.transpose()
>>> b
array([[[ 0, 15],
[ 5, 20],
[10, 25]],
[[ 1, 16],
[ 6, 21],
[11, 26]],
[[ 2, 17],
[ 7, 22],
[12, 27]],
[[ 3, 18],
[ 8, 23],
[13, 28]],
[[ 4, 19],
[ 9, 24],
[14, 29]]])
>>> b.shape
(5, 3, 2)
>>> b = a.transpose(1,0,2) # First axis 1, then axis 0, then axis 2
>>> b
array([[[ 0, 1, 2, 3, 4],
[15, 16, 17, 18, 19]],
[[ 5, 6, 7, 8, 9],
[20, 21, 22, 23, 24]],
[[10, 11, 12, 13, 14],
[25, 26, 27, 28, 29]]])
>>> b.shape
(3, 2, 5)
|
| SciPy Example |
| SciPy Examples [from SciPy Example List and John Cook's Getting started with the SciPy library] |
>>> from scipy import * >>> value, err = integrate.quad(func=pow, a=0., b=1., args=(5,)) >>> value # integral of x^5 over [0,1] 0.16666666666666669 >>> integrate.quad(lambda x: math.exp(-x*x), -inf, inf) (1.7724538509055159, 1.4202636780944923e-008) |
| Text Processing Examples |
| Word Index Example [adapted from Alex Martelli] |
[cmd-line-prompt] cat index.py
# build a word -> line numbers mapping from an input file
from re import compile,sub
from sys import stdin
from string import lower
# punctuation marks you want to match (? and . are escaped)
regex_punct = compile(r"[,:\?\.]")
# create a dictionary in which each key is a word and each
# value is a list of line numbers on which that word appears
idx = {}
for n,line in enumerate(stdin):
for word in line.split():
word = regex_punct.sub('',word)
word = word.lower()
if idx.has_key(word): # shortcut to the if-else is to use
idx[word].append(n) # idx.setdefault(word,[]).append(n)
else:
idx[word] = [n]
# display by alphabetically-sorted word
words = idx.keys()
words.sort()
for word in words:
print "%s:" % word,
for n in idx[word]: print n,
print
[cmd-line-prompt] cat input
A man walks into an office.
Man: Ah. I'd like to have an argument, please.
Receptionist: Certainly sir. Have you been here before?
Man: No, this is my first time.
Receptionist: I see. Well, do you want to have the full argument,
or were you thinking of taking a course?
[cmd-line-prompt] python index.py < input
a: 0 9
ah: 2
an: 0 2
argument: 2 8
been: 4
before: 4
certainly: 4
course: 9
do: 8
first: 6
full: 8
have: 2 4 8
here: 4
i: 8
i'd: 2
into: 0
is: 6
like: 2
man: 0 2 6
my: 6
no: 6
of: 9
office: 0
or: 9
please: 2
receptionist: 4 8
see: 8
sir: 4
taking: 9
the: 8
thinking: 9
this: 6
time: 6
to: 2 8
walks: 0
want: 8
well: 8
were: 9
you: 4 8 9
|
| Input Line Tokenizer Example |
[cmd-line-prompt] cat splitter
#!/usr/bin/env python
# command to split input lines based on multiple separators:
# one or more spaces, comma, open parenthesis, close parenthesis
#
# will read from stdin unless file name specified on command line
#
# command line options are -r for reserve and -s for sorted;
# they can be combined for reverse sorted order
import re,sys
# default values
options = ''
file = sys.stdin
# process command line arguments
args = sys.argv
args.pop(0) # removes initial command name
while len(args) > 0:
# get next argument
argument = args.pop(0)
# an option argument has a leading dash
if argument[0] == '-':
if argument[1] == 'r':
options = options + 'r'
elif argument[1] == 's':
options = options + 's'
else:
print 'unrecognized option:',argument
# otherwise treat argument as file name
else:
file = open(argument,'r')
# process input line by line
input_line = file.readline()
while input_line:
contents = re.split(' +|,|\(|\)',input_line.strip())
# remove any empty string members
while contents.count(''):
contents.remove('')
# process line according to options
if 's' in options:
contents.sort()
if 'r' in options:
contents.reverse()
print contents
input_line = file.readline()
file.close()
[cmd-line-prompt] chmod a+x splitter
[cmd-line-prompt] cat input.txt
a,b,(c,d)
spams and eggs
dead (parrot)
[cmd-line-prompt] ./splitter input.txt
['a', 'b', 'c', 'd']
['spams', 'and', 'eggs']
['dead', 'parrot']
[cmd-line-prompt] ./splitter < input.txt
['a', 'b', 'c', 'd']
['spams', 'and', 'eggs']
['dead', 'parrot']
[cmd-line-prompt] ./splitter -s input.txt
['a', 'b', 'c', 'd']
['and', 'eggs', 'spams']
['dead', 'parrot']
[cmd-line-prompt] ./splitter -r -s input.txt
['d', 'c', 'b', 'a']
['spams', 'eggs', 'and']
['parrot', 'dead']
|
| BeautifulSoup HTML Parsing Example |
| HTML Pretty-Print Example Using BeautifulSoup |
>>> from BeautifulSoup import BeautifulSoup
>>> import re
>>> input = '''<html>
... <head><title>Page title</title></head>
... <body>
... <p id="firstpara" align="center">This is paragraph <b>one</b>.
... <p id="secondpara" align="blah">This is paragraph <b>two</b>.
... </html>'''
>>> soup = BeautifulSoup(input)
>>> pretty_print = soup.prettify()
>>> print pretty_print
<html>
<head>
<title>Page title
</title>
</head>
<body>
<p id="firstpara" align="center">This is paragraph
<b>one
</b>.
</p>
<p id="secondpara" align="blah">This is paragraph
<b>two
</b>.
</p>
</body>
</html>
|
| Natural Language Tool Kit Example |
| Tokenizer Example [from NLTK HowTo] |
>>> from nltk import word_tokenize, wordpunct_tokenize
>>> s = ("Good muffins cost $3.88\nin New York. Please buy me\n"
... "two of them.\n\nThanks.")
>>> word_tokenize(s)
['Good', 'muffins', 'cost', '$', '3.88', 'in', 'New', 'York.',
'Please', 'buy', 'me', 'two', 'of', 'them', '.', 'Thanks', '.']
>>> wordpunct_tokenize(s)
['Good', 'muffins', 'cost', '$', '3', '.', '88', 'in', 'New', 'York', '.',
'Please', 'buy', 'me', 'two', 'of', 'them', '.', 'Thanks', '.']
|
| Resources |