Vertical Sync

I haven't been taking much freelance work recently, and for good reason. I've recently incorporated Badran LTD with myself and my brother Lloyd as directors, and we are currently setting up an internet and lan gaming centre/cyber cafe in cardiff, trading under the name "Vertical Sync".

If things continue at current pace we should be open by years end, hopefully around late October time.

While there is only a placeholder currently up, the website will be located at http://www.vertical-sync.com

To clarify, i am still trading under badrunner.net, and will continue to accept freelance work, although for the next few months my availability will be diminished.

lennardwoodcreations.com is now live

I've finally pushed www.lennardwoodcreations.com live. It's actually been up for about a week, but it still had a few issues that needed fixing which i've finally got round to today.

It's all built on django, and the entirety of the content is generated by the owner of the site, using a combination of flatpages and the models I created.

Updated Django image thumbnailer

I've actually gotten a few emails about my django thumbnailer code recently, turns out a search for "django image thumbnails" now has me on the front page of listings from google.

Anyway, i actually use an updated version from my previous post which i've included here:

import os
import Image
from django import template
from django.conf import settings

register = template.Library()

ERROR_IMG = settings.DEFAULT_ERROR_IMAGE

def create_thumbnail_image_file(file, width, height):
    """ Create a thumbnail image and return the path """
    if not file:
        return ERROR_IMG
    
    basename, format = file.rsplit('.', 1)
    thumb = basename + '_' + str(width) + 'x' + str(height) + '.' + format
    thumb_filename = os.path.join(settings.MEDIA_ROOT, thumb)
    thumb_url = os.path.join(settings.MEDIA_URL, thumb)

    filename = os.path.join(settings.MEDIA_ROOT, file)
    if os.path.exists(thumb_filename):
        if os.path.getmtime(thumb_filename) < os.path.getmtime(filename):
            os.unlink(thumb_filename)
        else:
            return thumb_url
    
    image = Image.open(filename)
    image.thumbnail([width, height], Image.ANTIALIAS)
    image.save(thumb_filename, image.format)
    return thumb_url

class ThumbnailerNode(template.Node):
    def __init__(self, image, width, height):
        self.image = image
        self.width = width
        self.height = height
    
    def render(self, context):
        try:
            actual_image = template.resolve_variable(self.image, context)
            return create_thumbnail_image_file(actual_image, self.width, self.height)
        except template.VariableDoesNotExist:
            return ERROR_IMG
          
@register.tag(name="thumbnail")
def thumbnail(parser, token):
    try:
        tag_name, image, width, height = token.split_contents()
    except ValueError:
        raise template.TemplateSyntaxError, " %r tag requires exactly 3 arguments" % token.contents[0]
    return ThumbnailerNode(image, int(width), int(height))
<

I think calling this from a template is much nicer than the old code using a filter. You can now just insert:

{% thumbnail <image_object> width height %}

Encoding Video for XBox 360

It took me ages to find info on creating Xbox 360 compatible videos on a linux box. In the end it turns out to be really easy using ffmpeg. Now, the problem with the 360 is that it requires videos to be in a very specific format, namely WMV video, WMA Audio and ASF container format. The following command will setup ffmpeg to use the source video settings for as much as possible, and sets the bitrate to 1.2 MBit/Sec.

ffmpeg -i "$INFILE" -vcodec wmv2 -acodec wmav2 -y -b 1200000 "$OUTFILE"

Syntax Highlighting using Pygments

I've had a few requests for information on how I syntax highlight my python code on this blog. Truth be told, I was using vim to output HTML and just pasting that into my blog posts. Pretty nasty.

So, I set about creating a template tag to do just that. It uses Pygments and BeautifulSoup, and has a few advantages over some similar code I found with the google world brain, namely:

  • Will syntax highlight any code, not just python, and will do so automatically
  • Only replaces <code> blocks
  • Replaces all code blocks, so you can have a chunk of html and css in the same blog post for instance
  • On failure just returns the original string

Anyway, here is the code:

from django import template

from pygments import highlight
from pygments.lexers import guess_lexer, PythonLexer
from pygments.formatters import HtmlFormatter
from BeautifulSoup import BeautifulSoup

register = template.Library()

@register.filter
def pygmentize(value):
    try:
        soup = BeautifulSoup(value)
        code_blocks = soup.findAll('code')
        for code in code_blocks:
            try:
                lexer = guess_lexer(code.string)
            except ValueError:
                lexer = PythonLexer()
            code.replaceWith(highlight(code.string, lexer, HtmlFormatter()))
        return str(soup)
    except:
        return value

Dynamic Thumbnails for Django ImageField

UPDATED:  New version of code

Here is a template tag that generates arbitrary sized thumbnails of static images on the fly if needed. It also checks if the original image is newer than the template, and updates the thumbnail if need be. It's partially based on some code from django snippets.

import os
import Image
from django import template
from django.conf import settings

register = template.Library()

@register.filter
def thumbnail(file, size):
    width, height = size.split('x')
    width = int(width)
    height = int(height)
    basename, format = file.rsplit('.', 1)
    thumb = basename + '_' + size + '.' + format
    thumb_filename = os.path.join(settings.MEDIA_ROOT, thumb)
    thumb_url = os.path.join(settings.MEDIA_URL, thumb)

    filename = os.path.join(settings.MEDIA_ROOT, file)
    if os.path.exists(thumb_filename):
        if os.path.getmtime(thumb_filename) < os.path.getmtime(filename):
            create_new = True
            os.unlink(thumb_filename)
        else:
            create_new = False
    else:
        create_new = True
    
    if create_new:
        image = Image.open(filename)
        image.thumbnail([width, height], Image.ANTIALIAS)
        image.save(thumb_filename, image.format)

    return thumb_url

Toying with Django

As you know, I've been spending some time learning turbogears, and think it's pretty damn good. However you can't do any work in python these days without hearing about Django. So, I thought I should at least take some time to evaluate it, and see how it compares with what I already know. Luckily, 0.96 (the latest stable release) is already packaged in gutsy, so it was trivial to install. 

To go about testing it, I endeavoured to rewrite the backend to badrunner.net using django, and also replace the wordpress instance with a custom blog. Note that replacing wordpress is not anything to do with a dislike of that great software, its merely so I can exerciie my django skills.  I'm also currently working on a website for my cousin which should be up at lennardwoodcreations.com in the near future, and have chosen to use Django for this site too.

The Good

So what's good about Django?

The automatically generated admin interface. This feature alone is wow with a capital w! This is the primary reason I have opted for django over turbogears for my other project.

Regex url matching

The flexibility here is staggering, and it gave me an excuse to really try an learn pythons re syntax, whereas I'm normally lazy and just cheat with string functions. I honestly believe (and advocate to my friends) that any programmer worth his salt should learn to love regular expressions, and here I'm eating my own dog food as it were.

Generic Views

Trivial redirection and 'static' file template rendering, fantastic. The object list and detail setups are nice to. Again Django excels at making the easy easy, and the hard possible.

The Bad

NIH Syndrome 

One of the drawing factors to turbogears for me, is that the project tries to simply be a layer on top of the best of breed tools. I like that you can relatively easily replace your ORM, or your template engine, or whatever, yet still have great defaults to use. The only downside to this in turbogears is that for developers using a 1.0.X release, you know that all the major components are being changed for 2.0. The reasons the turbogears project are doing this are sound, but its annoying to think tech that you are already using has been deprecated.

Django on the other hand, uses virtually nothing from anyone else, and so reinvents the wheel for every supplied component. Again, their reasons for doing this make (or at least made sense when django was first being developed), but it does mean its hard to replace any component, and you lose alot of the niceness of django if you do.

Strictly speaking this isn't necessarily a bad thing, but I do get the impression that this will (if it has not already) lead to unnecessary wheel reinventing, possibly producing something lower than the standard of what already exists.

Template Language

I simply just dislike the template language of django. It does offer all the features I want, it's just that i dislike the syntax.

Initial Configuration

There are more steps to setting up a django project, due to the 'site' and 'app' separation. This is fine, but when starting out it seemed like i had to do more to get django to give me a result than turbogears. However, now I know a lot more about it, I much prefer the flexibility offered, so perhaps this should be in "The Good".

Summary

Anyway, I think the admin interface building alone is a huge boost in favour of Django. It's not hard to write replacements, but the reason you use something like this in the first place is to do as little work as possible. I currently would use django over turbogears for any project where I can make that choice, however I'm looking forward to turbogears 2.0 as i think this may be a fantastic release.

Ignoring files in bzr

Bazaar has a great feature where  you can quickly do a

bzr ignore <file>

And it gets added to a local .bzrignore file in your branch root. Obviously very handy for things like sqlite development data. There is of course a global ignore feature, with many presets for things like object files, python compiled byte code etc.

Olive is a nice gtk front end to bzr include in bzr-gtk. One thing thats annoying though,  especially when you have lots of .pyc files all over the place is that it doesnt hide ignored files. Five minutes of hacking later and I had a patch to fix this, a quick chat in #bzr and its already been commited. Open source is just fantastic.

Turbogears Webfaction Deployment

This site's hosting is provided by the excellent WebFaction. It took me a little while to get a workflow I was comfortable with using, that had the various properties I require, namely:

  • Static content served by the webserver, not cherrypy
  • Everything version controlled, in a single repository (exluding the wordpress instance)
  • Commits to the VCS without requiring those changes to be reflected on the server
  • The ability to rollback updates on the server easily
  • Completely offline working with commit access

Webfaction works on the principle of "applications" and "websites" that can be joined in fairly arbitrary ways. An application for instance could be a directory of files, a turbogears instance, or even a wordpress blog. A website is just an association of an application to a domain name and a path on that domain. So in the case of badrunner.net i have 3 applications, a static directory of files in /static, a turbogears instance for the main site, and a wordpress instance for this blog. The static content of the turbogears instance is serverd by symbolically linking the directories inside the turbogears webapp directory into the static webapp directory. Version control is all handled by bazaar, which is just a fantastic system to work worth. To set this up, just go into the project directory then:

bzr init
bzr add <files I care about>  
bzr commit

Now all the files I want versioned are under bazaar's control, see bazaar-vcs.org for more infofmation on using bazaar. Then you can just copy the whole directory to your local machine (or bzr branch if you so choose), and its already configured to use vcs. However, the next stage involves getting changes from the local machine onto the server and vice versa. First thing to do is create a bazaar repository to push code to (you push commited changes from a branch, and can then pull those changes into another). Having a bazaar repository is actually useful for me anyway, as it provides a place I can push other projects too easily (I am a software developer after all), without any extra work. To do this I created directory bzr in my home directory, and then initialised that with:

bzr init-repository ~/bzr

I can now push and pull to this location over sftp in the case of my local machine, or just locally on the server. I end up with the simple work flow of editing the site locally, commiting as often as I want along the way without fear of server downtime, doing a bzr push at each point I feel the site is ready to deploy. Now, this push alone does not cause the site files to change, it merly gives me the option of being able to log into the server, change to the webapps directory and doing a bzr pull when im fully commited to deploying. At this point you need to restart the turbogears application as you normally would, and all is well!

Hello World!

Well this is my first post, hopefully of many, unless of course beer gets in the way. As way of introduction, my name is Tom Badran, and I am a software engineer.

Recently I left work to go into business for myself as a consultant, so far so good. I've taken a good month off, had a bit of holiday, spent some time learning all the things I missed out on over the last few years. Anyway, more details on that at badrunner.net (well, soon, curretnly that just points back here!). My general expertise is pretty broad, I've worked on things ranging from games programming (including consoles such as the game boy), to big effects rendering farms, and everything in between. Currently, i seem to be doing a lot of work in python, with gtk and turbogears (though obviously not mixed!). Primarily I'm a linux guy, last windows box I worked with properly was back in the late ninties, and since then I've only fought with some of the xp boxes at my old job a few times. I have a pretty low tolerance for poor quality systems, so if you want me for work on the windows platform, it better be really interesting stuff.