Today while coding my first Facebook application running on Google App Engine I was getting odd errors related to time from the datastore.

The problem was that Google App Engine’s time is always running from Coordinated Universal Time (UTC), even when you use something like:

1
2
import datetime
datetime.datetime.now()

How you should and I have compensate for this is taking the difference of the time you want to UTC time in order to shift the time to the time zone of interest:

1
2
3
4
5
import datetime
time = datetime.datetime.utcnow() - datetime.timedelta(hours = 5) # for East Coast United States
time = datetime.datetime.utcnow() - datetime.timedelta(hours = 6) # for Central United States
time = datetime.datetime.utcnow() - datetime.timedelta(hours = 7) # for Rocky Mountains United States
time = datetime.datetime.utcnow() - datetime.timedelta(hours = 8) # for West Coast United States

This will give you the correct time in your time zone

Tagged with:  

Python XML Parser Tutorial

On January 12, 2010, in Web Coding, by admin

A lot of times developers use REST services or other data feeds that move data using XML. I tried doing this today and noticed the lack of a simple, extremely easy to follow tutorial on how to parse XML using python.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#import library to do http requests:
import urllib2
 
#import easy to use xml parser called minidom:
from xml.dom.minidom import parseString
#all these imports are standard on most modern python implementations
 
#download the file:
file = urllib2.urlopen('http://www.somedomain.com/somexmlfile.xml')
#convert to string:
data = file.read()
#close file because we dont need it anymore:
file.close()
#parse the xml you downloaded
dom = parseString(data)
#retrieve the first xml tag (<tag>data</tag>) that the parser finds with name tagName:
xmlTag = dom.getElementsByTagName('tagName')[0].toxml()
#strip off the tag (<tag>data</tag>  --->   data):
xmlData=xmlTag.replace('<tagName>','').replace('</tagName>','')
#print out the xml tag and data in this format: <tag>data</tag>
print xmlTag
#just print the data
print xmlData

There you have it, that is all you need to do to get a value out of a simple web based xml file using python.

If you want to do it from a file you can do it in a similar fashion:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#import easy to use xml parser called minidom:
from xml.dom.minidom import parseString
#all these imports are standard on most modern python implementations
 
#open the xml file for reading:
file = open('somexmlfile.xml','r')
#convert to string:
data = file.read()
#close file because we dont need it anymore:
file.close()
#parse the xml you got from the file
dom = parseString(data)
#retrieve the first xml tag (<tag>data</tag>) that the parser finds with name tagName:
xmlTag = dom.getElementsByTagName('tagName')[0].toxml()
#strip off the tag (<tag>data</tag>  --->   data):
xmlData=xmlTag.replace('<tagName>','').replace('</tagName>','')
#print out the xml tag and data in this format: <tag>data</tag>
print xmlTag
#just print the data
print xmlData

And thats all you need to know to do the same with a file on your local machine.

If you have any questions post some comments on this post, thanks.

Tagged with: