Sunday, September 14, 2014

Python: return a dictionary from MySQLdb query

By default MySQLdb library in Python only returns an array of values in the order you retrieve them. But you can return an dictionary by changing the cursor type to: MySQLdb.cursors.DictCursor:

cursor = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)

Have a try and have fun.

How to make MyObject serializable

1.  Implement one method to convert your Object to a 'Dict'. e.g.
class MyObject:
    #    ...
    #    MyObject has 3 properties: name (a string), number (an int), and parent (a MyObject)
    @staticmethod
    def serialize(obj):
        return {
            "name":   obj.name,
            "number": obj.number,
            "parent": obj.parent
        }

    #    ...

simplejson.dumps(myObjInstance, default=MyObject.serialize)
The cool thing is that dumps will, like every other JSON-serializable object, call the serialization method recursively. That is, obj.parent will get serialized as well, without any further action from you.

2. If all you want to do is 1-1 map instance variable names to their respective values, you can use some built-in Python magic. Whenever you want to serialize a more complex object (again, only using a 1-1 variable-value map), just call this line:
simplejson.dumps(anyObj, default=lambda obj: obj.__dict__)
For the given instance of MyObject, it will behave identically to the aforementioned serialize method.

Note: The 2nd one works for me, so I don't have to write a serialize method for my class.

Saturday, September 13, 2014

Detect the charset and then decode and encode with UTF-8

        # detect charset of this webpage
        chardit1 = chardet.detect(webPage)
        charset = chardit1['encoding']
        # convert it into UTF-8 if necessary
        if charset.lower() != "utf-8":
            print("charset=", charset, "convert to string in UTF-8")
            webPageInUnicode = webPage.decode(charset, "ignore")
            webPage = webPageInUnicode.encode("UTF-8")


For more information:

https://docs.python.org/2/howto/unicode.html#the-unicode-type