07 - Web Services, Database Connectivity, and Structured Query Language (SQL)
07 - Web Services, Database Connectivity, and Structured Query Language (SQL)
LESSON 7
import xml.etree.ElementTree as ET
data = '''
<person>
<name>Tim the Shrubber</name>
<phone type="intl">
+1 555 337 3545
</phone>
<email hide="yes"/>
</person>'''
tree = ET.fromstring(data)
print 'Name:',tree.find('name').text
print 'Attr:',tree.find('email').get('hide')
Name: Tim the Shrubber
Attr: yes
import xml.etree.ElementTree as ET
data = '''
<person>
<name>Tim the Shrubber</name>
<phone type="intl">
+1 555 337 3545
</phone>
<email hide="yes"/>
</person>'''
tree = ET.fromstring(data)
print 'Name:',tree.find('name').text
print 'Attr:',tree.find('email').get('hide')
input = '''
<stuff>
<users>
<user x='42'>
<id>001</id>
<name>Tim the Shrubber</name>
</user>
<user x='23'>
<id>009</id>
<name>Sir Galahad</name>
</user>
</users>
</stuff>'''
stuff = ET.fromstring(input)
lst = stuff.findall('users/user')
print 'User count:', len(lst)
input = '''
[
{ "id" : "001",
"x" : "2",
"name" : "Chuck"
},
{ "id" : "009",
"x" : "7",
"name" : "Chuck"
}
]'''
info = json.loads(input)
print 'User count:', len(info)
import urllib
import json
serviceurl =
'http://maps.googleapis.com/maps/api/geocode/json?'
while True:
address = raw_input('Enter location: ')
try: js = json.loads(str(data))
except: js = None
if 'status' not in js or js['status'] != 'OK':
print '==== Failure To Retrieve ===='
print data
continue
lat = js["results"][0]["geometry"]["location"]["lat"]
lng = js["results"][0]["geometry"]["location"]["lng"]
print 'lat',lat,'lng',lng
location = js['results'][0]['formatted_address']
print location
},
"viewport": {
"northeast": {
"lat": 33.114249,
"lng": -116.90816
},
"southwest": {
"lat": 32.534856,
"lng": -117.2821665
}
},
"location": {
"lat": 32.715738,
"lng": -117.1610838
}
},
"address_components": [
{
"long_name": "San Diego",
"types": [
"locality",
"political"
],
"short_name": "San Diego"
},
{
"long_name": "San Diego County",
"types": [
"administrative_area_level_2",
"political"
],
"short_name": "San Diego County"
},
{
"long_name": "California",
"types": [
"administrative_area_level_1",
"political"
],
"short_name": "CA"
},
{
"long_name": "United States",
"types": [
"country",
"political"
],
"short_name": "US"
}
],
"place_id": "ChIJSx6SrQ9T2YARed8V_f0hOg0",
"formatted_address": "San Diego, CA, USA",
"types": [
"locality",
"political"
]
}
]
}
lat 32.715738 lng -117.1610838
San Diego, CA, USA
Enter location:
def oauth() :
return { "consumer_key" : "h7Lu...Ng",
"consumer_secret" :
"dNKenAC3New...mmn7Q",
"token_key" : "10185562-
eibxCp9n2...P4GEQQOSGI",
"token_secret" :
"H0ycCFemmC4wyf1...qoIpBo" }
import sqlite3
conn = sqlite3.connect('music.sqlite')
cur = conn.cursor()
conn.close()
conn.close()
conn = sqlite3.connect('music.sqlite')
cur = conn.cursor()
print 'Tracks:'
cur.execute('SELECT title, plays FROM Tracks')
for row in cur :
print row
cur.close()
The INSERT commands insert two new rows into our Tracks
table.
cur.execute('INSERT INTO Tracks (title, plays) VALUES ( ?, ? )', ( 'Thunderstruck', 20 ) )
cur.execute('INSERT INTO Tracks (title, plays) VALUES ( ?, ? )', ( 'My Way', 15 ) )
conn.commit()
The fields are specified, and then the values. We specify “?” for the
values to indicate that the succeeding tuple contains the actual
values.
The COMMIT command writes the data to the database.
title plays
Thunderstruck 20
My Way 15
Tracks:
(u' Thunderstruck' , 20)
(u' My Way' , 15)
The u' indicates that the strings are unicode, which supports an
extensive non-Latin character set.