開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python (プログラミング言語)
Head First Python (Paul Barry(著)、 O'Reilly Media )のChapter 9(Manage Your data: Handling input)、EXERCISE(p.343)を解いてみる。
EXERCISE(p.343)
コード(BBEdit, Emacs)
coachapp.py
import android
import json
import time
from urllib import urlencode
from urllib2 import urlopen
hello_msg = "Welcome to Coach Kelly's Timing App"
list_title = 'Here is your list of athletes:'
quit_msg = "Quitting Coach Kelly's App."
web_server = 'http://192.168.1.34:8080'
get_names_cgi = '/cgi-bin/generate_names.py'
get_data_cgi = '/cgi-bin/generate_data.py'
def send_to_server(url, post_data=None):
if post_data:
page = urlopen(url, urlencode(post_data))
else:
page = urlopen(url)
return page.read().decode("utf8")
def status_update(msg, how_long=2):
app.makeToast(msg)
time.sleep(how_long)
app = android.Android()
status_update(hello_msg)
athletes = sorted(json.loads(send_to_server(web_server + get_names_cgi)))
athlete_names = [row[0] for row in athletes]
app.dialogCreateAlert(list_title)
app.dialogSetSingleChoiceItems(athlete_names)
app.dialogSetPositiveButtonText('Select')
app.dialogSetNegativeButtonText('Quit')
app.dialogShow()
resp = app.dialogGetResponse().result
if resp['which'] in ('positive'):
selected_athlete = app.dialogGetSelectedItems().result[0]
which_athlete = athletes[selected_athlete][1]
athlete = json.loads(send_to_server(web_server + get_data_cgi,
{'which_athlete': which_athlete}))
athlete_title = '{0} ({1}), top 3 times:'.format(
athlete['Name'], athlete['DOB'])
app.dialogCreateAlert(athlete_title)
app.dialogSetItems(athlete['top3'])
app.dialogSetPositiveButtonText('OK')
app.dialogShow()
resp = app.dialogGetResponse().result
status_update(quit_msg)
0 コメント:
コメントを投稿