photo
Jordan Sissel
geek

Tue, 11 Jul 2006

Pyblosxom single-entry page title plugin

The page titles pyblosxom provides are usually great. However, when there is only one entry displayed, I feel it would be better to rely on that entry's title.

I wrote a very short plugin to do just that. Turns out the plugin api for pyblosxom is quite easy to understand, and this hack was only about 10 lines.

pagetitle.py adds a new variable which will contain the standard page title, unless there is only one entry in view. If there is only one entry in view, the page title is augmented with the story title aswell. This makes search engine results and browsers happier, as they can recognize what your page is about by the title. User experience good, also good for search engines.

The new variable you want to use is: $blog_title_or_entry_title

If you want to get a better idea of what this plugin does, you can click the permalink below to view only this entry. The page title (in the url bar) should now reflect this entry's title.

download pagetitle.py

Comments: 1 (view comments)
Tags: , , , , ,
Permalink: /geekery/pyblosxom-pagetitle-plugin
posted at: 03:12

Wrap method calls in python

Function wrapping is quite useful, especially when you need to make code threadsafe by wrapping with a mutex locker or adding debug entry/exit traces. We can easily wrap methods in python using lambda.

A standalone module for wrapping can be found here: wrap.py If you don't understand what the * and ** stuff means, that's fine. I'll post about those shortly.

A fun, crappy example can be found here: wrapexample.py

That example shows how to wrap a simple method (X.Foo) with pre- and post-execution function calls. Notice how we can access the parameters passed to the original function (Foo) in both the pre and post functions. That's all good and pretty, but how about a better example?

A better example would be to wrap a function call with a mutex locker.

Let's take an example class happyfoo. A sample script that uses happyfoo can be found here: main.py. However, time passes and now I require the happyfoo.makenoise method to be locked while we are inside it. If you look at the code, it doesn't lock and is not threadsafe (for our purposes).

In an ideal situation, you might add locking to the 'happyfoo.py' module itself. What if you can't do that (no access) or don't have time to hack through the code? There's an easier way.

Python lets you modify classes at runtime. The new locking code can be found here: main-locking.py

The coolest part about this is I do not have to modify the 'happyfoo.py' module. Perhaps this is a dangerous feature, but I think it's neat. Anyway, the bulk of the new code should be self explanatory, with the possible exception of this:

happyfoo.makenoise = wrap.wrapmethod(happyfoo.makenoise, do_lock, do_unlock)
This is where I override the 'happyfoo.makenoise' method with a generated one that calls the original 'happyfoo.makenoise' function wrapped in 'do_lock' and 'do_unlock' functions. If you run the script, you'll see it locking aswell as the threads waiting for the lock.

If you want to download all of the code from the post, try this tarball: python-method-wrapper.tar.gz

The wrap module needs a lot of work, potentially. It would be nice to be able to wrap and also pass other arguments to both pre and post functions. I've got a hack that adds a reference to the function being called to the keyword (kwds) list, which lets you figure out which function is actually being called. Useful if you use the same pre/post functions to wrap more than one function.

Comments: 0 (view comments)
Tags: ,
Permalink: /geekery/python-method-call-wrapper
posted at: 00:06

Search this site

Navigation

Metadata

Home About Resume My Code (SVN)

Articles

ARP Security Dynamic DNS with DHCP OpenLDAP+Kerberos+SASL PPP over SSH SSH Security: /bin/false Week of Unix Tools Work Efficiency

Projects

fex firefox tabsearch firefox urledit grok keynav liboverride newpsm (FreeBSD) nis2ldap pam_captcha poor man's backup Solaris audio utility xboxproxy xdotool xmlpresenter xpathtool misc scripts

Presentations

Yahoo! Hack Day '06 Unix Essentials Vi/Vim Essentials

Tag Cloud

Calendar

< July 2006 >
SuMoTuWeThFrSa
       1
2 3 4 5 6 7 8
9101112131415
16171819202122
23242526272829
3031     

Friends

BarCamp Kent Brewster Tantek Çelik John Resig Wesley Shields Tyler Shields

Technorati