Ralph Zeller wrote:

> KBob's script works fine in python2.2, but how do you make work with
> regard to inheritance using python1.5.2 ?

Inheriting from built-in types is new with Python 2.2.  In
1.5.2, you'd have to go back to the way I did it first.

    class StatSample:

        def __init__(self):
            self.__data = []

        def __len__(self):
            return len(self.__data)

        def __getitem__(self, index):
            return self.__data[index]

        def __setitem__(self, index):
            return self.__data[index]

        def append(self, datum):
            return self.__data.append(datum)

And change "a += b" to "a = a + b", and change the comprehension to a
map().

I've attached a version of the script that works in Python 1.5.2.

-- 
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 of numbers that can calculate simple statistics."

    def __init__(self):
        self.__data = []

    def __len__(self):
        return len(self.__data)

    def __getitem__(self, index):
        return self.__data[index]

    def __setitem__(self, index):
        return self.__data[index]

    def append(self, datum):
        return self.__data.append(datum)

    def minimum(self):
        min = None
        for d in self:
            if min is None or min > d:
                min = d
        return min

    def maximum(self):
        max = None
        for d in self:
            if max is None or max < d:
                max = d
        return max

    def mean(self):
        if len(self) == 0: raise
        sum = 0
        for d in self:
            sum = sum + d
        return sum / len(self)

    def variance(self):
        if len(self) == 0:
            return 0
        sum, sum2 = 0L, 0L
        for d in self:
            sum = sum + d
            sum2 = 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()
o, t = None, None
for line in fileinput.input():
    timestamp = re.match(r'\[(\d+)\:(\d+)\:(\d+)\]', line)
    if timestamp:
        # hr, min, sec = [int(n) for n in timestamp.groups()]
        hr, min, sec = map(int, timestamp.groups())
        o, t = t, (hr * 60 + min) * 60 + sec
        if re.search('Finished a frame', line) and o is not None:
            ftimes.append((t - o + 86400) % 86400)


# Calculate and print statistics.

print len(ftimes), "frames"
print "fastest:", time_format(ftimes.minimum()),
print " slowest:", time_format(ftimes.maximum())
print "mean:", time_format(ftimes.mean())
print "standard deviation:", time_format(ftimes.std_deviation())

Reply via email to