Wiley.Mobile.Python.Rapid.prototyping.of.applications.on.the.mobile.platform.Dec.2007 (779889), страница 24
Текст из файла (страница 24)
Foranother example of the Ao timer object, see Section 9.3.The result of the timer operation is that the function showlocation() is called after the specified interval. This function checksand reports your current location, based on the known cells dictionary, and logs your current location using the file object log, which isopened when the application starts. Also the current time, as returned bythe function time.ctime() is added to each location logged.
Beforethe function show location() returns, it sets the timer to call itselfagain after the interval, so the location is checked periodically.The function current location() is responsible for fetching theactual GSM cell ID. It works in a similar way to Example 49 above.
Here,however, we convert the result to a string, so it can easily be printed outon the screen and saved to a file.Note that, when the user quits the application, we have to cancel anypending timer request using the function timer.cancel(). Otherwise,the request could activate after the application has already quit, causingthe PyS60 interpreter to crash.Once you have the application running, you should take a trip aroundyour neighborhood.
First, try to find different cells. Once you have spottedtwo or three cell IDs in different physical locations, give a name to eachof them. Walk the same route again and see if the names shown on thescreen correspond to the real locations.Do not be surprised if the cells seem to overlap or disappear, or newcells appear although you stay in one location. In reality, building a reallyaccurate positioning system solely based on GSM cell IDs is a majorchallenge.VOCABULECTOR: A LANGUAGE-LEARNING TOOL6.4.2127GPS PositioningThe PyS60 API documentation contains more information, includingsome examples, about the usage of the position module, which relieson the internal GPS. This module should be of interest to those who owna phone, such as Nokia N95, that includes a built-in GPS receiver.
Notethat also in this case you need a signed PyS60 interpreter, as describedabove.6.5 Vocabulector: A Language-Learning ToolThe final example of this chapter is a personal language-learning tool.Let’s assume you are traveling in the Basque country and you want tobuy some cheese. You may find in your dictionary that cheese is ‘gazta’in Basque, but you have no idea how to pronounce it correctly. Probablyyou would forget the word anyway before you get to a local cheese-shop.Vocabulector solves the problem. In your hotel room, you type ‘cheese’as the native word and ‘gazta’ as the foreign word into your Vocabulectorand take a photo of a half-eaten piece of cheese.
After this, you find afriendly person on the street, show her the photo and ask her to pronounceit like a native.The correct pronunciation is recorded by Vocabulector and saved withthe two words and the photo. Once you get to the cheese shop, you havethe correct pronunciation and a descriptive photo readily available. Notonly do you get the cheese, but you can practice pronunciation andhearing comprehension with this handy little application afterwards inyour hotel room, while eating your gazta.This example relies heavily on the ability to load and save data.Words, photos and sounds are saved to an application-specific directory at E:\Data\Vocabulector. Besides loading and saving files, thisexample also uses many concepts from Chapter 5, most notably thecamera and sounds.The code is divided into three parts.
Example 51 deals with addingnew entries to Vocabulector and loading any previously saved ones.Example 52 is responsible for showing the entries and Example 53 contains the standard application UI boilerplate code. You should combinethese parts together in one file to form the full application.6.5.1 Adding New Entries to VocabulectorExample 51: Vocabulector (1/3)import os, os.path, audio, appuifw, camera, key_codes, graphics128DATA HANDLINGPATH = "E:\\Data\\Vocabulector\\"def load_translations():global trans, dict_fileif not os.path.exists(PATH):os.makedirs(PATH)dict_file = file(PATH + "trans.txt", "rw")trans = []for line in dict_file:line = line.decode("utf-8")native, foreign = line.strip().split(":")trans.append((native, foreign))def add_entry():global text, photo, fnametext = photo = Nonenative = appuifw.query(u"Native word:", "text")if not native:returnforeign = appuifw.query(u"Foreign word:", "text")if not foreign:returnfname = PATH + nativetrans.append((native, foreign))line = "%s:%s" % (native, foreign)print >> dict_file, line.encode("utf-8")if appuifw.query(u"Record sound", "query"):record_sound()if appuifw.query(u"Take photo", "query"):camera.start_finder(viewfinder)canvas.bind(key_codes.EKeySelect, take_photo)else:appuifw.note(u"Entry added!", "info")def record_sound():snd = audio.Sound.open(fname + ".wav")snd.record()appuifw.query(u"Press OK to stop recording", "query")snd.close()def viewfinder(img):canvas.blit(img)def take_photo():global photocanvas.bind(key_codes.EKeySelect, None)camera.stop_finder()photo = camera.take_photo(size = (640,480))handle_redraw(None)photo.save(fname + ".jpg")The constant PATH defines a directory for the Vocabulector’s files.
Ifyou do not have a memory card, replace E: in the path with C: to referto the phone’s internal memory. The function load translations()loads the saved word pairs from a text file when the application starts.The file is opened in rw mode, meaning that we may read from and writeVOCABULECTOR: A LANGUAGE-LEARNING TOOL129to the file using the same File object dict file. A new file is createdif it does not exist already.Each line in the trans.txt file contains a pair of words separated bya colon, for instance, cheese:gazta.
Since the application deals withforeign words, we have to encode Unicode strings properly in UTF-8before writing them to a file. Correspondingly, the UTF-8-encoded linesthat are read from the file must be decoded back to Unicode strings. Thecore data structure is the list trans that contains the list of native–foreignword pairs, saved in tuples.The function add entry() is invoked from the application menu.It asks for a new native word and the corresponding word in a foreignlanguage.
The new pair is then appended to the file using the dict filefile object. If the user can provide a sound or a photo to accompany thenew word, these files are named according to the native word by addinga .jpg or a .wav extension to the base file name, fname.Recording sound is a straightforward operation, as can be seen inthe function record sound(). Taking a photo is not much moredifficult: once the camera.start finder() call has switched theviewfinder on, the function viewfinder() starts showing frames fromthe camera on the canvas. We also bind the select key to the functiontake photo().
Once the key is pressed and take photo() is called,we unbind the key so that only one photo can be taken at a time.6.5.2 Showing Entries from VocabulectorExample 52: Vocabulector (2/3)def show_native():lst = []for native, foreign in trans:lst.append(native)idx = appuifw.selection_list(choices = lst, search_field = 1)if idx != None:foreign = trans[idx][1]fname = PATH + lst[idx]show(fname, foreign)def show_foreign():lst = []for native, foreign in trans:lst.append(foreign)idx = appuifw.selection_list(choices = lst, search_field = 1)if idx != None:native = trans[idx][0]fname = PATH + nativeshow(fname, native)def show(fname, translation):global photo, text, sndphoto = None130DATA HANDLINGtext = translationtry:photo = graphics.Image.open(fname + ".jpg")except: passhandle_redraw(None)try:snd = audio.Sound.open(fname + ".wav")snd.play()except: passThe functions show native() and show foreign() are invokedfrom the application menu.
The functions share a similar structure: theychoose either the first or the second item, that is, either a native or aforeign word from the list of word pairs to show a list of words in aselection list dialog. Once the user has chosen a word from the list,the function show() is called; it loads the photo, plays the sound andrequests the translation to be displayed on screen. Since both the photoand the sound may be missing, we ignore any exceptions that may occurwhile the files are being loaded.In Python, statement pass means ‘no operation’. It is a syntacticplaceholder saying that we have deliberately decided to do nothing if anexception occurs during loading photos or sounds. You could replace thepass statements with appropriate error messages, for example.6.5.3 Vocabulector Boilerplate TextExample 53: Vocabulector (3/3)def handle_redraw(rect):canvas.clear((255, 255, 255))w, h = canvas.sizeif photo:canvas.blit(photo, target = (0, 0, w, int(0.75 * h)), scale = 1)if text:canvas.text((20, h / 2), text, fill = (0, 0, 255), font = "title")def quit():dict_file.close()app_lock.signal()photo = Nonetext = u"<vocabulector>"appuifw.app.title = u"Personal vocabulary trainer"appuifw.app.exit_key_handler = quitappuifw.app.screen = "large"canvas = appuifw.Canvas(redraw_callback = handle_redraw)appuifw.app.body = canvasappuifw.app.menu = [(u"Add entry", add_entry),(u"Show native entries", show_native),(u"Show foreign entries", show_foreign)]SUMMARY131load_translations()app_lock = e32.Ao_lock()app_lock.wait()Similar to previous examples that have used the Canvas object, such asthe UFO Zapper in Section 5.5, the drawing operations are handled in theredraw callback function handle redraw().