Python: testing
June 21, 2009
I am impressed! When writing the header of a function, why not simply write an example and automatically test the function?
#!/usr/bin/env python import doctest def fact(n, stop=1, r=1): """ An iterative factorial function. n! = n*(n-1)*(n-2)*..*(n-(n-stop-1))*r stop fact(5,3)= 5*4 r fact(5,3,10)=5*4*10 >>> fact(5,3,10) 200 >>> fact(4) 24 """ while n > stop : r *= n n -= 1 return r def main(): doctest.testmod() return 0 if __name__ == '__main__': main()
More here: http://docs.python.org/tutorial/stdlib.html#quality-control
lighttp: python
June 21, 2009
To get lighttp to run python files, you need to tell it to do so. The standard configuration file that comes with Slitaz only allows scripts to be run in the /cg-bin/ folder. I guess this is personal preference – but I want to be able to run scripts everywhere! This can easily be changed ( the orginal line is commented out):
# $HTTP["url"] =~ "/cgi-bin/" {
$HTTP["url"] =~ "/" {
".cgi" => "/usr/bin/python",
".py" => "/usr/bin/python"
)
How hard can it be, right?
lighttp virtual hosts
June 21, 2009
Setting up lighty is simply too easy! On Slitaz it is a matter of downloading and start the server. Just point your browser to localhost and expect it to work!
When working with different projects it is handy to use subdomains or virtual hosts. Or – if you host different sites, virtual hosting is a must. It is also very simple to setup. Make sure the vhost.conf is read by lighttp.conf, and modify the former with your virtual host:
# /etc/lighttpd/vhost.conf
$HTTP["host"] =~ "(^|\.)bridge$" {
server.document-root = "/home/tux/www/bridge"
server.errorlog = "/home/tux/www/bridge/log"
}
Make sure you create the log file and set the approriate ownership on the file which is set in lighttpd.conf before restarting. I use www:www, thus
# su # chown www:www /home/tux/www/bridge/log
Make sure you add the hostname to your /etc/hosts:
# echo "127.0.1.1 bridge" >> /etc/hosts# /etc/init.d/lighttpd restart
Mute/unmute mic alsamixer in commandline
June 14, 2009
The sound control on lxpanel only handles one channel. But let’s say I want to control mute/unmute on mic by a handy shortcut! Two handy scripts fixes this, which can be assigned to a keyboard shortcut in Openbox or your preferred WM/DE.
#!/bin/sh amixer set Mic 80% mute > /dev/null #!/bin/sh amixer set Mic 80% unmute > /dev/null
You may of course choose any other control you want – ie PCM:
amixer set PCM 10%+ > /dev/null
Will increase sound by 10%.
Need to learn: regulare expressions
June 7, 2009
Oh – the possibilities!
sed, grep and a lot of editors support the use of regexp. It’s about time I learned it!
Need to learn: zsh
June 7, 2009
Bash, ash, sh and zsh – it’s a jungle! Zsh is apparently more filled with features and tuning capabilities than the others. The Bash is the default shell in many distrobutions. But it does not mean that it is the best shell out there!
Zsh is not new – it has been available for several years. But I am having a feeling its userbase has increased, as I see more references to it in forums in the cloud.
So – I will have to learn more about it! No way around it! I am working on some shell-scripts, and got a little frustrated when I realised busybox’ sh was not capable of everything I picked up of tips around the net. Bash does the job, but maybe zsh can do it even better?
Openbox “error”
June 1, 2009
I like to have my desktop setup in a certain way. Especially I like to have Firefox/Minefield in fullscreen mode on its own desktop. Openbox lets you automate this. You might want to use xprop, as the inline comments in rc.xml says, to identify windows. I did this – but I was fooled. I use Slitaz at the moment, and xprop gives the following information:
WM_CLASS(STRING) = "Navigator", "Minefield"
The name attribute returns the webpage you’re on, so that’s useless. BUT – using either Navigator or Minefield in the rc.xml fails – it is executed by “firefox-bin”! I spent way to much time on this, so I am actually considering notifying the developer about this behaviour. My rc.xml now looks like this:
<application name="firefox-bin"> <fullscreen>yes</fullscreen> <desktop>3</desktop> </application>