Wednesday, July 29, 2009

XML Document processing comparison between java and python

I had a project that requires xml document processing. At first, I was thinking to do it using Java, but since the project was not so big, I decided to use Python. I really have to say that it was a good decision. In my opinion, xml doc processing feels more natural in Python compare to Java.

Let's go straight to the code comparison.
There are many steps involved in java before actually getting the xml doc elements that match with xpath expression defined.

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true); // never forget this!
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("books.xml");

XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile("//book[author='Neal Stephenson']/title/text()");

Object result = expr.evaluate(doc, XPathConstants.NODESET);

NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getNodeValue());
}


I took the example from IBM website, here. You should check out the link if you are interested using xpath in java.

Now, in Python, using lxml library.

doc = etree.parse("books.xml")
nodes = doc.xpath("//book[author='Neal Stephenson']/title/text()")
print(nodes)


I mostly use java for my projects, pretty much happy with it, even though sometimes I wish that java has some syntactic sugars like in C#.
So, if you have the option to choose which programming language for your xml projects, give Python a try. I am sure you will like it.

Saturday, July 18, 2009

Set super+l shortcut key for locking screen in gnome

The default shortcut keys to lock screen in gnome is <ctrl><alt>l (it is small caps 'L'). I do not like that shortcut and prefer to use the combination of "super key" and "l", which actually defined as <Mod4>l. The keybinding GUI setting somehow does not accept any combination of super key with other keys. So, to be able to do that, you need to use gconf-editor.
Perform the following:

run gconf-editor,
option 1, from CLI:
$ gconf-editor


option 2, from GUI:
press <Alt>F2 to get the 'run application' dialog and type in gconf-editor and click 'run'

In gconf-editor window, go to the following key location, by expanding the keys tree:
/apps/gnome_settings_daemon/keybindings


then, double click 'screensaver' key and change the value to: <Mod4>l

and you are done. Test it by pressing super key and l.

There is also feature to search the key name and value, by pressing <Ctrl>f in gconf-editor. Actually, I found the correct location of the 'screensaver' key by searching it.

Friday, July 10, 2009

backup and restore MBR

Partition Table backup

Perform the following command to backup the partition table:
sfdisk -d /dev/hdx > /part/to/pt_info.sfdisk



Partition Table restore

Perform the following command to restore the partition table:
sfdisk -d /dev/hdx < /part/to/pt_info.sfdisk



MBR backup

Perform the following command to backup the MBR:
dd if=/dev/hdx of=/path/to/image count=1 bs=512



MBR restore

Perform the following command to restore the MBR:
dd if=/path/to/image of=/dev/hdx count=1 bs=446

Notice that you need to include the option "count=1 bs=446", otherwise your partition table will be overwritten.

Wednesday, July 8, 2009

Install chromium browser with flash plugin in ubuntu

Install chromium browser with flash plugin in ubuntu

Add the following repository in your apt's sources.list
deb http://ppa.launchpad.net/chromium-daily/ppa/ubuntu jaunty main


Add the key for it
$ sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 4E5E17B5


Perform the following
$ sudo aptitude update
$ sudo aptitude install chromium-browser


Chromium browser should be installed and ready to be used. If you already installed flashplugin you can add the plugin to chromium-browser. I noticed the browser takes a lot of resource after adding flashplugin, so I no longer user. To set flashplugin perform the following.
$ cd /usr/lib/chromium-browser/plugins
$ sudo ln -s /usr/lib/flashplugin-installer/libflashplayer.so