What's new
Dec 23, 2009 - 05 p.m.

Its been a while since I wrote an update here, and this is because its been a while since I've spent much of my leisure time hacking on anything particularly interesting. I've had some fairly interesting problems to play with at work, and the thing is, I really only have so much coding in me every day. When that isn't being used up with work related stuff, I find myself itching to spend my leisure time cranking stuff out. Lately that hasn't been the case. I know that it's in my best interest to spend time coding and writing about coding outside of work, but my personal resolve to do that tends to vary in the extreme. A graph of my posting frequency looks like a sine wave with a rapid interval.

What have I been up to? Pretzels. If you've got the ingredients, give it a shot. They're real awesome, and it's very satisfying to see them come out of the oven. If there are too many, freeze those suckers. I'm still experimenting with how to thaw them, but 300 degrees for about 7 minutes seems to be the sweet spot.
Using pygments for syntax highlighting
Oct 18, 2009 - 06 p.m.

In a previous post I talked a bit about trying to get syntax highlighting working properly in blog posts. I said something or other about it being more difficult than all the implementations available would imply, but it turns out its (as suspected) just as simple as I thought it might be. The real issue was that it didn't work the way I wanted it to. So I slapped together two other implementations with features I liked. I've lost the links to those (sorry anonymous other programmers).

My version requires Beautiful Soup and Pygments and combines two features that I liked in other versions - automatically including the relevant stylesheets (cause I don't want to write my own) and understanding the code language based on the class of the page element (because using the lexer to automatically figure out the code is a crappy method).

Anywho, here is the code:
from django import template
from BeautifulSoup import BeautifulSoup
import pygments
import pygments.lexers as lexers
import pygments.formatters as formatters

register = template.Library()

@register.filter(name='pygmentize')
def pygmentize(value):
    try:
        formatter = formatters.HtmlFormatter(style='trac')
        tree = BeautifulSoup(value)
        for code in tree.findAll('code'):
            if not code['class']: code['class'] = 'text'
            lexer = lexers.get_lexer_by_name(code['class'])
            new_content = pygments.highlight(code.decodeContents(), lexer, formatter)
            new_content += u"%s" % formatter.get_style_defs('.highlight')
            code.replaceWith ( "%s\n" % new_content )
        content = str(tree)
        return content
    except KeyError:
      return value


Used like so in a template:
{{ value|pygmentize }}
If you need some instructions for putting that in place, the django book has some pointers

Anywho, back to trying to solve the rubik's cube.
dirty little database secret
Oct 2, 2009 - 01 a.m.

Recently I tweeted about one of my blog posts, and surprisingly saw a mini spike in traffic - when traffic is almost zero, any traffic is a spike. Regardless, when slapping together this blog, in many places I took the path of least resistance. With regards to database, that path was sqlite. I knew full well that it couldn't hold up against any real sort of traffic, but the idea of creating a database and users and yada yada yaaa was just not something that looked like a lot of fun at the time. So I've been running the site on sqlite.
Now back to that traffic spike - it got me thinking about using sqlite. Maybe not so much thinking as worrying. Nobody wants concurrency issues. Nobody really wants to switch database engines either. Both are unpleasant, but only one affords the opportunity to learn PostgreSQL. I could have gone with MySQL, which I have installed and know, but I've been on a bit of a learning kick (this site is the product of that).

I installed the macports Postgres on my mac to teach myself how to port the data from sqlite. Installing it that way was super easy. The hard parts were installing the python database library and actually using Django's manage.py to dump and reinstate the data. I won't go into my particular difficulties (unless somebody asks) but lets just say manage.py makes it much easier than it would be otherwise, but it still could go a ways towards making it easier. After all, I'm no DBA :) I think I may continue to use sqlite in dev, but I'm glad I got through the switch.
Overall, I've got a little Postgres experience now and I don't have to lay awake thinking about my database concurrency issues.
rockin the alpha version
Sep 27, 2009 - 07 p.m.

Hillary is out seeing a play, so I've had a whole afternoon to watch the Mets' game and play around on the computer.

First up, although I didn't actually work on it today, I've now got Othello code for everyone to download - take and look and give it a whirl: Othello.py
I would love feedback (especially on my python skill/unskill). Here's a quick usage example:
phils-macbook-2:othello phil$ python Othello.py
7 | | | | | | | | 
6 | | | | | | | | 
5 | | | | | | | | 
4 | | | |w|b| | | 
3 | | | |b|w| | | 
2 | | | | | | | | 
1 | | | | | | | | 
0 | | | | | | | | 
   0|1|2|3|4|5|6|7
white, it is your move
Enter your move x,y format:2,3
7 | | | | | | | | 
6 | | | | | | | | 
5 | | | | | | | | 
4 | | | |w|b| | | 
3 | | |w|w|w| | | 
2 | | | | | | | | 
1 | | | | | | | | 
0 | | | | | | | | 
   0|1|2|3|4|5|6|7
black, it is your move
Enter your move x,y format:
And so on. It doesn't do much beyond that - win/loss conditions aren't setup and it doesn't digest bad input very well, but it's a work in progress. You can comment on this post or email phil [at] philwade.org and let me know what you think.

In things I actually did today: Adding code highlighting to blog posts proved a much harder task than the 150 django snippets utilizing pygments would lead someone to believe. I ended up using this guy, mainly because it provides css style support right out of the box. The styles aren't quite what I had dreamed of, but they're nice enough for government work.

I also got a chance to put the debug toolbar in place, and it is slick as hell - I'm not sure why it isn't included in Django core. Symfony includes their own debug bar in the default install, and based on what I'm seeing so far from the toolbar, django should be making developers aware of the amount of database legwork pages require. If this blog actually got any traffic, I would have to worry about caching pretty soon. As a side note, every python package install option I've had to deal with so far (setup.py and easy_install) is just awesome. Not allowing developers the chance to place library code in the wrong place is a good move. I'm a big fan of anything that makes it harder to be stupid, for my sake and for the sake of anyone else that has to deal with my code.

pages pages
Sep 19, 2009 - 04 a.m.

One thing I've been meaning to just bang out here is pagination. Interestingly, I ended up writing posts more quickly than code, leaving older posts stranded with no way to navigate to them. Even with stranded posts, I continued meaning to write the code instead of doing it.
Anyways, nothing like a bit of insomnia to tackle something you've been putting off. The pager is available on the bottom of pages, although I can't be bothered to make it look pretty.