Archive for the ‘Python’ category

Python in-memory zip file

March 6th, 2011

I needed this for something where I wanted to send the visitor of a website a bunch of files he selected at once.
An easy way to do this would be to add the files to a zip file and then send that zip file to the user.
Unfortunately Python doesn’t have an in-memory zip file library, you can only interact with zip files on disk.

After a bit of googling around I came to this StackOverflow answer.
That worked like a charm, and here is my more reusable version:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from zipfile import ZipFile
from StringIO import StringIO
 
class InMemoryZipFile(object):
	def __init__(self):
		self.inMemoryOutputFile = StringIO()
 
	def write(self, inzipfilename, data):
		zip = ZipFile(self.inMemoryOutputFile, 'a')
		zip.writestr(inzipfilename, data)
		zip.close()
 
	def read(self):
		self.inMemoryOutputFile.seek(0)
		return self.inMemoryOutputFile.getvalue()
 
	def writetofile(self, filename):
		open('out.zip', 'wb').write(self.read())

Save as “inmemoryzip.py” and import it as “inmemoryzip”.
This is quite limited compared to the standard ZipFile class, but this gets the job done for what I needed it.



English (auto-detected) » English

Solving 8 Queens problem on an 8×8 board with a Genetic Algorithm.

January 20th, 2011

I have an Artificial Intelligence course and one type of search algorithm handled is Genetic Algorithms.
Very interesting because it uses the principle of evolution to find a solution to a problem.

You start with a population of begin states and each state has a ‘fitness’ which indicates how close it is to a solution.
Based on that fitness you will crossjoin or mate 2 states. Higher fitness = higher chance of mating.

I implemented the example given in my AI course’s slides in python.
The example is trying to find a solution to solve an n-queens problem, in this case 8 queens on an 8×8 chessboard.
Briefly explained: you need to place 8 queens on a chessboard so that none of the queens can attack eachother.
(A queen can move like a Bishop and a Tower, horizontally and diagonally)

» Read more: Solving 8 Queens problem on an 8×8 board with a Genetic Algorithm.

Django on Google App Engine – 2

January 9th, 2011

The previous post was about the basics of setting up an application using Django on Google App Engine.
This one will continue on that as we follow the Django Tutorial for building a poll application, but with some modifications so it will run on GAE.

» Read more: Django on Google App Engine – 2

Django on Google App Engine – 1

January 9th, 2011

It’s been a while since I’ve written something here, that’s cause I’ve been quite busy with school. (recreational as well as educational :))
I was planning on writing a big series of blog posts about solving the exercises in SICP as a way of studying for my exams, but as I was solving them I noticed there are quite a lot of them and there are a bunch of pretty hard ones in there so that didn’t quite fit in my schedule :)
But I’ve made a start so I’ll probably continue it one day or an other.

As I was studying I naturally got bored of it, so I looked for more interesting things to do – Learn something else :D
I already looked for information about setting Django up on GAE (Google App Engine) but only found information scattered on different websites/blogs/.. but I finally managed to figure it out.

This post should serve as a single list of instructions to follow how to get Django running on GAE.

» Read more: Django on Google App Engine – 1

Using C#-style Events In Python – a more Pythonic version

August 28th, 2010

I was asked by Davy how my previous code would be written in idiomatic python.
First of all I had a vague recollection of what idiomatic meant but I looked it’s meaning up just to be sure.
And then I had to find out how this relates to Python.

» Read more: Using C#-style Events In Python – a more Pythonic version

Using C#-style Events In Python

August 26th, 2010

After reading this post by Davy Brion (C#-style Events In Ruby) I got interested in seeing whether or not I could do the same thing in Python.

» Read more: Using C#-style Events In Python

Programming Praxis – Word Cube

July 20th, 2010

This is my first solution for a Programming Praxis assignment, the Word Cube problem.

It’s definitely not an elegant solution. It’s almost as brute force as it can get, but I have only a negligible amount of training in this field so I’m satisfied for now with being able to find a solution at all.
It takes a few minutes to find all the possible words. I did however find quite a few more than the ones mentioned in the assignment.
Click here to see the source code.

» Read more: Programming Praxis – Word Cube