This notebook was put together by Jake Vanderplas for UW's Astro 599 course. Source and license info is on GitHub.
%run talktools.py
An important part of coding (in Python and in other modern language) is organizing code in easily re-used chunks.
Python can work within both a procedural and an object-oriented style.
Procedural programming is using functions
Object-oriented programming is using classes
We'll come back to classes later, and look at functions now.
Function definitions in Python look like this:
def function_name(arg1, arg2, ...,
kw1=val1, kw2=val2, ...)
(Note that line-breaks between the parentheses are ignored)
argX are arguments, and are required
kwX are keyword arguments, and are optional
The function name can be anything, as long as it:
print, or for)Note for IDL users: there is no difference between functions and procedures. All Python functions return a value: if no return is specified, it returns None
def addnums(x, y):
return x + y
result = addnums(1, 2)
result
3
addnums(1, y=2)
3
addnums("A", "B")
'AB'
Note that the variable types are not declared (as we've discussed Python is a dynamic language)
def scale(x, factor=2.0):
return x * factor
scale(4)
8.0
scale(4, 10)
40
scale(4, factor=10)
40
Arguments and Keyword arguments can either be specified by order or by name, but an unnamed argument cannot come after a named argument:
scale(x=4, 10)
File "<ipython-input-13-f0cbe9ee0750>", line 1 scale(x=4, 10) ^ SyntaxError: non-keyword arg after keyword arg
Returned values can be anything, which allows a lot of flexibility:
def build_dict(x, y):
return {'x':x, 'y':y}
build_dict(4, 5)
{'y': 5, 'x': 4}
def no_return_value():
pass
x = no_return_value()
print(x)
None
Keyword arguments can be a very handy way to grow new functionality without breaking old code.
Imagine, for example, you had the build_dict function from above:
def build_dict(x, y):
return {'x':x, 'y':y}
build_dict(1, 2)
{'y': 2, 'x': 1}
Now what if you want to change the names of the variables in the dictionary? Adding a keyword argument can allow this flexibility without breaking old code:
def build_dict(x, y, xname='x', yname='y'):
return {xname:x, yname:y}
build_dict(1, 2) # old call still works
{'y': 2, 'x': 1}
build_dict(1, 2, xname='spam', yname='eggs')
{'eggs': 2, 'spam': 1}
This is admittedly a silly example, but it shows how keywords can be used to add flexibility without breaking old APIs.
Python functions have their own local variables list:
def modify_x(x):
x += 5
return x
x = 10
y = modify_x(x)
print(x)
print(y)
10 15
Modifying a variable in the function does not modify the variable globally... unless you use the global declaration
def add_a(x):
global a
a += 1
return x + a
a = 10
print(add_a(5))
print(a)
16 11
Warning: Simple and Compound types are treated differently!
def add_one(x):
x += 1
x = 4
add_one(x)
print(x)
4
def add_element(L):
L.append(4)
L = [1, 2]
add_element(L)
print(L)
[1, 2, 4]
Simple types (int, long, float, complex, string) are passed by value.
Compound types (list, dict, set, tuple, user-defined objects) are passed by reference.
Question to think about: why would this be?
def cheeseshop(kind, *args, **kwargs):
print("Do you have any", kind, "?")
print("I'm sorry, we're all out of", kind)
for arg in args:
print(arg)
print(40 * "=")
for kw in kwargs:
print(kw, ":", kwargs[kw])
cheeseshop("Limburger", "It's very runny, sir.",
"It's really very, VERY runny, sir.",
shopkeeper="Michael Palin",
client="John Cleese",
sketch="Cheese Shop Sketch")
Do you have any Limburger ? I'm sorry, we're all out of Limburger It's very runny, sir. It's really very, VERY runny, sir. ======================================== shopkeeper : Michael Palin sketch : Cheese Shop Sketch client : John Cleese
(example from Python docs)
Documentation is not required, but your future self (and anybody else using your code) will thank you.
def power_of_difference(x, y, p=2.0):
"""Return the power of the difference of x and y
Parameters
----------
x, y : float
the values to be differenced
p : float (optional)
the exponent (default = 2.0)
Returns
-------
result: float
(x - y) ** p
"""
diff = x - y
return diff ** p
power_of_difference(10.0, 5.0)
25.0
(Note that this example follows the Numpy documentation standard)
With documentation specified this way, the IPython help command will be helpful!
power_of_difference?
%%file myfile.py
def power_of_difference(x, y, p=2.0):
"""Return the power of the difference of x and y
Parameters
----------
x, y : float
the values to be differenced
p : float (optional)
the exponent (default = 2.0)
Returns
-------
result: float
(x - y) ** p
"""
diff = x - y
return diff ** p
Writing myfile.py
# Pydoc is a command-line program bundled with Python
!pydoc -w myfile
wrote myfile.html
from IPython.display import HTML
HTML(open('myfile.html').read())
| myfile | index /Users/jakevdp/Opensource/2014_fall_ASTR599/notebooks/myfile.py |
| Functions | ||
| ||
Modules are organized units of code which contain functions, classes, statements, and other definitions.
Any file ending in .py is treated as a module (e.g. our file myfile.py above).
Variables in modules have their own scope: using a name in one module will not affect variables of that name in another module.
%%file mymodule.py
# A simple demonstration module
def add_numbers(x, y):
"""add x and y"""
return x + y
def subtract_numbers(x, y):
"""subtract y from x"""
return x - y
Overwriting mymodule.py
Modules are accessed using import module_name (with no .py)
import mymodule
print('1 + 2 =', mymodule.add_numbers(1, 2))
print('5 - 3 =', mymodule.subtract_numbers(5, 3))
1 + 2 = 3 5 - 3 = 2
Note that namespaces are important:
add_numbers(1, 2)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-49-1b5a76eb5e06> in <module>() ----> 1 add_numbers(1, 2) NameError: name 'add_numbers' is not defined
import mymodule
mymodule.add_numbers(1, 2)
3
from mymodule import add_numbers
add_numbers(1, 2)
3
from mymodule import add_numbers as new_name
new_name(1, 2)
3
from mymodule import *
subtract_numbers(5, 3)
2
This final method can be convenient, but should generally be avoided as it can cause name collisions and makes debugging difficult.
Your modules can have their own documentation, can define module-level variables, and can execute code when they load. For example:
%%file mymodule2.py
"""
Example module with some variables and startup code
"""
# this code runs when the module is loaded
print("mymodule2 in the house!")
pi = 3.1415926
favorite_food = "spam, of course"
def multiply(a, b):
return a * b
Overwriting mymodule2.py
import mymodule2
mymodule2 in the house!
# import again and the initial code does not execute!
import mymodule2
# access module-level documentation
mymodule2?
mymodule2.multiply(2, 3)
6
mymodule2.pi
3.1415926
# module variables can be modified
mymodule2.favorite_food
'spam, of course'
mymodule2.favorite_food = "eggs. No spam."
mymodule2.favorite_food
'eggs. No spam.'
sys: exposes interactions with the system (environment, file I/O, etc.)os: exposes platform-specific operations (file statistics, directories, paths, etc.)math: exposes basic mathematical functions and constantsimport sys
sys?
import sys
import os
print("You are using Python version", sys.version)
print(40 * '-')
print("Current working directory is:")
print(os.getcwd())
print(40 * '-')
print("Files in the current directory:")
for f in os.listdir(os.getcwd()):
print(f)
You are using Python version 3.3.5 |Anaconda 1.6.1 (x86_64)| (default, Sep 2 2014, 13:57:31) [GCC 4.2.1 (Apple Inc. build 5577)] ---------------------------------------- Current working directory is: /Users/jakevdp/Opensource/2014_fall_ASTR599/notebooks ---------------------------------------- Files in the current directory: .ipynb_checkpoints 00_intro.ipynb 01_basic_training.ipynb 02_advanced_data_structures.ipynb 03_IPython_intro.ipynb 04_Functions_and_modules.ipynb 05_NumpyIntro.ipynb 05_Trapezoid_Solution.ipynb 06_Denoise_Solution.ipynb 06_MatplotlibIntro.ipynb 07_GitIntro.ipynb 08_ScipyIntro.ipynb 09_AdvancedStrings.ipynb 10_AdvancedPython2.ipynb 11_EfficientNumpy.ipynb 12_AdvancedMatplotlib.ipynb __pycache__ demo_agg_filter.py fig_code fig_moving_objects_multicolor.py images mod.py myfile.html myfile.py myfile.pyc mymodule.py mymodule2.py nth_fib.html number_game.py sankey_demo_rankine.py style.css talktools.py test.txt Untitled0.ipynb vizarray.py
Built-in modules are listed at http://docs.python.org/2/py-modindex.html
# try importing antigravity...
When a script or module is run directly from the command-line (i.e. not imported) a special variable called __name__ is set to "__main__".
So, in your module, if you want some part of the code to only run when the script is executed directly, then you can make it look like this:
# all module stuff
# at the bottom, put this:
if __name__ == '__main__':
# do some things
print "I was called from the command-line!"
Here's a longer example of this in action:
%%file modfun.py
"""
Some functions written to demonstrate a bunch of concepts
like modules, import and command-line programming
"""
import os
import sys
def getinfo(path=".",show_version=True):
"""
Purpose: make simple us of os and sys modules
Input: path (default = "."), the directory you want to list
"""
if show_version:
print("-" * 40)
print("You are using Python version ", sys.version)
print("-" * 40)
print("Files in the directory " + str(os.path.abspath(path)) + ":")
for f in os.listdir(path):
print(" " + f)
print("*" * 40)
if __name__ == "__main__":
"""
Executed only if run from the command line.
call with
modfun.py <dirname> <dirname> ...
If no dirname is given then list the files in the current path
"""
if len(sys.argv) == 1:
getinfo(".",show_version=True)
else:
for i,dir in enumerate(sys.argv[1:]):
if os.path.isdir(dir):
# if we have a directory then operate on it
# only show the version info
# if it's the first directory
getinfo(dir,show_version=(i==0))
else:
print("Directory: " + str(dir) + " does not exist.")
Overwriting modfun.py
# now execute from the command-line
%run modfun.py
---------------------------------------- You are using Python version 3.3.5 |Anaconda 1.6.1 (x86_64)| (default, Sep 2 2014, 13:57:31) [GCC 4.2.1 (Apple Inc. build 5577)] ---------------------------------------- Files in the directory /Users/jakevdp/Opensource/2014_fall_ASTR599/notebooks: .ipynb_checkpoints 00_intro.ipynb 01_basic_training.ipynb 02_advanced_data_structures.ipynb 03_IPython_intro.ipynb 04_Functions_and_modules.ipynb 05_NumpyIntro.ipynb 05_Trapezoid_Solution.ipynb 06_Denoise_Solution.ipynb 06_MatplotlibIntro.ipynb 07_GitIntro.ipynb 08_ScipyIntro.ipynb 09_AdvancedStrings.ipynb 10_AdvancedPython2.ipynb 11_EfficientNumpy.ipynb 12_AdvancedMatplotlib.ipynb __pycache__ demo_agg_filter.py fig_code fig_moving_objects_multicolor.py images mod.py modfun.py myfile.html myfile.py myfile.pyc mymodule.py mymodule2.py nth_fib.html number_game.py sankey_demo_rankine.py style.css talktools.py test.txt Untitled0.ipynb vizarray.py ****************************************
Note some of the sys and os commands used in this script!
This breakout will give you a chance to explore some of the builtin modules offered by Python. For this session, please use your text editor to create the files. You'll have to
Create and edit a new file called age.py. Though you can do this via the %%file magic used above, here you should use your text editor.
within age.py, import the datetime module
use datetime.datetime() to create a variable representing your birthday
use datetime.datetime.now() to create a variable representing the present date
subtract the two (this forms a datetime.timedelta() object) and print that variable.
Use this object to answer these questions:
How many days have you been alive?
How many hours have you been alive?
What will be the date 1000 days from now?
Create and edit a new file called age1.py. When run from the command-line with one argument, age1.py should print out the date in that many days from now. If run with three arguments, print the time in days since that date.
[~]$ python age1.py 1000
date in 1000 days 2016-06-06 14:46:09.548831
[~]$ python age1.py 1981 6 12
days since then: 11779