PATRICK ROBERTS'S BLOG

egoFile.com/blog: Formalizing Intelligence in Code

Sun, 15 Aug 2004

wxSetting Python Module

I might be qualified to say something useful about Python because I seem to be one of the few who has built a consumer Windows application with it and has started selling some copies.

The program's named egoClip (was less imaginatively named the NewsRanker until I noticed that Topix trademarked NewsRank; presumably they're playing on Google's PageRank and don't simply have it in for me). It's an RSS aggregator that learns to sort your news by how interesting it thinks you'll find them. Think Bayes meets RSS. Many others have had this idea, but hardly anyone seems to have done anything about it.

Anyway, since code beats prose, here's some open-sourced code from egoClip: wxsetting.py. Example usage:

from wxsetting import *

def font_change(new_value):
    [use the new font size]

app.settings = wxSettings(
    first_use = wxSetting(default=True),
    font_size = wxSetting(default=12, handler=font_change),
    )

# and then from your prefs code, you could go ...
font_size_ctrl.Bind(wx.EVT_SLIDER, lambda e: setattr(settings, 'font_size', e.GetInt()))

Mainly, the point is that when you initialize your application, you define this bag of settings with defaults and optional functions to be called when the setting is changed. Then you can, e.g., pass this to your preferences dialog code and it can change settings without having to know what has to be done as a result. No doubt it's messy and there's much room for improvement, but if you find it useful or have suggestions, let me know.

Now that I've reviewed the module, I'm reminded that I originally wanted to use descriptors so I wouldn't have to put all the settings in a wxSettings bag to handle assignment, but couldn't because before wxPython 2.5, the frame and app classes weren't derived from object. I can change that now, though it's not so bad having all settings alone inside one object.

permanent link

©2004-2008 Patrick Roberts | Burlington, Ontario, Canada