Thursday, May 14, 2009

Google is really open minded

I use Google Reader to read a few feeds. And it has this nifty little box called Recommended for you. Usually, that box is full of Python, PHP, Linux-related feeds. But today, something new: a website called rule #34 - If it exists there IS porn of it. Wow. I swear I don't have any kinky feeds in my reader :-\.

Upgrading to Ubuntu 9.04

1) The Desktop As always, I did a full reinstall on the desktop. I don't like having old packages and configuration files in my system, so I always to a fresh install no matter what. However, I have a /home partition, so all my settings, e-mails, documents, work is saved (I still do backups, just in case). Installing was a breeze (as always with Ubuntu), so here are my impressions (it's already been about two weeks since I updated), I'll start with the good ones. Boot time -- definitely improved. I had little faith in this as I saw it being advertised everywhere, but it seems that they have done quite a good job. My system definitely boots faster now (although I have yet to install additional startup software, like Apache, MySQL, VirtualBox, and the like). My bluetooth dongle is working again -- yay! The previous release had some buggy kernel module which rendered my dongle useless, but it seems it has been fixed. Finally, I can send stuff to my mobile phone without having to connect it via USB. Well, it still doesn't work perfectly, for example I can't send files from my phone to my PC, but otherwise everything works (even browsing the contents of the mobile phone on my PC). The new notifications are pretty cool, but only if you use a compositor, which I don't generally use. If you don't use a compositor, they suck. Thus, I have enabled metacity compositing (so I don't have to run Compiz). Ok, now with the bad ones: first off, what's with THE PINK!? Seriously, way too much pink in the new theme. I think that the 8.04 theme was the best (I love orange and brown). This theme is way too pink! Here's Transmission with no torrents: What the hell? That's so wrong. Anyway, on to the other bad things. For some reason, it seems Ubuntu uses more RAM than before, and I only have 512MB, which makes it constantly use 2-300MB swap, and that really overwhelms my hard drive. It may be because it's the first time I've installed the 64bit version (which uses a bit more RAM by definition, because memory pointers are twice as big). I've been having this PC for two years and a bit and I just found out a month ago it has a 64bit CPU :-\. Other than that, no huge improvement. Finally having Python 2.6 is pretty cool, it has some nice new features (like the json module). I'm waiting for a more substantial (and hopefully good looking) update with Karmic, although I might just switch to Archlinux in the mean time. 2) The Server Upgrading the server was a breeze. No conflicts, no problems, nothing. Smoo-ooth. Everything just works like before, I didn't need to change a thing -- Apache, MySQL, PHP, all functional. I didn't even have to change my small WSGI app, it just worked. All I had to do was just answer with "no" to all the "do you want me to overwrite this configuration file for you?" questions. Cool :)

Saturday, May 2, 2009

RhythmToWeb

Attention: RhythmToWeb has moved to Google Code

RhythmToWeb is a plugin for Rhythmbox (a music player for the GNOME desktop). It sends information about the currently playing song over the web. The information is sent as a series of GET variables: artist, title, album, genre, year, duration (seconds). The configuration dialog:
  • URL: This is the URL that is going to be called.
  • Interval: This is NOT the interval at which the URL will be called. This is the interval at which the plugin checks if the song changed (this has multiple advantages). The reason for polling at an interval is that this way you don't risk the server to be flooded if you change songs really quickly.
Installation
  1. Download: RhythmToWeb.tar.gz
  2. Extract the RhythmToWeb directory to ~/.gnome2/rhythmbox/plugins/
  3. (Re)start Rhythmbox, go to Edit → Plugins, find RhythmToWeb, enable it and configure it
If you have any problems with it (won't activate, crashes Rhythmbox, something doesn't work), run Rhythmbox like this: $ rhythmbox -D RhythmToWeb and send me the output, either as a comment on this post or on my email (it's in the plugin properties). As you may have guessed, I'm using this plugin for the widget in the sidebar of this blog (the "Now Playing" one). Thus, I will show the PHP script that is called when I change the song which creates a simple Javascript file: <?php $G = $_GET; function prep($var) { // Prepare a value to be included in a Javascript string $var = htmlentities($var, ENT_COMPAT, 'utf-8'); if (!get_magic_quotes_gpc()) $var = addslashes($var); return $var; } function test($var) { if ( strlen($var) && mb_strtolower($var) != 'unknown' && $var != '0' ) { return true; } return false; } $s = 's_nowplaying = "'; if (test($G['title'])) { $s .= '<b>Song:</b> ' . prep($G['title']) . '<br />'; } if (test($G['artist'])) { $s .= '<b>By:</b> ' . prep($G['artist']) . '<br />'; } if (test($G['album'])) { $s .= '<b>From:</b> ' . prep($G['album']) . '<br />'; } if (test($G['genre'])) { $s .= '<b>Genre:</b> ' . prep($G['genre']) . '<br />'; } if (test($G['year'])) { $s .= '<b>Year:</b> ' . prep($G['year']) . '<br />'; } if (test($G['duration'])) { $min = floor($G['duration'] / 60); $sec = $G['duration'] % 60; if ($sec < 10) $sec = '0' . $sec; $s .= '<b>Length:</b> ' . $min . ':' . $sec; } if (empty($G)) { $s .= 'Not playing anything.'; } $s .= '";'; $s .= <<<EOS if (document.getElementById && document.getElementById('nowplaying_info')) document.getElementById('nowplaying_info').innerHTML = s_nowplaying; else document.write(s_nowplaying); EOS; $fp = fopen('nowplaying.js', 'w'); fwrite($fp, $s); fclose($fp); ?> Then, in my blog I have a HTML widget with the following content: <span id="nowplaying_info">Loading...</span> <script src="http://znupi.ath.cx/felix/nowplaying/nowplaying.js" type="text/javascript"></script> And there you have it :). This is just an example, you could make a script that generates an image on which it writes that information. This way, you could include that image in your signature on forums or emails. Don't hesitate to ask questions either here or on my email address if you have them.

Python: alternative class initializers

You may notice that some Python modules have classes that define more than one initializer. Why you may want this is if you have more than one input method of initializing it. For example, you can have an RPC class (just an example) which can take both XML and JSON arguments when initializing. There are more than one way of doing this, but having a couple of initializers is the most beautiful way, in my opinion. Basically, we're going to have two methods on the class: from_json and from_xml. These will be @classmethods. From what I understand (I'm still quite new at Python), classmethods are somewhere between instance methods and static functions. The main difference between a class method and an instance method is the fact that the class method receives, as a first argument, the class itself, not an instance of it. Static methods don't receive a special first argument at all. Okay! Let's get to the code: class MyClass: @classmethod def from_json(cls, json_string): obj = cls() obj.created_from = 'json' # Do stuff, parse json_string, set attributes on obj, etc return obj @classmethod def from_xml(cls, xml_string): obj = cls() obj.created_from = 'xml' # Do stuff, parse xml_string, set attributes on obj, etc return obj def get_create_method(self): return self.created_from We have our two initializers. Here's some example usage: instance_one = MyClass.from_json(json_string) print instance_one.get_create_method() # 'json' instance_two = MyClass.from_xml(xml_string) print instance_two.get_create_method() # 'xml' Sure, classmethods aren't used solely for multiple initializers, but it's one of the most useful things you can do with them.