Archive for the ‘Programming’ category

Updated SoundSwitch

June 23rd, 2011

I updated my SoundSwitch Application.
You can install it by going here.

The purpose of SoundSwitch is to be able to easily switch between  2 or more Audio output devices.
For example, I have a wireless headset and my regular speakers.
Both are detected as a separate audio card in the sound panel.
The only way to switch between them is to open the playback devices panel and select one of them and choose to put it as default device.

That’s such a hassle!
With SoundSwitch you just press Ctrl+Alt+F11 and it toggles between the cards you selected.

Todos:

  1. Let the application start up when the user logs in. (currently you have to start it yourself)
  2. Allow users to set their own shortcut (instead of  Ctrl+Alt+F11)
  3. Make the configuration easier. (Won’t be possible I’m afraid though)

UPDATE:
Changed the hosting to CodePlex, apparently you can host ClickOnce applications there!

jQuery scroll to element plugin

June 14th, 2011
$.fn.scrollTo = function(duration){
    if(duration == null){
        duration = 1000;
    }
 
    var offset = $(this).offset().top - $(window).height()/2 + $(this).height();
    $('html,body').animate({
       scrollTop: offset
    }, duration);
}

This small plugin will scroll to a selected element when calling .scrollTo() on it.
It has an optional parameter which specifies the duration of the scroll.

For example, if i would have a div with the id “#mydiv” I would let the screen scroll to it using this code:

$('#mydiv').scrollTo();
 
//Or, if you want the animation to be slower
$('#mydiv').scrollTo(2000);

The top of the selected element will be displayed at 50% of the screen unless the scrollbar is too short, then the element will be lower on the screen.

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

Setting a default value for input fields

August 4th, 2010

I needed a textbox that has a default value, when you click it that value disappears and when you deselect it and nothing has been entered the default value appears again.

I wanted something simple like adding a class to a textbox and it should just take whatever you set as value as it’s default value.
And preferably a different font color when the default value is visible.

» Read more: Setting a default value for input fields

Using widgets recursively in Yii.

August 1st, 2010

I am working on a personal project where I have an class which has children of the same type.
So each object has a parent attribute and a list of children. This way a sort of hierarchy can be made.

I wanted to generate some html for displaying this recursive list decently. My first thought was to write a recursive function which echoed out the required html. Almost instantly I realized this is a terrible way of achieving what I wanted.

A better way would be to use some kind of sub-view. In Yii I found out that this is done by using Widgets.

» Read more: Using widgets recursively in Yii.