Saturday, February 13, 2010

Python and CSS Selector

Python has a lot of awesome modules for working with html documents. My favorite one is lxml modules. Last time I wrote about xpath using that module.
Now, it is time for css selector using lxml.cssselect.

Straight to an example of using it:

from lxml.cssselect import CSSSelector
css_sel = CSSSelector('table.tableSchedule tr')
html_doc = lxml.html.fromstring(some_web_html_string)
row_els = css_sel(html_doc)
print row_els


Sweet and simple.

I am still playing around with it and found that using direct descendant css code is not possible. Something like the following:

css_sel = CSSSelector('table.tableSchedule > tobdy > tr > td')


Please leave a comment if you are able to use ">" in the css code part.

Friday, February 12, 2010

django deep serializers

Django has it's own very nice serialization module. A very useful tool to serialize models in django projects. There is limitation to it, though.
When one serialize a model (table) which has relationship (ForeignKey) to other model, then you will only get the foreign_key, instead the whole object. Something like the following:

[ {
"pk": 1,
"model": "store.book",
"fields": {
"name": "Mostly Harmless",
"author": 42
}
} ]



In some cases, deep serializing is often necessary. Now, let me introduce you to this magnificent python module which extends django's built-in serializers. DjangoFullSerializers.
With this, you can get something like the following:

[ {
"pk": 1,
"model": "store.book",
"fields": {
"name": "Mostly Harmless",
"author": [ {
"pk": 42,
"model": "store.author",
"fields": {
"name": "John Doe",
"title": "Dr."
}
} ]
' }
} ]


I have used it and really satisfied with the result. Many thanks to the developer.

Thursday, February 4, 2010

Send key in Linux (Ubuntu)

I have a dell mini 9 laptop which only have Fn keys from F1 to F10. Sometimes I need F11 key for fullscreen browsing, and maybe even F12 key, but they are missing. This little thing annoys me, so I was thinking about a way to send key in Linux.
I did not want to install any other piece of software for this little thing. So, I was thinking about using what is already there in my Ubuntu (default) installation.
xte is just the tool that I needed.
To give you an example, here is the command to send F11 key to active window.

$ xte "key F11"


Pretty neat.

Notes: I just figured out to use F11 and F12 in dell mini 9. Use Fn+z for F11, Fn+x for F12. I tested both keys in Ubuntu 9.04.

Monday, January 25, 2010

dos to *nix text file

One of annoyances when working in both Windows and Linux OS is the different format of the text files created.
In Linux OS, when you open a text file (using vi) which happened to be created within Windows OS, you will see a lot of "^M" character. Really annoying.

In the past I used dos2unix to convert, but I don't use it anymore since I could just use sed, instead.

Here's the command.

$ sed -i -e 's/\r//g' file_name


To make it even easier, I created a shell script which contain that command, like the following:

#!/bin/bash
sed -i -e 's/\r//g' $*


... and you're ready to go.

Tuesday, January 19, 2010

Serialize Form to JSON

I came across a handy javascript function to serialize form to JSON, taken from here

$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name]) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};


add above script to your document, then if you are using jQuery you can just do something like the following:

$("form#data").serializeObject();


and as per need, simply use json2.js to convert the (JSON) form object into string:
JSON.stringify($("form#invite_users").serializeObject());

Monday, January 11, 2010

Python and RS323 (ESC/POS) Receipt Printer

Currently, I am working a bit with RS323 (ESC/POS) Receipt Printer. The most interesting part working with it is I do not need to have a driver. This is very helpful because I am planning to use it with Linux OS.
I decided to use Python, so anytime I need to switch to Windows OS, nothing need to be changed in the code part.

Since the printer is using RS323 connection, I need to find the simplest way of communicating with that device in Python. With no effort I found pySerial. Taken from the website itself: This module encapsulates the access for the serial port. It provides backends for Python running on Windows, Linux, BSD (possibly any POSIX compliant system), Jython and IronPython (.NET and Mono). The module named "serial" automatically selects the appropriate backend.

After installing pySerial, I started to play around with it. It is really simple and straight-forward.
It took me sometimes to understand ESC/POS commands, since I was totally not familiar with it.

Just to give some idea on how simple it is, here is an example:

import serial
ser = serial.Serial(port='/dev/ttyS0', baudrate=19200)
ser.portstr #check which serial port
ser.write('Hello World!\n')
ser.close()


I spent sometimes trying to figure out how to work with ESC/POS. One of the issue is the baud rate setting. This must be configured properly otherwise the printer will not print the data properly.

Another thing is that the printer will not print anything if you don't tell it to print data in it's buffer. One way to do this is terminate any string/data you want to print with '\n' (or ASCII decimal 10).

Next step is to know what ESC/POS commands available for your type of printer. I downloaded some docs from the vendor's website, but unfortunately there are only minimum info about ESC/POS programming.
Fortunately, ESC/POS commands is pretty much a standard, so I found some other documents from different vendor, by googling it, and got more informations.

Another interesting thing is that when I tried to print several lines of data in Linux, somehow only partial string in certain line that was printed. Got me confused for a bit, then I realized that this is printer buffer issue. The buffer in the printer is too small for the amount and speed of the data I sent to it. In this case adjustment need to be done, so the printer buffer won't get overloaded and discarded the incoming data.

I don't really like current receipt printer that I work on. Minimum documentations, vendor specific power supply cable, and some other stuff.
I haven't search much about other receipt printer from other vendors, but I am pretty sure that there are some that are more portable to work with.