Archive

Archive for April, 2007

Django!

April 30th, 2007

pici which is an image hosting site I own is written in php, even though this is a language that I despise, hate and dislike. I figured I should do something about this and started reimplementing everything in mod_python which turned out quite ok, but when I was pretty much finished (except for some php-scripts that I left the way they were) I realized that even though my code was a LOT better than my messy php code, it wasn’t really optimal so I started researching web frameworks.

Not doing this before rewriting the entire page in mod_python was of course not very smart… was it? :)

Anyways, I started having a look at django and it’s really really nice. Been messing around a bit with it now for a few nights and I really can’t see any reason to switch to anything else now. I’m maybe 50% done with reimplementing pici in django and I just keep on getting amazed on how much prettier the code turns out when using the MVC-approach. Finally I have a nice clean separation between my templates and my backbone code, and I can let django deal with all the low level management (users, database-connections and so on).

This might actually lead to me doing a lot more on pici since I won’t have to feel physically ill due to being forced to develop using php any more :)

buffi Programming & scripting, Python, Uninteresting

Yay, pyweek!

April 22nd, 2007

My entry Dashman DX placed at sixth place in the individual competition in pyweek (out of 33 individual entries).
I’m very pleased with this!

Also the winner definitely deserved it. Which way is up was probably my favourite entry.

For the ratings go here:
Pyweek 4 ratings

buffi Programming & scripting, Python

Why I think map and filter is more “pythonic” than list comprehension

April 19th, 2007

I don’t really like the word pythonic, but I do kind of understand what the basic concept is.

That being said, I can’t see why people prefer list comprehension before map and filter, and there are even rumors about removing them from the language.

Let’s just take a common example: converting a string into it’s integer ordinal values.

First there is the “classical” approach, defining a new list, iterating through the string and appending each ordinal into the new list… something like this

text = "some words"
ordinals = []
for c in text:
  ordinals.append(ord(c))

This is obviously unpythonic.
Let’s have a look at the list comprehension method

ordinals = [ord(c) for c in text]

MUCH BETTER! But here is the catch… we still need the temporary variable c (which will even be declared in the global scope afterwards in python 2.4, haven’t checked in 2.5 yet). This is good, but map makes it even better.

ordinals = map(ord, text)

This is so straight forward that it can’t be misunderstood. While list comprehension translated to regular text would be something like:
“For each character in text, assign it to the variable c and then append ord(c) to the list to return”.
Map would simply translate into:
“For each character in text, apply ord to it and append the result to the list to return”.
There is no side effects and it is even easier to read.

Let’s have at one of the explanation of the word pythonic that I usually see gets passed around.

The text in there is pretty obvious, but let’s just have a look at one of the examples.

Unpythonic:

for (i=0; i < mylist_length; i++) {
   do_something(mylist[i]);
}

Pythonic:

for element in mylist:
   do_something(element)

The pythonic version clearly abstracts the underlying implementation, and is much easier to read, without needing any extra variables for iterating through the iterator… kind of like map and filter compared to list comprehension?

There is however one thing that list comprehension does better than map and filter, and that is applying multiple mapped functions or filters at only one passing. However using simply this as a reason for removing map and filter from the language feels somewhat silly.

buffi Programming & scripting, Python

Abstract art using python and file structures :P

April 12th, 2007

I was bored the last few nights and played around a bit with python and pygame and wrote a small app that takes a directory and then draws a structure depending on the underlying directories and files.
I probably won’t get around to finish this so I’ll just post a few screens since some turned out pretty! :)

Color is decided based on the files extension and the radius of circle is based of the size of the file

Click them for large!

Structure om a python project (with file names)
dashmandx structure

My programming folder

Structure type one
struct1

Structure type two
struct2

buffi Programming & scripting, Python, Uninteresting

pyweek!

April 8th, 2007

My pyweek entry got finished in time :)

I present to you Dashman DX
dashman titlescreen
Download for windows
Download for all platforms (requires python and pygame)
Dashman DX Pyweek Project Page

It turned out quite awesome in my opinion. There’s a weird collision bug when travelling very fast upwards but otherwise I’m very pleased with everything.
I decided very early in the competition that I would make a game about wall-jumping since wall-jumping games are awesome and that was all the motivation I needed. Took me a few hours to plan the structure of the application and then like one or two hours to get a working prototype to build upon.
I then spent about a day tweaking the physics and controls until I liked what I got, and went on to write the level editor. Then the only thing left to do was glue together the pieces and make a bunch of levels. All in all a project without any major issues :)

I don’t expect to win the competition or even place well as I think that the game might be too hard for most people, but I’m very satisfied with myself for throwing this together in about 6 days. Especially since I’ve had school and work as well.

Looking forward to the next pyweek as my entry then will be even more kick ass ;)

buffi Programming & scripting, Python

kernel panic’s and mixed code

April 6th, 2007

Ageha (the server containing pici.se as well as buffis.com) had it’s first kernel panic a few days ago.
I’ve almost been a bit surprised over how well the server has handled the load generated by pici since it is rather old hardware, but everything has it’s bad moments I guess. A hard restart later and everything was up and running again anyways, so let’s hope that it wasn’t anything serious.

For the last days I’ve been coding an entry to pyweek and I’m almost done. My game is going to be a dash-based platformer with the ability to construct own levels and a bunch of other neat stuff, so I hope I’ll do well even though my graphics look lite crap :)
At least it’s been educational as always, and I feel that my grasp of python is starting to become rather solid. I just need a new (more serious) project to dig into when I’m done with this. Might try to write an emacs clone or something :)

buffi Programming & scripting, Python