Python is a language that allows you to write code by the brute-force method, as in the lines above, or in a more elegant way using concepts like iteration. If you were to define all 12 buttons in a linear fashion, you would need 26 lines of code. Using the Python for construct, you can accomplish the same task in a mere seven lines of code. That equates to less than one-third of the code for this simple example, but the difference would be substantial for a larger table. Here's the code in a more "Pythonic" way:
def create_table(self): self.table = gtk.Table(3,4,True) for row in range(3): for col in range(4): name = 'Twitter %i' % (row*4 + col + 1) button = gtk.Button(name) self.table.attach(button, col, col+1, row, row+1)
Keeping your code manageable is important when scripts start to get large. Python functions are a good way to break down your code into small, manageable pieces. It's also important to point out that everything in Python is an object. You can see this to some extent in the create_table function through the use of the self construct. The function create_table creates a gtk.Table and returns it as an object -- hence, the use of self to refer to the object being created. There is an abundance of resources on the Web if you're not familiar with object-oriented programming concepts.
Taking advantage of all the built-in language features and functions is another way to keep your source code manageable. Python has a module for reading and writing configuration files named ConfigParser. This module provides all the tools you need to save and read program configuration information. It supports different sections and creates a file of name-value pairs within each section. There are even individual methods to retrieve specific types, such as getboolean, getint, and getfloat.
If you can't find what you need in the Python standard library, chances are that someone else has already written what you need. A quick Google search typically returns multiple choices for a specific tool. Python-Twitter is a good example of a helper library to accomplish the heavy lifting of sending messages to the Twitter service. It's hosted on Google Code and even comes with several sample applications.
Testing and Debugging
You can test code on the Compal MID in several ways. File transfer over a USB port is drop-dead simple and works well. Optionally, you could attach a USB keyboard to the Compal MID and use the VI editor directly on the device for your editing and the Python interpreter for testing. This method works okay for small proof-of-concept efforts but gets out of control for anything but small, simple programs. Another, similar approach is to use Virtual Network Computing (VNC) to remotely view the screen on the device through your workstation.
Another way is to use the emulator approach. The Moblin project has a tool called the Moblin Image Creator (MIC) for building platform-specific images. With MIC you can also use the Xephyr emulator tool to launch an independent session for testing purposes. This method has the advantage of a rapid build/test cycle to help work the bugs out of your code in short order.
A final way might be to test the initial version of the software on your Linux desktop. The advantage is that you don't need MIC. The disadvantages are that you may need additional hardware for your desktop (GPS, for example) and that you can't test MID specific functionality (such as screen characteristics).
Don't get bogged down in the details too early in the process. It's important to completely flesh out your requirements in the beginning, then make some design decisions based on a clear picture of what you're trying to accomplish. Get comfortable with your development tools -- especially the debugging portion -- as you'll probably use them more than you think.
The easier it is for you to test and debug your code, the quicker you'll get it running.
Have a convenient way to transfer files to your device that doesn't require a lot of motion. This could be as simple as keeping an easily accessible USB cable plugged into your workstation. When you get down to squashing bugs, it helps to make the process as smooth and painless as possible, especially if you're editing all the code on a workstation and have to move it over to the device for testing.
Building a solid application for the MID platform requires the same set of disciplined steps you would use in any software project. Be sure you don't skip steps like these:
- Take your time on the design process, and consider alternatives.
- Think through the UI design from a user's perspective before you start coding.
- Write your code with testing in mind.
- Have a clear set of requirements that you can test.
- Use tools such as source code control, and check in your code frequently.
For More Information
- Dr. Dobb's MoblinZone
- Developing for Mobile Internet Devices: Part 1 Developing for Mobile Internet Devices: Part 2