Python Workbook v1 0 En

83
 Developer Training BO-EN-Python-20080613.pdf Python Workbook

description

Workbook intended for those learning and teaching the Python programming language.

Transcript of Python Workbook v1 0 En

Page 1: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 1/83

 

Developer

Training

BO-EN-Python-20080613.pdf 

Python

Workbook

Page 2: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 2/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

2BO-EN-Python-20080613.pdf 

Page 3: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 3/83

 

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

3BO-EN-Python-20080613.pdf 

[NOKIA OFFICAL COURSEWARE]

IMPORTANT: READ CAREFULLY BEFORE INSTALLING, DOWNLOADING, OR USING THE

NOKIA OFFICIAL COURSEWARE

NOKIA END-USER AGREEMENT

This Nokia End-User Agreement (this “Agreement”) is between “You” the end user (either an indi-vidual or an entity), and Nokia Corp. (“Nokia”), the licensor. This Agreement authorizes You to usethe Nokia Official Courseware specified in Section 1 below, which may be stored on a CD-ROM, sentto You by electronic mail or in written hardcopy, or downloaded from Nokia’s Webpages or Serversor from other sourcesunder theterms andconditionsset forth below.If You downloadthe Softwarefrom a CD-ROM, then thelegal notices andany other terms stated therein(collectively,the “CD-ROMTerms”) shall be incorporated into and made a part of this Agreement by this reference and alsogovern yourdownloadingand usageof the Nokia Official Courseware. This is an agreement providincertain limited end-user rights and notan agreementfor sale. Nokia continues to own thecopyof the Nokia Official Courseware You receive and the physical media contained in any package andany other copy that You are authorized to make pursuant to this Agreement.

Read this Agreement carefully before installing, downloading, or using the Nokia OfficialCourseware.  By breaking the seal of the Nokia Official Courseware packet or clicking on the “I Accept” button while installing, downloading, and/or using the Software, You agree to the terms andconditions of this Agreement.  If You do not agree to all of the terms and conditions of this Agreement, promptly click the “Decline” or “I Do Not Accept” button, cancel the installation or downloading, or destroy or return the Nokia Official Courseware and accompanying documentation to Nokia.YOU AGREE THAT YOUR USE OF THE NOKIA OFFICIAL COURSEWARE CONSTITUTES YOUR ACKNOWLEDGE-MENT THAT YOU HAVE READ THIS AGREEMENT, UNDERSTAND IT, AND AGREE TO BE BOUND BY ITS TERMAND CONDITIONS.

1. NOKIA OFFICIAL COURSEWARE.  As used in this Agreement, the term “Nokia OfficialCourseware” means, collectively: (i) any software identified in this Agreement, (ii) all contents of thdisk(s), CD-ROM(s), electronic mail and its file attachments, or other media with which thisAgreement is provided, includingthe objectcodeformof thesoftware delivered via a CD-ROM, elec-tronic mail, or Web page, (iii) digital images, video presentations, stock photographs, clip art, or othartistic works (“Stock Files”), (iv) related explanatory written materials and any other possible

documentationrelated thereto (“Documentation”), (v) fonts, (vi)upgrades, modified versions,updates, additions, and copies of the Nokia OfficialCourseware (collectively “Updates”), if any, and(vii) any printed or written materials provided and licensed to You by Nokia under this Agreement.

2. END-USER RIGHTS AND USE. Nokia grants to You a non-exclusive, royalty-free, non-transferableright to use and install the Nokia Official Courseware on the local hard disk(s) or other permanenstorage media of one computer and use the Nokia Official Courseware on a single computer or terminal at a time.

3. LIMITATIONS ON END-USER RIGHTS. You may not copy, distribute, or make derivative works of the Nokia Official Courseware, except as follows:

(a)You maymake onecopyof theNokia OfficialCourseware on magnetic media as an archival

backup copy,provided Your archival backup copy is notinstalled or usedon anycomputer. Any

other copies You make of the Nokia Official Courseware are in violation of this Agreement.

(b)You may notuse, modify, translate, reproduce,or transfer theright touse theNokia Official

Courseware or copy the Nokia Official Courseware except as expressly provided in this Agree-

ment.

(c) You may not distribute, sale, resell, sublicense, rent, lease, or lend the Nokia OfficialCourseware or any part thereof.

(d) You may not reverse engineer, reverse compile, disassemble, or otherwise attempt to dis-

cover the source code of any software included in the Nokia Official Courseware (except to the

extent that this restriction is expressly prohibited by law) or create derivative works based on

the Nokia Official Courseware.

(e) Unless stated otherwise in the Documentation, You shall not display, modify, reproduce, or

distribute any of the Stock Files included with the Nokia Official Courseware. In the event that t

Documentation allows You to display the Stock Files, You shall not distribute the StockFiles on a stand-alone basis, i.e., in circumstances in which the Stock Files constitute the pri-

mary value of the product being distributed. You should review the ”Readme” files associated

with the Stock Files that You use to ascertain what rights You havewith respect to such mate-

Page 4: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 4/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

4BO-EN-Python-20080613.pdf 

rials. Stock Files may not be used in the production of libelous, defamatory, fraudulent, infring-

ing, lewd, obscene, or pornographic material or in any otherwise illegal manner. You may not

register or claim any rights in the Stock Files or derivative works thereof.

(f) You may not use the Nokia Official Courseware to provide training in any class that is not acertified Nokia Course.

(g) You agree that You shall only use the Nokia Official Courseware in a manner that complieswith all applicable laws in the jurisdiction in which You use the Nokia Official Courseware, includ-

ing, but not limited to, applicable restrictions concerning copyright and other intellectual prop-

erty rights.

4. COPYRIGHT.  The Nokia Official Courseware and all rights, without limitation including proprietaryrightstherein,are owned by Nokia and/or its licensorsand affiliates andare protected by internationaltreaty provisions and all other applicable national laws of the country in which it is being used. Thestructure, organization, and code of the Nokia Official Courseware are the valuable trade secrets andconfidential information of Nokia and/or its licensors and affiliates. You must not copy the Nokia Offi-cial Courseware, except as set forth in Section 3 above. Any copies that You are permitted to make pur-suant to this Agreement must contain the same copyright and other proprietary notices that appearon the Nokia Official Courseware.

5. MULTIPLE ENVIRONMENT SOFTWARE / MULTIPLE LANGUAGE SOFTWARE / DUALMEDIA SOFTWARE/ MULTIPLE COPIES / UPDATES. If the Nokia Official Courseware supports multiple platforms or lan-

guages, or if You receive the Nokia Official Courseware on multiple media, or if You otherwise receivemultiplecopies of theNokia OfficialCourseware, thenumber of computers on which all versionsof theNokia Official Courseware are installed shall be one computer. You may not rent, lease, sublicense,lend, or transfer versions or copies of the Nokia Official Courseware that You do not use. If the NokiaOfficial Courseware is an Update to a previous version of the Nokia OfficialCourseware, You must pos-sess valid end-user rights to such a previous version in order to use the Update, and You may use theprevious versionfor ninety (90)days after You receivetheUpdatein order to assist You in thetransitionto the Update. After such time, You no longer have a right to use the previous version, except for thesole purpose of enabling You to install the Update.

6. THIRD-PARTY LINKS. THE NOKIA OFFICIAL COURSEWARE MAY INCLUDELINKS TO THIRD PARTY SITES.SUCH LINKED SITES, IF ANY, ARE NOT UNDER THE CONTROL OF NOKIA AND NOKIA IS NOT RESPONSIBLEFOR THEIR CONTENTS, OR ANY OTHER LINK CONTAINED THEREIN. NOKIA PROVIDES THESE LINKS ONLY ASA CONVENIENCE, AND THE INCLUSION OF ANY SUCH LINK DOES NOT IMPLY ENDORSEMENT BY NOKIA OFTHE LINKED SITE.

7. COMMENCEMENT & TERMINATION.  This Agreement is effective from the first date that You installthe Nokia Official Courseware. You may terminate this Agreement at any time by permanently deletindestroying, and returning, at Your own costs, the Nokia OfficialCourseware, all backup copies, and allrelated materials provided by Nokia. Your end-user rights automatically and immediately terminatewithout notice from Nokia if You fail to comply with any provision of this Agreement. In such an event,You mustimmediately delete,destroy, or returnat Your owncost, theNokia OfficialCourseware, all backcopies, and all related material to Nokia.

8. NO WARRANTY.  YOU ACKNOWLEDGE THAT THE NOKIA OFFICIAL COURSEWARE IS PROVIDED “AS IS”WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, AND TO THE MAXIMUM EXTENT PERMITTED BYAPPLICABLE LAW, NEITHER NOKIA, ITS LICENSORS OR AFFILIATES, NOR THE COPYRIGHT HOLDERS MAKE ANREPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THEWARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE OR THAT THE SOFTWARE WINOT INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS, TRADEMARKS, OR OTHER RIGHTS. THE NOKIAOFFICIAL COURSEWARE, ANY DOCUMENTATION AND OTHER INFORMATION, MAY INCLUDE TECHNI- CALINACCURACIES OR TYPOGRAPHICAL ERRORS. THERE IS NO WARRANTY BY NOKIA OR BY ANY OTHER PARTYTHAT THEOPERATIONOF ANY SOFTWARE INCLUDEDWITH THENOKIA OFFICIALCOURSEWAREWILL BE

UNINTERRUPTED OR ERROR-FREE.

9. NO OTHER OBLIGATIONS. This Agreementcreates no obligationson thepartof Nokia otherthanasspecifically set forth herein.

10. LIMITATION OF LIABILITY. TOTHE MAXIMUM EXTENTPERMITTEDBY APPLICABLE LAW,IN NOEVENTSHALL NOKIA, ITS EMPLOYEES OR LICENSORS OR AFFILIATES BE LIABLE FOR ANY LOST PROFITS, REVENUE,SALES, DATA, OR COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, PROPERTY DAMAGE, PER-SONAL INJURY, INTERRUPTION OF BUSINESS, LOSS OF BUSINESS INFORMATION, OR FOR ANY SPECIAL, DI-RECT, INDIRECT, INCIDENTAL, ECONOMIC, COVER, PUNITIVE, SPECIAL, OR CONSEQUENTIAL DAMAGES,HOWEVER CAUSED AND WHETHER ARISING UNDER CONTRACT, TORT, NEGLIGENCE, OR OTHER THEORY OFLIABILITY ARISING OUT OF THE USE OF OR INABILITY TO USE THE SOFTWARE, EVEN IF NOKIA OR ITS LI-CENSORS OR AFFILIATES ARE ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. BECAUSE SOME COUN-TRIES/STATES/JURISDICTIONS DO NOT ALLOW THE EXCLUSION OF LIABILITY, BUT MAY ALLOW LIABILITYTO BE LIMITED, IN SUCH CASES, NOKIA, ITS EMPLOYEES OR LICENSORS OR AFFILIATES' LIABILITY SHALL BELIMITED TO U.S. $50.

Nothing contained in this Agreementshall prejudice thestatutory rights of anyparty dealing as a con-sumer. Nothing contained in thisAgreement limits Nokia's liability to You in the event of death or per-

Page 5: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 5/83

 

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

5BO-EN-Python-20080613.pdf 

sonal injury resulting from Nokia's negligence. Nokia is acting on behalf of itsemployeesand licensorsor affiliates for the purpose of disclaiming, excluding, and/or restricting obligations, warranties, andliability as provided in this Section 9, but in no other respects and for no other purpose.

11. TECHNICAL SUPPORT.  Nokia has no obligation to furnish You with technical support unless sepa-rately agreed to in writing between You and Nokia.

12. EXPORT CONTROL. The Nokia OfficialCoursewareand any relatedtechnicaldataare subject to the

U.S. Export AdministrationRegulationsand may be subject to exportand import laws, regulationsandrestrictions in other countries. You agree to comply strictly with all such laws, regulations and restric-tions, andacknowledgethatYou have theresponsibility to obtainany licenses or permits that mayberequired to import, use, export, re-export, or otherwise transfer or disclose of the Nokia OfficialCourseware or any related technical data. You further agree that except as authorized, You shall notexport, re-export or otherwise transfer or disclose of the Nokia Official Courseware, the related technicaldata or any direct product thereof; (i) into any country embargoed by U.S. export regulations; (ii)to entities controlled by such countries or to nationals of such countries; or (iii) to anyone on the U.S.Treasury Department's list of Specially Designated Nationals and Blocked Persons, the U.S. CommerceDepartment's Denied Persons List, or the U.S. Commerce Department's Entity List.

13. NOTICES. Allnoticesand return of theNokia Official Coursewareand Documentation shouldbe de-livered to:

NOKIA CORPORATIONForum Nokia Developer Training

Keilalahdentie 4FIN-02150 EspooFinlandAttn: Tero Putkonen

14.  GOVERNING LAW, ARBITRATION & GENERAL PROVISIONS.

This Agreement shallbe governed by and construed in accordance with the laws of Finland withoutregard to its conflicts of laws rules.Any disputes relating to or arising in connection with this Agreement shall be finally settled

in arbitration. The arbitrator is to be appointed by the Arbitration Committee of the Central

Chamber of Commerce of Finland and the rules of the said Committee are to be followed in

the arbitration. The award shall be final and binding and enforceable in any court of 

competent jurisdiction.

The arbitration shall be held in Helsinki, Finland, in English language.

The parties undertake and agree that all arbitral proceedings conducted with reference

to this Agreement shall be kept strictly confidential and all information disclosed in the

course of such arbitral proceeding shall be used solely for the purpose of those proceedings.

Notwithstanding the foregoing, nothing in this Agreement shall be deemed to limit the

Parties' rights to seek interim injunctive relief or to enforce an arbitration award in any court

of law.

If any part of this Agreement is found void and unenforceable, it will not affect the validity of the bal-ance of the Agreement, which shall remain valid and enforceable according to its terms. This Agree-ment may be modified only in writing by an authorized officer of Nokia.

This is the entire agreement between Nokia and You relating to the Nokia Official Courseware, and it

supersedes any prior representations, discussions, undertakings, end-user agreements, communica-tions, or advertising relating to the Nokia Official Courseware.

PLEASE SUBMIT ANY ACCOMPANYING REGISTRATION FORMS TO RECEIVE REGISTRATION BENEFITS WHEREAPPLICABLE.

*** End ***

Page 6: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 6/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

6BO-EN-Python-20080613.pdf 

Page 7: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 7/83

 

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

7BO-EN-Python-20080613.pdf 

About This Course

Course Description

This course introduces object-oriented programming language Python.

AudienceThe readers of this document are Python developers who want to use Python in the S60

platform. This is not a generic Python course, so, for example, Python use in unit testing is

not covered.

Student PrerequisitesBefore attending this course, students must have:

Object-oriented programming experience with some programming language (C++,

 Java™);

Helpful: generic Symbian and S60 knowledge.

Course ObjectivesAt the end of the course, students will be able to do the following:

Write Python programs;

Deploy and test programs;

Use Python S60 APIs.

Page 8: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 8/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

8BO-EN-Python-20080613.pdf 

Document ConventionsThe following conventions are used in course materials to distinguish elements of the

text.

Convention Use

Courier10 BT Represents code samples or examples of screen text.

| Separates an either/or choice in syntax statements.

[ ] Encloses optional items in syntax statements. Type only the in-

formation within the brackets, not the brackets themselves.

... Represents an omitted portion of code sample.

{ } Encloses required items in syntax statements. Type only the in-

formation within the brackets, not the brackets themselves.

// Distinguishes short comments in C++ and Java programmingcode.

//

//

Distinguishes long comments in C++ and Java programming

code.

/* ... */ Distinguishes long comments in C, C++ and Java programming

code.

<!-- ... --> Distinguishes comments in HTML, WAP & XHTML programming

code.

Page 9: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 9/83

 

Developer

Training

BO-EN-Python-20080613.pdf 

Python

Contents

Introduction ............................................................12

What is Python?..............................................................................................................13Why Python?.....................................................................................................................14

Design tradeoffs..............................................................................................................15

Growing user base........................................................................................................16

Common Use Cases for Python..........................................................................17

Development environment..................................................................................18

Interactive prompt.......................................................................................................19

Hello world..........................................................................................................................20

Python on S60 .........................................................21S60 Python (PyS60) consists of..........................................................................22

Deployment of Python programs (1/2)......................................................23

Deployment of Python programs (2/2)......................................................24

S60 Python compatibility.......................................................................................25

Active object – Framework (1/2).......................................................................26Active object – Framework (2/2).......................................................................27

Application Framework...........................................................................................28

S60 screen components in Python.................................................................29

Forms ......................................................................................................................................30

Listboxes...............................................................................................................................31

Dialogs....................................................................................................................................32

Text............................................................................................................................................33

Canvas (1/2) .......................................................................................................................34

Canvas (2/2) .......................................................................................................................35

Programming Basics .............................................36Standard types.................................................................................................................37

Strings.....................................................................................................................................38

Indexing and slicing....................................................................................................39

Lists............................................................................................................................................40

Tuples......................................................................................................................................41

Dictionaries.........................................................................................................................42

Control flow........................................................................................................................43

For loop..................................................................................................................................44

if-then-else..........................................................................................................................45

Functions..............................................................................................................................46

Object model.....................................................................................................................47

Classes 101..........................................................................................................................48

Modules .................................................................................................................................49

File manipulation..........................................................................................................50

Files and directories.....................................................................................................51

Standard library miscellanea...............................................................................52

Other useful functionality.......................................................................................53

Page 10: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 10/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

10BO-EN-Python-20080613.pdf 

Zen of Python....................................................................................................................54

Documenting code ......................................................................................................55

Unit testing..........................................................................................................................56

Exception handling......................................................................................................57

Reserved Keywords .....................................................................................................58

Python S60 Modules.............................................. 59Camera Module...............................................................................................................60

Audio Module....................................................................................................................61

Databases.............................................................................................................................62

Databases - Contacts ...................................................................................................63

Databases - Calendar...................................................................................................64

Other modules 1/2.......................................................................................................65

Other modules 2/2.......................................................................................................66

Python Sockets ....................................................... 67Socket programming 1/4.......................................................................................68

Socket programming 2/4.......................................................................................69

Sockets: Bluetooth 3/4 ..............................................................................................70

Sockets 4/4..........................................................................................................................71

Course summary ....................................................72

Thank you. ............................................................... 73

Page 11: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 11/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

11BO-EN-Python-20080613.pdf 

Page 12: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 12/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

12BO-EN-Python-20080613.pdf 

Introduction

Page 13: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 13/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

13BO-EN-Python-20080613.pdf 

What is Python?

Python is a general purpose programming language optimized for rapid application

 prototyping. Because it is interpreted during execution, it is not as fast as compiled

languages such as C++.

Page 14: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 14/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

14BO-EN-Python-20080613.pdf 

Why Python?

The number of Python users is growing. It is portable and extensible, it integrates well with

native C++ environments, it has large standard library with ready made functions, and still

it is easy to learn and use.

SWIG (Simplified Wrapper and Interface Generator) allows a developer to wrap C/C++

functions for use with scripting languages, such as Python.

BoostPython is a C++ library which enables seamless interoperability between C++ and the

Python programming language.

Page 15: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 15/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

15BO-EN-Python-20080613.pdf 

Design tradeoffs

Page 16: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 16/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

16BO-EN-Python-20080613.pdf 

Growing user base

Page 17: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 17/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

17BO-EN-Python-20080613.pdf 

Common Use Cases for Python

Page 18: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 18/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

18BO-EN-Python-20080613.pdf 

Development environment

Page 19: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 19/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

19BO-EN-Python-20080613.pdf 

Interactive prompt

Running bt_console is easy from S60 and is recommend way to test out functions.

Do not use it for lengthy programs that might crash the console since all your data is lost if 

console raises exception

Another way of stepping through the code is pdb.py (Python debugger supplied with

 python S60 package).

Page 20: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 20/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

20BO-EN-Python-20080613.pdf 

Hello world

Let’s see if the environment has been set up correctly by creating helloworld program and

running it on both PC and S60.

All PCs need a bluetooth module (eighter built-in or dongle) that is used for connecting PC

to Mobile phone.

Page 21: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 21/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

21BO-EN-Python-20080613.pdf 

Python on S60

Page 22: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 22/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

22BO-EN-Python-20080613.pdf 

S60 Python (PyS60) consists of 

When developing python scripts they are run under the execution environment, but it is

 possible to create ’stand-alone’ python scripts using py2sis – utility program to make

symbian installation files (.SIS) that have a generated application executable which enablesusers to start python scripts without explicitly starting the execution environment. This way

Python scripts appear to users like any other application in the system and the user is

unaware that the implementation was made with Python.

Python installer will allow .py modules to be installed as runnable scripts or as libraries that

can be imported for use of other scripts.

Page 23: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 23/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

23BO-EN-Python-20080613.pdf 

Deployment of Python programs (1/2)

Page 24: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 24/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

24BO-EN-Python-20080613.pdf 

Deployment of Python programs (2/2)

Page 25: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 25/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

25BO-EN-Python-20080613.pdf 

S60 Python compatibility

Page 26: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 26/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

26BO-EN-Python-20080613.pdf 

Active object – Framework (1/2)

Consult S60 SDK documentation on how active objects and the active framework works on

Symbian.

Page 27: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 27/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

27BO-EN-Python-20080613.pdf 

Active object – Framework (2/2)

Page 28: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 28/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

28BO-EN-Python-20080613.pdf 

Application Framework

If you want to create a non – application framework python scripts (servers) you can call

e32.start_server at the start of your program.

Page 29: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 29/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

29BO-EN-Python-20080613.pdf 

S60 screen components in Python

Above image is normal view of S60 applications.

Python supports three different view types for applications: normal, large and full screen.

To set another view type, programmer can set appuifw.app.screensize = ’normal’ ; ’large’ ;

’full’

Please note that the application icon cannot be set from Python. Only the Py2sis – utility

allows you to do this.

Page 30: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 30/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

30BO-EN-Python-20080613.pdf 

Forms

Page 31: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 31/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

31BO-EN-Python-20080613.pdf 

Listboxes

To load icons you need to use Icon class that can read Symbian multibitmap format

(.MBM).

Check the python API reference to see how to do this.

Page 32: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 32/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

32BO-EN-Python-20080613.pdf 

Dialogs

Page 33: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 33/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

33BO-EN-Python-20080613.pdf 

Text

Page 34: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 34/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

34BO-EN-Python-20080613.pdf 

Canvas (1/2)

Page 35: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 35/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

35BO-EN-Python-20080613.pdf 

Canvas (2/2)

Page 36: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 36/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

36BO-EN-Python-20080613.pdf 

Programming Basics

Page 37: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 37/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

37BO-EN-Python-20080613.pdf 

Standard types

Arbitary size integers is a speciality of Python. E.g. in C++ the int is limited to current

machine word of your environment (32bit in ARM).

In Python the following initialization is allowed and perfectly valid but would overflow in

many other programming environments

fiveBillion = 5*10**9

oneMillion = 1*10**6

 print fiveBillion *oneMillion

Division operation on integer depends on the Python version. Check the documentation to

see wheter you should use / - operator or // - operator.

Page 38: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 38/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

38BO-EN-Python-20080613.pdf 

Strings

Multiline spanning is widely used when commenting code. See extra slides about

commenting and documenting conventions with Python.

Page 39: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 39/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

39BO-EN-Python-20080613.pdf 

Indexing and slicing

Slicing and indexing is a very powerful tool when extracting data out of strings, though

strings are not the only data type that supports it; slice can be used to extract

data out from any kind of list like structure (string/list/tuple).

For more powerful string handling look up Regular Expression module from the

documentation.

Page 40: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 40/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

40BO-EN-Python-20080613.pdf 

Lists

The difference between append and extend might not be clear at first. Consider the

following example:

myList = [1,2,3]

myList.append([4,5]) #appends a single element

myList.extend([6,7]) #extends with elements

 print myList

[1, 2, 3, [4, 5], 6, 7]

Page 41: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 41/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

41BO-EN-Python-20080613.pdf 

Tuples

Examples of structure:

#simple tuple containing 4 members

a = (1,2,3,4) print a

(1,2,3,4)

# tuple containing tuple a and a string ’hello’

 b = (a,’hello’)

 print b

((1,2,3,4),’hello’)

#tuple containing two arrays

c = ([1,2],[3,4])

 print c[1]

(3,4)

#unpacking a tuple

d,e = c

 print d[1,2]

# tuples are immutable hence trying to assign to it

c[1] = 5 # creates runtime error 

Page 42: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 42/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

42BO-EN-Python-20080613.pdf 

Dictionaries

Page 43: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 43/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

43BO-EN-Python-20080613.pdf 

Control flow

Python does not use braces to create a block scope, instead Python uses indentation to

create a code block.

Indentation is normally done with whitespaces and you should set your editor to insert

spaces instead of TAB delimitters to ensure portability of your code.

Indentation is four spaces to create a new code block.

It has been widely argued whether this is a good or bad desicion, use it for a while and you

will not even notice the ’missing’ braces.

 Note that no delimitter is required to end command, for example ,no ’;’ at the end of line.

Python command sequences end in whitespace or in explicit delimitter of ’;’.

Commands expanding to multiple lines end with newline char.

Page 44: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 44/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

44BO-EN-Python-20080613.pdf 

For loop

How to read for loops in Python: ”for every element in the list do the following lines”

for fruit in fruits:

 print ”is ” + fruit + ” ripe?”

Page 45: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 45/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

45BO-EN-Python-20080613.pdf 

if-then-else

Page 46: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 46/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

46BO-EN-Python-20080613.pdf 

Functions

Here you see the problem of late binding, since the functions can take any type of object as

a parameter 

the environment cannot do type checking until the command is being executed.

Only way environment can handle such situations is to raise TypeError – exception.

Object model and late binding is discussed in the following slides.

Page 47: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 47/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

47BO-EN-Python-20080613.pdf 

Object model

Python is a full object language - even the built-in types are objects and what programmer 

sees are instances of these objects.

#Easy example to prove that integer is truely an object

dir(1) # will output all the functions of integer object type

Page 48: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 48/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

48BO-EN-Python-20080613.pdf 

Classes 101

def __init__(self)

Defines the constructor for class. Unlike C++ you cannot overload constructor definitions.

Only single definition can be defined.

def __call__(self)

Makes this a callable object, if the object has been constructed and user calls the Function

operator again it will lead to calling of this function.

There are various useful standard functions to be implemented for a class. One very nice is

the string representation of the class:

def __str__(self):

return ”this is how I like to be printed out”

Page 49: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 49/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

49BO-EN-Python-20080613.pdf 

Modules

Page 50: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 50/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

50BO-EN-Python-20080613.pdf 

File manipulation

Page 51: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 51/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

51BO-EN-Python-20080613.pdf 

Files and directories

Page 52: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 52/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

52BO-EN-Python-20080613.pdf 

Standard library miscellanea

Page 53: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 53/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

53BO-EN-Python-20080613.pdf 

Other useful functionality

Iterators and generators seem like a complex idea but provide an easy solution for doing

loops and avoiding return values being lost into void.

Example:

def fruitsInFreeze():

fruits = ['apple','orange','banana']

for f in fruits:

yield f 

for fruit in fruitsinFreeze():

 print fruit

apple

orange

 banana

Page 54: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 54/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

54BO-EN-Python-20080613.pdf 

Zen of Python

Page 55: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 55/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

55BO-EN-Python-20080613.pdf 

Documenting code

Page 56: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 56/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

56BO-EN-Python-20080613.pdf 

Unit testing

Page 57: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 57/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

57BO-EN-Python-20080613.pdf 

Exception handling

Page 58: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 58/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

58BO-EN-Python-20080613.pdf 

Reserved Keywords

Page 59: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 59/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

59BO-EN-Python-20080613.pdf 

Python S60 Modules

Page 60: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 60/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

60BO-EN-Python-20080613.pdf 

Camera Module

Page 61: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 61/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

61BO-EN-Python-20080613.pdf 

Audio Module

Page 62: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 62/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

62BO-EN-Python-20080613.pdf 

Databases

e32dbm databases deliver the promise of working in the same way as regular DBM Python

modules. You can even load anydbm in S60, and the e32 backend is automatically selected.

e32dbm is based on e32db, a SQL-based mini storage system supplied by Symbian OS.You can use either, depending on which semantic (DBM or SQL) is more convenient for 

you.

Page 63: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 63/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

63BO-EN-Python-20080613.pdf 

Databases - Contacts

Page 64: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 64/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

64BO-EN-Python-20080613.pdf 

Databases - Calendar

Page 65: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 65/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

65BO-EN-Python-20080613.pdf 

Other modules 1/2

Page 66: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 66/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

66BO-EN-Python-20080613.pdf 

Other modules 2/2

Page 67: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 67/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

67BO-EN-Python-20080613.pdf 

Python Sockets

Page 68: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 68/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

68BO-EN-Python-20080613.pdf 

Socket programming 1/4

Page 69: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 69/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

69BO-EN-Python-20080613.pdf 

Socket programming 2/4

Page 70: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 70/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

70BO-EN-Python-20080613.pdf 

Sockets: Bluetooth 3/4

Page 71: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 71/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

71BO-EN-Python-20080613.pdf 

Sockets 4/4

Page 72: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 72/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

72BO-EN-Python-20080613.pdf 

Course summary

Page 73: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 73/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

73BO-EN-Python-20080613.pdf 

Thank you.

Page 74: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 74/83

Page 75: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 75/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

75BO-EN-Python-20080613.pdf 

Lab XXXXX.cb1

Introduction

Title: HelloWorld

Overview: In the lab, the classic HelloWorld program is written in Python.

More importantly, students learn how to run Python programs in

the S60 emulator and device.

Objectives: To learn how to:

Edit and write Python programs

Run programs in the S60 emulator

Run programs in the S60 device

Compatible SDK(s): Nokia C++ S60 3rd Edition, Feature Pack 2 SDK

Starter Code

Provided:

No Solution Code

Provided:

Yes

Estimated Time To

Complete:

30 minutes

Lab Instructions

Exercise 1 – Preliminary steps

Task 1.1 – Checks

1. Write Python script, which prints “HelloWorld”on the screen using any available editor.

2. Copy the script to theC:\python folder (<Symbian installation

folder>\epoc32\winscw\c\python). If you do not know the path of the Symbian

installation folder, open a command prompt and type devices. After that, type in

devices –info@<default device id>. For example, below the installation path is

the same as root, i.e., C:\S60\devices\s60_3rd_fp2_SDK. Note that this course

assumes you are using the S60 3rd Edition SDK and Python S60 SDK 1.4.3. Go to

http://sourceforge.net for a list of the available Python SDKs and their compatibility withS60 SDKs.

Page 76: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 76/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

76BO-EN-Python-20080613.pdf 

Task 1.2 – Running in the emulator

1. Launch your emulator either from the Start menu or in the command prompt by typingepoc. Launch the Python interpreter from Installations.

2. After starting Python script shell, open the application menu using the left softkey and

execute Run script. Select your HelloWorld to be executed. The solution folder also

contains a GUI version of HelloWorld, which will be studied later.

Page 77: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 77/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

77BO-EN-Python-20080613.pdf 

Task 1.3 – Running in the device

1. If you are using S60 3rd Edition, you can use Ensymble developer utilities to create and

sign a .SIS file (Symbian Installation System). The .SIS package allows you to install the

Python script file to a mobile device. For details on how to create a .SIS package, go to

www.nbl.fi.

2. If you are using S60 2nd Edition, create a .SIS installation package with the py2sis tool,

which is part of the Python SDK. The tool creates a standalone Python application that

can be executed in the device like a native application. Read the *readme*.txt files for

help; they are located in epoc32\tools\py2sis folder.

3. Transfer and install the created .SIS package to your mobile device.

4. Execute your script in the phone.

Page 78: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 78/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

78BO-EN-Python-20080613.pdf 

Lab XXXXX.cb12

IntroductionTitle: Phonebook application

Overview: This lab is our first GUI Python program. In addition, some S60

Python module APIs are used.

Objectives: To learn how to:

Use the S60 application framework

Use S60 Python module APIs

Compatible SDK(s): Nokia C++ S60 3rd Edition

Starter CodeProvided:

No Solution CodeProvided:

Yes

Estimated Time To

Complete:

45 minutes

Lab Instructions

Exercise 2 – Phonebook application

Task 2.1 – S60 application framework

1. The solution folder contains UiTest.py script, which shows examples of how to use S60

GUI controls. Refer to this folder for guidance, if needed.

2. Import the application framework and e32 modules to your program.

3. Yield execution to any outstanding active objects.

4. Lock the AO scheduling.

5. Define a menu. You may have for example:

a. Dial functionality;

b. SMS functionality;

c. About functionality.

6. At this point, define the empty functions for your menu.

7. Define a listbox, which shows a list of names. Define also the list yourself.

d. You need a listbox observer for listbox. Use any function as an observer. The

function does not have to do anything so you may implement is by writing just

pass in function defitnion.

8. Add the listbox to your UI framework.

9. Store the previous application title and define a new title. In the same way, store theprevious exit handler and replace that with a new one.

Page 79: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 79/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

79BO-EN-Python-20080613.pdf 

e. In the new exit handler, restore the old exit handler and application title;

f. Call signal() for the AO lock.

10. Call wait() for the AO lock.

11. Run and test. You cannot send messages or make phone calls yet.

Task 2.2 – Calling module APIs1. Now implement Dial, SMS, and About functionality:

a. Dial: Call dial() in the telephone. The number is the current list box number.

b. SMS: Query the message content from the user. Send the message to the current

listbox number.

c. About: Print the information data or your application.

2. Test your program.

Page 80: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 80/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

80BO-EN-Python-20080613.pdf 

Lab XXXXX.cb13

IntroductionTitle: Word guessing game

Overview: The game implemented in this exercise introduces basic Python

programming issues; there is no emphasis on GUI yet.

Objectives: To learn how to:

Use Python types

Use Python programming structures

Use modules and types from modules

Compatible SDK(s): Nokia C++ S60 3rd EditionStarter Code

Provided:

No Solution Code

Provided:

Yes

Estimated Time To

Complete:

45 minutes

Lab Instructions

Exercise 3 – Game implementation

Task 3.1 – Modules, types, programming structures

1. Import Random from the random module.

2. Define an empty list of words, which a player tries to guess.

3. In the main function, occupy the word list with some elements (strings).

4. Select a random word to be the solution.

5. Define a hint string that is shown to the user. Initially, it can show the first character of 

the solution.

6. Define a loop, where as long as the solution length has not been exceeded, the following

will occur:

g. A hint is printed;

h. The player inputs a guess character;

i. The correctness of the guess is checked. Before the check, the guess is converted

to lowercase characters.

 j. If the answer is incorrect, the next hint character is provided.

Page 81: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 81/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

81BO-EN-Python-20080613.pdf 

Task 3.2 – Running

1. Run and test your script.

Page 82: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 82/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

82BO-EN-Python-20080613.pdf 

Lab XXXXX.cb1

Introduction

Title: Bluetooth sockets

Overview: More complicated functionality will be added to the exercise.

Objectives: To learn how to:

Use S60 sockets

Use Bluetooth sockets

Compatible SDK(s): Nokia C++ S60 3rd Edition SDK

Starter Code

Provided:

No Solution Code

Provided:

Yes

Estimated Time To

Complete:

45 minutes

Lab Instructions

Exercise 4 – Bluetooth sockets

Task 4.1 – Sockets programming1. Use the UI framework skeleton from the previous exercise.

2. You need four functions: connect(), recv(), send(), and close().

3. Connect()

a. Create a streaming BT socket.

b. Use the device and service discovery protocol to find devices and services.

c. Print out information about found devices and services.

d. If any services are found, create a choicelist. Sort the services and create a pop-

up menu, where you show the sorted list of available services.

e. Define the host to be the device selected by the user.

f. Try to connect to the selected device (and service).

4. Recv()

g. Just pass.

5. Send()

h. Print the message that the data is sent.

i. Send the data to the socket.

6. Close()

 j. Close the socket.

k. Close the application.

Page 83: Python Workbook v1 0 En

5/14/2018 Python Workbook v1 0 En - slidepdf.com

http://slidepdf.com/reader/full/python-workbook-v1-0-en 83/83

 

Python

© Nokia 2008. Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation

83BO-EN-Python-20080613.pdf 

7. Run and test. Start the Bluetooth service in your PC and use HyperTerminal to connect to

the Bluetooth COM port. Start the Python program in your mobile device and browse the

PC device.