create global variables?-the full story

J. Clifford Dyer webmaster at cacradicalgrace.org
Thu Nov 2 09:46:27 EST 2006


> OK...
> 
> from the start.
> 
> im trying to develop a simple command line application for determining
> the degree of substitution (DS) on a polymer backbone from elemental
> analysis, i.e., the % weights of different elements in the
> monomer-substituent compound ( i want each element to give a result and
> heaviest atoms give the most accurate results).
> 
> most basic comp chem programs use input files but i dont know anything
> about file iteration yet and want the program to be as user friendly as
> possible..i.e. command line prompt. GUI would be great but too much for
> me at this stage
> 
> at the start of the script i have 2 dictionaries 1) containing every
> atom in the periodic table with associated isotopic average masses 2)
> containing the molecular forumla of the monomer unit...eg for cellulose
> AGU {'C': 6, 'H': 10, 'O': 5}.
> 
> the basic steps are
> 
> 1. calculate the weight percentage values for each atom in the monomer
> 2. iterate into dictionaries from DS=0 - DS=15 (0.00005 step) the
> projected % values for the monomer plus substituent, for EACH atom in
> the compound.
> 3. find the (local) minimum from each dictionary/atom to give the
> appropriate DS value.
> 
> *Note* I have to iterate ALL the values as there is a non-linear
> relationship between % values and DS due to the different atomic weights
> The computer seems to cope with this in about 10 seconds with the above
> parameters and about 8 elements for the iteration step
> 

Since you have a parallel structure for each element, consider using a 
dictionary with the element names as keys:

 >>> atomicdata = {}
 >>> for element in 'C','H','U':
...	atomicdata[element] = getAtomVars(element)
...
 >>> print atomicdata
{ 'C': (1, 2), 'H': (4, 5), 'U': (78, 20) }

The first value of each tuple will be your Xaa, and the second value 
will be Xma.  Do you really need to keep the names Caa, Cma, Haa, Hma 
around?  Instead of Caa, you have atomicdata['C'][0] and Cma becomes 
atomicdata['C'][1].  Completely unambiguous.  A bit more verbose, 
perhaps, but you don't have to try to sneak around the back side of the 
language to find the data you are looking for.  That's very much against 
the tao.  If you really want the names, nest dicts, but don't try to get 
the element name into the keys, because you already have that:

 >>> atomicdata = { 'C': { 'aa': 1,
...                       'ma': 2},
...                'H': { 'aa': 4
...                       'ma': 5},
...                'U': { 'aa': 78
...                       'ma': 20} }

and to get from there to storing all your data for all however many 
steps, change the value of each entry in atomic data from a tuple (or 
dict) to a list of tuples (or dicts).


 >>> atomicdata = { 'C': [ (1,2), (4,6), (7,8), (20,19) ],
...                'H': [ (5,7), (2,986), (3,4) ] }
 >>> atomicdata['H'].append((5,9))
 >>> atomicdata
{ 'C': [ (1, 2), (4, 6), (7, 8), (20, 19) ], 'H': [ (5, 7), (2, 986), 
(3, 4), (5, 9) ] }

You can build up those lists with nested for loops. (one tells you which 
element you're working on, the other which iteration).

The indexes of your lists, of course, will not correspond to the DS 
values, but to the step number.  To get back to the DS number, of 
course, let the index number be i, and calculate DS = i * 0.00005

That should get you off and running now.  Happy pythoning!

Cheers,
Cliff



More information about the Python-list mailing list