Skip to content

Instantly share code, notes, and snippets.

@colegleason
Created December 28, 2014 04:13
Show Gist options
  • Save colegleason/af4c74a59875a996b86e to your computer and use it in GitHub Desktop.
Save colegleason/af4c74a59875a996b86e to your computer and use it in GitHub Desktop.
Here's another early Python script. Weather forecast script. Date: 2/7/10
#Cole's Weather Wizard
#First we need to import some modules.
import urllib
from xml.dom import minidom
from string import lower
#Ask the user for their zip code.
zip = input("Enter your zip code: ")
#Create the correct Google url by appending the user's zip code.
url = 'http://www.google.com/ig/api?weather=%d' % (zip, )
#Parse the XML document into a usable DOM.
xmldoc = minidom.parse(urllib.urlopen(url)).documentElement
#Create some easy to use variables for grabbing data at different node levels.
weatherNode = xmldoc.firstChild
infoNode = weatherNode.firstChild
currentNode = weatherNode.childNodes[1]
forecastNode1 = weatherNode.childNodes[2]
forecastNode2 = weatherNode.childNodes[3]
forecastNode3 = weatherNode.childNodes[4]
forecastNode4 = weatherNode.childNodes[5]
#Pull the values out of the nodes.
infoCity = infoNode.firstChild.attributes["data"].value
curCondition = currentNode.firstChild.attributes["data"].value
curTempF = currentNode.childNodes[1].attributes["data"].value
curTempC = currentNode.childNodes[2].attributes["data"].value
curHumidity = currentNode.childNodes[3].attributes["data"].value
curWind = currentNode.childNodes[5].attributes["data"].value
foreDay1 = forecastNode1.firstChild.attributes["data"].value
foreDay2 = forecastNode2.firstChild.attributes["data"].value
foreDay3 = forecastNode3.firstChild.attributes["data"].value
foreDay4 = forecastNode4.firstChild.attributes["data"].value
foreCond1 = forecastNode1.childNodes[4].attributes["data"].value
foreCond2 = forecastNode2.childNodes[4].attributes["data"].value
foreCond3 = forecastNode3.childNodes[4].attributes["data"].value
foreCond4 = forecastNode4.childNodes[4].attributes["data"].value
foreHi1 = int(forecastNode1.childNodes[2].attributes["data"].value)
foreHi2 = int(forecastNode2.childNodes[2].attributes["data"].value)
foreHi3 = int(forecastNode3.childNodes[2].attributes["data"].value)
foreHi4 = int(forecastNode4.childNodes[2].attributes["data"].value)
foreLo1 = int(forecastNode1.childNodes[1].attributes["data"].value)
foreLo2 = int(forecastNode2.childNodes[1].attributes["data"].value)
foreLo3 = int(forecastNode3.childNodes[1].attributes["data"].value)
foreLo4 = int(forecastNode4.childNodes[1].attributes["data"].value)
#Print result for the user. First we will give the conditions.
print "-"*50
print "The weather in " + infoCity + " is currently " + lower(curCondition) + "."
#The degree symbol must be represented as a unicode string.
deg = u"\u00B0"
#And now the temp.
print "It is currently " + curTempF + deg + "F (" + curTempC + deg + "C)."
#Humidity and wind
print "The humidity is " + curHumidity[10:] + ". " + curWind + "."
print "-"*50
#Define a yes or no question prompt.
def ask_yn(prompt, complaint='Yes or no, please!'):
while True:
ok = raw_input(prompt)
if ok in ('y', 'ye', 'yes'):
return True
elif ok in ('n', 'no', 'nop', 'nope'):
return False
else:
print complaint
forecast_result = ask_yn('Would you like to see the four day forecast? [yes/no]: ')
if forecast_result == True:
print "-"*50
print "Day Lo Hi Condition"
print "--- -- -- ---------"
print foreDay1 + " " + '%(foreLo1)02d' % {'foreLo1': foreLo1} + " " + "%(foreHi1)02d" % {'foreHi1': foreHi1} + " " + foreCond1
print foreDay2 + " " + '%(foreLo2)02d' % {'foreLo2': foreLo2} + " " + "%(foreHi2)02d" % {'foreHi2': foreHi2} + " " + foreCond2
print foreDay3 + " " + '%(foreLo3)02d' % {'foreLo3': foreLo3} + " " + "%(foreHi3)02d" % {'foreHi3': foreHi3} + " " + foreCond3
print foreDay4 + " " + '%(foreLo4)02d' % {'foreLo4': foreLo4} + " " + "%(foreHi4)02d" % {'foreHi4': foreHi4} + " " + foreCond4
print "--- -- -- ---------"
else:
quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment