Alsuren

September 11, 2009

Telepathic Ramblings

Filed under: collabora — Tags: , , , , , , — alsuren @ 11:09 am

Okay, so I should probably inform people of what I’ve been working on recently.

A few of you might remember my cupsandstring Telepathy (IM) client from a few years back. Well I’ve recently revisited that, because someone was asking on IRC, so if you want to read the source, it won’t make you want to puke quite so much these days.

I made a skeleton Skype connection manager way back when too. (Don’t get excited: it’s based on the public Skype API provided by their UI, and only connects to your currently configured account). It never really got off the ground though. A few weeks back (go check the bzr log if you care) I picked it up again. I got it to the point where I could use it for text chat, and have been using it to replace the ugly bits of the Skype UI. It’s currently made of gaff and verbose, and has to be run in a konsole because I make it drop into a debugger whenever anything unexpected happens. This will continue to be the case until I have a decent testing framework for it.

Guess what my next project is: A testing framework! The spec has been discussed on the Telepathy mailing list. I’m trying to make it as generic as possible, so that I can try it on a well-tested connection manager (gabble) and then move on to butterfly, and then spyke when I’m feeling brave. Since I don’t want it to depend on the protocol too much, I’ve designed it around the idea of an echo bot (which can run remotely or locally) and a set of test scripts, which mostly just poke the echo service (like Skype’s echo123), and expect a sane reply. The bonus of this is that you can test interoperability between haze and gabble/butterfly/idle (and Kopete’s protocol code if it ever becomes telepathic) for free.

Due to all of my noise in Telepathy-related communication channels, daf suggested that I apply for a job at Collabora. I did so, and officially started on Wed. That was a fun day. I arrived in the office, and daf and wjt greet me with “Hey. We’ve thought what we want you to work on first: an echo service”. To which, I could only respond “What? Like this one?”, flashing my eee at them. Turns out their priority is something that works with gabble, and handles media streams (initially just using gstreamer’s audiotestsrc but eventually actually echoing stuff, and then doing things like disabling codecs). Hopefully I will get something workable by the time daf moves to America, but I would like to write a few automated tests for the text functionality before I start adding lots of crazy features.

Also, I’ve had to re-wrap most of the functionality from python’s telepathy.client library, because it contains too much magic, and is impossible to inherit from. I’ve tried to do it in a way that could feasibly be auto-generated from the spec, so that’s a project for when I’m done with my echo service. Maybe I’ll even push it into telepathy.client. It’s great to be working on Open Source software again. :)

June 12, 2009

commandline.py

Filed under: Uncategorized — Tags: , , , , , , , — alsuren @ 11:16 pm

Anyone who has ever written a quick-and-dirty demo script in python will probably have found themselves doing something like this at the bottom of their file:

if __name__ == "__main__":
    import sys
    main(*sys.argv[1:])


(I think everyone has done this at least once, so don’t try to deny it.) This is fine, but kinda screws your user over if they type “scriptname.py –help”.

What we really want is something that will give us –help functionality, and all of the useful stuff that optparse gives us, without having to jump through the many hoops that optparse makes us jump through. I think it would be enough if we were able to do this at the bottom of our file and get everything to work:

if __name__ == "__main__":
    import commandline
    commandline.run_as_main(main)

Well it just so happens you can. Just easy_install commandline (if you have setuptools) or go to http://pypi.python.org/pypi/commandline/ and download it by hand.

So what run_as_main() does is uses the inspect module to read the function’s docstring, and determine what arguments it takes. It then creates an optparse.OptionParser (using the information gathered from inspect). The OptionParser is then used to read sys.argv (this can be changed by passing an extra argument to run_as_main) and get a list of arguments to pass to the function. It then runs main with the right arguments by doing main(**kwargs).

So how do we use it? Well there is a simple example function included in commandline.py, defined like this:

def example_function(   string1, 
                        string2='something',
                        int1=1):
    """This is just an example. You should really try writing your own
    commandline programs."""
    print string1, string2, int1


If we run it, and ask for help, we get:

$ python commandline/commandline.py --help
Usage: commandline/commandline.py string1 [string2 [int1]] [Options]

Options:
  -h, --help         show this help message and exit
  --string2=STRING2  default="something"
  --int1=INT1        default=1
  --string1=STRING1  This must be specified as an option or argument.

This is just an example. You should really try writing your own commandline
programs.


You can clearly see where everything from the function signature is going. I hope you agree that this is a pretty powerful time-saving device. If you have any comments/suggestions, give me a shout.

May 21, 2008

Cool Python hack

Filed under: Uncategorized — Tags: , , , , — alsuren @ 12:58 am

Some of you might be familiar with python (or haskell, or erlang or …)’s map, filter, and reduce functions. You may also be familiar with lambda expressions, and think that they’re really ugly (especially in python)

If so, check this out (genuine input and output lines from ipython)


In [358]: filter(var==2, [1,2,3,4,5,4,3,2,1])
Out[358]: [2, 2]

In [360]: filter(var>=2, [1,2,3,4,5,4,3,2,1])
Out[361]: [2, 3, 4, 5, 4, 3, 2]

In [363]: filter(var>2, [1,2,3,4,5,4,3,2,1])
Out[362]: [3, 4, 5, 4, 3]

So how did I do it? Well if I could get it to indent correctly then it might look like this:

class Placeholder(object):
    def __lt__(self, other):
        def match(arg):
            return arg < other
        return match
    def __ge__(self, other):
        def match(arg):
            return arg >= other
        return match
    def __le__(self, other):
        def match(arg):
            return arg <= other
        return match
    def __eq__(self, other):
        def match(arg):
            return arg == other
        return match

var=Placeholder()

Now, I *think* this should generalise to map as well, for things like
map(var*5, [1,2,3,4,5,6,5,4,3,2,1] )
just implement __mul__ and friends. Annoyingly, there is also an __rmul__ function, so you don’t know which will be called. Also, don’t let python try to use __cmp__, because it actually checks the type of its return value (so you can’t return a curried function)

Okay, that’s enough g33k for tonight.

[edit: fixed lessthan and greaterthan signs]

March 2, 2008

IPython

Filed under: Uncategorized — Tags: , , , — alsuren @ 3:04 pm

Okay, so when someone said they were trying to use ipython, I thought they meant IronPython(an MS implementation of python). Turns out that they in fact meant. http://ipython.scipy.org/

I checked it out, and Oh My God: I have fallen in love with it already. Basically, it’s an interactive shell for python, but with colour, and with lots of cool features stolen from Ruby, Matlab and Mathematica. (note: I’ve never used mathematica. I hear it is very powerful for algebraic manipulation)

For those of you on linux:
sudo aptitude install python-scipy python-matplotlib ipython
ipython -pylab
t = arange(0, 4*pi, 0.1)
plot t, sin(t)
?

or check out http://www.scipy.org/Getting_Started

For those of you on Windows:
get python either from http://www.python.org/download/ or http://www.activestate.com/store/activepython/download/
get the latest version of ipython and pyreadline from http://ipython.scipy.org/dist/
good luck (you might want to look at pywin32.sourceforge.net as well. I don’t know what that’s for.)

Theme: WordPress Classic. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.