Feed on
Posts
Comments

Since October 2008 my good friend Atom ( Deepak singh to most of you) and myself have spent many of our Sundays recording the coast to coast bio podcast.  With the last episode the c2cbio podcast has completed 25 episodes, and we are thrilled. These 25 episodes have seen us talking about everything from version control , to meta-programming to synthetic biology to to pubsubhubub  to synchrotoron data collection to hadoop and all things cloud computing. The unifying theme  ( if any) is that mixing technology and science makes both better and definitely a lot more fun.

Its mainly thanks to c2cbio that I finally decided to sit down and start coding . Thanks to our many conversations about what makes a good programmer ,  version control , IDEs , test driven development and agile programming, I decided to try and put these into practice while I attempted to statisfy my code-itch.  Thanks to all the git topics from Atom , I too decided to start playing with git. The fun part was when I could see myself coding a lot more and writing code that even I reusedAnother conversation had us talking about Living code , about how Paul Buchheit built gmail and add-sense  . The take home message for me was to prototype and test extensively, and release often .   All of these lessons combined with version control have ensured that I am few days away from a simple app that hopefully makes creating and keeping track of crystallization solution grids easier.

Thanks to the podcast I feel I get to keep up with the goings on, especially when bench work leaves me with little time to browse and catch up.

Thanks to everyone for listening and writing in  . Heres to podcasting , coding and hopefully a lot more episodes of c2cbio.

Ahh ! for argparse

Its been a while since I blogged . Just a lot happening on the crystallography side of things to allow me the time to blog.

I have also been coding a lot lately and have gotten started with some GUI writing in wxpython.

This post is about trying to get back into the groove by telling you about argparse . Having talked about optparse and command line parsing , I heard about argparse thanks to a talk that I caught  by clicking a link on  the Pycon2009 master schedule .  This link has all the Pycon 2009 slides and video links in one place – a great resource.

For those interested in argparse check out the “Plenary Evening session ” on Sunday Mar 29th 1.20 pm at PyCon2009 where Steven Bethard talks about argparse.

The most important difference between argparse and optparse is that argparse provides better handling for positional arguments in addition to optional arguments . Argparse also provides automatically generated useage information and   takes care of handling cases like when the user forgets to provide any arguments and prints the help information by default.

The key differences are summarized in the excellent documentation at this URL , so I will not rehash it . But I have happily switched over to argparse from optparse and hope argparse becomes part of the python standard libraries soon.

For some code examples check out my github repository especially the script maskconvert.py

I own a kindle2 and its very easy to push pdfs to the kindle. Unlike the misconceptions out there , the Kindle2 is NOT protected and offers “free” ( see below)  conversion of pdfs to the *.azw format that you can then “push” to your kindle2. 

Here is how it works:

When you buy a kindle2 . The owner can register the device with his/her amazon account and then associate a new email address with the kindle2. for eg alberteinstein@kindle.com.

Once you do this you can send pdfs to your device in the following two ways
1) email the pdf attachment to alberteinstein@kindle.com. Amazon converts the file and pushes it to your device wirelessly ( over the “whispernet” ) . you get charged 0.10 per email for pushing it to your device 

OR 

2) You can email the same atatchment to alberteinstein@free.kindle.com  ( note the FREE) . Amazon converts the file and emails it to the amazon account email address (in my this case say alberteinstein@gmail.com) . I can then save the file to my computer and use the provided usb cable to save the converted document to the “documents” folder in the kindle. This way you dont get charged anything. 

In addition to the above “amazon blessed” ways of pushing content to your kindle2. There are standalone applications like Calibre and mobipocket that convert documents (pdfs , word doc files , html pages) to the mobibook format on which kindle2 *.azw format is based. Calibre runs on Mac, Windows and Linux and Mobipocket is a windows only app.  I have not yet tested mobipocket but  Calibre by kovid Goyal is a free and open source app that offers a multi-platform itunes-like front end to push and manage  third party content on the kindle2. With Calibre you can convert pdfs to the *.mobi format and save it to the documents folder for reading on your kindle .

I recently tested an old Acta Cryst paper (1999) for conversion to the kindle2 format using all three approaches above. The conversion and upload to the device was trivial using either the whispernet -push or save to kindle2 via usb via free email or Caliber.app on the Mac.  The pdf immediately shows up in your library . The text of the paper was immensely readable but when it came to equations and symbols all hell breaks loose . Most of the equations in these papers were probably not embedded as images but instead as their equivalent fonts. I am sure the encoding of these fonts to the mobibook format is not trivial and it shows when simple equations line 

B = A + T have the = and + symbols all messed up  with (?) symbols once converted  i.e B ? A ? T

SO it is quite difficult to read the paper on the kindle2 , when many of the equations are garbled.

This is obviously an evolving space. Even for pdf until say five to six years ago it was not uncommon to have  funky characters replacing our alphas and taus in the printed pdf . Whats most intersting is that projects like Calibre bring the power of open-source approaches to the kindle and other ebook reader platforms . I wont be surprised if the open source world rallies behind open software to allow users to create and make available content that can be read on your ebook reader of choice .

Seeing how it took pdf nearly ten years to be the format of choice for electronic wysiwyg documents , I hope we dont have to wait too long for all content to be seemlessly transcoded for reading on any given ebook reader 

refs: Nature in its 2nd April 2009 issue has two news features on ebook readers. , ireadreview an good site for reviews on all thinks ebook readers

Command lines interfaces to programs are very empowering. I started using computers with the  Linux command line and  I have never strayed too far from programs that are predominantly command line based. Whether it was rasmol , povray , phenix or ffmpeg I always found that the command line gave the program a more  transparent interface . By that I mean it was easier ( at least for me ) to figure out how to do decipher a  manual page  than to go looking for a particular functionality in a GUI window.

No in the case of python ,  I have been writing scripts that take in user input from the command line for a while now. In these scripts , I would generally accept only one input and that would be the first in the sys.argv list. If I had more than one input I would iterate over the input list and try and figure out what the inputs were . Even worse in most cases I would  hard code the order  of inputs into my code ( terrible practise). Fortunately  for me , my discovery of the optparse module has changed all that.

The optparse module is  an object oriented ( dont let that scare you) and super-intuitive way to add command line options to any python scriptSo say you want to add an input file command line switch with the -i attribute , All you have to do is

from optparse import OptionParser

optparse_object =OptionParser()

optparser_object.add_option(“-i”,”–infile”, dest=”infile”,help=”input file for script” , metavar=”[infile.txt]“)

Once you do this you can easily have the module parse the sys.argv list and make sense of it .So you would add the following line

options_object, spillover_options = optparser_object.parse_args()

Then options_object.infile  will have the value of the input option . This is specified by the dest section in the add_option argument list) . The nice thing with the module is that all possibilities can be mapped to the same options_object.infile destination . So for eg I have mapped “-i” and “–infile” to the same destination .Even better is the option to add a help string with the help=”help text” argument . This help is then printed out if the user provides an option that the script cannot handle or if the code specifically calls  the optparser_object.print_help() function.

For a concrete example on how to use the opt_parse module consult the docs or my example code on github .

My Mac-laptop’s been running Leopard ( OSX ) for quite some time. For the times when I wanted to use a windows app I had installed both VMWare fusion and Parallels Desktop ver 3 . Both of these did get the job done , but that came at a price ( $49 to $75)

A few days back I tried out a FREE virtualization application from Sun called VirtualBox.

Besides being free, Virtualbox was amazingly easy to install and very very functional.  I did have to go through a full Windows XP install , but I was up and running immediately. This was a lot better than  say VMware fusion , where I had to struggle to install the applications that allowed my mouse to roam freely between the host OS and the guest OS .  Also on linux where I have been using it for some time ..its very stable.

While Virtualbox is free ,  as of version 2.1.3 , it does lack some of the features that Parallels and VMWare provide , like drag and drop , and it does not support the function key for Mac osX ( read this Ars technica review).  Also virtualbox is improving at quite a pace and you never know any feature you are missing might already be in the newest version .

All in all its a great way to test your apps on windows while runing linux for free!

« Newer Posts - Older Posts »