Wishful Coding

Didn't you ever wish your
computer understood you?

How to install Python/web.py on a shared host

In the past I have written a few Python web applications, but when I asked my host about Python, they told me they where specialized in PHP and nothing but PHP(that is why this site runs Wordpress). If your are lucky and have a very expensive dedicated server you can install whatever you want, but how about my poor projects? I'm not going to re-write them in PHP obviously. I figured that as long as you have a CGI bin, you can do whatever you want. I started writing a CGI script that would install Python and Web.py for me; Check out the resulting web.py example. This is the script that I came up with. It should be doable to customize this for Pylons, TurboGears, Django, or any other Python framework. You should at least fill in your own home directory twice, we're doing a Python installation inside you home directory.
#!/bin/sh

echo "Content-type: text/html

";

curl http://www.python.org/ftp/python/2.6.4/Python-2.6.4.tgz | tar -zx
cd Python-2.6.4
./configure --prefix=/your/home/directory #change this to your home
make
make install
cd ..
curl http://www.saddi.com/software/flup/dist/flup-1.0.2.tar.gz | tar -zx
cd flup-1.0.2
/your/home/directory/bin/python setup.py install
cd ..
curl http://webpy.org/static/web.py-0.33.tar.gz | tar -zx
cp -r web.py-0.33/web .
Note that you should add .py as a CGI handler in your .htaccess file:
AddHandler cgi-script .py
Since our fresh Python installation is not on the PATH variable, you should include a shebang header in your scripts to point to the right path:
#!/your/home/directory/bin/python
Again, check the result!

How to navigate the browser ballot screen

How to navigate the browser ballot screen This is my response to the EU/Microsoft browser ballot screen. Honestly, most people don't even know what their browser is, what do you think the difference in IE market share will be? http://xkcd.com/627/ also applies here. I recommend any of the five except IE. In case you're wondering, I'm using Safari.

Wordpress.com Stats: Top Posts Widget

Wordpress.com Stats: Top Posts WidgetThis hack is so cool and so simple, I expect anyone to run up to me and yell that this has been done before. I created a 'Top Posts' sidebar widget based on the widget that comes with any Worpdpress.com blog. I know there are several widgets like this, but they all include their own statistics tracking. Mine is nothing more than an excerpt of the Dashboard widget that comes with the Wordpress.com Stats plugin. For people not interested in technical details, here is a plugin file, that works as usual; Make sure you install the Wordpress.com Stats plugin first! I said this hack was simple, here is way: The code is already there! I'm surprised Wordpress did not include the widget themselves. Lets have a look at stats.php: Line 925-926:
foreach ( $top_posts = stats_get_csv( 'postviews', "days=$options[top]$csv_args[top]" ) as $post )
        $post_ids[] = $post['post_id'];
Line 942-949:
<?php foreach ( $top_posts as $post ) : if ( !get_post( $post['post_id'] ) ) continue; ?>
<p><?php printf(
        $printf,
        '' . get_the_title( $post['post_id'] ) . '',
//                        '' . $post['post_title'] . '',
        number_format_i18n( $post['views'] )
); ?></p>
<?php endforeach; ?>
My resulting guesswork looks like this:
<h4>Top posts</h4>
<ul>
<?php foreach($top_posts = stats_get_csv('postviews', "days=7") as $post): if(!get_post($post['post_id'])) continue; ?>
        <li></li>
<?php endforeach; ?>
If anyone is interested, I can upload this to the Wordpress plugin repository. Done: http://wordpress.org/extend/plugins/wordpresscom-stats-top-posts-sidebar-widget/