Friday, June 19, 2009

Auto disable touchpad while typing using syndaemon

Auto disable touchpad while typing using syndaemon

Often time, when working on laptop, my hands touch (tap) the touchpad while typing, which is really annoying, cause I end up writing in different location. Disabling the touchpad permanently is not a good idea, specially if you don't have a mouse with you. Fortunately, there is a very neat tool that monitors keyboard activity and has the ability to disable the touchpad during that event. The tool is syndaemon.

usage example:
$ syndaemon -d -i 1

That would execute syndaemon as daemon and set 1 second as the delay period.

To automatically run this daemon, for example in KDE desktop environment, I created a script which contains the code above, make it executable, and put it in ~/.kde/Autostart folder.

Also check out synclient, taken from the 'man page', synclient - commandline utility to query and modify Synaptics driver options.

Friday, June 12, 2009

Victor Wooten Introduction

Victor Wooten Introduction

My favorite jazz bassist. Enjoy!

Processing xml document in python using lxml

Processing xml document in python using lxml

In previous entry, I discussed about libxml2. Now, I am going to discuss about lxml, which in my opinion is awesome. I really appreciate the developer team that create lxml. It makes processing xml document easier.
The best way to get it by going to this link and choose the appropriate version. As soon as you installed lxml, try to use it. Here is a sample of how to use lxml.

from lxml import etree
import sys

def getChildInfo(child):
return "id: " + child.get("id") + ", data: " + child.text + "\n"

xmlString = "c1c2"
tree = etree.fromstring(xmlString)
children = tree.xpath("//child")
sys.stdout.write(str(len(children)) + "\n")
for el in [ getChildInfo(child) for child in children ]:
sys.stdout.write(el)

childList = tree.xpath("//child[@id='1']")
sys.stdout.write(getChildInfo(childList[0]))
child = tree.xpath("//child[@id='2']")
sys.stdout.write(getChildInfo(childList[0]))

For more examples and other information about lxml, check out this link.

Thursday, June 11, 2009

Processing xml document in python using libxml

Processing xml document in python using libxml

I had to spent sometimes to gather information about processing xml document in python. There are several different libraries to use, such as minidom, libxml, etree, etc. During my work, I had been working with xpath, so I need xpath support in the built-in library that I work with, so I chose libxml. It is difficult for me to gather information on how to use that API. There are minimum documentations, guides, examples, and tutorials out there.
After spending sometimes, I became familiar on using it. I am writing this, so other people could simply learn on how to use libxml in more realistic situation. Many examples I found in the internet is way too basic, and does not suit my need.


example 1:
--------------------------------------

import sys
import libxml2

def main():
doc = libxml2.parseFile("data.xml")
ctxt = doc.xpathNewContext()
# get student elements in xml doc
res = ctxt.xpathEval("//student")
# output all properties for "student" element using xpath
sys.stdout.write(res[0].get_properties().content + "\n")

for chld in res[0].children:
if chld.type == "element":
# output all information within the child element
sys.stdout.write(chld.content + "\n")

doc.freeDoc()
ctxt.xpathFreeContext()

if __name__ == '__main__':
main()


--------------------------------------


example 2:
--------------------------------------

import sys
import libxml2

def main():
doc = libxml2.parseFile("layout.xml")
ctxt = doc.xpathNewContext()
# get all "property" element that has attribute "Location",
# which parent is "object" element that has attribute "pickList*"
res = ctxt.xpathEval("//Object[starts-with(@name,'pickList')]/Property[@name='Location']")
for r in res:
sys.stdout.write(r.content + "\n")

sys.stdout.write(str(len(res)))

doc.freeDoc()
ctxt.xpathFreeContext()

if __name__ == '__main__':
main()


--------------------------------------


It is also useful to see the source code of libxml.py, just to see what's going on under the hood.

Not long after writing this post, I found even better xml API, which is lxml. I will make separate post about this wonderful xml API for python.

Monday, June 8, 2009

"service" like redhat

"service" like redhat

One of many things that I like in Redhat is "service" tool to start, stop registered service (daemons). I realised that in my current ubuntu 8.10, this tool is already installed by default, which is really good. I am pretty sure that it will still available in newer release.
What about in Debian? To have such tool, you need to install "sysvinit-utils" package.

# aptitude install sysvinit-utils


to use it, simply type like the following
# service  start
# service stop
# service restart
# service status


In ubuntu (also in debian, if I'm not mistaken), you can use "tab" command completion, when you are trying to enter the . Very neat!

Saturday, June 6, 2009

Trim Filename Extension

Trim Filename Extension

There is built-in functionality in Linux shell to trim extension of a file name.
${filename_env_var%.ext} --> note that dollar ($) sign in the beginning is NOT shell prompt.

Usage example:
$ filename=trace_20090606.txt
$ echo ${filename%.txt}
trace_20090606 --> this is the output of previous command