Thursday, November 18, 2010

Python scripting

There are a few things which are easily overlooked while scripting using Python CLI.

  1. Reloading modules: I generally program with a module structure, while maintaining documentation and test suite simultaneously (Using doctest). To quickly reload modules, I use the reload command:

    >>> import My.Very.Large.Module.Name as M
    >>> M.this_function_to_test( with_these_arguments )
    Python: Umm ... there is a bug on line number ***

    (* making changes to My/Very/Large/Moudle/ file *)

    >>> reload(M)
    >>> M.this_function_to_test( with_these_arguments )
    Python: Yeah, I think it works now, but you might want to fix ***

    (* more fixing *)

    >>> reload(M)
    >>> M.this_function_to_test( with_these_arguments )
    Python: Perfect, now the turn of world poverty, go solve.

    >>> pack_bags_and_leave()

    However, there is a caveat here: If you have changed the name of the function to test, then the old name will still persist and calling M.this_function_to_test( ... ) will continue to return the results of the old version. Hence, old functions will not be dropped automatically from the namespace.
  2. Reverse Intelligent Search: I knew of this feature in my bash shell, but did not know that Python CLI had it too! To search for an old command used on the CLI, hit Ctrl-R and type a few words and Voila!
  3. iPython: This interactive shell for Python has many many features, but the killer feature for me is the bash-like Tab Completion!

As I find more, I'll keep putting them up here.

No comments: