Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

July 03, 2013

Back in the (scientific) python business!


The above five lines of python represent the accomplishment of a (small) summer goal of mine: to have one cohesive python install with all of my favorite python packages for writing code for physics classes and research.

I don't know what happened earlier this year, but I had b0rked up my python install on my school laptop. I have a Macbook Pro, and was running into all sorts of problems: 32 bit vs. 64 bit, which install of python to use and whether or not I could use matplotlib and vpython at the same time.

What I ended up with was two installs of python: one that could use vpython and one that could use matplotlib, but never the two at the same time.

As you can see in the screenshot, I have everything working now with the excellent enthought distribution of python. What I learned today was that ALL  the packages in the vpython dmg file are required to run vpython. I don't remember my original thought process which led me to believe I didn't need the other packages, but I did. Also, I learned today that you can't always (simply) run vpython calls from the python shell, unless you limit the rate of displaying frames by putting rate() inside a loop.  More on that later, maybe.

I know the above packages have significant overlap (scipy extends numpy, pylab has matplotlib, etc.) but I've used each of those in various forms, so I wanted to be able to call any of them without having to THINK about it.  Done.

Okay, so if you made it this far into this post, you deserve a reward. How about 8 tutorials on scientific python programming? My favorites are the SciPy tutorial and the excellent matplotlib tutorial.

February 06, 2013

Python learning opportunity

I have an honors student this semester who is working on vpython simulations of general physics systems.  She knew no programming as of two weeks ago, but tonight was really close to having a projectile motion simulation done.

In trying to put velocity vector components on the projectile's position every 10 time steps, she was running into a problem: using the modulus operator only worked on the first two vectors, then did not.

Here was her code:

 while ball.y >= -2.75 and int_velo > 0:  
   rate(100)  
   t = t + dt   
   ball.pos= ball.pos + ball_velocity*dt  
   ball_velocity.y = ball_velocity.y - 9.81*dt  
   if (t*100)%10 == 0: #doesn't work!!!   
     vel_xvec = arrow(pos=(ball.pos.x,ball.pos.y,0), axis=(int_xvelo,0,0), shaftwidth=0.5)    
     vel_yvec = arrow(pos=(ball.pos.x,ball.pos.y,0), axis=(0,ball_velocity.y,0), shaftwidth=0.5)  

The comment tells the story. I guess I should know more about the modulus operator in python. It seems simple to use, but I couldn't really figure out what the problem was initially.

The short version of this story is that my student gets to learn about computer round-off errors and how to debug code by sticking a print command in the code so that she can figure out these problems without too much intervention on my part.

Here's my debug line I ended up using, followed by the fixed if statement:

   print t, t*100, (round(t*100)%10.0)  
   if (round(t*100)%10) == 0: #works now!!!   

I'm looking forward to a fun semester of python projects!

August 22, 2011

Parsing XML with python

I'm transitioning my classes from the "Desire to Learn" (D2L) CMS to the ANGEL CMS.  Unfortunately, I didn't export my classes before I lost access to D2L.  Fortunately, I know a great person who was able to export the D2L files and send them to me. 


Unfortunately, ANGEL doesn't recognize D2L files.  Bummer.  


All of my "Reading Quiz" questions are stored in a question bank which is an xml file.  If I knew how to parse XML, I could probably cut and paste questions in to ANGEL.


I don't know how to parse XML, but I do know how to use Google. I found a bunch of tutorials, but ultimately settled on the Dive into Python tutorial.  


My question bank had 2345 entries in it.  The entries were made up of three things: a) the actual questions, b) multiple choice answers and distractors, and c) empty entries between questions.   All the entries were identified with the 'mattext' tag, although there didn't seem to be an easy way to separate the various types of entries.


Here is some code I wrote.  I'm putting it here so I don't lose it.

#!/usr/bin/env python


from xml.dom import minidom 


xmldoc = minidom.parse('./questiondb.xml')
mattextlist = xmldoc.getElementsByTagName('mattext')


for i in range(2345):
#print i
try:
print mattextlist[i].firstChild.data
print "
\n"

except AttributeError:
print "
\n"



An AttributeError was triggered by every one of the empty entries.  Printing the blank lines every time an empty entry was reached made the output of the script easier to interpret visually.


This was a fun little bit of python.  I'm glad I was able to salvage stuff from the D2L class files.