Title: Information Infrastructure II
1Information Infrastructure II
2Topics To Be Covered
- Functions
- Writing your own
- The Python library
- Modules
3What is a Function?
- A piece of code that does a specific thing
- Might be as simple as adding 2 numbers
- Might be as complex as processing an HTTP request
- If your code is doing many things, especially if
they are repeated - Make a function out of the repeated tasks
4Anatomy of a Function
Arguments
Return value
Input
Function
Output
Input
Input
- Could be any type of Python object
- So if you return a list object, it means that
you can return multiple items - Document your functions!!!
5Writing a Python Function
- Decide what arguments it will need
- Decide what it will return
- Note we dont declare the type of a variable
- This is why documentation is very important
def myFunction(x) return x2 print
myFunction(4) print myFunction(hello,
world)
6Returning from a Function
- Any time you have a return the function will exit
- You can have more than one return statement
- Too many returns can make it difficult to
understand whats going on
def sign(x) if x lt 0 return -1 if x gt
0 return 1 return 0
7Returning from a Function
- If you dont write a return the function will
return None - But you can explicitly return None if it makes
sense
- If we didnt find a valid name, theres nothing
to return - Returning None lets us know that nothing was found
def validName(x) if x.find(John) gt 0
return Found it return None
8Scope of Variables
- Scope indicates when and whether a variable is
visible
myname Rajarshi def validName(myname)
print The name I got was s (myname) print
myname Rajarshi print validName(John) John
print myname Rajarshi
9Parameter Passing Rules
- When a parameter is sent to a function, the
function receives a copy - But a variable holding a list really holds the
reference to the list - So if we change the list elements inside a
function and the changes will be visible outside
the function
10Parameter Passing Examples
- You have to be careful when handling lists in
functions - Changes made withinthe function will bevisible
outside it - This may not be whatyou want
def myFunction(arg1, arg2) arg1 arg1
extra arg2.append(extra) x hello y
a, b myFunction(x,y) print x, y Hello a,
b, extra
11Laziness Parameter Passing
- We saw that changing a list inside a function,
makes the changes visible outside the function - So we can avoid returning changed list, right?
- WRONG
- Doing this can lead to obscure code
- Its OK in some cases
12So How Should We handle Lists?
- If the list is not going to be modified in a
function, we dont need to do anything - If the list is going to be modified, send in a
copy of the list - Slicing makes this easy, since a slice returns
a new list
def myFunction(arg1, arg2) arg1 arg1
extra arg2.append(extra) x hello y
a, b myFunction(x,y) print
x Hello print y a, b
13Default Parameter Values
- In many cases, we can specify a default value for
an argument - Allows us to avoid specifyingit if we want
thedefault value
def total(x, start 0, end None) if end
is None end len(x) result 0 for I in
range(start, end) result xI
return result x 1,2,3,4,5,6 total(x) 21 tota
l(x, end 2) 3 total(x, start2, end5) 12
14Default Parameter Values
- Actual parameters are passed in are processed
leftto right - All parameterswith defaultsmust come
afterparameterswithout them
def total(x, start 0, end None) if end
is None end len(x) result 0 for I in
range(start, end) result xI
return result x 1,2,3,4,5,6 total(x) 21 tota
l(x, 2) 3 total(x, 2, 5) 12
15A Function is an Object
- Well go into more details of objects later on
- But when we say x 2 or y Hello, x and y are
variables holding objects - 2 is an integer object
- Hello is a string object
- Functions are also objects
- We can store functions in lists
- We can pass a function to another function
- Lets us write very elegant code
16Functions as Objects
- We can assign a function to another variable,
which is then a function
def circumference(radius) return 2 3.14159
radius def func(aFunction, x) return
aFunction(x) circumference(2) 12.56636 circ
circumference circ(2) 12.56636 func(circ,
2) 12.56636 y circ, func ltfunction
circumference at 0x682b0gt, ltfunction func at
0x62530gt
- We can pass a function to another function
- We can store functions in a list
17Using Functions as Objects
- Find the sum and mean of a sequence of numbers
- Easier than callingeach functionseparately
- Why is the mean reported as 2?
def sum(x) ret 0 for I in x ret I
return ret def mean(x) ret 0 for I
in x ret I return ret / len(x) def
summary(numbers, functionList) values
for func in functionList result
func(numbers) values.append(result)
return values x 1,2,3,4 summary(x, sum,
mean) 10,2
18Function Attributes
- All functions have an attribute called __name__
- This is like a variable that is always associated
with a function - Python makes it available you dont need to
create it - But its not really a variable - it cant be
changed - Its value is the name the function was defined
as - In general, variables with 2 underscores are
special and well look at them later on
19Function Attributes
def sum(x) ret 0 for I in x ret I
return ret gtgt sum.__name__ sum gtgt myfunc
sum gtgt myfunc.__name__ sum
- Assigning a function toanother variable doesnot
change its initial name - But dont use the __name__ as a variable in a
function - It doesnt do what you thinkitll do!
def sum(x) print __name__ return x2 gtgt
sum.__name__ sum gtgt sum(2) __main__ 4
20Modules
- Every Python file is a module
- Say we write 2 functions called sum and mean in a
file called func.py - func.py is a module called func
- We can access the functions and variables in
func.py by writing - func.sum
- func.mean
21Modules
- Lets us keep things smalland simple
- Good design
22Many Ways to Import
- We can import modules in a variety of ways
- Some ways make life easier
- Other ways are best avoided
- Try to avoid the last form
- You might overwrite a functionwith the same name
import func x 1,2,3 func.sum(x) OR import
func as f x 1,2,3 f.sum(x) OR from func
import s 1,2,3 sum(x)
23Import Will Execute Statements
- A function definitionmakes the function
available for later use - Individual statements will be executed
- Variables can also be accessed from a module
just likefunctions
import func func.sum(1,2,3) func.funcVar
Hello from func.py 6 7
24Module Attributes
- Within a module, we can access the __name__
attribute - If the module was imported, this gives the name
of the module - If the module was run as a program, its value is
__main__ - This is a good way to include tests
- If the module is run as a program perform tests
- If it was imported by other code, dont do the
tests
25Module Attributes
When imported as a moduleno test is performed
func.py
def sum(x) ret 0 for I in x ret I
return ret def mean(x) ret 0 for I
in x ret I return ret/len(x) if __name__
__main__ x 1,2,3 c1 sum(x)
if c1 6 print sum() works fine
else print sum() failed!
import func func.sum(3,4,5)
When run as a programthe test code is run
Macintosh-3 rguha python func.py sum() works
fine
26Exploring Python Libraries
- Python comes with a variety of modules
- Send/receive mail
- Handle pictures and audio
- Cryptography, numerical analysis
- File system operations
- HTTP connections
- The library is a collection of modules
- Import the ones you need to do the job
- See the Python Library documentation
- See the quick tour or a slightly more advanced
description
27System Library (sys)
- Use it to handle command line args, exiting a
program, writing to STDOUT/STDERR - Generally we return 0 whenthe program exits
- sys.path is a variable that lists the places
where Python will look for modules when asked to
load them - The current directory is automatically added
- How many directories are in your sys.path?
import sys for i in sys.argv print
i sys.exit(0)
28Working with the File System
- File systems are different on Unix and Windows
- Python provides the os module
- Contains many functions that hide the differences
- Always try and use the functions in the os
module, rather than OS specific functions - Better chance of your program running on
different machines
29Working with the Filesystem
- List contents of a directory
- os.listdir(/tmp)
- Change to a new directory
- os.chdir(/home/rguha/src)
- Get the current directory name
- os.getcwd()
- See the documentation page for all the functions
30More about Files Directories
- You can do lots of things with filenames and
directory names - Create a new path
- Split a path in to components
- Check whether something is a file or a directory
- The rules to do these can be tricky
- Usually differ from OS to OS
- Use functions from the os.path module
- Makes life much easier!
31More about Files Directories
- Check whether something is a directory or a file
- os.path.isdir(/home/rguha)
- os.path.isfile(/home/rguha/x.txt)
- Strip the path from a filename
- os.path.basename(/home/rguha/x.txt) gives
x.txt - Join parts of a path to give a full path
- os.path.join(home, rguha, x.txt) gives
/home/rguha/x.txt (on Unix)
32Using Math Functions
- The math module contains a variety of functions
- All math functions willreturn something toyou
- Assign it to a variable if you want to do
something with the result of the function
import math print math.sqrt(4) 2.0 print
math.cos( math.pi / 2) 6.12323399573677e-17 print
math.log10(100) 2.0
33Doing Things Randomly
- Sometimes you want to get a random number
- Or select one element from a list randomly
- The random module provides methods to do this
34Doing Things Randomly
- Its useful to remember that randomnumbers are
notreally random! - If you run this program3 times, youll get
3different outputs - How do you test such a program?
import random x 1,2,3,4 print
random.choice( x ) print random.randrange(6) pri
nt random.random()
35Doing Things Randomly
- If you want the same sequence of random
numbers, setthe seed - Now, if you run the program 3 times, you get
the same output - If you dont, the seedgets set from the current
time
import random random.seed(1234) x
1,2,3,4 print random.choice( x ) print
random.randrange(6) print random.random()
36Reading From a CSV File
- Reading lines from a CSV files is quite trivial
- Read the file line by line
- Split on comma
- Do something with it
- But there are differences between CSV files
- Some use , some use
- Some will have extra whitespace
37Reading From a CSV File
- The csv module allows you to handle CSV files
easily - Saves you some time
- Makes your code more general
import csv f open(example.csv, r) r
csv.reader(f, quoting csv.QUOTE_NONE) for row
in r print row
import csv f open(example.csv, r) r
csv.reader(f, quoting csv.QUOTE_NONUMERIC) for
row in r print row
38What Can We Do/
- Internet related stuff
- Connect to web pages, CGIs
- Send email
- Do various forms of compression
- zip, tar, bzip2
- Manipulate raw image/audio data
- If its not in the standard library, someone has
probably written it
39Library Usage Policy
- Good programmers will not reinvent the wheel
- Libraries (modules) save you time and effort
- They dont always do exactly what you need
- But they should not be used to hide lack of
understanding - For simple library methods, you should be able to
re-implement that functionality yourself