Python’s unicode documentation could use a little bit of love. It does a great job of discussing utf-8 support, but doesn’t really discuss any other codecs. Yesterday I found my self wondering what other codecs were supported & eventually found the codecs page. There are the usual, ‘utf-8’, ‘utf-16’, ‘latin-1’, etc., but at the bottom of the page is the bit that interested me.
Using the methods, “encode” and “decode”, which are meant to support unicode encoding, you’re capable of converting your data into all sorts of useful serializations. The 2 that piqued by interest the most are ‘base64’ and ‘zlib’.
Sometimes its useful to compress data being sent back and forth through a web session. When handling a request, you will want to base64, then unzip to decode the data
request_data = request.POST['data']
deserialization = request_data.decode('base64_codec').decode('zlib_codec')
Then when replying to the request, the data should be zipped, then base64 encoded
reply_data = '...'
serialization = reply_data.encode('zlib_codec').encode('base64_codec')
This example is rather contrived, and relies on under-documented features of python, but I thought it was pretty cool.
[edited to fix the sample code]