Robert Cunningham wrote:
> Thanks for the code! I'll study it, and the alternatives you
> recommended, then get back to the list with the result.
let us know.
> If anyone wants to beat on this to make it more "Pythonic", I'm here to
> learn...
* use numpy -- you will be glad you did. for example:
> # A really dumb max() function. There's got to be a better way...
> max = theChannels[chan].aTheSpectrum.aSpectrum[0]
> for i in range(1,count):
> if max < theChannels[chan].aTheSpectrum.aSpectrum[i]:
> max = theChannels[chan].aTheSpectrum.aSpectrum[i]
import numpy as N
Spectrum = N.array(theChannels[chan].aTheSpectrum.aSpectrum)
# though it should probably be a numpy array to begin with
max = Spectrum.max()
Not only clean, but much faster.
(and if you store all the channels in one 2-d array, you could calculate
all the maximums in one call)
Not only clean, but much faster. though now that I think about it,
python's max() function works fine on lists an tuples too:
Max = max(N.array(theChannels[chan].aTheSpectrum.aSpectrum))
(careful using "max" as a name, it overwrite python's "max()" function)
But numpy is still faster, and better for this sort of thing in a lot of
ways.
> str = "Max counts = %d. Each '|' = %0.2f counts. Longest line
> is 100 '|' long.\n"%(max,(1/scale))
> for i in range(0,count):
> str += "% 4d "%i +
> "|"*int(theChannels[chan].aTheSpectrum.aSpectrum[i]*scale) + "\n"
don't use str += when building up a long string -- it makes a new string
each time through the loop. Instead, use a list of string, then put them
together:
> bars = []
> for i in range(0,count):
> bars.append("% 4d "%i +
"|"*int(theChannels[chan].aTheSpectrum.aSpectrum[i]*scale) + "\n")
str = "".join(bars)
-Chris
--
Christopher Barker, Ph.D.
Oceanographer
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
[EMAIL PROTECTED]
_______________________________________________
FloatCanvas mailing list
[email protected]
http://mail.mithis.com/cgi-bin/mailman/listinfo/floatcanvas