Python Programming, news on the Voidspace Python Projects and all things techie.

A Little Bit of Python: Episodes 5 and 6

Two more episodes of A Little Bit of Python have been posted. A Little Bit of Python is an occasional podcast on Python related topics with myself, Brett Cannon, Jesse Noller, Steve Holden and Andrew Kuchling.

Episode 5 is a round table discussion of some of the new things coming in Python 2.7. This includes a discussion of the changes coming to the logging module and distutils (with rambling side discussions at every point of course). Episode 6 is the first of a series of interviews I recorded at PyCon. This interview is with Van Lindberg, who lists amongst his achievements being the PyCon chair this year , being the PSF lawyer and a PSF member, and also the author of Intellectual Property and Open Source. In his spare time he works as an intellectual property lawyer. In the interview, recorded after PyCon 2010 had finished, we discuss how PyCon went, what we can look forward to in PyCon 2011 and a bit about his book and his work.

General links for the podcast feeds and a webpage with an embedded flash player:

Note Unfortunately the audio quality in episode 5 is not great. This is because of the way we have been recording. We have one more episode recorded using the same technique, but future episodes should be higher quality.

If you have feedback, insults or suggestions for new topics you can email us on: all@bitofpython.com. We don't yet have the podcast listed on iTunes; we'll set that up once our permanent online home goes live.

We do have a twitter account, so for news on new episodes follow @bitofpython. A Little Bit of Python is also syndicated on Hacker Public Radio (although they're only up to episode two so far).

Encoding json on Silverlight with System.Json

At the backend of last I year I wrote a blog entry on decoding json on Silverlight. Well, the time has finally come and we're now encoding json to post back to our Django application. The code to encoode json is just as simple as the code for decoding. It makes extensive use of clr.Convert(...) as encoding json relies heavily on implicit conversions:

import clr from System.Collections.Generic import Dictionary from System.Json import JsonValue , JsonArray , JsonObject NoneType = type ( None ) def encode_string ( data ): return encode_object ( data ) . ToString () def encode_object ( value ): if isinstance ( value , ( int , long , bool , float , str , NoneType )): # JsonValue has implicit conversion operators for all # these types return clr . Convert ( value , JsonValue ) elif isinstance ( value , ( list , tuple , set )): return JsonArray ([ encode_object ( obj ) for obj in value ]) elif isinstance ( value , dict ): values = Dictionary [ str , JsonValue ]() for key , val in value . items (): values [ key ] = encode_object ( val ) return JsonObject ( values ) # raise an error if we encounter an unhandled type raise TypeError ( value )

encode_string takes any data-structure of Python primitive / container types and serializes it into JSON. This json seems to be consumed fine by simplejson on the other end. As well as being simple this code is blazingly fast. The actual encoding operation is done in the .ToString() call, which all happens in C#.

Note We used to use simplejson inside Silverlight for our json handling. This pulls in several standard library modules and importing them had a big impact on our startup time. Switching to using System.Json was a big win for us both in terms of performance and startup time.

When you send the json over the wire then it will be encoded as UTF-8 for the simple reason that this is the only encoding that Silverlight ships with.

This is relevant because the reason we are shipping huge data structures over the wire is in order to export Excel files from our data-grid views. I initially tried to use xlwt inside Silverlight. Unfortunately it relies on either latin-1 or utf-16 encodings and so my efforts were doomed to failure. Fortunately it works grand on the server side, but the encoding restriction does place limits on the Python libraries that you can use in the browser (unless you are prepared to write your own encoding functions of course).

Archives