Software Freedom Day KL

http://softwarefreedomday.org/teams/asia/Malaysia/KualaLumpur

Software Freedom Day will be started in KL real soon. This year, we try to do something different. We will try to get unused but usable PC, then install Free Software on it. And give the pc to those in need.

The main event of SFD KL will be training, and install-a-thon. 

I still can’t decide the date yet. Because 19 September is the day before Hari Raya. Which will be a long holiday for a lot of people, so SFD will not be held like others. I hope to do it either by 5/6 Sept or 12/13 Sept.

So what do we need.
1) Old, Unused, but usable PC, or PC part. We promised a few, already.
2) Volunteer that is available.

The first meeting is Sunday 30 August 2009, at 6-7pm, in Burger King, Mid Valley KL.

“yield”ing some python

def even():
    for i in range(10):
        if i%2==0
           yield i


Was playing around with python simply because i am bored. I discovered the keyword yield. Which is a Simple Generator in Python. Which is in PEP 255.
For information, generator is a type of routines that can be used to control a loop. Instead of having a function that return the whole list or array. You can yield it one by one, using the yield keyword. As the function above. 
With the function above, you can. 

for i in even():
    print i
 

Or 
i=even()
while i:
    print i.next()

While the second one will ends with the error. I should find out what is the handler for the exception. 
I think it is pretty cool. But really I still trying to figure out what i can use this for. 

Automate interactive script with pyexpect

Sometime it is actually nice that automate interactive script, aka the one that need user to input something to continue. Such as django manage.py script

So I found pexpect, it is a python version of expect, which does that.

For example to use with reset the database in django:

import sys
import pexpect
#call the commands
result = pexpect.spawn(“python manage.py reset_db”)
#tell it to expect what should shows
result.expect(“Type ‘yes’ to continue, or ‘no’ to cancel”)
#send the results
result.sendline(“yes”)

Actually you can use it to run programs. Without the expect methods or sendline. The program will just runs

import sys
import pexpect

#just run the command

commands = pexpect.spawn(“ls -a”)
print commands



BTW, do check expect, it is the one that runs without python. Which is what  pexpect based on. Either way, it should be available to any linux distro. On ubuntu find expect and pexpect on synation, it is there.

output json on django

I just finish a job in django, and I use it for some ajax part of the project. So I generate json for this. Below is a snippet sanitize from the code my job use…

try:
import json
except:
from simplejson as json
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required
from django.shortcuts import render_to_response
def generate_json(request):
#it can be a dictionary too, anything supported by simplejson
somelist=[]
#do something here, to fill in somelist
encoded=json.dumps(somelist)
response[‘content-disposition’] = ‘attachment; filename=properties.json’
response=HttpResponse(encoded,mimetype=”application/json”)
return response

So there it is…

p.s I need a way to display source code properly on my blog, idea?