If a function will be used in multiple programs, it should be written as a module instead.
All one has to do is put the functions in a program_name.py file and import it (the whole thing) or the functions, then use them in the main program.
Exactly the same way how you import and use other libraries.
Given mass and velocity, this function calculates the kinetic energy of a particle in meters/kilograms/seconds (mks) units.
In [ ]:
def eKinetic(mass, velocity):
return 0.5 * mass * velocity**2
In [ ]:
eKinetic(1, 10)
The following function calculates the x, y, and z accelerations of a particle resulting from forces acting upon it given its mass.
All units are SI.
$a=\frac{F}{m}$ rearranged from $F=ma$
Note: These techniques are also related to some of the tasks in the homework and the final project.
In [ ]:
def acceleration(xForce, yForce, zForce, mass):
xAccel = float(xForce) / float(mass)
yAccel = float(yForce) / float(mass)
zAccel = float(zForce) / float(mass)
return (xAccel, yAccel, zAccel)
In [ ]:
acceleration(10, 20, 30, 5)
In [ ]:
# remember that this line overwrites the local definition
# because it has the same name as above!
from kinematics import eKinetic, acceleration
mass = 100
velocity = 10
xForce = 10
yForce = 20
zForce = 30
kEnergy = eKinetic(mass, velocity)
mAccel = acceleration(xForce, yForce, zForce, mass)
kEnergy, mAccel
Input is often supplied to Python scripts via the command line.
Put another way, "arguments" are provided to scripts.
Here are some Linux examples:
echo $PATH
echo is the command, $PATH
is an argument. Or,
cd some_directory
cd is the commmand, some_directory
is an argument.
cd
No arguments here -- default behavior: cd $HOME
We can do the same sort of thing in Python using the sys module. The following script (lecture_08_wavetofreq.py) converts a user-supplied wavelength (in Angstroms) to frequency (in Hz).
I show you here how to quickly load an existing script into the notebook, using %load:
In [ ]:
# %load lecture_08_wavetofreq.py
#!/usr/bin/env python
import sys
wave = float(sys.argv[1])
freq = 3.0e8 / (wave / 1e10)
print('frequency (Hz) = %e' % freq)
In [ ]:
import sys # "sys" is short for "system"
wave = float(sys.argv[1])
freq = 3.0e8 / (wave / 1e10) # pass wavelength in Angstroms
print('frequency (Hz) = %e' % freq)
In [ ]:
sys.argv
sys.argv contains a list of the command line arguments to the program.
sys.argv[0] is always the name of the program.
To run it in a Linux terminal (must be in same directory as file):
python lecture_08_wavetofreq.py 5000
To run it within here or a simple ipython terminal (file must be in same directory that you launched the notebook from):
In [ ]:
%run lecture_08_wavetofreq.py 5000
In [ ]:
import sys
for i, element in enumerate(sys.argv):
print("Argument #{} = {}".format(i, element))
You will practice with sys in the tutorial!
In [ ]:
%run lecture_08_systest.py 'hello' 2 4 6
The script lecture_08_wavetofreq.py expects an argument, the wavelength in Angstroms:
In [ ]:
# lecture_08_wavetofreq.py
import sys
wave = float(sys.argv[1]) # Attempting to use the argument here.
freq = 3.0e8 / (wave / 1e10) # Convert wavelength in Angstroms to frequency in Hz
print('frequency (Hz) = %e' % freq)
If we forget to supply that argument, we get an error message:
In [ ]:
%run lecture_08_wavetofreq.py
It tells us what file and what line where the error occured and the type of error (IndexError)
In [ ]:
# lecture_08_wavetofreq2.py
import sys
if len(sys.argv) < 2:
print('Enter the wavelength in Angstroms on the command line.')
sys.exit(1) # Exits and 1 indicates failure
# sys.exit() or sys.exit(0) is used to indicate success
wave = float(sys.argv[1])
freq = 3.0e8 / (wave / 1e10)
print('frequency (Hz) = %e' % freq )
In [ ]:
%run lecture_08_wavetofreq2.py 5000
In [ ]:
%run lecture_08_wavetofreq2.py
In [ ]:
%tb
Alternatively, the program can try to run the code and if errors are found, jump to statements that handle the error as desired.
This is done with two new reserved words, "try" and "except", which are used in a similar way as "if" and "elif".
This is the script lecture_08_wavetofreq3.py:
In [ ]:
# lecture_08_wavetofreq3.py
import sys
try:
wave = float(sys.argv[1])
except:
print('Enter the wavelength in Angstroms on the command line.')
sys.exit(1)
freq = 3.0e8 / (wave / 1e10)
print('frequency (Hz) = %e' % freq)
If the command in the try block produces an error, the except block is executed.
In [ ]:
%run lecture_08_wavetofreq3.py x
In [ ]:
%tb
The program could also fail if something other than a number is given on the command line!
That produces a ValueError, not an IndexError.
We can fix this with two separate exceptions appropriate for the two possible errors (this is similar to if/elif/elif):
In [ ]:
# lecture_08_wavetofreq4.py
import sys
try:
wave = float(sys.argv[1])
except IndexError:
print('Enter the wavelength in Angstroms on the command line.')
sys.exit(1)
except ValueError as error:
#print 'The wavelength must be a number'\
#' not %s.' % type(sys.argv[1])
print("The error is:", error)
sys.exit(2)
freq = 3.0e8 / (wave / 1e10)
print('frequency (Hz) = %e' % freq)
In [ ]:
%run lecture_08_wavetofreq4.py 5000
In [ ]:
%run lecture_08_wavetofreq4.py
In [ ]:
%run lecture_08_wavetofreq4.py x
In [ ]:
data = range(9)
data[9]
In [ ]:
y = float('x')
In [ ]:
y = float('3')
y
In [ ]:
x
In [ ]:
4.0/0
In [ ]:
iff 2 > 1:
print('it is.')
In [ ]:
10.0 * 'blah'
In [ ]:
5 * 'blah '
Nice flowchart on error handling (http://i.imgur.com/WRuJV6r.png):