Alsuren

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.

Advertisements

April 4, 2009

Notes on Kant

Filed under: Uncategorized — alsuren @ 12:55 am

These are a few notes from reading James W. Ellington’s translation of Kant’s “Grounding for the Metaphysics of Morals”. It’s mostly from stuff I wrote in my copy of the book, so if you want me to post page numbers/context for anything, shout. Most “quotes” are paraphrased either to represent my take on them, or for comic effect.

Intro (by Ellington): “This book is meant to be an introduction to Kant’s ideas. I will now proceed to run over them all in a summary as if you’ve already read his entire collected works.”… in an intro to an intro. Nice work.

Section 1: “From the Ordinary Knowledge of Morality to the Philosophical”

“A will has ‘moral worth’ due its motivations, rather than its actions.” (obviously, it is difficult to analyse this kind of thing externally, so you could probably infer from this a “judge not”-like statement.)

“There is a distinction between instinct and reason.” (which I’m not sure I agree with).

“Any action not explained by anything like self-interest will be explained be ‘duty’ and therefore have ‘moral value’.” (all other actions seem to be considered neutral)

At the end of this section, I have a few notes on the Categorical Imperative.
“You should always be able to desire that your policy should become the policy used by all.” (Kant’s formulation refers to a ‘maxim’ guiding an action, whereas I prefer to think of ‘policy’ guiding actions/choices. )

So when evaluating policy, I would say at this point that there are two approaches:
a) Pick the policy which gives you the highest expected reward if you follow it and everyone else acts normally (This will be morally neutral according to Kant).
b) Pick the policy which gives you the highest expected reward if everyone follows it (This will have ‘moral value’ according to Kant).
Kant seems to be wary of condemning any action to having negative moral value, but I’m not, so I’m going to say “Any policy which gives you really obviously poor expected reward in both of these cases is immoral.”

Section 2 “Transition from Popular Moral Philosophy to a Metaphysics of Morals”

“If it is useful, it can’t be said to have moral worth.”

There are a few examples of moral decision problem, which are evaluated under the categorical imperative. The charity example is the most interesting, as I suspect that you could probably make some extra assumptions so that giving to the poor becomes immoral under my formulation.

Also, it seems to be suggested that “I am not just a means to an end; I am moral so I am an end in myself” + categorical imperative => “I must not treat him as if he is just a means to an end” which I don’t think follows. Luckily, it’s possible to read the rest of the book without agreeing with this conclusion.

He then outlines the concept of a “kingdom of ends”, as in a community of ‘moral’ citizens (ones who follow the Categorical Imperative) The idea is that there is no need to have externally enforced laws, as each citizen legislates for himself by applying the categorical imperative to all of his decisions.
I think it’s an interesting thought experiment, and if anyone fancies running a simulation comparing the evolution of a kingdom of ends against a kingdom of nature (morally neutral, under my formulation) give me a shout.

He says that in the kingdom of ends, everyone acts as a “supreme legislator” (I agree) but he then says that they can’t be motivated by self-interest (I don’t agree: I think that being moral under *my* formulation provides a great simplifying assumption, so a self-interested party without infinite time for logical reasoning might expect greater rewards more quickly by acting morally)

He then goes on to introduce a concept of “reltive values” (“market price” for skills etc, and “affective price” for humour etc) and says that they are completely different from an “intrinsic worth” for morality.
My objection to this is that under his formulation, for a maxim to have moral worth, “it should be desired that it become the universal maxim”. I can only assume that it must be “desired” for a reason, namely that it would increase the availability of the aforementioned relative values. Therefore, “intrinsic” moral worth is still surely dependant on these relative values. I think you can either conclude this, or conclude that someone could desire the end of the world, and therefore be completely moral for going on a murderous rampage.

Somewhere towards the end of the second section, Kant starts punching holes in his own concepts, as I’d been waiting for him to do for half the book already. He mentions that there’s no way to construct a true kingdom of ends, as there is no incentive for people to behave morally.

He also refers to his universal impirtive as “synthetic”, which is philosopher for “I made this shit up. Maybe I’ll justify it some other time”

There is at some point a comparison between the categorical imperative and “do as you would be done by”. It’s essentially a less strict condition for morality, which removes the convicted man’s cry of “You wouldn’t want to be sent to prison. This is immoral.” I quite like that. Shame it’s a bit too woolly, because he’s trying to apply it to every possible situation. It’s also a shame that he he adds loads of questionable assumptions without justification in order to get the formulations of “Treat others as ends in themselves” and “Rational beings must legislate universal law”.

Section 3: Transition from Metaphysics of Morals to Critique of Practical Reason.

I don’t have so many notes in this section as I read it a bit more quickly. Quite a lot of it is just more picking apart and limitations of the Categorical Imperative, which I had already concluded was pretty useless for encouraging moral behaviour in *anyone* in Kant’s form, but might provide the groundwork for a nice simplifying assumption at least.

Also included was “On a right to lie because of Philanthropic concerns”, which I have just realised I haven’t read. Maybe I’ll edit this some other time to add my views on that.

February 25, 2009

Music Visualisation Project Update

Filed under: Uncategorized — alsuren @ 11:56 am

Okay, so this is a *very* long overdue post about my project. I have an hour before lectures, and I’m running a couple of computer jobs that will take a while. (edit: about half an hour)

As some of you will know, my 4th year (MEng) project is all about music visualisation. The idea is to create a system that will take MP3 files, and turn them into thumbnail images. Songs which sound similar should also *look* similar. The idea is that it should act as a visual memory aid for DJs.

Right at the moment, I have a “baseline” system, which produces images like
this. Looking at the images from the baseline system, there don’t seem to be many similar-looking images (If you see any other than The Fox and Christopher Columbus, post a comment below).

So what’s going wrong?

There are a lot of configurable parameters of the system, so it might just be that it needs tuning. If you want to compare the performance of the system with a few parameter changes, try exploring the matrix found here It might also be that I’m trying to pack too much information into each (very small) image. Currently I’m trying to squeeze 20 independent (scalar) pieces of information into each 20×20 image. What I need to try next is cutting down to the 3 to 10 pieces of information which are actually relevant, and making 50×50 images. I think I will also need to gather different pieces of information to include (initially extracted by hand, and then automatically extracted).

Also, the human eye is not very good at comparing brightness. I will try adding fake colour to the images, and see whether a different colour map performs better.

I’ll post something else this afternoon/evening. Have to run to lecture now.

January 24, 2009

Ideas for Programming Aids

Filed under: Uncategorized — alsuren @ 1:37 am

First off, it should be noted that I’m spending my friday night posting on my blog. Yes. There’s definitely something wrong with that. Also, I have just instinctively written this document in restructuredtext, and can’t be arsed to change it to html for wordpress. If anyone knows of blogging software that supports restructuredtext, I might be tempted to change once again.

The rest of this post is me trying to describe how I’ve recently found myself developing software, and how I think a software development/testing framework should behave in order to fit in with this methodology.

I write all of my code in python, making extensive use of ipython and scipy/matplotlib. Most of the things I write are to solve specific problems in a very throw-away manner (I basically use python in the same way most people might use perl/bash/matlab scripts).

The development method goes something like this:

* Open up kate, and add the standard boilerplate to a python file:

#!/usr/bin/python
from __future__ import division
from sys import argv
import numpy as N

def main(arg1=”default value”, arg2=”something else”):
“”” Does whatever. “””
pass

if __name__ == “__main__”:
main(*argv[1:])

* Start writing some code in kate, (generally top-down to solve the problem at hand, with functions missing).
* Implement a few of the lower level functions (without really thinking about speed or comments, but generally following most of PEP8.)
* Either %run whatever.py in ipython.
* Fix missing/broken implementations/add “return result” to the end of some functions.
* %run whatever.py and repeat until it does what you want it to do.
* If it’s taking too long, %prun main(some parameters that will take less time to compute results for) and check for the function with the highest cumulative time (say it’s called slow_function().
* Rename slow_function() to canonical_slow_function() and write another implementation with a few loops unrolled or something.
* Add “assert result_of_slow_function == canonical_slow_function()” to wherever the function is used.
* Reload, and %prun main() again (and notice that the new implementation takes 10x less time than the canonical one. Also feel safe that it’s numerically correct, since there is a built in regression test)
* Comment out the assert, and run again with the parameters that were taking too long before.

So what do I want from a rapid-command-line-development framework?

* A command-line program a bit like django-admin.py (or python paster or whatever) or a set of magics/functions that can be used from within ipython, that will create a single python file with the boilerplate for a simple command-line app, and open your default editor. It should look something like this (and already be executable on unix):

#!/usr/bin/python
from alternatives import costs, reimplementation
from commandline import run_if_main

AUTHOR=”David Laban ”

@costs.cputime
def main(arg1=”default value”, arg2=”something else”):
“”” Does whatever. “””
for i in range(100000000000):
pass # waste time

@reimplementation(“faster”)
def main(arg1, arg2):
pass

run_if_main()

* Running this program with no arguments should run main() with its default arguments (see below for more information on how this should be implemented)

* Running the program with the option “–help” or “-h” will print a usage message of the following form:
whatever.py [–arg1=”default value”] [–arg2=”something else”]
Does whatever.

* The admin script/ipython magics should also be able to do things like:
* script-admin commit -m “First revision of whatever.py”, which will search for evidence of a revision control system, and then commit (running any tests that exist first, obviously, and prompting for any)
* script-admin set-defaults-from whatever.py, which would take any modifications you make to the boilerplate variables, and save them somewhere useful, so that they become the defaults.
* script-admin create-manpage whatever.py, which would generate a manpage with the author information at the bottom.
* script-admin create-bashcomp whatever.py, which would create a bash completion file
* script-admin install whatever.py /usr/local, which will install the script, and man pages to the prefix /usr/local.
* script-admin convert-to-project whatever.py, which will take the script, and put it into a directory with a setup.py and its own revision control (if possible, keeping any revision history from the original file intact)
* script-admin test whatever.py should run whatever tests are associated with whatever.py.

* The framework should always try to import the module and inspect it dynamically, rather than trying to parse python code. All of the above things can be done dynamically.
* run_if_main() may use stack introspection or whatever means necessary to make the API simple. It should look for a local or global variable in the calling scope called main, check whether it is callable, and inspect its arguments list. It should then use the optparse module or similar in order to forward command line arguments in to main. If no main function exists, it should look for objects of the form “command_*”, where * is the first command-line argument. It should then strip the first command-line argument, and act as before, but using command_* instead of main.

* script-admin should be written using the commandline module

* alternatives should focus on functions without side-effects. This means it can use concepts like invariants (haskell-inspired) for testing.

* A possible implementation of the alternatives module might do something like this:
* Run either the first or second implementation (or both) of main() with the default arguments taken from the first one (initially at random, but probably using something smarter once there’s some data ( I’m sure Alex will be first to suggest http://www.gene-expression-programming.com ) ).
* Optionally store statistics about the CPU time in ~/.alternatives/costs, so that the best implementation can be used in future.

* All decorators in alternatives should have a lightweight alternative implementation which doesn’t add any overhead to function calls or tracebacks, and only incurs a small penalty at define-time, when the choice of best implementation is made, and docstrings/default arguments are copied from the canonical implementation.

* alternatives should include a function/command called “test”, “train”, “sleep” or “dream”, which goes through all functions registered in ~/.alternatives, and test them against each other (possibly using cached input values from real runs, and possibly using the coverage testing methodology). This should run as a separate process at nice 19 or something. There should also be a function/command called “report”, which produces either profiler-like output data, or matplotlib-quality graphs for each of the functions.

* If the @alternatives.costs.cputime decorator is used, then the return values should always be the same, and the implementation which returns in the shortest time should be chosen. It should be possible to create other cost functions that take the return value as in input, and return a score. This might be useful for evaluating alternative implementations of things like video compression or stochastic optimisation schemes.

So these are two things that I think would be useful on a reasonably regular basis. I probably won’t be thinking about implementing such things any time soon though, but if anyone can think of anything that already does what I want, please tell me.

November 19, 2008

Dance Pointers

I might have called this post “Dance Philosophy”, or “Best Practices”, but that would imply that I have checked it for self-consistency and so-forth. This is a set of beliefs/observations that I have about leading, and I am completely happy if other people don’t hold the same beliefs, but I will defend my beliefs if attacked (maybe “Dance Theology” would be more appropriate). I make no comments about which bits apply to following as well.

1) If you are having trouble dancing with a follow (of any ability), then a good solution is *always* to become a better lead.

1.1) Telling a follow that she’s doing something “wrong” is *always* the wrong thing to do.
1.1.1) In most cases, she can feel that something’s wrong, and so doesn’t need to be told.
1.1.2) You will (in most cases) be incapable of explaining it in words.
1.1.3) In most cases, it’s because you’re doing it wrong yourself.
1.1.4) If you can lead the difference between doing it “wrong”, and doing it “right”, then it will be more effective than words (see point 1).
1.1.5) Telling someone that they should *not* do something is dangerous.
1.1.5.1) What you want someone not to do may be exactly what someone else wants them to do in some situation.
1.1.5.2) If you can show people alternatives, and let them pick for themselves, this will reduce the number of people doing the “wrong” thing, without removing their ability to do so if the need arises.

1.2) If you find yourself wanting to “know” more “moves”, then this is a sign that your understanding of the things you already “know” is not strong enough.
1.2.1) It is more enjoyable to play with the subtleties of a few moves than a lot of different moves done in the same way.
1.2.2) If there are 7 independent layers of lead/follow (ask Andrew Sutton for a list and he will consistently produce at least that many (though they may not always be the same 7)) then you have at least 2^7=128 variations on each move. If you get bored with 2^7, then try 3^7 and so on.

1.3) If you are getting a lot of awkward moments, then you need to go back to basics.
1.3.1) If your connection is broken, you will be impossible to follow.
1.3.1.1) If your frame is too weak then your follow will not be able to feel what you want her to do.
1.3.1.2) If your frame is too rigid/jerky, it will break your follow’s frame. Similarly, if you break your frame by doing an awkward arm lead, then it will break your follow’s frame.
1.3.1.3) If you have too little tension, your follow may not know when you want her to move (especially problematic with fast music).
1.3.1.4) If you have too much tension, then your follow will feel too forced, and have no freedom to do her own thing (especially in slow music, and music she knows well). This is often referred to as a lack of responsiveness.
1.3.1.5) All of the above things affect all of the above things
1.3.2) Having “just enough” control to reliably lead what you want is ideal.
1.3.2.1) It is important to find out what “just enough” is for every follow.
1.3.3) If you are doing a lot of different “moves” then you both need to “know” them and get them “right” in order to avoid conflicts.
1.3.4) If you are doing lots of subtle variations on basic moves, then there will be no conflict if your follow ignores them.
1.3.4.1) If there is a conflict, then your “subtle variation” is not a subtle variation.

To be continued, I suspect.

If someone wants to expand this onto a wiki somewhere, please do so. I’m thinking that each of the points should be the title of a page, with each page being a stub with a “Consider first” link pointing to its parent, “Consider also” links pointing to its siblings, and “Consider next” pointing to its children. It would then be possible to flesh out each page with an “Examples” section.

November 17, 2008

209 Radio

Filed under: Uncategorized — Tags: , — alsuren @ 1:13 am

Over the past few years, I haven’t been listening to much radio, because my music collection has been so large (especially since finding http://jazz-on-line.com/)

Recently though, since breaking my decent phone, I have been listening to the radio more. After a little channel-hopping, I have discovered 209 community radio. I’ve been listening to it today, and it has been made entirely of yay. This is likely to be because it’s the weekend, and therefore full of real people. If you look at the schedule then you will see that this is not always the case.

The website also has links to an archive, and all content is available as both mp3 and ogg. I suspect that this touch was an ad-lib by whichever linux geek set up the website. (It really does look like it’s become someone’s personal playground, with a mixture of cool features and questionable style)

November 14, 2008

Versioned Home Directory, and other Ideas for Projects.

I have started using version control (bzr) on my home directory. This hopes to eventually solve a few problems:

1) Sharing settings with other people. This is something that I’ve been looking for a solution to for a while (there are standard ways to share apps and themes (kde-apps.org and pals) but not configs. If everyone keeps their configs versioned, then it should be possible to cherry-pick changes more easily.

2) Creating consistant settings across many different linux machines (as discussed in my colourhash post. (side note relating to colourhash: many graph plotting programs have ways of automatically assigning distinct colours. I will look at that at some point too.)

3) As a backup framework: If I have all of my settings under distributed version control on 4 machines, then when I accidentally delete large chunks of my home directory (like the other day, when cmake created a folder called ‘$HOME’ which I wanted to delete…) then I don’t lose all of my rss feeds, proxy settings (which I stil haven’t managed to get working again, thanks to KDE’s incredibly fragile socks[lack-of] implementation/configuration) and email settings (resulting in me not being alerted about emails for 2 days(>20 emails))[/rant]

The progress so far is a ~/.bzrignore file as long as your arm ).

Eventually, I plan to host it on launchpad (as soon as I’ve verified that it doesn’t contain any security-critical information (I don’t think I have anything else that I would have a problem hosting on launchpad. Reading the content of my other posts might give you an idea of my views on privacy.))

==== Technobabble-filled braindump below this line ====

If anyone knows how to do nested repositories (eg. so I can get bzr to manage my ~/src as well, and so that I can have sensitive information like ~/.ssh and ~/.gpg versioned in some way that lets *me* merge them between computers, but doesn’t expose them on launchpad) give me a shout.

In other news, I taught myself a bit of perl last night, when trying to add sed-style text replacement to pidgin (by hacking apart script called replaceX.pl whose interface was arbitrarily horrible, and only allows output replacement). I’m currently fighting with pidgin’s settings management to get persistent rules. If anyone wants it, get in touch. Otherwise, it will be in launchpad under ~/.purple/sed.pl when I get my home directory on there :D.

If anyone wants to give me input on the interface, it would be muchly appreciated. Currently, we have:
/sed foo-to-bar s/foo/bar/g
(to add a replacement rule)
/sed foo-to-bar s///
(to replace the old rule with an existing rule)

I’m thinking something more like:
/sed s/foo/bar/g
(to add a rule; a number will be assigned to each rule as an identifier)
/sed -l
(to list all rules, and associated numbers)
/sed -d #number
(to delete rules)
/sed -o s/foo/bar/g
(to only correct outgoing text)
/sed -i s/foo/bar/g
(to only correct incoming text)

Unfortunately, perl is a *horrible* language (doesn’t even have a concept of named function arguments) so the resulting code is unlikely to be anything I’m proud of.

While I think about it, I also had a load of ideas for python-based projects:
A man-page parsing command-line completion handler for ipython (and possibly bash, but bash scripts take so much longer to debug, and I get the feeling I will soon be using ipython as my default shell anyway.)
Given that debian policy forces all commands to have a man page, this is a pretty reliable way to write a powerful tab-completer. Also, since you only ever read the man page when the tab completion doesn’t work, you might as well get the tab-completer to read the man page for you.

A callback/decorator library for creating command-line programs, with an interface along the lines of:

#!/usr/bin/python
@clargs.handles("-f", "--filename")
@clargs.arguments("FILENAME")
def input_filename(filename):
    """The filename you want to read."""
    global input = filename

@clargs.main
@clargs.arguments("REPEATS", int)
def main(repeats):
     """Reads FILENAME to stdout REPEATS times."""
    text = open(input).read()
    for i in xrange(repeats):
        print text

if __name__ == "__main__":
    clargs.go()

It should also auto-generate help and man pages using the information given. An even more fun thing to do (with python3000) would be to use nose-style runtime inspection to to detect a function of the form:

def handle_filename(name:str):
    "The filename you want to read."""
    global input = filename

and make that handle –filename input.txt (maybe with an @shortopt(‘f’) decorator.

A subclass of numpy.ndarray that has named axes, and user-specified ranges, so…

likelihoods = semantic_array( ('t', 1000), ('x', -100, 100), ('v', -10, 10) )
do_something_with(likelihoods) 
# sum over v, and preserve the x and t axes.
position_likelihoods = sum(likelihoods, axis='v')
# get the best guess of x for each time t.
maximum_likelihood_x_estimate = argmax(position_estimate_probs, axis='x') 


A delayed evaluation library (might end up stealing a lot of ideas from scipy and sympy, with a good chunk of twisted to boot)
An interesting feature of python is that it doesn't have an assignment operator is *purely* a pointer-update. When you say "x=y" it just makes x point to the same thing y points to. This means that if you get passed x into a function, you can safely write x = x*10, and it won't modify x in the code that called you. This lack of side-effects (and all manner of other things) makes many python libraries look like pure-functional libraries.

On the down-side, it won't let let you override the assignment operator, so when you're dealing with large amounts of data, you can't re-use arrays without jumping through hoops. If X is a 1000000x1000000 matrix, your choices are:
X = multiply(X,10) # The canonical form, but it creates a temporary variable for the return value which is alive at the same time as X (potentially taking up twice as much memory as needed)
multiply(X, 10, output=X) # The numpy interface (potentially does the multiplication in place)
X *= 10 # works in-place, and is all very good, but what if I want to do something that's not +=,-=,*= or /=?

Then there are the slightly more hacky options, which involve delayed evaluation:
X == multiply(X, 10) # Override the logical equals operator X.__eq__ (the sympy method for writing symbolic equations). This is the most horrible, because it stops you being able to do X = (X==Y)
context.X = multiply(context.X, 10) # Override the attribute assignment operator context.__setattr__
X[:] = multiply(X, 10) # Override the item assignment operator X.__setitem__  (or sometimes X.__setslice__, I think)
Note that this last one feels quite like fortran, but it might be the least horrible of all the interfaces.


So how would these things work? A simple sympy-style one looks like this:
def multiply(X, Y):
    def deferred_calculation(output):
        numpy.multiply(X, Y, output=output)
    return deferred_calculation

# In X's class definition:
def __eq__(self, deferred_calculation):
    deferred_calculation(output=self)

The problem is that if you accidentally do X = multiply(X,Y) then X is just the deferred_calcuation function. That’s not very useful. On the other hand, if the returned “deferred_calculation” object can be made to behave like a numpy array, then you’re in for a win.

The fun stuff will start to happen when you start using these “deferred_calculation” objects, and passing them in and out of other functions, so you have a massive chain of deferred calculations. If you then include an interface for inspecting chains of deferred objects, you can start to write deferred-to-$LANGUAGE compilers, which would let you write “say what you mean” algorithmic code in python.

A way of writing twisted applications in a blocking style (using generator expressions).
This idea is in some ways quite similar to the idea above. I’m sure I sketched an implementation up somewhere (possibly on the eee), but I don’t seem to have posted about it. The jist of it is as follows:

In the example below, unblockify (implementation omitted) acts like a filter in two ways:
From the caller’s point of view, some_generator produces a sequence, but only things that aren’t deferreds get let out of the filter to the caller.
From the generator’s point of view, “yield” acts like a filter (or in compsci terms, a “map”). Any “deferred” objects sent through it get turned into real objects, and any real objects sent through it disappear. I’m still deciding whether to do something magical when None gets yielded. We’ll see.

 
@unblockify
def some_generator():
    while True:
        result_of_deferred = yield function_which_returns_deferred()
        yield some_immediate_function(results_of_deferred)

for out in some_generator():
    print out

While this program appears to be blocking, it shouldn’t cause unresponsiveness in GUI applications. This is because filter passes control to the twisted reactor when it’s waiting for each deferred function.

If anyone is interested in any of these projects, please shout.

October 27, 2008

Colour Prompt

Filed under: Uncategorized — Tags: , , , , , , , , — alsuren @ 9:25 pm

Inspired by konversation’s pretty colours for users in an IRC channel, and pissed off with forgetting which of the many (smaug, soup, pip, concorde, harrier, excalibur, telford, dl325) linux boxes I’m remotely logged into at any given time, I decided to assign a different prompt to each host. Since some of the machines share home directories but not installed software, hard-coding a colour into each machine’s ~/.bashrc wouldn’t hack it.

That’s enough talking… I present to you “colourhash”.

#!/bin/bash
# Save this file as 'colourhash', and put it somewhere in your $PATH
# Usage: colourhash [string] ; echo message
# Wherever you see a colour escape sequence in your ~/.bashrc (like 33[01;32m (bold green))
# Replace it with '$(colourhash)'
if [[ -n $@ ]]
then
# We have command line arguments
string=${@}
else
# We don't have command line arguments. Create a colour for this user@host combo
string=${USERNAME}@${HOSTNAME}
fi
#
echo -n ${string} | cksum | (read hash tail
# We only really care about the first word printed by cksum: a crc hash of $string.
# Also note that crc is not a cryptographic checksum. This is not important, since we
# only have 7 colours to pick from, so hash collisions will be frequent.
#
# The readable colour codes lie between 31 and 37
colour_code=$(( 31 + ($hash % 7) ))
# Magic escape sequence follows:
echo -en '33[01;'${colour_code}'m'
)

Right now I’m only picking one of 7 colours. Who fancies doing the birthday problem on that?

If I end up getting too many hosts the same colour, I will start thinking of ways to increase the number of colours available. There are potentially lots of colour combinations to pick from, but changing the background colour might look be a bit odd. If anyone wants to wrap this up and push it into ubuntu/gentoo, give me a shout.

This is a good reference for anyone adding colours to shell programs. Code to generate the above grid can be found here.

October 22, 2008

Apple Goodies

Filed under: Uncategorized — Tags: , , , , , , , , — alsuren @ 9:19 pm

Went into the apple shop today to get a Kensington lock for my eee. Ended up buying one of those bluetooth keyboards as well. I’ve decided that this style of keyboard is pretty much as good as it’s going to get until you start getting zigbee keyboards: It’s reported to have a pretty decent battery life (6 months) and decent range (though obviously not as far as you’d get out of a mesh network like zigbee). It’s pretty much only as heavy as the batteries, and the keys are far better than your average laptop keyboard. It’s even a toss-up between these and the ones on the MS ergonomic keyboard. If you like thin keys for fast tapping, these are probably better for you, but I’ve been brought up on microsoft keyboards, with a bit of depth to them, so I personally prefer the ergo. The ergo is also easier on the wrists (unsurprisingly).

I think the best thing about bluetooth keyboards is that they can be used on your phone (I had to install an app using the nokia “download” program from my phone, which took about 5 minutes, but n95 users get it pre-installed). It has the potential to turn my dad’s old phone (which I absolutely hate) into something even more productive than my old phone (which served me well, but is no longer with us, rest its soul)

It also turns out that the kensington lock isn’t suitable for me, since it’s right next to the VGA port, and the only time I will ever be locking it is in the lab, when it’s connected to a VGA screen, and plugged into the wall. What I’ve now bought is a bike lock (uber-thin 2-loop cable lock, and a padlock), which I can thread through the slot where the battery goes. Cost me a total of £10, compared to £25 for the kensington, which I took back.

Still haven’t done any work today other than getting wavread and wavwrite working for my project. I still need to port a few things to use the new implementations. At some point, I may push for their inclusion in scipy, because the python standard library’s wave module is really quite annoying to use, (It sends data in and out as byte arrays, and expects you to convert them to arrays of integers yourself. Also, it was truncating my files when it saved them. It has probably cost me at least a week’s hacking in total, if you count the time it took me to port my code over to scikits.audiolab and the time it’s going to take me to port it all back again now I’ve found out that scikits.audiolab’s setup.py doesn’t work with easy_install (because of the dependence on C’s libsndfile, which doesn’t get bundled in) and debug my wave-based implementation, once I port across my other code.)

I should get on with that examples paper, shouldn’t I?

October 21, 2008

Lindy Exchanges

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

Okay… I haven’t blogged about Edinburgh yet, so I might as well write something about both exchanges.

In terms of general style of dancing, I’ve been mostly sitting out of anything fast, because there can be less playing in fast lindy, and it becomes a test of your fast swing-outs (which are a completely different move from normal ones) rather than an actual conversation between the dancers and the music. Also, it’s possible to make the same song seem either fast or slow depending on what emphasis you put on which parts of which basics. I could do a blog about why I hate soil-dancing fast lindy, but that will have to wait. The only good fast lindy dance I had was with Meg, who usually needs really heavy connection, because she [almost always] delays the hell out of the start of swing-outs, so that she can come in at a stupid speed and force a heavy connection. It didn’t seem like it took any extra effort at all to do fast swing-outs with her than normal ones, because she just dropped the delay, and came in at the same speed.

At Cambridge, since going to lots of Tango lessons, and not much Lindy, I had the aim of being as subtle as possible, while still leading whatever. This had some good responses, especially from Heather, who said that I seemed responsive to what she was wanting. I was also trying to play with the feel of the basics I was doing to fit the feel of the music, after the musicality workshop (though generally without changing the footwork too much, since that would require a lot of bal-like-fu). My next challenge on the lindy front will be to incorporate the subtleties of the rhythm/melody into a standard basic like the swing-out. I do suspect that in order to get it followed, I might need to change the footwork in some noticeable way.

I think that a special mention needs to go to Erika. At Edinburgh we had some random fun dances (including one in which brownies and ice cream were consumed, if I remember correctly (I’ve decided that I can feed myself better when leading than following [blues])) but nothing that made me go “wow” any more than the dozen or so other amazing dancers at the exchange. Then, on the Sunday night, there was a blues after-party with a little alcohol and tea, and I nabbed a few dances with her. At some point she led some smooth blues on me, as an “I really love this kind of thing” demo, and she managed to make me do something with my arse that I may never understand. I gave it a shot at leading too and there was definitely a lot of melty. I was very tempted to just keep dancing with her forever, but I knew that attempting to do so would probably result in less dances in the future. I also became aware that the more I danced with her, the more likely I was to do something stupid (made even more stupid by the fact that I had consumed a variety of beers, ciders, and chilli that evening). I remember thinking that I wasn’t in much control of anything: Everything I led was dictated by a mixture of what she was [/we were] already doing and what the music was doing.

In Cambridge, on the Friday night and early (before one) Saturday night, we had strings of really playful dances. I felt like there weren’t more than a handful of basics that weren’t twisted and stylised somehow by one of us [insert reference to Andrew Sutton’s ideas on levels of leading and following], and there were a good few bluesy-flourishes thrown in too. I actually offered her all of the remaining dances of the night at one point, and would have done so if I hadn’t signed up to help out with the refreshments for the next hour. By the time I started looking for her again, she was pretty engrossed in dancing with James (another Edinburgh dancer). I spent the next half hour or so variously trying to pretend that I wasn’t supremely jealous (and didn’t, in fact, have a massive crush on her) and sleeping, so that I could help out with the packing up at 6 like I’d stupidly offered to do. I think I may have refused a few dances in that time which I never managed to get back (and at least one from Holly, who I felt like I had danced with at least half a dozen times that night anyway). When I eventually got my blues dance with Erika, it was dreadful. Probably a good thing, as it meant that I could more happily let her get back to dancing with James, and have a few decent dances with other people myself.

On the Sunday of Cambridge, we had some decent dances again. I caught myself leading slow tuck turns (in the same way) more times than I might have liked, but honestly, who cares: at least they turned out as slow tuck turns and didn’t morph themselves into one-footed pivots [mentioning no names]. I just love it when you feel like you’re picking up on everything in your partner’s body[/mind], as well as the music. Due to my being all bunged up and probably infectious, and breaking some of my own terms, I think that Erika came firmly under the heading of “I know this is a really bad idea… why do I want it so much?”

It’s probably also worth observing that Gill looked stunning in her saturday night costume, but since we were busy pretending not to know each other, I didn’t say anything. I’m sure there will be pictures on facebook for the curious.

In terms of health, my shoulder went to hell again on the sunday when we were at the pub. I think it was probably just one dance that was the last straw, after a weekend’s punishment. I really need to go easy on it over the next few months, and do all of the exercises that my physio showed me. I suspect that it may mean ducking out of troupe, which is very annoying, as I really wanted to be part of an ADC show before I left. I guess this gives me yet another reason to focus on Tango.

Older Posts »

Create a free website or blog at WordPress.com.