2/11/2007

Note Log in January


2/09/2007

decode and encode in Python

encode -> create a str
decode -> create a unicode str

In addition, you can't print a unicode string + "\n" together. This will confuse python program and fire an error.

print unicode string
print "\n"

This is ok.

This is because you can't print a unicode directly. To see if a string is a unicode string, print type(test_string). If it is a unicode string, you must print that by using test_string.encode("utf-8")

2/08/2007

UTF-8 issue in python: imaplib and MySQLdb

When I tried to save UTF-8 charset words to a MySQL database, I had a lot of problems. I finally figured it out.

The table in MySQL should be in utf-8 format. It can be configured in MySQL

When I connected to the MySQL, I must specify the connection to utf8: db.set_character_set('utf8').

I thought all emails have the same charset, however, I found that they have different charsets. Thus in the python code, I process them by using different decoder.

r,data =M.fetch(num, "(BODY[HEADER.FIELDS (CONTENT-TYPE)])")
m = email.message_from_string(data[0][1]) #data[0][1] is a string that contains the content-type
Message_encoding = m.get_charsets()[0] #get_charsets() return a list, the first one([0]) indicates the charset of the message body

if Message_encoding == "utf-8": #if the encoding is ascii, I don't need to decode that
Error_Description = Error_Description.decode("utf-8")
elif Message_encoding == "iso-8859-1":
Error_Description = Error_Description.decode("ISO8859-1")
elif Message_encoding == "us-ascii":
pass
else:
Error_Description = Error_Description.decode("utf-8", "replace")

2/06/2007

A Game that I played

The name of the game is "Zoom ShiWarZ PoFeiGeLiANo BiterMan". ShiWarZ returns the virtual ball to the people who give the ball to you. PoFeiGeLiANo is the person on the right hand side, whereas BiterMan is the person on the left hand side.
Three rules:
1. Can't say three consecutive same words in the group or from the same person
2. Can't say the name of the game
3. Can't zoom a zoomer. That means you must use ShiWarZ to a zoomer.

2/03/2007

Cookie can be written by Python and read by Javascript

If you set a cookie by using Python, you must specify the path to "/", which means it is valid for the entire site. The default path is "/cgi-bin/".
For example:
In Python:
cookie["SID"]["path"] = "/"

In Javascript,
document.cookie can read this one.

2/02/2007

chgrp

chgrp will change the group of a file

Common path in Linux

/usr/lib
/var/www
/etc/.host
/etc/httpd