開発環境
- 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 8(Mobile app Development: Small devices)、EXERCISE(p.285)を解いてみる。
EXERCISE(p.285)
コード(BBEdit)
サーバー
cgi-bin/athletelist.py
#!/usr/bin/env python3
#-*- coding: utf-8
class AthleteList(list):
def __init__(self, name, dob=None, times=[]):
list.__init__([])
self.name = name
self.dob = dob
self.extend(times)
@staticmethod
def sanitize(time_string):
if '-' in time_string:
splitter = '-'
elif ':' in time_string:
splitter = ':'
else:
return(time_string)
mins, secs = time_string.split(splitter)
return mins + '.' + secs
@property
def top3(self):
return sorted(set([self.sanitize(t) for t in self]))[0:3]
@property
def to_dict(self):
return {'name':self.name, 'DOB':self.dob, 'top3':self.top3}
クライアント(Android, sl4a)
sample285.py
#!/usr/bin/env python
#-*- coding: utf-8 -*-
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://127.0.0.1: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)
athlete_names = sorted(json.loads(send_to_server(web_server + get_names_cgi)))
app = android.Android()
status_update(hello_msg)
app.dialogCreateAlert(list_title)
app.dialogSetSingleChoiceItems(athlete_names)
app.dialogSetPositiveButtonText('Select')
app.dialogSetNegativeButtonText('Quit')
app.dialogShow()
resp = app.dialogGetResponse().result
if resp['which'] == 'positive':
i = app.dialogGetSelectedItems().result[0]
name = athlete_names[i]
athlete = json.loads(send_to_server(
web_server + get_data_cgi, {'witch_athlete': name}))
app.dialogCreateAlert(name)
app.dialogSetItems(athlete.to_dict['top3'])
app.dialogSetPositiveButtonText('Close')
app.dialogShow()
status_update(quit_msg)
アクセスを許可してるつもりだけど、上手くいかない。どこか設定上手くいってないのかなぁ。
0 コメント:
コメントを投稿