Home arrow Python arrow Page 3 - Mobile Programming in Python using PyS60: Getting Started

Accessing Services, Step By Step - Python

Smart phones are becoming a common sight nowadays. Above all, the "smartness" of smart phones come from the fact that they can be programmed. In other words, one can develop custom applications for these mobile devices and use them in the devices. This article covers the basics of developing these kinds of applications in a variant of Python.

TABLE OF CONTENTS:
  1. Mobile Programming in Python using PyS60: Getting Started
  2. Dynamically loadable libraries or extensions
  3. Accessing Services, Step By Step
  4. PyS60 Programming in the Real World
By: A.P.Rajshekhar
Rating: starstarstarstarstar / 16
October 17, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

The ease of programming using Python reduces the steps required to access the different services. However, there are three steps that are always required. They are:

  1. Instantiating the object of the service.

  2. Setting the required properties.

  3. Calling the methods to access the required functionality.

These steps are generic to not only the S60 platform but also to any mobile platform, including embedded Linux, which is another mobile platform. I will use the sound module as an example for all the steps.

Instantiating the object of the service

Most of the time this is the first step in accessing the service. This step is essential when working with built-in extensions. However, for most of the dynamically loadable extensions, objects are not required as their functionalities can be directly accessed. The reason for this is that the methods of the dynamically loadable modules are static. For example, to create a text field (the name of text field component is query) the statements would be

input= query(u”Enter Text”,”Text”)

On the other hand, to use sound module to load a file, one must simply call the open method of the Sound class. The open method is a static, so the object of the Sound class is not required. The following statement opens a .wav file named test.wav.

Sound.open(“test.wav”)

 Setting the required attributes

The next step is to set up the attributes of the object instantiated. If the service can be accessed through static methods, then attributes can also be set or accessed through static mutators (setters) or accessors (getters). As with all the classes of Python, the attributes can be set at the time of instantiating the object. For example, selection_list can be used to show a list of items to the user, from which he or she can make his or her choice. The option to show a search field can be set at instantiation. The following statements display a list of fonts available on the device and let the user select one of the available fonts.

li =available_fonts()

sl=selection_list(li,1)

However, to set the volume of the Sound object one may simply call the set_volume() method, which is again a static method. The following statement sets the volume to 10.

Sound.set_volume(10)


Calling the methods to access the required functionality

The last step is to call the required methods on the objects to access the services. If the object is a UI component then the ‘service’ will either be displaying some value to the user or getting some value from the user. If it were a device-based service such as Sound, then accessing the functionality would mean requesting the platform to either play or stop the audio file. For example, the query component returns the value entered by the user after initialization. Therefore, the instantiation returns the value entered by the user. The returned value can be displayed using note object. The following statements accept a string from the user and display it to the user

data=entry(u”Enter your message:”, “text”)

note(u”Your message was: “+data, “info”)

If one needs to access the play service provided by Sound module, he or she has to just call the play() method of Sound class. The following statement just does that: 

Sound.play()

That completes the steps required to work with PyS60. One point to keep in mind is that the string being passed to the methods and constructors needs to be Unicode i.e. the string should be prefixed with “u” otherwise the string may not be displayed correctly. Next, I am going to develop a small application that would accept a path to a .wav file and then play it.



 
 
>>> More Python Articles          >>> More By A.P.Rajshekhar
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: