I take it back, this is the most inneficient code ever

July 1st, 2009

Here's the most convoluted “Hello World!” script I could come up with (in response to this). I don't know if it works. I've proven it correct, but I haven't tested it.

from random import choice
from itertools import count
from zlib import crc32
import sys
any(crc32(h)==472456355 and not sys.stdout.write(h) for h in(''.join(choice('! edHlorW')for _ in '.'*12)for _ in count()))

I promise my production code is (marginally) more readable this this…

 

Jen's Wedding

June 23rd, 2009

My little sister got married! Congratulations Jen & Scott, I'm so happy for you both.

www.flickr.com

More Photos

 

A 3D Texture-Mapped Globe Rendered with Javascript and HTML

May 30th, 2009

I've been working on a large-ish Javascript project lately (still in stealth mode). One that could be described as a Javascript application. And since I've been up to my elbows in Javascript, I have found my contempt for the language waning. Not entirely, of course – I'm still a Python fan-boy. But enough for me to knock out a 3D(ish) spinning texture-mapped globe, using nothing but Javascript and HTML, as a way of honing my JS skills.

Without further ado, I present you with a 3D Javascript Globe.

 

Django like templates in Javascript

April 5th, 2009

Here's a quick ‘n’ dirty Javascript function I hacked together that provides Django-like template substitution.

function sformat(template, data)
{
    return template.replace(/{{(.*?)}}/g, function(m, n) {
        return eval('data.'+n);
        });
}

Used something like this:

sformat("Hello, {{ name }}!", {name:"World"});

Which returns the following string:

Hello, World!

Alas, it doesn't support anything other than substitution. If you need anything more advanced (loops etc), you should investigate Javascript template engines.

 

There's always a way

April 3rd, 2009
Always a way

Where there is a Will, there is always a way.

Spotted in a deli in Oxford. It says ‘Where there is a Will, there is always a way’.

Thats right – always a way!

Just saying.

 

Fuzzy Thinking

April 1st, 2009

I have an idea germinating that involves using fuzzy logic, something I only had a passing knowledge of. After reading the Wikipedia articles and Googling for it I'm more informed but still haven't found a perfect resource.

The only Python code I have found was informative, but it was from way back in 1990. If there are any Pythonistas who know of a more up-to-date fuzzy logic module, or fuzzy logic resource in general, please let me know!

 

Playing Possum

March 31st, 2009
A possum

An Opposum

No reason, I just love this little guy. I think I will use a possum as a mascot for my next creation – whatever that may be!

I don't know why people don't like Possums. I wouldn't mind one for a pet.

Here is what Wikipedia has to say on the subject:

Didelphimorphia (pronounced /daɪ.dɛlf.ə.mɔr.fi.ə/) is the order of common opossums of the Western Hemisphere. They are commonly also called possums, though that term is also applied to Australian fauna of the suborder Phalangeriformes. The Virginia Opossum is the original animal named opossum. The word comes from Algonquian wapathemwa. Opossums probably diverged from the basic South American marsupials in the late Cretaceous or early Paleocene. A sister group is Paucituberculata (shrew opossums).

Full article from Wikipedia

 

Misleading ImportErrors in Django

March 30th, 2009

I was debugging an issue with our Django app at work today; an admin.py file wasn't being picked up, and nothing was appearing in the admin pages. It turned that an ImportError was being thrown in the admin.py and Django was interpreting this as the file not existing.

I'm assuming that the reason for this is that Django uses __import__ to import the module, and catches ImportError's if the admin.py doesn't exist. The trouble with this is that if admin.py does exist, and throws an ImportError of its own, Django will also interpret that as a missing admin.py – which can be misleading.

The only solution I can think of to more accurately handle this would be to programmaticaly examine the traceback to determine where the ImportError is thrown. If the traceback is one level deep, we know that the ImportError was thrown because the module doesn't exists. If it is greater than one level then we know the module was found, but has thrown an ImportError of its own.

Here's a simple proof of concept:

#check_import.py
import traceback
import sys

def check_import(name):
    try:
        __import__(name)
    except ImportError:
        exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
        return len(traceback.extract_tb(exceptionTraceback)) > 1
    return True

print check_import("os")
print check_import("noimport")
print check_import("anotherlevel")
#anotherlevel.py
import thismoduledoesnotexist

This produces the following when run:

True
False
True

It would be nice if Django did something similar for its implicit imports. I think the best behaviour would be re-raise the ImportError if it the module does actually exist. That way, it is clear what the problem is. I may attempt to write a patch at some point, unless someone knows of a better solution.

 

On Ego and Software Development

March 28th, 2009

I've been thinking a lot about ego lately, and how it applies to software development. Writing software – like any other human endeavor – is influenced by the need to be recognized and hopefully praised for our work. I am no different; it is most likely ego that drives me to release open source software and to refine it even when there is no direct benefit to be had for that work. In this respect, ego is beneficial as it compels me to writer better code, but in the context of a day-job work, ego is a liability.

 

Improving video player applications with an old-school feature

March 25th, 2009

Dear video player application writers, there is a simple feature that would improve my movie watching experience greatly. A feature that I have never seen implemented – on Linux or Windows, and that is the ability to remember the position in a video file where I stopped watching. With current video players, if I want to finish watching a movie that I started watching the day before, I have to drag the slider around and to do a mental binary search to find the point where I left off. What I would like the player to do is to remember where I left off and start playing from that point when I open the file again.

A VHS cassette

Remember these?

There is an obsolete piece of technology popular in the 80s and 90s that had this feature. You may remember it; the VHS cassette. Granted, it was a side-effect of the antiquated way it stored video, and not a design feature per se, but that doesn't make it any less useful. When you inserted a VHS cassette you could be pretty confident that it would start playing at the point where you left off…

It wouldn't be difficult to look up the last played position of a movie file and restore it when the file is next played. A filename and file-size is probably adequate to identify a movie file, and the player would only need to keep track of a small number of recent files. On operating systems that support it, the current play time could be stored in an extended file attribute, making it independent of a specific application.

However it is implemented, I think this is one old-school feature that deserves a come-back!

 

My name is Will McGugan. I am an unabashed geek, an author, a hacker and a Python expert – amongst other things!

My Blogs
Search for Posts
Popular Tags
 
Archives
2009
 
Recent Comments
Dear Sir, I recently purchased your book, as part of my decision to quit science and retrain as a computer ...
- Malcolm Sutherland on Table of Contents for Pygame Book
red hair is common in north western Europe. Why dosn't it get more common in a gradient towords the north ...
In a software developer, the ego factor might be called for in the sense that a developer needs to be ...
Atree, I'm afraid there is no documentation as yet. You were on the right lines, here is how to do ...
- Will McGugan on Django Techblog markup system
i have try: {..summary} {..} and: but it take no effect :-(
 
© 2008 Will McGugan.

A technoblog blog, design by Will McGugan