Skip to main content

Posts

Showing posts from March, 2005

A task-scheduling module

Part of the next release of Spyce will be the new scheduler module. This actually has no Spyce dependencies, so it may be useful in any (web or non-web) application that needs to run tasks at given times or intervals. Some things I have used it for: Scan the log for errors and email me a summary vacuum (pre-autovacuum daemon days...) Purge stuff from the global cache Send email to users whose accounts are about to be suspended Why a python module instead of cron? The primary benefit in my mind is that scheduler runs as a thread of your application (or app server, in Spyce's case), so you can access your database connection pool, cache, or other global state without jumping through nasty hoops. This is a bigger deal the more complex your application is... Putting logic here also makes deploying to testing or release servers (or to a client) a matter of running "svn up" rather than having to mess with crontab. You can browse the svn source here: http://svn-hosting.com

Spyce subversion

As mentioned on the mailing list, Spyce development is going on in a subversion repository. We (well, I) got tired of waiting for sourceforge to leave the CVS dark ages... Get your bleeding edge spyce from http://svn-hosting.com/svn/spyce . The web site will be updated with this information eventually soon. (Incidently, I use svn-hosting.com for several projects and it's an excellent, reasonably priced service if you feel you have better things to do than learn how to admin a new source control system. I run the svn repository for my day job and I'd rather let someone else do it when I have the choice.)

When a tuple isn't enough

Using a tuple to pass aggregated date around is a Good Thing, but if you do it a lot or your tuple gets large then you should really use a class. Or should you? NamedTuple lets you write code like this: names = ("name", "age", "height") person1 = NamedTuple(zip(names, ["James", "26", "185"])) person2 = NamedTuple(zip(names, ["Sarah", "24", "170"])) print person1.name for i,name in enumerate(names): print name, ":", person2[i] Okay, that's pretty ugly. But reading the comments of a related recipe led me to Ganesan Rajogpal's attrdict: class attrdict(dict): def __getattr__(self, name): return self[name] def __setattr__(self, name, value): self[name] = value data = attrdict(red=1, green=2, blue=3) print data.red data.black = 4 Much cleaner to use, as well as less code to implement. (Although not immutable, so you couldn't use an attrdict a

Toward Spyce components

Making progress towards WebObjects/Tapestry/ASP.NET/JSF-style reusable components. As a first step, I've checked in code to svn trunk allowing the defining of handlers to run on form submissions. Working example: [[\ def __init__(self): self.i = int(request.getpost1('i', 0)) def plusone(self): self.i += 1 def plustwo(self): self.i += 2 ]] <f:form> [[= self.i ]] <f:hidden name="i" value="=self.i"> <f:submit handler="self.plusone" value="Add one"> <f:submit handler="self.plustwo" value="Add two"> </f:form>

How well do you know Python, part 1

Here is a stripped down script that tests a part of Spyce's code to "export" variables from an active tag to the main spyceProcess scope. Even without knowing the background, though, you should be able to figure out: What error will running this code produce? class spyceImpl: def spyceProcess(self): ___tagexports={'y': 1, 'item': 0} for ___tagkey in ___tagexports: exec("%s = ___tagexports['%s']"%(___tagkey,___tagkey)) test = spyceImpl() test.spyceProcess()