Python Properties

One of the criticisms of many languages is that they are so complex, that people do not use all of the features of them. Meaning that one person might write a program in a language that another, fluent reader/writer of that language may not be able to understand. I think this is just rubbish, at least to some respect. If you see some code in a language that doesn’t make sense, then hopefully you should be able to either figure it out, or look up in the documentation (what, your language doesn’t have complete online, searchable documentation?) what is going on.

A similar thing happened to me the other day - and I didn’t even need to look up in the Python documentation to see what the story was.

People often criticise python because it lacks the ability to mark a variable as private, or protected. All attributes of a class are automatically public, and it is only really a guideline that if you prefix a variable with one or two underscores, you are marking it as protected or private. Namespace mangling means that this becomes slightly more than just a guideline, IIRC, although I’m not really sure of how this all works. Basically, the theory is that if you prefix an attribute, that is a marker to users of your API that you really shouldn’t use this.

I’ve often been frustrated when writing Java programs that attributes aren’t accessible, but I can see the value in only allowing protected access, through getters and setters. You can do this to prevent user classes/objects from putting garbage data into your attributes. This becomes even more important in Python, as its dynamic (but still strong) typing means I could put a “GOO” in where you expected an integer.

I discovered yesterday, as I was implementing a database in SQL Alchemy, that python has the ability to only allow protected access, through getters and setters, using the property function.

So, for this project, I have a database, where Sheep objects are stored, along with a variety of attributes. Sheep can be either Rams or Ewes, but the data that is stored about a Ram is virtually identical as that stored about a Ewe. However, using polymorphic mapping, I can have Ram and Ewe objects both stored in the Sheep table, since they are both sub-classes. Furthermore, I can use the gender attribute to determine if the object retrieved from the database should be a Ram or a Ewe. This is much better than anything I was able to do with J2EE, at least as far as I could find.

Because I am using a composite primary key (again, not something that is easy to do with J2EE, without defining a Primary Key class, ugh), and a Sheep can have a .sire and a .dam, which internally is stored with three columns in the table each, and that a sire/dam must be older than the sheep in question (and of particular gender), then some form of restricting which data can be assigned to the sire and dam attributes (which, using SQL Alchemy are references to other objects in the table).

Thus, I can have the following lines in the class definition for Sheep:

1        sire = property(_getSire, _setSire)
2        dam = property(_getDam, _setDam)

The crux of this post is that now, if you attempt to set a sire, using: sheep.sire = otherSheep; then it will pass the contents of otherSheep to the _setSire method of the Sheep class.

This goes even further - it now allows me to either use a Ram object, or a Sheep object (and uses some checking to ensure that it is a Gender=M), or even just a string that is the string representation of a sheep - the value that I am using for the three parts of the primary key. It will then look up the sheep in question, or create a new one if it doesn’t exist in the database.

Thus, a sheep is identified to the real world by three things: it’s flock number, such as 160188, the year it was born, like 2004, and the tag number, such as 040001, which is not unique between flocks, or necessarily years. For the flock this system is designed for, the tag number is unique for sheep of a given century, which would generally suffice, but in a situation where there were more than 9999 sheep born every year in a flock, then you wouldn’t just be able to use the last two digits of the year for the start of the tag number.

Thus a sheep might be declared as 160188-2007-070001, which is a unique identifier of that sheep in the world (or at least Australia!), but having this as a primary key on it’s own means more jigglery would be required to get the flock number and year. By storing as three seperate parts it is possible to keep an easy reference to the flock, and the year. There is no chance of a data integrity error as there might be if I stored the id, then the year and flock number. It would be possible then to change one without the others being udpated.

I’ve also done something quite clever with the mapping of the sire/dam relationship. By having different classes for Ram and Ewe, and mapping sire/dam to these, it is possible to use the same backreference term, offspring, to generate a list of sheep who are offspring of the Ram or Ewe in question. If you don’t use subclasses, then it isn’t nearly as neat.

If you can’t tell, I’m loving SQL Alchemy. I had some issues figuring out what the hell was going wrong with my relations between sheep and sire, but it turned out that you have to use the remote_side argument to the relation function to do what I wanted. Funnily, it was working with dam, but not with sire, but now it’s much more solid. No more circular reference constraints either.

By the way, this project is going to be an online database for livestock information, allowing potential purchasers to research pedigrees online. If anyone else is interested in purchasing it, send me an email. At this stage, it is sheep only, but will be easy to change to, say cattle if they are your thing…

Automatic Updates?

I’m about to create a small application that uses Sparkle to do updates - not to publish anything, but just to see how it works, and make sure I can. I read a couple of things before beginning. One was an argument against Sparkle, or rather, an argument for an Independent Software Update…

ISU would allow all applications to be upgraded at once

From Stoneship > Thoughts on Sparkle

Now, I have quite a few applications on my machine that I don’t use very often. Even something like Acorn, which I use maybe once a week, right down to stacks of apps I almost never use.

Some of these get updated quite often. I don’t really care to download every update on a regularly updated application, when I’m probably going to have multiple updates before I use the application again. So, I quite like that it checks for updates about as often as I use it.

Which means applications like, which I use virtually every day, and ExpanDrive, which I use virtually every hour, are kept up to date, whilst Acorn, which I just used a couple of minutes ago for the first time for ages, are only updated infrequently.

This can be taken with a grain of salt - I don’t know much about Sparkle other than it is pretty cool.

EyeTV and ScreenRecycler

Or how to make a single-display Mac Mini Media Center even better.

I have a Mac Mini that records all of my favourite programs (almost) automatically. It runs EyeTV (by default), and can play movies stored on either a local FireWire hard drive, or a LAN server (running OpenSolaris, and using ZFS, incidentally).

One annoying thing is that to set up a new smart playlist, or edit a movie, or do anything on the machine, whilst I can remote in and do stuff, this all displays on the TV.

However, using ScreenRecycler, it seems like I can trick the Mac Mini into thinking he (his name is jens!) has two monitors. Thus, with some swizzling, I can have the EyeTV full-screen display on the “real” monitor, and the desktop on the other one.

Doing this means that I can happily edit a movie (say, to remove advertisements), whilst the TV still displays the live TV (or being viewed video).

Run ScreenRecycler on the Media machine. You may need to restart if this is the first time you have run it, as it installs some driver. I’m hoping it’s pretty safe…

After running ScreenRecycler when the system has the driver installed, I needed to run JollysFastVNC to get ScreenRecycler to give me a second display. I was using ARD (and still do), which when I then reconnected showed me the whole desktop, with a menu enabling me to choose which display (or both) to view. I chose to view both displays for now.

Picture 1.png

I then loaded up the Displays preference pane, and moved the menu bar to the ScreenRecycler display.


I made the ScreenRecycler display run in a higher resolution (1280x960, so that it fits nicely onto my external monitor on my laptop at full-size). I then went to the EyeTV preferences, and made sure the full-screen display went onto the “real” monitor. This apparently has the side effect that quicktime movies will also play on this monitor, so that’s quite good.


Finally, I made new Live TV and Recording open in a new window. This enables me to edit a movie while Jaq is watching another. It does, however, mean that it is a little cumbersome to switch between tuners (need to view both monitors, and do some more swizzling), but it’s workable. It was always hard to switch tuners anyway, at least with the Apple Remote.

This makes for a fairly cool setup. I’m fiddling a little more before I buy ScreenRecycler (like, does it work better upon startup?), but it looks pretty promising.

Safari 4 Broke My WebApp

Well, not mine, exactly.

Flinders Learning Online doesn’t render properly in Safari 4 (Developer Preview). It doesn’t display the sidebar, which has quick access to topic information.

It basically makes it unusable.

I think the system is the Blackboard Learning System, or something similar.

Safari 4 Breaks Lots of Stuff

Okay, I’ve deleted the Safari 4 Preview.

It just breaks too many things.

Firstly, it makes my Online Learning system not display properly.

Secondly, it makes ecto not work very well. It causes it to not post, and in fact not even retrieve posts from the sever very well. And it makes data just magically vanish.

Finally, it kills the ability to edit posts in WordPress Admin area. That’s right, it breaks TEXTAREA tags.

How the hell can you do that?

Sometimes, you just have to use a Microsoft Program.

Ugh. Ecto just stole my post. Oh well, time to start again.

I pride myself on being almost 100% Microsoft free. I don’t use Windows on a day-to-day basis. I have to test a Windows program against a server I write, but that is it. I used to have to use Windows to run AutoDesk Inventor and a custom MS Access Database, but that was in my prior life as a teacher.

Lately, I’ve been using Pages for all of my Word Processing needs, and I think it works better than MS Word. It is snappier, performs more reliably (doesn’t make stuff jump around randomly), and has all of the features I need and use.

I’ve also been using Numbers as a Spreadsheet. It, unfortunately, doesn’t hold up to the competition, Excel, nearly as well. Firstly, it doesn’t have Pivot Tables, which are a great invention that I just could not live without if I was doing any sort of collation of data. Like the custom Excel Spreadsheet I built for my partner’s expenses and tax purposes. However, she is likely to shift over to MYOB anyway.

My problems with Numbers are mainly to do with Charting. I had to collate data that had run times of various algorithms, as they were implemented using different techniques across different programming languages and paradigms. Most of the data was easy to compare, but as I went to present all of the tests I found an issue.

There is no way to do an X-Y, or Scatter Plot in Numbers.

Your data will be okay if you have the same values for all of your X values for every column, but, if you have the situation I came across where I had some data sets that were increasing by an order of magnitude, and some data sets that were doubling (and, one data set that was incrementing).

Thus, I wasn’t able to obtain the following plot, and had to use Excel.


Pleased to say I used Apple Remote Desktop and a machine that already had it on it, rather than install in on my laptop!

Oh, and before anyone says anything, I tried Tables (which I bought a license to before Apple brought out Numbers), and OpenOffice.

Now, don’t get me started on OpenOffice. I think I’d rather use Microsoft Office. I mean, all they have done is clone it, and haven’t really gotten everything to work. At least there is a Cocoa/Aqua version, but it is clunkier and slower than Excel.

I might actually try Excel 2008 and see how that goes if I need to do some more number crunching.

Blackboard Learning System and Safari

I can’t post this in ecto - I think the Safari 4 DP is fucking with that too. Anyway, if I log into the BLS using any application that uses WebKit as its renderer, I see the following: FLO wrong render If I resize the window, it fixes itself: FLO wrong render

Best. Ringtone. Ever

I’m not even going to tell you what it is. You must just listen to it. And laugh.

World's Fattest

One good thing about doing benchmarking of algorithms for homework is that I have time to watch TV while I wait for code to run.

And then I see things like the World’s Fattest Man, and World’s Heaviest Teenager.

How the fuck does someone get to 1000 pounds. And you can’t tell me that the teenager’s Mother isn’t to blame for him being so large.

I just couldn’t see the point of living if I could not move around.

Adding Extra MIME-types to WordPress.

(Site Admin) → Options → Mime types

File Extension/Associated mime-type.

Add mime-type »