Contact Us

Use the form on the right to contact us.

You can edit the text in this area, and change where the contact form on the right submits to, by entering edit mode using the modes on the bottom right. 

Oak Ridge, TN, 37830
United States


Shortcuts, easter eggs, and the exaustingly fun pursuit of solving lifes problems in unconventional ways.

Tank Project - Part 6 - Parsing the XML

Wade Cantley

I left off scratching my head as to how I was going to parse this chunk of XML.  Turns out that is pretty easy to do in python. 

I am really growing fond of this language.  It is easy to follow, there is lots of resources online for finding answers to questions, and the language makes good sense.  I even like the tabbing behavior that while initially odd, makes great sense after thinking about how it saves a few keystrokes by not having to use braces {} to create closures and enforcing good code formatting at the same time.

So, I spent a little time figuring things out in my field notes book.

It goes like this.

  1. Fetch the sunrise and sunset time based on my location and some additional parameters (covered in the code below).
  2. Figure out what minute each is on.  (6:06 pm is minute 1086)
  3. Divide the number of minutes between the Sunrise and Sunset by 20 (round number to start with).

So we will have a Sunrise minute, Sunset minute, Current minute., and incremental period in minutes.

from urllib2 import Request, urlopen, URLError
import xml.etree.ElementTree as ET
import datetime
import time

#So we have to prep this URL with the day, month and whether to use daylight savings time.
#AND it has to be a string so that it is one big url string.
requestURL = "" + str(  + "/" + str( + "/-5/" + str(time.localtime().tm_isdst)
request = Request(requestURL)

#Setup the variables that will be used and default values to start with.
sunrise = ""
sunset = ""
sunriseMinute = ""
sunsetMinute = ""
currentTimeMinutes = * 60 +
isError = False
incrementBy = 20
incrementalPeriod = ""

print "current time minute : " + str(currentTimeMinutes)

#So we fetch the time stamp
    response = urlopen(request)
    timestuff =
    #print timestuff[1:100000]
except URLError, e:
    print 'UUUHHHGGGGG. Got an error code:', e
    isError = True

# and we parse the data if there is no error
if isError == False:

    #get to the root of the XML
    root = ET.fromstring(timestuff)

    #pick out the sunrise and sunset from the XML, replace the colons with commas
    #then split it into a list with multiple objects. So from 07:10:25 into ['07','10','25']
    sunrise = root[3][0].text.replace(":",",").split(',')
    sunset = root[4][0].text.replace(":",",").split(',')

    #now we calculate the minutes into the day for each. These will be our base numbers.
    sunriseMinute = int(sunrise[0]) * 60 + int(sunrise[1])
    sunsetMinute = int(sunset[0]) * 60 + int(sunset[1])

    print "Sunrise Minute : " + str(sunriseMinute)
    print "Sunset Minute : " + str(sunsetMinute)

    #I need to reound this so that it is a whole number.  Just easier to deal with.

    incrementalPeriod = round((sunsetMinute - sunriseMinute) / incrementBy)
    print "Increment by Minutes : " + str(incrementalPeriod)

And when we run it......


So the point behind getting these numbers is that the next step is to do 20 loops and increment by 44 minutes each time.  Each loop I will add 44 minutes to the sunrise minute and I will check to see if my current minutes is less than my incremented number.  If I passed my current time, then do an IF statement check against the current loop count to see what light setting to use.

But why would we need to go through all that effort of looping and checking.  Couldn't we just look at the time and say "at 1pm make it yellow and bright"?

Yes we could do that but it wouldn't be true to the sunrise and sunset times.  Middle of winter has only 10 hours of day light where middle of summer is about 12 hours.  So I want the calculations to fluctuate so that the lighting in my tank is true to the lighting outside.  I will do all this again when calculating the night time lighting since that span is going to be different.

More later though.