I got some nice
feedback
from a recent
post
about scientific programming, and the thing that seemed to resonate most
with the good folks out there, was the suggestion of using config files.

I want to go into some details of this in python only because I think
it's a pretty neat way of getting into dynamic programming (also
meta-programming).

I bring this up because, as a scientific programmer, I was fed on a
stringy diet of imperative programming (Fortran, and C), and bypassed
the twisted power of Lisp (the mother, or meta, of all dynamic
languages). Fortunately, the guys behind Python have sneaked some
lisp-like dynamic features back into python. And these features make it
very easy to do config files.

Here's how I might abstract out a config file. Let's say I've written a
routine to run a simulation. First, I wrap everything in a function:

With Python, I can collect all these parameters together into a
dictionary:

parameters={}# a dictionaryparameters['pdb']='1jbc.pdb'parameters['n_step']=10000parameters['force_field']='OPLS'defsimulate(parameters):dosomethingwithparameters['pdb']andparameters['n_step']etc.if__name__=='__main__":simulate(parameters)

This seems like a major complication, but look what I've done. I've
collected all the parameters into one thing – a dictionary. I can pass
this whole dictionary to any other function, maybe

defanalyze_results(parameters):blah

or

defdisplay_trajectory_in_a_viewer(parameters):blah

This is really easy with Python's dynamic typing, because the values in
the dictionary can be anything, be it a string, or an integer, or a
float. And when you save the parameters to a text file, it becomes,
voila, a config file: