New to python - parse data into Google Charts

2010-09-03 Thread alistair
I'm new to python and my programming years are a ways behind me, so I
was looking for some help in parsing a file into a chart using the
Google Charts API.

The file is simply a text file containing: Date, Total Accesses,
Unique Accesses.

I'd like date across the bottom, access numbers on the vertical axis
and lines for the amounts.

I'm not even really sure how to get started if someone wants to give
me some pointers or throw together a short example, I would greatly
appreciate it.

Here is a sample of the data:

2010-08-01, 2324, 1800
2010-08-02, 3832, 2857
2010-08-03, 7916, 4875
2010-08-04, 7004, 4247
2010-08-05, 6392, 4026
2010-08-06, 5396, 3513
2010-08-07, 3238, 2285
2010-08-08, 3579, 2588
2010-08-09, 7710, 4867
2010-08-10, 6662, 4123
2010-08-11, 6524, 4045
2010-08-12, 6438, 3965
2010-08-13, 5472, 3543
2010-08-14, 3059, 2193
2010-08-15, 3255, 2379
2010-08-16, 7149, 4482
2010-08-17, 6727, 4247
2010-08-18, 6989, 4328
2010-08-19, 6738, 4192
2010-08-20, 5929, 3816
2010-08-21, 3245, 2302
2010-08-22, 4091, 2900
2010-08-23, 8237, 4857
2010-08-24, 7895, 4575
2010-08-25, 7788, 4564
2010-08-26, 7616, 4527
2010-08-27, 6671, 4159
2010-08-28, 3595, 2484
2010-08-29, 4377, 2991
2010-08-30, 9238, 5427
2010-08-31, 9274, 5406
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: New to python - parse data into Google Charts

2010-09-03 Thread alistair
On Sep 3, 12:51 pm, Mike Kent  wrote:
> On Sep 3, 1:52 pm, alistair  wrote:
>
> > I'm new to python and my programming years are a ways behind me, so I
> > was looking for some help in parsing a file into a chart using the
> > Google Charts API.
>
> Try this:http://pygooglechart.slowchop.com/

Thanks for that.  I guess I should have noted that I am new enough
that I'm not even really sure how I would go about using this.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Complex sort on big files

2011-08-02 Thread Alistair Miles
Hi Dan,

Thanks for the reply.

On Mon, Aug 1, 2011 at 5:45 PM, Dan Stromberg  wrote:
>
> Python 2.x, or Python 3.x?

Currently Python 2.x.

> What are the types of your sort keys?

Both numbers and strings.

> If you're on 3.x and the key you need reversed is numeric, you can negate
> the key.

I did wonder about that. Would that not be doable also in Python 2.7,
using sorted(key=...)?

> If you're on 2.x, you can use an object with a __cmp__ method to compare
> objects however you require.

OK, right.

Looking at the HowTo/Sorting again [1] and the bit about cmp_to_key,
could you also achieve the same effect by returning a key with custom
implementations of rich comparison functions?

> You probably should timsort the chunks (which is the standard list_.sort() -
> it's a very good in-memory sort), and then merge them afterward using the
> merge step of merge sort.

Yes, that's what I understood by the activestate recipe [2].

So I guess my question boils down to, how do you do the merge step for
a complex sort? (Assuming each chunk had been completely sorted
first.)

Maybe the answer is also to construct a key with custom implementation
of rich comparisons?

Now I'm also wondering about the best way to sort each chunk. The
examples in [1] of complex sorts suggest the best way to do it is to
first sort by the secondary key, then sort by the primary key, relying
on the stability of the sort to get the desired outcome. But would it
not be better to call sorted() once, supplying a custom key function?

(As an aside, at the end of the section in [1] on Sort Stability and
Complex sorts, it says "The Timsort algorithm used in Python does
multiple sorts efficiently because it can take advantage of any
ordering already present in a dataset." - I get that that's true, but
I don't see how that's relevant to this strategy for doing complex
sorts. I.e., if you sort first by the secondary key, you don't get any
ordering that's helpful when you subsequently sort by the primary key.
...?)

(Sorry, another side question, I'm guessing reading a chunk of data
into a list and using Timsort, i.e., calling list.sort() or
sorted(mylist), is quicker than using bisect to keep the chunk sorted
as you build it?)

> heapq's not unreasonable for the merging, but I think it's more common to
> use a short list.

Do you mean a regular Python list, and calling min()?

> I have a bunch of Python sorts at
> http://stromberg.dnsalias.org/svn/sorts/compare/trunk/ - if you find your
> need is specialized (EG, 3.x sorting by a secondary key that's a string, in
> reverse), you could adapt one of these to do what you need.

Thanks. Re 3.x sorting by a secondary key that's a string, in reverse,
which one were you thinking of in particular?

> heapq is not bad, but if you find you need a logn datastructure, you might
> check out http://stromberg.dnsalias.org/~dstromberg/treap/ - a treap is also
> logn, but has a very good amortized cost because it sacrifices keeping
> things perfectly balanced (and rebalancing, and rebalancing...) to gain
> speed.  But still, you might be better off with a short list and min.

Thanks, that's really helpful.

Cheers,

Alistair

[1] http://wiki.python.org/moin/HowTo/Sorting/
[2] 
http://code.activestate.com/recipes/576755-sorting-big-files-the-python-26-way/

> On Mon, Aug 1, 2011 at 8:33 AM, aliman  wrote:
>>
>> Hi all,
>>
>> Apologies I'm sure this has been asked many times, but I'm trying to
>> figure out the most efficient way to do a complex sort on very large
>> files.
>>
>> I've read the recipe at [1] and understand that the way to sort a
>> large file is to break it into chunks, sort each chunk and write
>> sorted chunks to disk, then use heapq.merge to combine the chunks as
>> you read them.
>>
>> What I'm having trouble figuring out is what to do when I want to sort
>> by one key ascending then another key descending (a "complex sort").
>>
>> I understand that sorts are stable, so I could just repeat the whole
>> sort process once for each key in turn, but that would involve going
>> to and from disk once for each step in the sort, and I'm wondering if
>> there is a better way.
>>
>> I also thought you could apply the complex sort to each chunk before
>> writing it to disk, so each chunk was completely sorted, but then the
>> heapq.merge wouldn't work properly, because afaik you can only give it
>> one key.
>>
>> Any help much appreciated (I may well be missing something glaringly
>> obvious).
>>
>> Cheers,
>>
>> Alistair
>>
>> [1]
>> http://code.activestate.com/recipes/576755-sorting-big-files-the-pytho

Re: help - iter & dict

2006-08-04 Thread Alistair King
here goes...
>
>   


thanks simon for this, it seems a little easier to understand for me but 
i still get the error when i run the script:

#here is a sample dictionary from the 1000 key:values normally in it. 
they are all non zero and values to 15 decimal places

CDSitdict = {32.030822391220937: "'1.36799874'", 
29.150765445901769: "'2.20799727'", 27.930109636681877: 
"'2.7449993'", 28.590095427450688: "'2.43599813'", 
27.595161357952588: "'2.9217'", 29.961761413410386: 
"'1.92299674'", 36.311798000222424: "'0.66348'", 
34.358611987430052: "'0.93372'", 41.199188199569292: 
"'0.20413'", 29.560651138651014: "'2.0579967'"}


#i have tried to format the numerical key into the dictionary to give a 
string using %s but it didnt work so eventually i used
#
#itera = "\'" + `DSit` + "\'"
#CDSitdict[Cpcmax] = itera
#
#i am now wondering if i have been trying to iterate through the key 
value pairs, does the numerical part im trying to iterate over have to 
be the value #side or can it be any side?

#here is a sample exvalue Cpcb = 33.94
**
def pcloop(dictionary, exvalue):
'''
Return the key in dictionary whose value is
closest to exvalue.

If dictionary is empty, return None.
'''

# Get a iterator over *both* keys and values.
diter = dictionary.iteritems()

# Get the first (key, value) pair.
try:
u, z = diter.next()

except StopIteration:
# The dictionary was empty!
# You might want to do something else here
return

# Compute the closeness of the first value.
closest = abs(z - exvalue)

# Create a var to store the closest key
result = u

# Iterate through the rest of the dict.
for u, z in diter:

# Compute the closeness.
v = abs(z - exvalue)

# Check if it's closer than the closest.
if v < closest:

# If so, store the new closest.
closest = v

# And store the new closest key.
result = u

return result

Cpcb = input("\n\nPlease enter the carbon percentage value obtained from 
the microanalysis. If none, enter 0: ")
print"\n\nCellulose Carbon Percentage is " + `Cpca` + "\n\nMaximum 
potential monomer carbon weight is " + `Cwmax` + "\n\nMaximum potential 
carbon percentage is " + `Cpcmax` + "\n\nPercentage difference between 
Cellulose and Maximum is " + `Cdiff` + "\n\n"
exvalue = Cpcb
dictionary = CDSitdict
CDS = pcloop(dictionary, exvalue)


error is:


Traceback (most recent call last):
File "elementalDS.py", line 184, in ?
CDS = pcloop(dictionary, exvalue)
File "elementalDS.py", line 158, in pcloop
closest = abs(z - exvalue)
TypeError: unsupported operand type(s) for -: 'str' and 'float'


:(


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 50429, Mobile +358 (0)50 5279446
Fax +358 9 191 50366 

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: help - iter & dict

2006-08-04 Thread Alistair King
taleinat wrote:
>   mappi.helsinki.fi> writes:
>
>   
>> CDSitdict = {28.473823598317392: "'2.48699832'", 40.06163037274758:
>> "'0.2912'", 27.756248559438422: "'2.83499964'",
>> 33.2299196586726: "'1.12499962'", 29.989685187220061:
>> "'1.91399677'", 31.502319473614037: "'1.4909983'",
>> 28.487341570327612: "'2.4809983'", 30.017763818271245:
>> "'1.90499681'", 32.94343842266: "'1.17899943'",
>> 30.520103712886584: "'1.75199736'", 31.453205956498341:
>> "'1.50299826'", 29.484222697359598: "'2.0849968'",
>> 28.413513489228706: "'2.51399842'", 28.314852455260802:
>> "'2.5589986'", 28.652931545003508: "'2.40899803'"}
>>
>> heres the error i get after entering Cpcb
>>
>> Traceback (most recent call last):
>>   File "elementalDS.py", line 156, in ?
>> CDS = findClosest(CDSitdict, Cpcb)
>>   File "elementalDS.py", line 142, in findClosest
>> distance = (target - v) ** 2
>> TypeError: unsupported operand type(s) for -: 'float' and 'str'
>> alicat  linux:~/Desktop>
>> 
>
> The Python interpreter is being friendly and explaining exactly what the 
> problem
> is and where it occured. It's telling you that it can't subtract a string 
> from a
> float, in line 142 of your code (in the findClosest function). So the problem 
> is
> that 'target' is a float while 'v' is a string.
>
> 'v' should be a float as well, but it's a string since the values in your
> dictionary are strings instead of numbers. Try removing the quotes around the
> values in your dict (both the double-quotes and the single-quotes).
>
>
>   
yes i now the key and values are swapped and the solutions work fine, i 
cant let the numerical key be involved in the iteration as in some cases 
there may be an exvalue below 3. the only solution i could come up with 
is to put single quotes round it but unfortunately i couldnt even to that

thanks

a



-- 
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 50429, Mobile +358 (0)50 5279446
Fax +358 9 191 50366 

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: help - iter & dict

2006-08-05 Thread Alistair King
thanks to Simon Forman,

his solution worked, the key value pairs were entered the wrong way 
round in the dictionary...Doh!

-- 
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 50429, Mobile +358 (0)50 5279446
Fax +358 9 191 50366 

-- 
http://mail.python.org/mailman/listinfo/python-list


variable creation

2006-08-08 Thread Alistair King
Hei all,

im trying to create a list of variables for further use:


Els = raw_input("Are there any further elements you would like to 
include? if so type the element, eg, Pd. If not type No: ")

if Els != 'No':
el = Els in pt
while el == 1 and Els != 'Next':
elements = []
elements.insert(-1, Els)

Els = raw_input("Which further element would you like to 
include, eg, Pd, if not type Next: ")
el = Els in pt
   
while el == 0 and Els != 'Next':
Els = raw_input("This is not an element in the periodic 
table, please try again, eg Pd, If you dont wish to include any more, 
type Next: ")
if el == 1:
elements.insert(-1, Els)
  
while el == 0:
Els = raw_input("This is not an element in the periodic 
table, please try again, eg Pd. If you dont wish to include any more, 
type Next: ")

print elements


this works to a certain extent but gets stuck on some loop. Im a 
beginner and am not sure where im going wrong.

here is a portion of the dictionary containing typical elements:

pt = {'H': 1.00794, 'He': 4.002602, 'Li': 6.941, 'Be': 9.012182, 'B': 
10.811}


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 50429, Mobile +358 (0)50 5279446
Fax +358 9 191 50366 

-- 
http://mail.python.org/mailman/listinfo/python-list


dict problem

2006-10-25 Thread Alistair King
Hi,

ive been trying to update a dictionary containing a molecular formula, but seem 
to be getting this error:


Traceback (most recent call last):
  File "DS1excessH2O.py", line 242, in ?
updateDS1v(FCas, C, XDS)
NameError: name 'C' is not defined

dictionary is: 

DS1v = {'C': 6, 'H': 10, 'O': 5}



#'Fxas' in each case will be integers but 'atoms' should be a float

def updateDS1v(Fxas, x, XDS):
while Fxas != 0:
atoms = DS1v.get('x') + Fxas*XDS
DS1v[x] = atoms

updateDS1v(FCas, C, XDS)
updateDS1v(FHas, H, XDS)
updateDS1v(FOas, O, XDS)
updateDS1v(FNas, N, XDS)
updateDS1v(FSas, S, XDS)
updateDS1v(FClas, Cl, XDS)
updateDS1v(FBras, Br, XDS)
updateDS1v(FZnas, Zn, XDS)
print DS1v

I know there is probably a simple solution but im quite new to python and am 
lost?


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 

-- 
http://mail.python.org/mailman/listinfo/python-list


dict problem

2006-10-25 Thread Alistair King
Jon Clements wrote:

> > Alistair King wrote:
> >
> >   
>   
>> >> Hi,
>> >>
>> >> ive been trying to update a dictionary containing a molecular formula, 
>> >> but seem to be getting this error:
>> >>
>> >>
>> >> Traceback (most recent call last):
>> >>   File "DS1excessH2O.py", line 242, in ?
>> >> updateDS1v(FCas, C, XDS)
>> >> NameError: name 'C' is not defined
>> >>
>> >> dictionary is:
>> >>
>> >> DS1v = {'C': 6, 'H': 10, 'O': 5}
>> >>
>> >>
>> >>
>> >> #'Fxas' in each case will be integers but 'atoms' should be a float
>> >>
>> >> def updateDS1v(Fxas, x, XDS):
>> >> while Fxas != 0:
>> >> atoms = DS1v.get('x') + Fxas*XDS
>> >> DS1v[x] = atoms
>> >>
>> >> updateDS1v(FCas, C, XDS)
>> >> updateDS1v(FHas, H, XDS)
>> >> updateDS1v(FOas, O, XDS)
>> >> updateDS1v(FNas, N, XDS)
>> >> updateDS1v(FSas, S, XDS)
>> >> updateDS1v(FClas, Cl, XDS)
>> >> updateDS1v(FBras, Br, XDS)
>> >> updateDS1v(FZnas, Zn, XDS)
>> >> print DS1v
>> >>
>> >> I know there is probably a simple solution but im quite new to python and 
>> >> am lost?
>> >>
>> >> 
>> 
> >
> > I strongly suggest reading through the tutorial.
> >
> > I don't think there's enough code here for anyone to check it properly.
> > For instance, it looks like FCas exists somewhere as it's barfing on
> > trying to find C. Where is XDS defined etc...?
> >
> > I can't see updateDS1v() ever completing: any Fxas passed in not equal
> > to 0 will repeat indefinately.
> >
> > I'm guessing unless C is meant to be a variable, you mean to pass in
> > the string 'C'.
> >
> > A dictionary already has it's own update method
> >
> > Perhaps if you explain what you're trying to do in plain English, we
> > could give you some pointers.
> >
> > Jon.
> >
> >   
>   
sorry,

this has been a little rushed

XDS is defined before the function and is a float.
the Fxas values are also and they are integers


now ive tried

def updateDS1v(Fxas, x, XDS):
while Fxas != 0:
atoms = DS1v.get(x) + Fxas*XDS
DS1v['x'] = atoms

updateDS1v(FCas, 'C', XDS)
updateDS1v(FHas, H, XDS)
updateDS1v(FOas, O, XDS)
updateDS1v(FNas, N, XDS)
updateDS1v(FSas, S, XDS)
updateDS1v(FClas, Cl, XDS)
updateDS1v(FBras, Br, XDS)
updateDS1v(FZnas, Zn, XDS)
print DS1v

from this i get the error:

Traceback (most recent call last):
File "DS1excessH2O.py", line 242, in ?
updateDS1v(FCas, 'C', XDS)
File "DS1excessH2O.py", line 239, in updateDS1v
atoms = DS1v.get(x) + Fxas*XDS
TypeError: unsupported operand type(s) for +: 'int' and 'str'


with single quotes (FCas, 'C', XDS) to retrieve the value for that key
from the dictionary and then create the new value and replace the old value.

a


-- 
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 

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dict problem

2006-10-25 Thread Alistair King
Ben Finney wrote:
> "Jon Clements" <[EMAIL PROTECTED]> writes:
>
>   
>> We're still in the dark as to what you're trying to do, try
>> describing something like: "for each element there is an associated
>> 'F' value. For each element in an existing molecule I wish to change
>> the number of 'whatever' to be 'whatever' + my 'F' value * value
>> XDS..."
>> 
>
> Even better, work on a minimal program to do nothing but reproduce the
> unexpected behaviour. If you get to such a program and still don't
> understand, then post it here so others can run it themselves and
> explain.
>
>   
ive checked the values and XDS is actually returning a string where i
want a float ie '123.45' not 123.45.

-- 
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 

-- 
http://mail.python.org/mailman/listinfo/python-list


dict problem

2006-10-30 Thread Alistair King
Ben Finney wrote:
> Alistair King <[EMAIL PROTECTED]> writes:
>
>   
>> Ben Finney wrote:
>> 
>>> Even better, work on a minimal program to do nothing but reproduce
>>> the unexpected behaviour. If you get to such a program and still
>>> don't understand, then post it here so others can run it
>>> themselves and explain.
>>>   
>> ive checked the values and XDS is actually returning a string where i
>> want a float ie '123.45' not 123.45.
>> 
>
> Can you please post a small, complete program that shows the behaviour
> you want explained?
>
> You get this program by writing it -- either by cutting away
> irrelevant parts of the existing program, or (better) writing a new
> program from scratch that does nothing except demonstrate the
> behaviour.
>
> Note that in the process of getting such a program behaving this way,
> you may end up understanding the problem better.
>
>   
i have seemed to work out most of the problems from the previous code,
now i have:
...

heavy = raw_input("\n\n@@\n\nPlease enter the heaviest
atom for which you obtained percentage values for, but not Oxygen, eg,
'C', 'N', 'S', 'Br'...: ")

print DSvalues

def updateDS1v(Fxas, x):
if Fxas != 0:
value = DSvalues.get(heavy)
floatvalue = float(value)
atoms = DS1v.get(x) + Fxas*floatvalue
DS1v[x] = atoms

updateDS1v(FCas, 'C')

print DS1v
...

the problem now is converting badly formatted dictionary values into floats
ive been trying something like this, where 'value' is a typical entry
into the dictionary:
...

IDLE 1.1
>>> value = "'0.0642501084'"
>>> print float(value)

and get the error, in IDLE and the code as:

Traceback (most recent call last):
File "", line 1, in -toplevel-
print float(value)
ValueError: invalid literal for float(): '0.0642501084'

...

Is there any other way of removing double and single quotes from a
number, as a string, to give the float value again?

I know it would be easier to create a properly formatted dictionary
again and i will do that but it would be good to know as i want to get
this program running properly to get some results i need for work.
Everything else seems to be working fine but this.

thanks

a

-- 
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 

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dict problem

2006-10-30 Thread Alistair King
Fredrik Lundh wrote:
> Alistair King wrote:
>
>   
>> Is there any other way of removing double and single quotes from a
>> number, as a string, to give the float value again?
>> 
>
> help(str) describes what you can do with a string (an object of type 
> 'str', that is).  among the methods listed, you'll find:
>
>   
>>  |  strip(...)
>>  |  S.strip([chars]) -> string or unicode
>>  |
>>  |  Return a copy of the string S with leading and trailing
>>  |  whitespace removed.
>>  |  If chars is given and not None, remove characters in chars instead.
>>  |  If chars is unicode, S will be converted to unicode before stripping
>> 
>
> which looks like it should be pretty useful for this specific case:
>
>  >>> value = "'0.0642501084'"
>  >>> value
> "'0.0642501084'"
>  >>> value.strip("'")
> '0.0642501084'
>  >>> value.strip("'\"")
> '0.0642501084'
>  >>> float(value.strip("'\""))
> 0.0642501084
>
> 
>
>   
Thanks..

the code works great now. I know these things are quite simple to learn
from books etc.. but i would be lost without this mailinglist, from lack
of time. Hopefully soon i can give something more complicated.
I ended up doing the dictionary formatting properly and the new code is:
..
heavy = raw_input("\n\n@@\n\nPlease enter the heaviest
atom for which you obtained percentage values for, but not Oxygen or
Hydrogen, ie, 'C', 'N', 'S', 'Br'...: ")

def updateDS1v(Fxas, x):
if Fxas !=0 and DS1v.get(x)!=None:
value = DSvalues.get(heavy)
floatvalue = float(value)
atoms = DS1v.get(x) + Fxas*floatvalue
else:
value = DSvalues.get(heavy)
floatvalue = float(value)
DS1v[x] = Fxas*floatvalue

updateDS1v(FCas, 'C')
updateDS1v(FHas, 'H')
updateDS1v(FOas, 'O')
updateDS1v(FNas, 'N')
updateDS1v(FSas, 'S')
updateDS1v(FClas, 'Cl')
updateDS1v(FBras, 'Br')
updateDS1v(FZnas, 'Zn')

..
it works perfectly now

thanks for the help

a

-- 
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 

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dict problem

2006-10-30 Thread Alistair King
Peter Otten wrote:
> Alistair King wrote:
>
>   
>> the code works great now. I know these things are quite simple to learn
>> from books etc.. but i would be lost without this mailinglist, from lack
>> of time. Hopefully soon i can give something more complicated.
>> I ended up doing the dictionary formatting properly and the new code is:
>> 
>
>   
>> heavy = raw_input("\n\n@@\n\nPlease enter the heaviest
>> atom for which you obtained percentage values for, but not Oxygen or
>> Hydrogen, ie, 'C', 'N', 'S', 'Br'...: ")
>>
>> def updateDS1v(Fxas, x):
>> if Fxas !=0 and DS1v.get(x)!=None:
>> value = DSvalues.get(heavy)
>> floatvalue = float(value)
>> atoms = DS1v.get(x) + Fxas*floatvalue
>> else:
>> value = DSvalues.get(heavy)
>> floatvalue = float(value)
>> DS1v[x] = Fxas*floatvalue
>>
>> updateDS1v(FCas, 'C')
>> updateDS1v(FHas, 'H')
>> updateDS1v(FOas, 'O')
>> updateDS1v(FNas, 'N')
>> updateDS1v(FSas, 'S')
>> updateDS1v(FClas, 'Cl')
>> updateDS1v(FBras, 'Br')
>> updateDS1v(FZnas, 'Zn')
>> 
>
>   
>> it works perfectly now
>> 
>
> Probably not. Have you manually verified the result with more than one
> example? Where does 'heavy' come from? Is that black hole 'atoms'
> intentional? 
>
> # I'm just guessing here
> for k, v in DSvalues.iteritems():
> DSvalues[k] = float(v)
>
> def updateDS1v(Fxas, x):
> DS1v[x] = DS1v.get(x, 0) + Fxas*DSvalues[x]
>
> Peter
>   
yea...sorry i snipped one line by accident for the email
should be:

def updateDS1v(Fxas, x):
if Fxas !=0 and DS1v.get(x)!=None:
value = DSvalues.get(heavy)
floatvalue = float(value)
atoms = DS1v.get(x) + Fxas*floatvalue
DS1v[x] = atoms
else:
value = DSvalues.get(heavy)
floatvalue = float(value)
DS1v[x] = Fxas*floatvalue

thanks

a

-- 
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 

-- 
http://mail.python.org/mailman/listinfo/python-list


create global variables?

2006-10-30 Thread Alistair King
Hi,

is there a simple way of creating global variables within a function?

ive tried simply adding the variables in:

def function(atom, Xaa, Xab):
Xaa = onefunction(atom)
Xab = anotherfunction(atom)

if i can give something like:

function('C')#where atom = 'C' but not necessarly include Xaa or Xab

i would like to recieve:

Caa = a float
Cab = another float

ive tried predefining Xaa and Xab before the function but they are
global values and wont change within my function. Is there a simple way
round this, even if i call the function with the variables ('C', Caa, Cab)?
..

some actual code:

# sample dictionaries
DS1v = {'C': 6}
pt = {'C': 12.0107}

def monoVarcalc(atom):
a = atom + 'aa'
Xaa = a.strip('\'')
m = atom + 'ma'
Xma = m.strip('\'')
Xaa = DS1v.get(atom)
Xma = pt.get(atom)
print Xma
print Xaa

monoVarcalc('C')

print Caa
print Cma
......
it seems to work but again i can only print the values of Xma and Xaa

?

Alistair

-- 
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 

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: IE7 skulduggery?

2006-10-30 Thread Alistair King
BartlebyScrivener wrote:
> Found fix for this at Mozilla:
>
> http://kb.mozillazine.org/Default_browser
>
> Apparently, even though it LOOKS and ACTS like Firefox is still your
> default browser after an IE7 upgrade, it's not.
>
> To fix, you must run:
>
> firefox.exe -silent -nosplash -setDefaultBrowser
>
> Which also fixes the webbrowser. py problem.
>
> At least so far.
>
> rd
>
>   
yes, annoying

i know that this may not be related to python but occasionally when im
using firefox under windows and /i think/ when i open IE7, firefox hangs
and wont reopen until i restart. Is this likely to be anything related
to IE7?

-- 
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 

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: create global variables?

2006-10-30 Thread Alistair King
Gary Herron wrote:
> Alistair King wrote:
>   
>> Hi,
>>
>> is there a simple way of creating global variables within a function?
>>
>>   
>> 
> Use the "global" statement within a function to bind a variable to a
> global. 
>
> See http://docs.python.org/ref/global.html for details.
>
>
>   
>>>> def x():
>>>> 
> ...  global g
> ...  g = 123
> ...
>   
>>>> x()
>>>> g
>>>> 
> 123
>   
>
> Gary Herron
>
>
>
>
>   
>> ive tried simply adding the variables in:
>>
>> def function(atom, Xaa, Xab):
>> Xaa = onefunction(atom)
>> Xab = anotherfunction(atom)
>>
>> if i can give something like:
>>
>> function('C')#where atom = 'C' but not necessarly include Xaa or Xab
>>
>> i would like to recieve:
>>
>> Caa = a float
>> Cab = another float
>>
>> ive tried predefining Xaa and Xab before the function but they are
>> global values and wont change within my function. Is there a simple way
>> round this, even if i call the function with the variables ('C', Caa, Cab)?
>> ..
>>
>> some actual code:
>>
>> # sample dictionaries
>> DS1v = {'C': 6}
>> pt = {'C': 12.0107}
>>
>> def monoVarcalc(atom):
>> a = atom + 'aa'
>> Xaa = a.strip('\'')
>> m = atom + 'ma'
>> Xma = m.strip('\'')
>> Xaa = DS1v.get(atom)
>> Xma = pt.get(atom)
>> print Xma
>> print Xaa
>>
>> monoVarcalc('C')
>>
>> print Caa
>> print Cma
>> ..
>> it seems to work but again i can only print the values of Xma and Xaa
>>
>> ?
>>
>> Alistair
>>
>>   
>> 
>
>   
have tried:

def monoVarcalc(atom):
a = atom + 'aa'
Xaa = a.strip('\'')
m = atom + 'ma'
Xma = m.strip('\'')
global Xma
global Xaa
Xaa = DS1v.get(atom)
Xma = pt.get(atom)
print Xma
print Xaa

monoVarcalc('C')

print Caa
print Cma
...

where global Xma & Xaa are before and after any further functions

i get still get the error

Traceback (most recent call last):
  File "DS1excessH2O.py", line 54, in ?
print Caa
NameError: name 'Caa' is not defined


-- 
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 

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: create global variables?

2006-10-31 Thread Alistair King
Steve Holden wrote:
> [EMAIL PROTECTED] 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

-- 
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 

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: create global variables?-the full story

2006-10-31 Thread Alistair King
Steve Holden wrote:
> Alistair King wrote:
>   
>> Steve Holden wrote:
>>
>> 
>>> [EMAIL PROTECTED] 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}.


Re: create global variables?-the full story

2006-11-07 Thread Alistair King
J. Clifford Dyer wrote:
>> 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.5 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.5
>
> That should get you off and running now.  Happy pythoning!
>
> Cheers,
> Cliff
>   
Thanks Cliff,

this is what i need, it seems to make much more sense to do this way. I
think once i learn to include datastructures within each other im gonna
try to make up this 3D 'array' (i think this is a word from C, is there
a python equivalent?) of further iterations within each iteration to
take into account the excess water. For this i think ill have to add
another 100 values onto the values i already have, i.e. 1.8x10**8
entries in total and god knows how many calculations in potentially one
datastructure. Could my computer cope with this or should i try a series
of refinement iterations? Does anyone knows of a simple way of
determining how much processer time it takes to do these calculations?

thanks

a

-- 
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 

-- 
http://mail.python.org/mailman/listinfo/python-list