CS390-PYTHON
CS390-PYTHON
© Gustavo Rodriguez-Rivera 1
General Information
• Web Page:
http://www.cs.purdue.edu/homes/cs390lang/python
• Office: LWSN1185
• E-mail: [email protected]
• Textbook:
– Python in a Nutshell 2nd Edition, Alex Martelli,
O'REILLY
© Gustavo Rodriguez-Rivera 2
Sending your Questions
• E-mail questions to
[email protected]
• TAs office hours will be posted in the web page.
© Gustavo Rodriguez-Rivera 3
Grading
• Grade allocation
– Final Project: 100%
© Gustavo Rodriguez-Rivera 4
Syllabus
• Installing Python
• The Python Interpreter
• The Python Language
• Exceptions
• Modules
• Core Built-ins
• Strings and Regular Expressions
• File and Text Operations
• Persistence and Databases
• CGI-Scripting
Introduction to Python
• Python is a general purpose Language
• Created by Guido van Rossum in 1990
• It is High-Level, Dynamic, Object-Oriented
and Multiplatform.
• It is one of the few scripting languages that
has been used successfully in large
projects.
• It offers flexibility, elegance and, power.
Python Advantages
• Python programs are more compact than
in other languages because:
– High Level Data Types allow complex
operations in a single statement.
– Instead of using { } python uses indentation.
– No variables or argument declarations are
necessary.
Python Standard Library
• Python also offers a Standard Library that
contains useful modules for almost every
need.
– GUI
– Databases
– XML
– Numerical Analysis
Installing Python
• The Python website is:
http://www.python.org
• Go to Download->Releases->3.2 and
select your platform.
• For Linux download the tar ball.
• A great tutorial about python is in:
– http://docs.python.org/tutorial/
– Many of the slides are based on this tutorial.
Python Interpreter
• Python takes as input a text file written in python
language, compiles it and runs it.
• The executable is called “python” or
“python.exe”.
• Also you can run an interactive session using
the python shell with no text file.
• If you installed in Windows you can run the
python interpreter:
– Start->Python->Python (command line) or
– Start->Python-> IDLE (Python GUI)
Environment Variables
• The execution of python is affected by the
variables:
– PYTHONHOME
• The python installation directory
– PYTHONPATH
• A list of directories separated by “:” in UNIX or “;” in
Windows. Modules are imported from these directories.
– PYTHONSTARTUP
• The name of a python source file that is executed each time
an interactive session starts.
Running python
• python {options} file {arguments}
• Also in UNIX you can write a executable
script that starts with the line:
#!/usr/bin/python
Usage:
command xxx yyyy
prints command
>>>
String Constants
• Two string constants are concatenated if they appear
one after the other
>>> s = "Hello" "World"
>>> print(s)
HelloWorld
>>>
• To concatenate two strings even if they are not
constants use the “+” operator.
>>> s = "Hi "
>>> t= "How are you"
>>> s + t
'Hi How are you'
>>>
String Indexing and Splicing
• Strings can be indexed like in C
>>> h="Hello world"
>>> h[0]
'H'
>>> h[5]
''
>>> h[6]
'w‘
• Also strings can be sliced.
>>> h[2:5]
'llo'
>>>
• The notation a[i:j] indicates the string characters i to j-1
• If i is missing, it is assume 0. If j is missing, it is assumed the end of
the string.
Strings are Immutable
• Strings cannot be modified like in C.
>>> h="Hello world"
>>> h[2]='g'
Traceback (most recent call last):
File "<pyshell#31>", line 1, in <module>
h[2]='g'
TypeError: 'str' object does not support item
assignment
>>>
String Assignment
• Instead we can create a new string.
>>> h=h[:2] + "g" + h[3:]
>>> print(h)
Heglo world
>>>
Lists
• Python implements several compound types. The most
useful is the “List”
• Lists are denoted with []. Example
>>> a=["Hi", "how", "are", "you",9]
>>> a
['Hi', 'how', 'are', 'you', 9]
• Like strings, lists can be sliced, concatenated etc.
>>> a[2]
'are'
>>>
>>> a[1:3]
['how', 'are']
>>>
Lists
• Unlike strings, individual elements can be modified.
>>> a=["Hi", "how", "are", "you",9]
>>> a
['Hi', 'how', 'are', 'you', 9]
>>> a[2]="ARE"
>>> a
['Hi', 'how', 'ARE', 'you', 9]
>>>
• Also elements can be removed
>>> a
['Hi', 'how', 'ARE', 'you', 9]
>>> a[1:3]=[]
>>> a
['Hi', 'you', 9]
>>>
Lists
• Or can be added anywhere in the list.
>>> a=["Hi", "how", "are", "you",9]
>>> a
['Hi', 'how', 'are', 'you', 9]
>>> a[1:1]=["a", "b", "c"]
>>> a
['Hi', 'a', 'b', 'c', 'how', 'are', 'you', 9]
• To get the length of a list use len().
>>> len(a)
8
Indexing with negative indexes
• If a negative index is used to index lists and strings, the index will be
relative to the end of the list.
>>> a
['Hi', 'how', 'ARE', 'you', 9]
>>> a[0]
'Hi'
>>> a[4]
9
>>> a[-1]
9
>>> a[-2]
'you'
>>>
while statements
• The following program executes factorial:
>>> n=6
>>> result=1
>>> i=1
>>> while i <= n:
result = result * i
i=i+1
>>> i
7
>>> result
720
• Notice that the block statement is indented.
• Also notice the syntax for while statement.
• As in C, the boolean expression can be a number where
0 is false and different than 0 is true.
if statements
if x < 0:
print(“x is negative”)
elif x >0:
print(“x is positive”)
else:
print(“x is 0”);
0
1
2
3
4
>>>
Using break, continue and else
• The break statement exits from the inner for or while loop.
• The continue statement goes to the next iteration of the loop.
• The else branch in a for statement is exectued if no break was executed.
for n in range(2, 10): # Taken from the python tutorial
for x in range(2, n):
if n % x == 0:
print (n, 'equals', x, '*', n/x)
break
else:
# loop fell through without finding a factor
print (n, 'is a prime number')
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3
Pass statement
• The pass statement does nothing. It can be used
for busy waiting.
while True:
pass
>>> print(f(1))
[1]
>>> print(f(2))
[1, 2]
>>> print(f(3))
[1, 2, 3]
>>>
• If you do not wan this behavior default to None (no argument is passed) and initialize inside the
function.
>>> def f(val, list=None):
if list is None:
list = []
list.append(val)
return list
Variable Number of Arguments
• When the last arguments in a function are *args and **keys, the
arguments without keyword will be passed in *args and the
arguments with keywords will be passed in **keys.
>>>
def elements(a, b, *args, **keys):
print("Normal args a=",a," b=",b)
print( "Arguments without keys:")
for arg in args:
print(arg)
print("Keywords:")
for key in keys:
print("key=",key," val=",keys[key])
Variable Number of Arguments
>>> elements(1, 2, 3,"Hi", "everybody", color="green",
day="Friday")
Normal args a= 1 b= 2
Arguments without keys:
3
Hi
everybody
Keywords:
key= color val= green
key= day val= Friday
>>>
Arguments as Lists
• Also when we have arguments in the form of a list, it is
possible to make a call by passing the list as
*list_of_args
>>> list=[1,2,3]
>>> foo(*list)
a= 1 b= 2 c= 3
>>>
Coding Style
• Use 4 space indentation and no tabs.
• Wrap up line so they do not exceed 79
chars.
• Use blank lines to separate functions.
• Use CamelCase for classes and
lower_case_with_undersore for functions
and methods.
List Functions
• list.append(x)
– Add item at the end of the list.
– Similar to a[len(a):]=[x]
• list.extend(L)
– Add list at the end
– Siilar to a[len(a):]=L
• list.insert(i,x)
– Insert item at a given position.
– Similar to a[i:i]=[x]
List Functions
• list.remove(x)
– Removes first item from the list with value x
• list.pop(i)
– Remove item at position I and return it. If no index I is
given then remove the first item in the list.
• list.index(x)
– Return the index in the list of the first item with value
x.
• list.count(x)
– Return the number of time x appears in the list
List Functions
• list.sort()
– Sorts items in the list in ascending order
• list.reverse()
– Reverses items in the list.
Using Lists as Stacks
• You can use a list as a stack
>>> a = ["a", "b", "c“,”d”]
>>> a
['a', 'b', 'c', 'd']
>>> a.append("e")
>>> a
['a', 'b', 'c', 'd', 'e']
>>> a.pop()
'e'
>>> a.pop()
'd'
>>> a = ["a", "b", "c"]
>>>
Lists as Queues
• You can use a list as a queue but it is inefficient.
• For that you use queues
>>> from collections import deque
>>> queue = deque(["a","b","c"])
>>> queue
deque(['a', 'b', 'c'])
>>> queue.append("d")
>>> queue
deque(['a', 'b', 'c', 'd'])
>>> queue.append("e")
>>> queue.popleft()
'a'
>>> queue.popleft()
'b'
>>> queue
deque(['c', 'd', 'e'])
>>>
Multidimensional Arrays
• You can define a multidimensional array using
lists
>>>
mat = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, 11, 12]
]
Multidimensional Arrays
• To iterate over the matrix
>>> for j in range(4):
for i in range(3):
print(mat[j][i])
1
2
3
4
5
6
7
8
9
10
11
12
>>>
The del statement
• Del can be used to remove an element of a list
a=[1,2,3,4]
print(a)
del a[0]
print(a)
[2,3,4]
del a[1:3]
Print(a)
[2]
tuples
• A tuple is another data structure in python
• A tuple consists of a number of values
separated by comas
• A tuple is immutable.
>>> t=(1,2,3,4,5)
>>> print(t)
(1, 2, 3, 4, 5)
>>>
Sets
• A set is another python data structure that
is an unordered collection with no
duplicates.
>>> setA=set(["a","b","c","d"])
>>> setB=set(["c","d","e","f"])
>>> "a" in setA
True
>>> "a" in setB
False
Sets
>>> setA - setB
{'a', 'b'}
>>> setA | setB
{'a', 'c', 'b', 'e', 'd', 'f'}
>>> setA & setB
{'c', 'd'}
>>> setA ^ setB
{'a', 'b', 'e', 'f'}
>>>
Dictionaries
• A dictionary is a data structure that associates a
key with a value.
>>> address={"John":"Oakwood 345",
"Peter":"Evergreen 546", "Mary": "Kingston 564"}
>>> print(address["Mary"])
Kingston 564
>>> print(address)
{'John': 'Oakwood 345', 'Mary': 'Kingston 564',
'Peter': 'Evergreen 546'}
Dictionaries
>>> del address["Peter"]
>>> print(address)
{'John': 'Oakwood 345', 'Mary': 'Kingston 564'}
>>> address["Wayne"]="Young 678"
>>> print(address)
{'Wayne': 'Young 678', 'John': 'Oakwood 345', 'Mary':
'Kingston 564'}
>>> print(address.keys())
dict_keys(['Wayne', 'John', 'Mary'])
>>> "John" in address
True
Iterating over a dictionary
>>> for k in address.keys():
print(k,":", address[k])
fact.py:
def factorial(n):
r=1
for i in range(1,n+1):
r = r * i
return r
print(factorial(5))
$ Python fact.py
120
Passing Command Line Argument
if __name__ == "__main__":
import sys
print(factorial(int(sys.argv[1])))
Using Modules as scripts
$ python fact.py 5
120
$ python test1.py
3628800
PYTHONPATH
• The PYTHONPATH environment variable
is a list of the directories separated by “:”
(or “;” in Windows) that contains the
directories with modules to be imported
• In the instruction
from fact import *
The fact module is searched in the directories in
PYTHONPATH
Compiled Python files
• Python compiles files into bytecodes that
are faster to execute.
• The compiled version of a file xyz.py is
compiled into xyz.pyc and stored in the
same directory where xyz is stored.
• Future runs will use the compiled version if
the program has not been modified.
The dir() function
• The dir function is used to determine the functions exported by a
module.
>>> import sys
>>> dir(sys)
['__displayhook__', '__doc__', '__excepthook__', '__name__',
'__package__', '__stderr__', '__stdin__', '__stdout__',
'_clear_type_cache', '_current_frames', '_getframe', '_xoptions',
'api_version', 'argv', 'builtin_module_names', 'byteorder',
'call_tracing', 'callstats', 'copyright', 'displayhook', 'dllhandle',
'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix',
'executable', 'exit', 'flags', 'float_info', 'float_repr_style',
'getcheckinterval', 'getdefaultencoding', 'getfilesystemencoding',
'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof',
'getswitchinterval', 'gettrace', 'getwindowsversion', 'hash_info',
'hexversion', 'int_info', 'intern', 'maxsize', 'maxunicode', 'meta_path',
'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform',
'prefix', 'setcheckinterval', 'setprofile', 'setrecursionlimit',
'setswitchinterval', 'settrace', 'stderr', 'stdin', 'stdout', 'subversion',
'version', 'version_info', 'warnoptions', 'winver']
Packages
• Packages is a way to organize modules in
python.
• The module X.Y for example designates a
module Y inside package X.
• The packages can also include other
packages and they will be stored in
directories and subdirectories that
represent this hierarchy.
Packages
• For example:
sound/ Top-level package
__init__.py Initialize the sound package
formats/ Subpackage for file format conversions
__init__.py
wavread.py
wavwrite.py
aiffread.py
aiffwrite.py
auread.py
auwrite.py
...
effects/ Subpackage for sound effects
__init__.py
echo.py
surround.py
reverse.py
...
Packages
• The __init__.py file contains initialization
code that runs the first time the package is
imported.
• The statement:
import sound.effects.echo
Imports the echo function.
• To run one of the functions:
sound.effects.echo.echofilter()
Packages
• It is easier to use:
from sound.effects.echo import echofilter
And then call
echofilter()
Input/Output
• The easiest way to do formatting is to
format a string before printing.
• The function str(x) translates x to a string
that a human can understand.
• The function repr(x) translates x to a string
that the interpreter can understand.
Formatting
• Here are two ways of printing a table of squares and
cubes.
>>> for x in range(1,5):
print(repr(x).rjust(2),
repr(x*x).rjust(3), repr(x*x*x).rjust(4))
1 1 1
2 4 8
3 9 27
4 16 64
>>>
• rjust(n) right justifies a string by filling with spaces the left
of the string so the resulting string is the size n.
• There is also str.ljust() and str.center().
Formatting
• The second way uses the format function:
1 1 1
2 4 8
3 9 27
4 16 64
>>>
• The substrings {pos:format} indicate that the variable at that position
will be formatted in the way it is indicated.
def fac(n):
r=1
for i in range(1,n+1):
r=r*i
return r
import sys
n = int(sys.argv[1])
print("The factorial of ", n, " is ", fac(n))
>>> f.close()
>>>
The pickle module
• Converting an object to a string representation is
called “pickling” or serializing.
• Converting an object back from a string
representation to an object is called unpickling
or deserializing.
• pickle.dump(x,f)
– Writes object x as a string into file f
• x=pickle.load(f)
– Load x from string representation in file f.
Exceptions
• In python the best way to handle errors is
with exceptions.
• Exceptions separates the main code from
the error handling making the program
easier to read.
try:
statements
except:
error handling
Exceptions
• Example:
import sys
try:
f = open('myfile.txt')
s = f.readline()
i = int(s.strip())
except IOError as (errno, strerror):
print "I/O error({0}): {1}".format(errno, strerror)
except ValueError:
print "Could not convert data to an integer."
except:
print "Unexpected error:", sys.exc_info()[0]
raise
The finally statement
• The finally: statement is executed under all
circumstances even if an exception is
thrown.
• Cleanup statements such as closing files
can be added to a finally stetement.
Finally statement
import sys
try:
f = open('myfile.txt')
s = f.readline()
i = int(s.strip())
except IOError as (errno, strerror):
print "I/O error({0}): {1}".format(errno, strerror)
except ValueError:
print "Could not convert data to an integer."
except:
print "Unexpected error:", sys.exc_info()[0]
finally:
f.close() # Called with or without exceptions.
Predefined cleanup actions
• The with statement will automatically
close the file once it is no longer in use.
with open("myfile.txt") as f:
for line in f:
print line
#!/usr/bin/python
import cgi
import cgitb; cgitb.enable() # for troubleshooting
print """
<html>
<body>
print """
<p>form
</body>
</html>
""" % cgi.escape(message)
Example of string interpolation
• The mod (%) operator in strings is used for
formatting or to insert variables in a string.
# Required header that tells the browser how to render the HTML.
print "Content-Type: text/html\n\n"
age.cgi example
# Define function to generate HTML form.
def generate_form():
str = """
<HTML>
<HEAD>
<TITLE>Info Form</TITLE>
</HEAD>
<BODY BGCOLOR = white>
<H3>Please, enter your name and age.</H3>
<TABLE BORDER = 0>
<FORM METHOD = post ACTION = \"age.cgi\">
<TR><TH>Name:</TH><TD><INPUT type = text name = \"name\"></TD><TR>
<TR><TH>Age:</TH><TD><INPUT type = text name =\"age\"></TD></TR>
</TABLE>
<INPUT TYPE = hidden NAME = \"action\" VALUE = \"display\">
<INPUT TYPE = submit VALUE = \"Enter\">
</FORM>
</BODY>
</HTML>
"""
print(str)
age.cgi example
# Define function display data.
def display_data(name, age):
str="""
<HTML>
<HEAD>
<TITLE>Info Form</TITLE>
</HEAD>
<BODY BGCOLOR = white>
%s you are %s years old.
</BODY>
</HTML>
"""
print(str % (name,age))
age.cgi example
# Define main function.
def main():
form = cgi.FieldStorage()
if (form.has_key("action") and
form.has_key("name") and form.has_key("age")):
if (form["action"].value == "display"):
display_data(form["name"].value,
form["age"].value)
else:
generate_form()
Books Table
Title ISBN Publisher_ID Price
0-201-96426-0 DATE 1
0-201-96426-0 DARW 2
0-19-501919-9 ALEX 1
Publishers Table
Publisher_ID Name URL
./users/[email protected]:
album1 album2
./users/[email protected]/album1:
Frozen_Fruits_Vegetable.jpg paintings-35792675.jpg
vangogh_spblue.jpg baby.jpg
rose_1_bg_030703.jpg vangogh_spdark.jpg
./users/[email protected]/album2:
./users/[email protected]:
album1
./users/[email protected]/album1:
1976_steve_jobs3.jpg steve-jobs-1984-macintosh.jpg
steve_jobs3.jpg
Simple Picture Album Script
#!/usr/bin/python
import cgi
import cgitb; cgitb.enable() # for troubleshooting
import glob
print """
Simple Picture Album Script
<html>
<head><title>Picture Album Script</title></head>
<body>
"""
print """
<table border="0">
"""
Simple Picture Album Script
pics_in_row=0
for pic in pics:
print "<td>"
print '<a href="%s">' % pic
print '<img src="%s" width="100" height="100"><p>' % pic
print '</a>'
print "</td>"
pics_in_row = pics_in_row + 1
if pics_in_row == 5:
print "</tr>"
pics_in_row = 0
Simple Picture Album Script
#Close row if it is not closed
if pics_in_row > 0:
print "</tr>"
print "</table>"
print """
</body>
</html>
"""
Running the Album Script
• Put the script album.cgi in
PictureShare/public
• From a browser connect to:
http://host:port/PictureShare/public/album.cgi
Example:
http://sslab01.cs.purdue.edu:8085/PictureShar
e/public/album.cgi
IMPORTANT: Store information in Data Base
instead of htdocs.
Simple example: login.cgi
#!/usr/bin/python
# Required header that tells the browser how to render the HTML.
print("Content-Type: text/html\n\n")
try:
passwd_file = open("users/"+/homes/PictureShare/user+"/password.txt", 'r')
except:
#No user"
return "failed"
stored_password = passwd_file.readline().strip()
passwd_file.close()
#print "stored_password=\""+stored_password+"\""
if (stored_password==passwd):
return "passed"
else:
return "failed"
login.cgi
def display_admin_options():
html="""
<H1> Picture Share Admin Options</H1>
<ul>
<li> Create new album
<li> Delete album
<li> Make album public
<li> Change pawword
</ul>
"""
print html
login.cgi
# Define main function.
def main():
form = cgi.FieldStorage()
if form.has_key("username") and form.has_key("password"):
#Test password
username=form["username"].value
password=form["password"].value
#print "You typed " + username + " and \"" + password +"\"<p>"
if check_password(username, password)=="passed":
display_admin_options()
else:
login_form()
print "<H3><font color=\"red\">Incorrect user/password</font></H3>"
else:
login_form()
try:
passwd_file = open("users/"+user+"/password.txt", 'r')
except:
#No user"
return "failed"
stored_password = passwd_file.readline().strip()
passwd_file.close()
#print( "stored_password=\""+stored_password+"\"")
if (stored_password==passwd):
return "passed"
else:
return "failed"
Using Sessions
def display_admin_options(user, session):
html="""
<H1> Picture Share Admin Options</H1>
<ul>
<li> <a href="login2.cgi?action=new-album&user={user}&session={session}"> Create new
album</a>
<li> Delete album
<li> Make album public
<li> Change pawword
</ul>
"""
#Also set a session number in a hidden field so the
#cgi can check that the user has been authenticated
print(html.format(user=user,session=session))
def read_session_string(user):
session_file = open("users/"+user+"/session.txt", 'r')
session = session_file.readline().strip()
session_file.close()
return session
Using Sessions
def create_session(user):
n=20
char_set = string.ascii_uppercase + string.digits
session = ''.join(random.sample(char_set,n))
def check_session(form):
print("Checking session")
if "user" in form and "session" in form:
print("User here")
username=form["user"].value
session=form["session"].value
print("user=",username," session=",session)
session_stored=read_session_string(username)
print(" session_stored="+session_stored)
if session_stored==session:
return "passed"
return "failed"
Using Sessions
def new_album(form):
print("New album")
if (check_session(form) != "passed"):
login_form()
print("Wrong session:", sys.exc_info()[0])
return
html = """
<H1>New Album</H1>
<TABLE BORDER = 0>
<FORM METHOD=post ACTION="login2.cgi">
<TR><TH>Album Name:</TH><TD><INPUT TYPE=text NAME="album"></TD><TR>
</TABLE>
<INPUT TYPE=hidden NAME="action" VALUE="new-album-response">
<INPUT TYPE=hidden NAME=“user" VALUE=“{user}">
<INPUT TYPE=hidden NAME=“session" VALUE=“{session}">
<INPUT TYPE=submit VALUE="Enter">
</FORM>
"""
print(html.format(user=user,session=session))
Using Sessions
# Define main function.
def main():
# try:
form = cgi.FieldStorage()
if "action" in form:
action=form["action"].value
print("action=",action)
if action == "login":
if "username" in form and "password" in form:
#Test password
username=form["username"].value
password=form["password"].value
#print("You typed " + username + " and \"" + password +"\"<p>")
if check_password(username, password)=="passed":
session=create_session(username)
display_admin_options(username, session)
else:
login_form()
print("<H3><font color=\"red\">Incorrect user/password</font></H3>")
elif action == "new-album":
print("Here1")
new_album(form)
else:
login_form()
#except:
# login_form()
# print("Unexpected error:", sys.exc_info()[0])
<HTML>
</HTML>
Uploading files
• The corresponding python file upload.cgi
File:action.py
#!/usr/bin/python
import cgi
import sys
def gen_html_header() :
print "Content-Type: text/html\n\n"
print "<HTML>"
def gen_html_trailer() :
print "</HTML>"
gen_html_header()
form = cgi.FieldStorage()
try :
file_contents = form["upfile"].value
print file_contents
except :
print sys.exc_info()
gen_html_trailer()
A More Complex Example to
Upload Files
def upload_pic(form):
if (check_session(form) != "passed"):
login_form()
print("Wrong session:", sys.exc_info()[0])
return
html="""
<HTML>
#Get user
user=form["user"].value
else:
login_form()
#except:
# login_form()
# print("Unexpected error:", sys.exc_info()[0])
import shutil
shutil.copyfile(‘data.db’, ‘archive.db’)
shutil.move('/build/executables', 'installdir')
File wildcards
• The glob module can be used to obtained
the files matched by a wildcard.
import glob
glob.glob("*.py")
['__init__.py', 'manage.py',
'settings.py', 'urls.py']
>>>
Using sqlite3 in Python
• Python includes a simple database implementation
called sqlite3 that provides a database with fast disk
access.
• These presentation is based on the tutorial provided in:
http://docs.python.org/library/sqlite3.html
• To use the database you need to create a connection
object first:
import sqlite3
conn = sqlite3.connect('/tmp/example')
• If you want the database to be created in memory use
“:memory:” instead but your data will not be saved.
Using sqlite3 in Python
• Once you have a connection you can create a Cursor
and use it to execute SQL commands
c = conn.cursor()
# Create table
c.execute('''create table stocks
(date text, trans text, symbol text,
qty real, price real)''')
# Do this instead
t = (symbol,)
c.execute('select * from stocks where symbol=?', t)
# Larger example
for t in [('2006-03-28', 'BUY', 'IBM', 1000, 45.00),
('2006-04-05', 'BUY', 'MSOFT', 1000, 72.00),
('2006-04-06', 'SELL', 'IBM', 500, 53.00),
]:
c.execute('insert into stocks values (?,?,?,?,?)', t)
Using sqlite3 in Python
• To get the data after executing SELECT, you can use the
cursor as an iterator or use fetchall() to get a list of the
matching table entries.
>>> c = conn.cursor()
>>> c.execute('select * from stocks order by price')
>>> for row in c:
... print row
...
(u'2006-01-05', u'BUY', u'RHAT', 100, 35.14)
(u'2006-03-28', u'BUY', u'IBM', 1000, 45.0)
(u'2006-04-06', u'SELL', u'IBM', 500, 53.0)
(u'2006-04-05', u'BUY', u'MSOFT', 1000, 72.0)
>>>
\d
Matches any decimal digit; this is equivalent to the class [0-9].
\D
Matches any non-digit character; this is equivalent to the class [^0-9].
\s
Matches any whitespace character; this is equivalent to the class [ \t\n\r\f\v].
\S
Matches any non-whitespace character; this is equivalent to the class
[^ \t\n\r\f\v].
\w
Matches any alphanumeric character; this is equivalent to the class
[a-zA-Z0-9_].
\W
Matches any non-alphanumeric character; this is equivalent to the class
[^a-zA-Z0-9_].
Repeating Strings with “*” and “+”
• To represent the repetitions of a string you use
the “*” operator.
• “x*” will represent the repetition of “x” 0 or more
times like “x” or “xx” or “”
• For example “ab*c” will match the strings “ac”,
“abc”, “abbbbc”.
• The “+” operator can be used to represent 1 or
more repetitions of a string.
• For example “x+” will represent 1 or more
repetitions of “x”, like “x”, “xx” but not “”.
The “?” and “{}” operators.
• The “?” operators macthes a regular expression
0 or 1 time.
• For example “x?” will match “x” or “”.
• Also, “ab?c” will match “abc or “ac”.
• The “x{m,n}” qualifier represents that x can be
repeated at least m times and at most n times.
• For example x{1, 3} will match “x”, “xx”, “xxx” but
it will not match “” or “xxxx”.
• You can omit m or n that means that m=0 or n is
infinite.
• x{2,} matches “xx”, “xxx”, “xxxx….”
Compiling Regular Expressions
• To execute faster the matching of regular expressions, they have to
be compiled.
• The compilation generates a “Finite State Automaton” (FSM) that is
usually represented by a matrix that allows the execution of the
matching operation in linear time.
>>> p = re.compile('ab*c')
>>> print(p.match("a"))
None
>>> print(p.match("ac"))
<_sre.SRE_Match object at 0x00000000020FA718>
>>> print(p.match("abc"))
<_sre.SRE_Match object at 0x00000000020FA718>
>>> print(p.match("abbc"))
<_sre.SRE_Match object at 0x00000000020FA718>
>>> print(p.match("abd"))
None
>>>
Raw String Notation
• Sometimes the strings you want to match
contain “\” characters.
• Since “\” is a special character, you need to
escape it as in “\\”.
• To avoid the cascade of “\\” characters, the raw
strings or r”…” allow the “\” as a character itself.
• For example:
Regular String Raw string
"ab*“ r"ab*”
“\\\\section” r”\\section”
“\\w+\\s+\\1” r"\w+\s+\1"
Using Regular Expressions
• Once you have compiled regular expressions you can
use them for matching or replacing.
match()
Determine if the RE matches at the beginning of the
string.
search()
Scan through a string, looking for any location where this
RE matches.
findall()
Find all substrings where the RE matches, and returns
them as a list.
finditer()
Find all substrings where the RE matches, and returns
them as an iterator.
Testing matching
• You can use the result of a match as the test in a if or
while statement.
>>> p = re.compile('ab*c')
>>> s="abbbc"
>>> if p.match(s):
... print("Matches!")
...
Matches!
>>>
>>> s="abbbbd"
>>> if p.match(s):
... print("Matches!")
...
>>>
Testing matching
• Also notice that in match() the regular expression has to
match the beginning of the string to succeed.
>>> s="xabbbc"
>>> if p.match(s):
... print("Matches!")
...
>>>
• However, in search() only a substring needs to match to
succeed.
>>>>>> if p.search(s):
... print("Search found!")
...
Search found!
>>>
The MatchObject
• Also, you can assign the result of a match to a MatchObject and use it for
obtaining the matched string.
>>> import re
>>> p = re.compile('[a-z]+')
>>> p
<_sre.SRE_Pattern object at 0x00000000021BC718>
>>> print(p.match(""))
None
>>> m = p.match('tempo')
>>> print(m)
<_sre.SRE_Match object at 0x00000000021BA718>
>>> m.group()
'tempo'
>>> m.start(),m.end()
(0, 5)
>>>
MatchObject Operations
• group()
– Return the string matched by the RE
• start()
– Return the starting position of the match
• end()
– Return the ending position of the match
• span()
– Return a tuple containing the (start, end)
positions of the match
Testing for the MatchObject
p = re.compile( ... )
m = p.match( 'string goes here' )
if m:
print 'Match found: ', m.group()
else:
print 'No match'
Using findall
>>> p = re.compile('\d+')
>>> p.findall('12 drummers drumming, 11
pipers piping, 10 lords a-leaping')
['12', '11', '10']
Iterating over the finds
>>> iterator = p.finditer('12 drummers drumming,
11 ... 10 ...')
>>> iterator
<callable-iterator object at 0x401833ac>
>>> for match in iterator:
... print match.span()
...
(0, 2)
(22, 24)
(29, 31)
Server Sockets
• Python has a powerful Socket library that
is very easy to use.
• To use it import SocketServer
• Then define a class that inherits from
SocketServer.BaseRequestHandler
and overide the method handle.
• Then call the method
SocketServer.TCPServer((HOST, PORT), MyTCPHandler)
where MyTCPHandler is the listener class you implemented.
Server Sockets
import SocketServer
class MyTCPHandler(SocketServer.StreamRequestHandler ):
"""
The RequestHandler class for our server.
def handle(self):
# self.rfile is a file-like object created by the handler;
# we can now use e.g. readline() instead of raw recv() calls
self.data = self.rfile.readline().strip()
print "%s wrote:" % self.client_address[0]
print self.data
# Likewise, self.wfile is a file-like object used to write back
# to the client
self.wfile.write(self.data.upper())
Server Sockets
if __name__ == "__main__":
HOST, PORT = "localhost", 9999
python server.py
# Send the message via our own SMTP server, but don't
include the
# envelope header.
s = smtplib.SMTP("localhost")
s.set_debuglevel(1)
s.sendmail(fromaddr, [toaddr], msg.as_string())
s.quit()
Multiple Threads
• Multiple threads allow running tasks in
parallel with the program.
• To execute a thread use the command
define a class that inherits from
threading.Thread
• Inside the method run() define the body of
the function that will run in the thread.
• Use join to wait until the thread ends.
Threads Example: Unzipping a File
in the Background.
import threading, zipfile
class AsyncZip(threading.Thread):
def __init__(self, infile, outfile):
threading.Thread.__init__(self)
self.infile = infile
self.outfile = outfile
def run(self):
f = zipfile.ZipFile(self.outfile, 'w', zipfile.ZIP_DEFLATED)
f.write(self.infile)
f.close()
print 'Finished background zip of: ', self.infile
import tkinter
or
from tkinter import *
Reference:
http://www.pythonware.com/library/tkinter/introduction/in
dex.htm
Hello World in Tkinter
# File: hello1.py
class App:
frame = Frame(master)
frame.pack()
def say_hi(self):
print "hi there, everyone!"
root = Tk()
app = App(root)
root.mainloop()
Widget Classes
Button
A simple button, used to execute a command or other operation.
Canvas
Structured graphics. This widget can be used to draw graphs and plots, create graphics
editors, and to implement custom widgets.
Checkbutton
Represents a variable that can have two distinct values. Clicking the button toggles between
the values.
Entry
A text entry field.
Frame
A container widget. The frame can have a border and a background, and is used to group
other widgets when creating an application or dialog layout.
Label
Displays a text or an image.
Widget Classes
Listbox
Displays a list of alternatives. The listbox can be configured to get
radiobutton or checklist behavior.
Menu
A menu pane. Used to implement pulldown and popup menus.
Menubutton
A menubutton. Used to implement pulldown menus.
Message
Display a text. Similar to the label widget, but can automatically wrap
text to a given width or aspect ratio.
Widget Classes
Radiobutton
Represents one value of a variable that can have one of many values. Clicking
the button sets the variable to that value, and clears all other radiobuttons
associated with the same variable.
Scale
Allows you to set a numerical value by dragging a "slider".
Scrollbar
Standard scrollbars for use with canvas, entry, listbox, and text widgets.
Text
Formatted text display. Allows you to display and edit text with various styles
and attributes. Also supports embedded images and windows.
Toplevel
A container widget displayed as a separate, top-level window.
Mixins
• Similar to Java Swing Layouts
• Grid Manager
The grid geometry manager allows you to create table-like layouts, by
organizing the widgets in a 2-dimensional grid. To use this geometry
manager, use the grid method.
• Pack Manager
The pack geometry manager lets you create a layout by "packing" the
widgets into a parent widget, by treating them as rectangular blocks
placed in a frame. To use this geometry manager for a widget, use the
pack method on that widget to set things up.
• Place Manager
The place geometry manager lets you explicitly place a widget in a
given position. To use this geometry manager, use the place method.
Creating a small menu
# File: menu1.py
def callback():
print ("called the callback!“)
root = Tk()
# create a menu
menu = Menu(root)
root.config(menu=menu)
filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="New", command=callback)
filemenu.add_command(label="Open...", command=callback)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=callback)
helpmenu = Menu(menu)
menu.add_cascade(label="Help", menu=helpmenu)
helpmenu.add_command(label="About...", command=callback)
mainloop()
Standard Dialogues
try:
fp = open(filename)
except:
tkMessageBox.showwarning(
"Open file",
"Cannot open this file\n(%s)" % filename
)
return
# File: dialog1.py
Dialog Windows
from tkinter import *
class MyDialog:
Label(top, text="Value").pack()
self.e = Entry(top)
self.e.pack(padx=5)
def ok(self):
print ("value is", self.e.get())
self.top.destroy()
root = Tk()
Button(root, text="Hello!").pack()
root.update()
d = MyDialog(root)
root.wait_window(d.top)
Using Canvas
import tkinter
import tkinter.messagebox
top = tkinter.Tk()
C.pack()
top.mainloop()
Another Canvas Example.
# canvas2.py
master = Tk()
mainloop()