very good parallelization: "import cloud"
Neat trick: "whos" in ipython shows you what variables are set!!!
In [15]: whos
Variable Type Data/Info
-----------------------------
accel float 9.8
t float 1.0
Operators:
** is the power operator.
^ is a bitwise operator, very different from power! Be careful.
Strings:
- Strings are immutable. This sounds really weird.
- r before a string makes it a raw string: print r'this is a raw string\n\r'
- "eggs"*100 prints eggs a hundred times, duh.
- string join operator is +. Perl . does not work.
- s = "spam\n"; print len(s); print s[-1]; print s[2:4]; print s[1:]
Get simple input from command line:
T = float(raw_input("Enter the teperature in K: ")) #need to cast the input
Enter the teperature in K: 100
Data structures:
To find out what variable v is: > type(v)
To find out what you can do with a variable, in ipython, type v.TAB
for i,x in enumerate(a): # Very useful way to loop and keep track of index
print i, x, len(x)
x = range(0,100000,1000); # range([start,] stop[, step]) -> list of integers
# This makes a useful lsit of integers, with selectable start, stop, & step
FUNCTIONS: # Here's how to define one.
def addnums(x,y):
return x + y
addnums(2,3)
SCOPE:
global a # defines a variable as a global variable
*arg - a list of arguments. required
**kwargs - a list of keyword key=value pairs.
MODULES:
ip> from numpy import int[TAB], shows you the numpy modules starting w int
reload(module_name) # reload module when you've edited it.
ipython fanciness:
%lsmagic # lists all the magic functions
%quickref # quick reference on ipython
%reset
%doctest_mode
%history -f logfile.out
%pdb - python dbugger
%load_ext rmagic # run R from Python
%save -f saved_work.py #save your work
%run saved_work.py #load it back in
%%! (do stuff in the shell for a while, until a blankline return)
#Lots of great stuff in Jeremy's talk about ipython,
#especially how to talk to the command line and back. Flew by fast,
#so I re-read it carefully at home.
# this is like backticks in perl:
from_shell = !ls *.cat
!touch "$from_shell[0]"."new"
from_shell? # tells you about this new variable
from_shell?? # more verbose
Matplotlib:
running ipython --pylab automaticlly loads numpy as np, matplotlib.pylab as plt
How to format strings like in perl:
print "%.3f" % (math.pi) #the % sign is the key operator here.
print "%010f" % math.pi
"Here is formatted pi: {0:.3f}".format(math.pi)
# place format what gets formatted
# The syntax here is weird. {place : formatting} Study this!
Formatting strings, use nameofstring.format()
"on {1}, I feel {0}".format("saturday", "groovy")
Regular expressions!
re.split is more powerful than built-in split. Looks more like perl splitline = "I like the number 234 and 21."
re.split("([kn])", line) # splits on either k or n. Ah, regexp
# many ways to read a file: read, readlines, asciitable
module lets you run subcommands
os.system("sed s/love/hate/g %s > temp" % infile)
os.system("sed s/is/not/g temp2 > temp3")
#Lambda functions:
-lambda is a reserved keyword. Cannot use it for wavelength.
- Used for short, one-liner functions. If longer, name a function
- very nifty, can do function in 1 line
(lambda x,y: x**2+y)(2,4.5) # forget about creating a new function name...just do it!!
#Terri's slides had a very useful way to sort by several different columns
airline.flights.sort(key=operator.itemgetter(4,1,0))
Should wrap volatile (may fail) code in Try, Except, Finally wrapper
Python can forget about a varible, with del: "del x"
To run things in ipython to debug:
%run OO_breakout.py
a=b means that a follows b from here on, HOWEVER b changes later!
So it's not the way we normally think of assignment.
Copy does something different:
c=copy.copy(a)
#I asked Jeremy how to organize my Python scripts. He suggested I
#add a local dir to PYTHON_PATH, that has my files. This will
#make them globally visible to my python. Centralize, so I can canibalize.
Scipy is a big module of math and science programs.
-Includes numpy, matplotlib, ipython.
-scipy uses numpy arrays
-Rpy -- interface to R
-zis there an IDLPy wrapper?
MPFIT and MPFITFUN *have* been imported to Python, yay.
There are other ways of doing things, scipy.optimize.
Terri, Errors, Exceptions, Traceback:
DEBUGGER:
different ways to invoke:
pdb.pm() is the perl debugger post-mortem: it's like ILD STOP, but it
stops where the code dies, and lets you look around, work
pdb.set_trace() # sets cookies where you think your code is failing
pdb.run('myexamplecode.main()')
python /usr/stsci/pyssg/Python-2.7/lib/python2.7/pdb.py wed_breakout2.py
# this runs the python debugger on the command line
Andy Ptak's talk on astronomy applications
fits:
astropy.io.fits: FITS file handling. will subsume pyfits
asciitable will also be absorbed into astropy
astropy.constants is amazing! Can convert, combine units wantonly, it
keeps track.
from ds9 import * # now can display images in ds9, from python
#works on xpaset, xpaget, so ds.get(), ds.set()
Convolution in astropy is better than scipy, because it can handle NaNs.
python-crontab: module to handle crontab
Class website: github.com/kialio/python-bootcamp
No comments:
Post a Comment