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.
I read a post on Ask Ubuntu that suggested a number of free resources:
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:
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
So from this initial application, I had a few questions:
It's equivalent to “this” in Java - it refers to the object itself.
They donate an object's methods - object.method().
There's two ways - the first prevents the name being reused and the second reduces typing.
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")
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")
Looking over yesterday, I realise I need to look at 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.
quickly package
At this point I realised BZR needs configuring. Once that's done, the packages are created. Fantastic.
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.