Ralph Zeller wrote:
> I'm new to Python, too. I was wondering if there is a way to
> inherit some of the list properties like min, max, len, etc
> rather than redefine them? If it's possible it could save
> 25 lines of code at least.
Hey, you're right! Here's a new version where StatSample is derived
from list. I also incorporated Larry's printing suggestions (somewhat
twisted).
> Another newbie question--rather than parse the date/time yourself,
> would it make any sense to use a module like mxDateTime? I know
> it's overkill (bloat) but it might be more flexible.
It's not part of the base Python distribution. There's a Perl CPAN
module that does simple statistics, and I chose not to use it because
it'd make it harder for all us club members to run the script. I'd
use the same argument against mxDateTime.
--
Bob Miller K<bob>
kbobsoft software consulting
http://kbobsoft.com [EMAIL PROTECTED]
#!/usr/local/bin/python
import fileinput
import math
import re
def time_format(interval):
hr, min, sec = int(interval / 3600), interval / 60 % 60, interval % 60
if hr:
return "%d:%02d:%02d" % (hr, min, sec)
else:
return "%d:%02d" % (min, sec)
class StatSample(list):
"List of numbers that can calculate simple statistics on itself."
def mean(self):
if len(self) == 0:
raise ValueError, "mean of empty StatSample"
sum = 0
for d in self:
sum += d
return sum / len(self)
def variance(self):
if len(self) == 0:
return 0
sum, sum2 = 0, 0
for d in self:
sum += d
sum2 += d * d
return (sum2 - (sum * sum) / len(self)) / (len(self) - 1)
def std_deviation(self):
return math.sqrt(self.variance())
# Read the file.
ftimes = StatSample()
begin, end = None, None
for line in fileinput.input():
timestamp = re.match(r'\[(\d+)\:(\d+)\:(\d+)\]', line)
if timestamp:
hr, minutes, sec = [int(n) for n in timestamp.groups()]
begin, end = end, (hr * 60 + minutes) * 60 + sec
if re.search('Finished a frame', line) and begin is not None:
ftimes.append((end - begin + 86400) % 86400)
# Calculate and print statistics.
stats = {
'len': str(len(ftimes)),
'min': time_format(min(ftimes)),
'max': time_format(max(ftimes)),
'mean': time_format(ftimes.mean()),
'stdev': time_format(ftimes.std_deviation()),
}
print ("%(len)s frames\n"
"fastest: %(min)s slowest: %(max)s\n"
"mean: %(mean)s\n"
"standard deviation: %(stdev)s"
) % stats