Tuesday, May 29, 2007

Dynamic bitset for python

I started trying the Python part of Boost C++ libraries recently and found it surprisingly easy to use. In search of a mini-project, I started coding a Python extension module to expose the Boost dynamic bitset for use in Python. It started as a novelty for a quick talk at the DFW Python group but I got stumped by adding operators.. until last night.

Here's a recap:

The constructor can accept a string ('111000') or a number of bits or a number of bits and an initial value. You can have a very large number of bits - in the millions without a problem.

The logical operators (&|^) work as expected and even throw polite exceptions when the operands are of different sizes.

There is a count method to see how many bits are on, a test method that returns true if the bit at a given index is on, a flip (or toggle) method..

There are even docstrings!

Available at:
https://python.taupro.com/repo/Projects/boost_python

It may be interesting to build pure python version and then use both to implement Conway's Game of Life to see what the difference in speed is.

Saturday, May 26, 2007

DFW Pythoneers Meeting

We are winding up the current meeting and I am trying to capture what we talked about so that when newcomers ask, I don't have to scratch my head and say "um, stuff..".

As always a lively session, mainly driven by Jeff Rush, it has been a lively one with presentations and ad-hoc chats about topics like:
  • the Forrester Wave work that Jeff did (and I contributed to) about the use of Python for enterprise web applications, including a simple mashup
  • a presentation of named tuples aka nuples (something like an immutable dictionary)
  • a recap on the game that John Zurawski developed for a 48-hour competition
  • a quick look at Gizmo(QP), a python framework (yet another!)
  • an RSS feed reader
  • a quick peek at Pyjamas
.. and we had a drop-in from the local PHP group.

Monday, May 21, 2007

About Python Advocacy: Is(was) Looking For Help

About Python Advocacy: Seeking Four Code Samples for Forrester Research Survey - Jeff was looking for some help with this so I took on the mashup and came up with this: Link

I know it is simple but most of the good work in a mashup is done in javascript and I didn't want to spend too much time distracted from the purpose of demonstrating python.

I want to get around to playing with Mochikit and Pyjamas (separately or together) and see if I can dress up the sample a bit. Oh, and WSGI.. and SVG..

UTF-8 transcoder

I needed a quick program to convert from one character set to another, specifically from shift-JIS (a Japanese encoding) to UTF-8. I didn't really need the result but I did want to know if it could be *legally* converted. So here what I came up with:

#!/usr/bin/python

import codecs
import sys

print "opening file %s"% sys.argv[1]

fi = codecs.open( sys.argv[1],'r', 'shift-jis')
data = fi.read()
fi.close()

print "Writing results.txt"
fo = codecs.open('result.txt','w','utf-8'
fo.write(data)
fo.close()


I later added an option to specify the source codec.. and then after trying several japanese codecs, one after another, came up with the bulk transcoder to cycle through all japanese codecs until finding a suitable one (ie one that didn't throw an exception).

Sunday, May 13, 2007

Mad scientist creates robot fish



A team from Essex university has created a robot carp that can swim around a tank with real fish and avoid collisions. It hasn't learned to feed yet and the battery life is only 5 hours.

Code and chips, anyone?

Saturday, May 12, 2007

TuxDroid and D-Bus

D-bus is a local IPC protocol that allows applications on a single machine to signal each other and request or consume each other's services. Examples include the little notificiation pop-up that tells you your battery is charged.

Tuxdroid is a robot version of the linux mascot, available from www.kysoh.com. Developer pages here: www.tuxisalive.com/

Here's a tuxdroid service that lets other application speak with the mighty voice of Tux:


#!/usr/bin/python
# Borrowed from http://webcvs.freedesktop.org/dbus/dbus/python/examples/example-service.py

import dbus
import dbus.service
import dbus.glib
import gobject
import sys
sys.path.append('/opt/tuxdroid/api/python')
from tux import *
class TuxObject(dbus.service.Object):
def __init__(self, bus_name, object_path="/Tux"):
dbus.service.Object.__init__(self, bus_name, object_path)

@dbus.service.method("org.dfwpython.TuxInterface")
def Speak(self, hello_message, voice='Male'):
print (str(hello_message))
if voice == 'Male':
speaker = SPK_US_MALE
else:
speaker = SPK_US_FEMALE
pitch=100
tux.tts.select_voice(speaker,pitch)
tux.cmd.mouth_open()
tux.tts.speak( str(hello_message))
tux.cmd.mouth_close()


session_bus = dbus.SessionBus()
name = dbus.service.BusName("org.dfwpython.TuxService", bus=session_bus)
object = TuxObject(name)

mainloop = gobject.MainLoop()
mainloop.run()

Thursday, May 10, 2007

Snakeskin Pyjamas?

I was browsing around and I came across this project.. which promises that I can "build AJAX apps in Python (like Google did for Java)". A bold claim.. I'll have to try it out and see if it really is that easy.

It's not an encouraging sign that the webpage is borked (most links appear to point to something that went away) but follow the links to download and things improve.. Google Code is lurking back there.

Thursday, May 3, 2007

Tip for UPS users

Plug things in the right side!
We had a power glitch last night but we were fortunate; currently there are 200,000 homes without power in the Dallas area.

So the power went out for a second or so and the UPS didn't keep the computer alive. Why not? Because I had the PC plugged into the surge protect side, not the UPS side. Really should check these things..

Wednesday, May 2, 2007

Running Win2K in Qemu

I need to build a Python package for installation on Windows using distutils but I run Linux.. so I thought I'd try Qemu.

I built a disk image for Qemu using qemu-image, made an ISO from a Win2K CD and then installed win2k in a window on my main linux machine. All went well and the whole thing was installed in a couple of hours. Then I tried to run Windows Update. Boom.. gone. Hm.. maybe I'll try XP.