Python Deep Dive
A concise tutorial to a concise language
Prakat Solutions
9th July 2016
Contents
- Classes and objects (revisit)
- Tuples
- Modules & Packages
- Regular Expressions
- Exception Handling
- Automated testing
- Simple website setup
- Generators
- A simple scripting project
Classes and Objects
- Objects are an encapsulation of variables and functions
- Class is a template for an object
- Defining a class
- Creating objects
- Accessing object variables and functions
class MyClass(object):
variable = "blah"
def function(self):
print "This is a message inside the class."
myobjectx = MyClass()
myobjecty = MyClass()
myobjecty.variable = "yackity"
print myobjectx.variable # This would print "blah".
print myobjecty.variable # This would print "yackity".
myobjectx.function()
print type(myobjectx)
Exercise
In this exercise, we are going to create a banking system.
Create a class called Account that represents a customer account. It must contain the following variables and functions:
- Account Type- savings, personal or corporate
- User name- A string
- Account number- A number that uniquely identifies each account. This must be auto-generated
- Current balance- a floating point value that represents the current balance
- displayBal()- A function that displays the current balance
- DepositCash(amt) - A function that adds ‘amt’ to the current balance and updates it. In the case of savings accounts, the current balance cannot fall below 500
- DrawCash(amt) - A function that carries out cash withdrawals of ‘amt’ from the current balance. The funds must first be checked and then updated.
Create two objects of type Account:
-
‘Acct1’ is a savings account that belongs to “Mishra” and has a current balance of Rs. 10000.00. He makes a deposit of Rs. 1500.00 into it, followed by a withdrawal of Rs. 7000.
-
‘Acct2’ is a personal account that belongs to “Chopra” and has a current balance of Rs. 5000. He makes 2 withdrawals of Rs. 4000 and Rs. 2000 each.
Tuples
- Like lists, but cannot be changed
- Tuples are immutable lists
- Used when data integrity is essential
myList = [1,2,3]
myList.append(4)
print (myList)
myTuple = (1,2,3)
print (myTuple)
myTuple.append(4)
#Generates an error!
print (myTuple)
Modules
- A module is a .py file containing Python definitions and statements
- Importing modules
- Writing your own modules
# Fibonacci numbers module
# write Fibonacci series up to n
def fib(n):
a, b = 0, 1
while b < n:
print b,
a, b = b, a+b
# return Fibonacci series up to n
def fib2(n):
result = []
a, b = 0, 1
while b < n:
result.append(b)
a, b = b, a+b
return result
>>> import fibo
>>> fibo.fib(1000)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
>>> fibo.fib2(100)
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
>>> fibo.__name__
'fibo'
Packages
- Packages contain multiple packages and modules themselves
- For ex, the module name A.B designates a submodule named B in a package named A
- Like directories, but with a twist
#Package foo
#Defines packages & modules in foo
__init__.py:
#a list of packages and modules
__all__ = ["bar"]
>>> import foo.bar
>>> from foo import bar
Exception Handling
- Exceptions are run-time errors that halt program execution
- try-except-else block
- multiple arguments to except
- except without type
- finally block
import sys
try:
numerator = int(input("Enter the numerator: "))
denominator = int(input("Enter the denominator: "))
ans = numerator/denominator
except ZeroDivisionError: #when division by zero occurs
print "answer is undefined"
print "Oops!",sys.exc_info()[0],"occured :("
print "Please try again!"
except ValueError: #when incorrect value is entered
print "Incorrect value entered"
print "Oops!",sys.exc_info()[0],"occured :("
print "Please try again!"
except: #Handles all other exceptions
print "Oops!",sys.exc_info()[0],"occured :("
print "Please try again!"
else:
ans= numerator/denominator
print ans
finally:
print "This is in the finally block"
Exercise
You need to iterate over the list of 20 numbers, but the list is made from user input, and might not have 20 numbers in it.
Ask the user to input a list of integers. Iterate over a list of 20 numbers and display it. Check for Index errors(index value out of range) and any other errors encountered during user input.
In the case of index errors, make the rest of the numbers as a 0 for display.
In the case of any other errors, print “Exception has been handled”
RE- Raw strings
- The python parser interprets ‘\’ (backslashes) as escape characters in string literals.
- Raw strings signal Python not to interpret backslashes and meta-characters in the string
- begin with a special prefix (r)
string = 'This is a\nnormal string'
rawString = r'and this is a\nraw string'
print string
#This is a
#normal string
print rawString
#and this is a\nraw string
RE- functions
- re package
- re.match()- matches beginning
- re.search()- matches anywhere
- re.findall()- all matching objects
- re.search(pattern, input_str, flags=0)
import re
match = re.match(r'dog', 'dog cat dog')
print match.group(0)
#dog
match = re.match(r'cat', 'dog cat dog')
print match.group(0)
#NoneType object error
match = re.search(r'cat', 'dog cat dog')
print match.group(0)
#cat
re.findall(r'dog', 'dog cat dog')
#['dog', 'dog']
re.findall(r'cat', 'dog cat dog')
#['cat']
RE- match objects
- search() and match() return match objects
- match.group(0)- stores group members
- match.start()
- match.end()
import re
match = re.match(r'dog', 'dog cat dog')
print match.group(0)
#dog
print match.start()
#0
print match.end()
#3
RE- metacharacters
- ^ -beginning of a string.
- $ - end of a string.
- (x|y|z) -x, y or z.
- x? -zero or one times
- x* - zero or more times.
- x+- x one or more times.
- x{m,n} - m-n times.
- (x) -remembered group.
import re
regex = r"([0-9]{1,3}\.){3}[0-9]{1,3}"
IP = raw_input ("Please, enter your IP address: ")
if re.match(regex, IP):
match = re.search(regex, IP)
print match.group(0)
else:
print "Invalid IP address!"
RE- metacharacters
- \b - word boundary.
- \d - numeric digit.
- \D -non-numeric character.
- \w alphanumeric character
- \W non-alphanumeric character
- \s - whitespace
- \S - not whitespace
import re
# Lets use a regular expression to match a date string.
regex = r"(\w+) (\d+)"
if re.search(regex, "June 24"):
match = re.search(regex, "June 24")
# This will print [0, 7), since it matches at the beginning and end of the string
print "Match at index %s, %s" % (match.start(), match.end())
# So this will print "June 24"
print "Full match: %s" % (match.group(0))
# So this will print "June"
print "Month: %s" % (match.group(1))
# So this will print "24"
print "Day: %s" % (match.group(2))
else:
# If re.search() does not match, then None is returned
print "The regex pattern does not match. :("
Exercise
Write a program that inputs an email ID from a user and checks if it is a valid one using regular expressions
Also, extract the domain name of each matched string
Automated Testing
Mike Cohn's test automation pyramid
Cost
Quality
Time
So, where does Python fit in?
unittest
Code Introspection
help()
dir()
hasattr()
id()
type()
repr()
callable()
issubclass()
isinstance()
__doc__
__name__
>>> help(len)
Help on built-in function len in module __builtin__:
len(...)
len(object) -> integer
Return the number of items of a sequence or collection.
>>> dir()
>>> x = 10
>>> id(x)
>>> callable(x)
False
>>> callable(len)
True
>>>
Generators
- Used to create iterators
- Return an iterable set of items, one at a time, in a special way
- Returns control to for loop after yield statement
import random
def lottery():
# returns 6 numbers between 1 and 40
for i in range(6):
yield random.randint(1, 40)
lottery_list = lottery()
for random_number in lottery_list:
print "And the next number is... %d!" % random_number
Simple website setup
- HTML
- CSS
- DOM
- Hosting
- Create a basic index.html page
Setup web framework
-
Install lpth.web
- Simple hello world project
- Directory structure
-
goweb/
├── bin
├── docs
├── goweb
│ └── __init__.py
├── templates
└── tests
└── __init__.py
-
Simple hello world project
import web
urls = (
'/', 'index'
)
app = web.application(urls, globals())
class index:
def GET(self):
greeting = "Hello World"
return greeting
if __name__ == "__main__":
app.run()
Run the app
- Configure routes in bin/app.py
- Run the app
[abhiram@localhost goweb]$ python bin/app.py
http://0.0.0.0:8080/
127.0.0.1:58504 - - [08/Jul/2016 20:25:49] "HTTP/1.1 GET /" - 200 OK
127.0.0.1:58504 - - [08/Jul/2016 20:25:49] "HTTP/1.1 GET /favicon.ico" - 404 Not Found
- Fixing errors by removing the greeting line
- Creating simple templates - index.html
Create basic templates
- Create templates/index.html
$def with (greeting)
<html>
<head>
<title>Python at Prakat</title>
</head>
<body>
$if greeting:
I just wanted to say <em style="color: green; font-size: 2em;">$greeting</em>.
$else:
<em>Hello</em>, world!
</body>
</html>
Create basic templates
- Configure routes in bin/app.py
- Run the app
import web
urls = (
'/', 'index'
)
app = web.application(urls, globals())
render = web.template.render('templates/')
class index:
def GET(self):
greeting = "Hello World"
return render.index(greeting= greeting)
if __name__ == "__main__":
app.run()
A small scripting project
>>> python guess_number.py
Hello! What is your name?
Abhiram
Well, Abhiram, I am thinking of a number between 1 and 20.
Take a guess.
10
Your guess is too low.
Take a guess.
19
Your guess is too high.
Take a guess.
18
Good job, Abhiram! You guessed my number in 3 guesses!
Write a program able to play the "Guess the number"-game, where the number to be guessed is randomly chosen between 1 and 20. This is how it should work when run in a terminal:
Another python script
The International Civil Aviation Organization (ICAO) alphabet assigns code words to the letters of the English alphabet acrophonically (Alfa for A, Bravo for B, etc.) so that critical combinations of letters (and numbers) can be pronounced and understood by those who transmit and receive voice messages by radio or telephone regardless of their native language, especially when the safety of navigation or persons is essential. Here is a Python dictionary covering one version of the ICAO alphabet:
d = {'a':'alfa', 'b':'bravo', 'c':'charlie', 'd':'delta', 'e':'echo', 'f':'foxtrot',
'g':'golf', 'h':'hotel', 'i':'india', 'j':'juliett', 'k':'kilo', 'l':'lima',
'm':'mike', 'n':'november', 'o':'oscar', 'p':'papa', 'q':'quebec', 'r':'romeo',
's':'sierra', 't':'tango', 'u':'uniform', 'v':'victor', 'w':'whiskey',
'x':'x-ray', 'y':'yankee', 'z':'zulu'}
Another python script
Your task in this exercise is to write a procedure speak_ICAO() able to translate any text (i.e. any string) into spoken ICAO words. You need to import at least two libraries: os and time. On Linux, you have access to the system TTS (Text-To-Speech) as follows: os.system('say ' + msg), where msg is the string to be spoken. (pyttsx)(Under Windows and Mac, something similar might exist.) Apart from the text to be spoken, your procedure also needs to accept two additional parameters: a float indicating the length of the pause between each spoken ICAO word, and a float indicating the length of the pause between each word spoken.
Thank You!
Questions?
Python Deep Dive
By Abhiram Ravikumar
Python Deep Dive
Second lecture in the python workshop series
- 5,004