Adventures in Python

I used to do a fair bit of development, using languages like C++, Java and (ahem) COBOL. However I haven't in a while and the world has moved on a great deal.

So I thought I'd start refreshing my mind and learning a new language. Python seems a sensible choice, as its so heavily used in the Ubuntu world.

Day 1

Getting started

I read a post on Ask Ubuntu that suggested a number of free resources:

Quickly

Quickly is a rapid development system that uses templates. There's an introductory video here.

Hello World

The video above suggests a CouchDB application as a good place to start. So here's mine - not much different but it catalogues my OU courses.

I need to record:

  • Course title
  • Course code
  • Date started
  • Date finished
quickly create ubuntu-application ourecord

This creates the templates required for a PyGTK GUI application, in a folder called ourecord. If we go in to it and edit the GUI:

cd ourecord
quickly design

I removed the picture, that's where I'm putting the CouchDB grid.

quickly edit

I need a CouchDB grid, so I'm adding it to the def finish_initializing(self, builder) function in ourecord and I need to import the library:

from desktopcouch.records.couchgrid import CouchGrid

The code I added (commented) to def finish_initializing(self, builder)

        # Code for other initialization actions should be added here.
        """ Create the database and define it's key-value pairs """
        database = "ou"
        record_type = "http://wiki.lynxworks.eu/coding/python"
        keys = ["Course title", "Course code", "Start date", "End date"]
        self.couchgrid = CouchGrid (database, record_type=record_type, keys=keys)
        """ Show the couchgrid, make it editable, pack it and add a row """
        self.couchgrid.show()
        self.couchgrid.editable = True
        self.builder.get_object("vbox1").pack_end(self.couchgrid)
        self.couchgrid.append_row([]) 

You can run the application now:

quickly run

Questions?

So from this initial application, I had a few questions:

What's self?

It's equivalent to “this” in Java - it refers to the object itself.

What are the periods for?

They donate an object's methods - object.method().

Import statements?

There's two ways - the first prevents the name being reused and the second reduces typing.

Import MODULE

import module_name imports and creates reference in the current namespace, so you can use module_name.reference e.g.:

import birds
kestrel = birds.type("Bird of Prey")
From module_name Import *

This is the same, except it creates references in all public objects in the calling module, so you only need the reference:

from birds import *
kestrel - type("Bird of Prey")

CouchDB reference?

Day 2

Looking over yesterday, I realise I need to look at event handling.

Event handling

quickly design

I added a button and attached a signal, on_button1_clicked. It needs an associated function, which I added to OurecordWindow class.

quickly edit
    def on_button1_clicked(self, widget, data=None):
        self.couchgrid.append_row([])

This looks a bit crap and I want a button to remove a row as well. So back in Glade I add a toolbar, select it and edit. Under hierarchy I need to add two buttons - new and delete. Then they need signals.

I gave up trying to figure how to remove a row. I might be missing something but the documentation for this class library seems poor.

Packaging

  • setup.py contains information used on packaging, so I edited it first.
  • AUTHORS contains copyright information
  • .desktop.in contains the Desktop link details
quickly package

At this point I realised BZR needs configuring. Once that's done, the packages are created. Fantastic.

Day 3

Functions

every Python function returns a value; if the function ever executes a return statement, it will return that value, otherwise it will return None, the Python null value.

def buildConnectionString(params):

So Python is both dynamically typed (because it doesn't use explicit datatype declarations) and strongly typed (because once a variable has a datatype, it actually matters).

Functions documented using

""" doc """

- can be multiline.doc string is available at runtime as an attribute of the function.

Everything's an object.

Code deliniation is by indentation and a colon after a function name.

if __name__ == "__main__":

name is main when a module is executed as a standalone application, otherwise name is the modules's filename without path and extension. This allows you to put test suite's within a module that are only executed when its run on its own.

coding/python.txt · Last modified: 2010/12/28 19:37 by dougie
Kleine Websites, die ein Wiki als CMS verwenden.de