create global variables?-the full story

Alistair King alistair.king at helsinki.fi
Tue Oct 31 06:35:51 EST 2006


Steve Holden wrote:
> Alistair King wrote:
>   
>> Steve Holden wrote:
>>
>>     
>>> aking at mappi.helsinki.fi wrote:
>>>  
>>>
>>>       
>>>> J. Clifford Dyer wrote:
>>>>
>>>>    
>>>>
>>>>         
>>>>> Alistair King wrote:
>>>>>      
>>>>>           
>>> [... advice and help ...]
>>>
>>>  
>>>
>>>       
>>>> this worked a treat:
>>>>
>>>> def monoVarcalc(atom):
>>>>
>>>>   a = atom + 'aa'
>>>>   Xaa = a.strip('\'')
>>>>   m = atom + 'ma'
>>>>   Xma = m.strip('\'')
>>>>   Xaa = DS1v.get(atom)
>>>>   Xma = pt.get(atom)
>>>>   return Xaa, Xma
>>>>
>>>>
>>>> Caa, Cma = monoVarcalc('C')
>>>>
>>>>    
>>>>         
>>> In which case I suspect you will find that this works just as well:
>>>
>>> def monoVarcalc(atom):
>>>
>>>    Xaa = DS1v.get(atom)
>>>    Xma = pt.get(atom)
>>>    return Xaa, Xma
>>>
>>>
>>> Unless there is something decidedly odd about the side-effects of the 
>>> statements I've removed, since you never appear to use the values of a, 
>>> m, Xaa and Xma there seems little point in calculation them.
>>>
>>> regards
>>>  Steve
>>>  
>>>       
>> Yup...it works..but now i have to create a dictionary of 'a' and 'm',
>> ie... "Xaa" and "Xma" string, key:value pairs so i can use other
>> functions on the Xaa, Xma variables by iterating over them and
>> retrieving the values from the variables. I think if i just input Xaa
>> and Xma, only the values associated with those variables will go into
>> the dictionary and ill just be iterating over nonsence.....
>>
>> atomsmasses = {}
>>
>> def monoVarcalc(atom):
>> a = atom + 'aa'
>> m = atom + 'ma'
>> atomsmasses[a]=m
>> Xaa = a.strip('\'')
>> Xma = m.strip('\'')
>> Xma = pt.get(atom)
>> if DS1v.get(atom) != None:
>> Xaa = DS1v.get(atom)
>> else:
>> Xaa = 0
>> return Xaa, Xma
>>
>> Caa, Cma = monoVarcalc('C')
>> Oaa, Oma = monoVarcalc('O')
>> Haa, Hma = monoVarcalc('H')
>> Naa, Nma = monoVarcalc('N')
>> Saa, Sma = monoVarcalc('S')
>> Claa, Clma = monoVarcalc('Cl')
>> Braa, Brma = monoVarcalc('Br')
>> Znaa, Znma = monoVarcalc('Zn')
>>
>>
>>
>> i think? :)
>> thanks
>>
>> a
>>
>>     
> No fair: you only just added atomsmasses! ;-)
>
> However, it seems to me that your atomsmasses dictionary is going to be 
> entirely predictable, and you are still focusing on storing the *names* 
> of things rather than building up a usable data structure. Indeed I 
> suspect that your problem can be solved using only the names of the 
> elements, and not the names of the variables that hold various 
> attributes of the elements.
>
> Perhaps if you explain in plain English what you *really* want to do we 
> can help you find a more Pythonic solution. It'll probably end up 
> something like this:
>
> mass = {}
> for element in ['C', 'O', ..., 'Zn']
>      mass[element] = monoVarcalc(element)
>
> But I could, of course, be completely wrong ... it wouldn't be the first 
> time. Do you understand what I'm saying?
>
> regards
>   Steve
>   
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

I have a script which works perfectly from some time ago but the problem
is, some of the samples contain excess water so i was going to use the
heaviest atom to give the most accurate DS value i could and then adjust
the polymer back bone dictionary to include the initial substituent from
step 3. Then do the calculation again for water {'H': 2, 'O': 1} as the
new substituent. Once i determine the new 'DS' value of water its simple
to recalculate the % values...iterate..find (local) minima and get more
accurate DS values for all the heavy atoms (not including Hydrogen or
Oxygen).
...........................................................................................................

I would like to get the script running now to get some results from our
experimental data which shoudnt take too long but eventually i dont want
to rely on the monomer dictionary...just input the elements and number
of atoms of each element in the starting monomer and further
substituents...therefore adapting the program to not only cellulose as a
polymer but all polymers and small organic molecules, perhaps even
proteins of knows elemental analysis.

For this im taking my basic script that i started with, as a beginner to
python and all programming languages, and trying to make functions of
all the calculations i have to do. I guess that i could eventually use
just the elements as references in datastructures..i.e. 'C', 'H'..etc..
but would like to have many variables bound to those data structures

One other thing is that it would be nice to create a 3D iterated 'array'
of the % values to find the local minima but i really dont have a clue
how to do this.

Any comments would be appreciated as i am a beginner and just went out
and did this how i thought it should be done. Hope this is not too much
for peeps

thanks

Ali

-- 
Dr. Alistair King
Research Chemist,
Laboratory of Organic Chemistry,
Department of Chemistry,
Faculty of Science
P.O. Box 55 (A.I. Virtasen aukio 1)
FIN-00014 University of Helsinki
Tel. +358 9 191 50392, Mobile +358 (0)50 5279446
Fax +358 9 191 50366 




More information about the Python-list mailing list