jorgenmodin.net
2010-09-01
A buildout run from its own bootstrapped python
Just a quick note so I can find it again:
Because the recipe plone.recipe.zope2instance relies on zc.recipe.egg, it also supports the python option. However, the recipe plone.recipe.zope2install did not support that nice option. I added the option and made a new 3.3 release.
Märkliga krigsrubriker i SvD om valsystemet
Man bör väl egentligen mer uppmärksamma tidningar när de gör bra saker, men ibland är det så enkelt att peka ut något som blev fel. Det finns kritik i Danmark mot det svenska valsystemet. Det kan man skriva om. Ett valbevakningsorgan skickar inte observatörer till det svenska valet för att det inte har någon kritik att framföra. Det kan man också skriva om. Men om man sätter ihop dessa diametralt motsatta nyheter i en krigsrubrik så kan det bli konstigt:

Det verkar nämligen på rubriken som om danskarna och OSCE tycker samma sak: Danskarna kritiserar, och OSCE är så uppgivna över sakernas dåliga tillstånd att de inte en ids komma.
Det svenska valsystemet har blivit föremål för en internationell debatt. Det är framför allt i danska tidningar som den svenska valsäkerheten blivit en het fråga
Läs mer: Valobservatörer nobbar det svenska riksdagsvalet | SvD
2010-08-28
Europeiskt försvar oroande svagt
Alltsedan andra världskriget har den demokratiska delen av Europa förlitat sig på Pax Americana - att USA har varit tillräckligt starkt för att garantera vår säkerhet.
Men USA står mycket möjligt inför stora ekonomiska problem som kan göra att de inte kommer att kunna - eller vilja - garantera Europas säkerhet med samma emfas som idag.
Inom politiken kan man vara oense om vad staten skall göra, men ett gott försvar räknas alltid till kärnverksamheten. Europa behöver ta ett större ansvar för att kunna försvara vår frihet och välstånd.
The beginnings of a sound policy today, he argues, might be for America to withdraw from a costly war in Afghanistan and pull forces out of Europe. Such a move would shock Europeans who hope that the impact of their own defence cuts will be softened by American help in times of need.
Läs mer: The cost of weapons: Defence spending in a time of austerity | The Economist
2010-08-20
Catch errors before they become Import Errors
Just a note to self:
Läs mer: do3cc: Don't catch import errors, use pkg_resources
2010-08-18
A python script to count bpm (beats per minute) on Linux
As I have written before, soundstretch can do bpm analysis on soundfiles on Linux. However:
- it needs wav rather than mp3 files.
- For my music it sometimes report half the correct bpm
I have written a script that wraps soundstretch and lame into a command that takes an mp3 file as input and outputs the bpm.
Internally, it converts the mp3 into temporary wav file with lame, runs soundstretch on the file, gets the bpm guess from soundstretch and fits that into a window of bpms.
I do not usually write command line scripts, so there may be problems in the below code, and there are certainly some hard coded assumptions. Use at your own risk. I intend to package this up and release it in some way, but until then, here is is in its present state:
#!/usr/bin/python
DOC = ''' This program detects bpm for mp3 files. It relies on lame and soundstretch being
installed on your system.
Usage:
%s <filename>
-or pipe filenames to it.
Example:
find . -name "*.mp3"| %s\n''' % (__file__, __file__)
import os
import pipes
import select
import shutil
import subprocess
import sys
import tempfile
## Define the window for sane bpm values. This may depend on genre of music. ##
BPM_WINDOW_MAX = 240
# Do not change this one
BPM_WINDOW_MIN = BPM_WINDOW_MAX/2
###############################################################################
def _get_bpm_from_soundstretch(output):
"""Gets bpm value from soundstretch output"""
output = output.split("\n")
for line in output:
if 'Detected BPM rate ' in line:
bpm = line[18:]
return float(bpm)
return None # Could not parse output
def fit_bpm_in_window(bpm_suggestion):
"""Double or halve a bpm suggestion until it fits inside the bpm window"""
if bpm_suggestion is not None:
while bpm_suggestion < (BPM_WINDOW_MIN):
bpm_suggestion = bpm_suggestion * 2
while bpm_suggestion > (BPM_WINDOW_MAX):
bpm_suggestion = bpm_suggestion / 2
return bpm_suggestion
def analyze_mp3(mp3filespec):
"""Uses lame and soundstretch to analyze an mp3 file for its bpm rate"""
# Make a temporary working directory for storing the wav file
# that soundstretch should analyze
workingdir = tempfile.mkdtemp()
wavfilespec = workingdir + "/tempwavfile.wav"
# Use lame to make a wav representation of the mp3 file to be analyzed
wav_command = 'lame --decode %s %s' % (mp3filespec, wavfilespec)
subprocess.call([wav_command], shell=True, stderr=open(os.devnull, 'w'))
# Call soundstretch to analyze the wav file
bpm_command = 'soundstretch %s -bpm' % wavfilespec
p = subprocess.Popen([bpm_command], shell=True,stdout=subprocess.PIPE)
output = p.communicate()[0]
# Delete temporary working directory and its contents
shutil.rmtree(workingdir)
bpm_suggestion = _get_bpm_from_soundstretch(output)
return fit_bpm_in_window(bpm_suggestion)
def process_input(mp3filespec):
bpm_suggestion = analyze_mp3(pipes.quote(mp3filespec))
if bpm_suggestion is None:
print "Unable to detect bpm for file %s" % mp3filespec
else:
print "BPM rate for %s is estimated to be %s" % (mp3filespec, bpm_suggestion)
if __name__ == "__main__":
argv = sys.argv[1:]
if select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], []): # input is piped to program
mp3filespecs = sys.stdin.readlines()
for mp3filespec in mp3filespecs:
process_input(mp3filespec.rstrip('\n'))
elif len(argv) < 1: # No pipe and no input file, print help text and exit
print DOC
sys.exit()
else: # Input file present
mp3filespec = os.path.abspath(argv[0])
process_input(mp3filespec)
It needs soundstretch and lame to be installed.
Editing services on Ubuntu 10.4
On CentOS 5 there is a GUI for turning on and off services. on Ubuntu 10.4 I could not find anything similar installed. So I decided to go the the Ubuntu synaptic package installer and have a look around. I found bum and rcconf that both claim to have GUIs for editing runlevels, which seems close enough to what I needed.
Bum
Bum did not work. Bummer. It exited with the message: "cannot open display 1.0". I am testing through VNC to a daemonized tightVNC server, and maybe bum cannot handle that. Unrelated to this I had the same error message with an X utility called xcutsel, so maybe it is tightVNC doing something wrong, I do not know.
rccconf
rcconf worked, but it took me a while to figure out what key on the keyboard toggles a service on and off. It is the spacebar.

Screenshot of rcconf. No mouse support, use pgUp and pgDown to scroll (or arrows), space bar to toggle and tab to switch to next UI element (i.e. the OK button, and then the Cancel button).

Hur ofta borde man kolla hur börserna gått?
Varför skriver man om världbörsernas upp- eller nedgång som en nyhet snart varje dag på framsidan på de stora nyhetssajterna? Vore det inte bättre att ha en månatlig sammanfattning?
Eftersom börserna svänger varje dag, varje minut och varje sekund, så står det ju fritt att välja tidsintervall man vill rapportera. Att göra det till en daglig nyhet tillskriver dagsläget ett nyhetsvärde som det inte har. Om trenden är en fyraprocentig nedgång över en månad så är en tvåprocentig uppgång någonstans i mitten inte så intressant. Jag antar att man ändå rapporterar en snabb tvåprocentig uppgång för att vara först, eller i all fall inte sist, med nyheten.
Att rapportera det dagliga läget måste dock vara väldigt billigt att göra: Det är bara att läsa av förändringar under dagen, och lägga till ett känsloladdat ord: "Turbulens", "Uppgång", "Nedgång", "Oro". Det är inget fel att använda känsloladdade ord i sig för att beskriva en börs, känslor är i en funktion det vi använder för att koordinera en flock, men det blir lite mycket känslor upp och ner om man rapportera om det varje dag.
Nicholas Nassim Taleb rekommenderar i sin bok "The Black Swan" att man inte kollar börsen för ofta, just för att slippa den känslomässiga turbulensen kring rörelser som i det större perspektivet kan betraktas som slumpmässiga.
De ledande börserna i Asien steg under onsdagens handel i linje med börserna i New York. Undantaget var de kinesiska börserna.
Läs mer: Mest upp på Asiens börser - DN.se
2010-08-15
How to put an independent VNC server on Ubuntu 10.4
The following guide worked for putting an independent VNC server running on Ubuntu 10.4. With independent I mean that is separate from the desktop session running on the computer that is connected to the physical monitor. Instead the below guide will give you an "always on" desktop session independent of what you see on the monitor. One advantage compared to desktop sharing is that you can adjust the screen resolution to fit comfortably in a window on the controlling computer. Another advantage is that you can run the vnc session as another user than who is currently using the computer, or as the same user but with a different desktop.
This guide intends to show you how to build a headless server with tightvnc, such that you can remotely access the server’s gui using any vnc client. I’m still working on it, so if you have any comments or questions, please feel free to let me know.
Ubuntu TightVNC Server - Dave Lachapelle
I followed the instructions to a "T" and it worked. Lachapelle recommends you remove the geometry option for Ubuntu 10.4, but it worked fine on my 10.4 computer.
After install i ran the script as "/etc/init.d/vncserver start" once. Xtightvncserver then prompted me to define passwords for accessing the new desktop, one password for full control, and one for editing only.
You could extend the script to start several independent vnc sessions, to make the server work as a multiple desktop provider. That is how I use my CentOS 5 server (which is soon is going to be Ubuntified).
Update 2010-08-16, 2010-08-20
Currently drag and drop is broken in all remote desktop sessions in Gnome on Ubuntu 10.4, including then tightvnc. Everything else seems to work though, and in most cases you can get around drag and drop by e.g. instead do a cut and paste. However for e.g. reordering playlists in Rhythmbox there is no workaround. Actually I spend a bit of time trying to figure out why the heck drag and drop did not work, when everything else worked. A bug in Gnome itself was not the first thing that sprang to mind.
See here for bug report and workaround for the drag and drop issue:
https://bugs.launchpad.net/ubuntu/+source/gtk+2.0/+bug/587856
Basically, you need to slightly downgrade a couple of Gnome packages. I did this and drag and drop started working again. There is now what seems to be identification of the offending code upstream in the Gnome bug tracker, but it is not clear when a fix will find its way into Ubuntu.
The downgrade at the link above seems to be volatile. When you do other installs on your system, and possibly just straight updates, the downgraded Gnome packages will be replaced. I have currently done like this to fixate ("pin") the downgrade:
nano -w /etc/apt/preferences.d/pinGnomeForRemoteAccess
and in that file I put:
Package: libgtk2.0-0
Pin: version 2.20.0-0ubuntu4
Pin-Priority: 1001
Package: libgtk2.0-bin
Pin: version 2.20.0-0ubuntu4
Pin-Priority: 1001
Package: libgail18
Pin: version 2.20.0-0ubuntu4
Pin-Priority: 1001
Package: libgail-common
Pin: version 2.20.0-0ubuntu4
Pin-Priority: 1001
Package: gtk2-engines-pixbuf
Pin: version 2.20.0-0ubuntu4
Pin-Priority: 1001
I just performed this pinning so time will tell if it works, but it seemed to survive an apt-get update.
2010-08-26: Added version keyword to pin file
2010-08-13
Link - How to make Rhythmbox et al treat a storage as a player
Rhythmbox will not initially identify the device. To get rhythmbox to see the device, you simply place a text file into the root directory of the player named '.is_audio_player' (/media/Sansa View/.is_audio_player).You can also tell rhythmbox where to store files it transfers to the device:
audio_folders=MUSIC/
folder_depth=2
Taken from: Sandisk Sansa View on Ubuntu Linux
2010-08-12
Getting the code for Facebook's "Like" button
Here is a link going directly to a page on Facebook with boiler plate code for making a Facebook "Like" button.
This evening I tried filling out a form on Facebook that should give me the code for a Facebook button to put on a page. Trying with different browsers I could not get it to output any code, the submit did not seem to work. Analyzing the submit button, it submits to a url that if you just access it directly (i.e. a GET without parameters) you get some boiler plate code where you easily can replace relevant parts. I guess the form submit button will start to work again, but if not accessing the taget url directly works somewhat.
Recent Comments
2010-08-09 20:40:58
2010-07-19 18:42:38
2010-07-19 17:20:37
2010-07-07 12:01:32
2010-06-08 11:33:28
2010-05-05 13:11:30
2010-04-29 00:22:20
2010-04-15 22:06:20
2010-04-13 11:19:12
2010-04-09 22:29:01