On Wed, Mar 06, 2002 at 11:44:04PM -0800, Bob Miller wrote:
>class StatSample:

In Python 2.1 and earlier, you could have made this a sub-class of the
"UserList" class:

   >>> class foo(UserList.UserList): pass
   ... 
   >>> l = foo()
   >>> print l
   []

As I mentioned before, 2.2 includes the ability to subclass directly from
"list".  Either of these options would  prevent you from needing the __*__
and append() methods.

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

You could do these as:

   def minimum(self): return(min(self))
   def maximum(self): return(max(self))

Or, simply done away with the methods and relied on the fact that the
min()/max() builtins will operate on sequences:

   >>> list = [ 'a', 5, 1, 73 ]
   >>> min(list)
   1
   >>> max(list)
   'a'

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

A common way of doing this is by using "reduce()", which takes a list and
applies a function to return a single value:

   def mean(self):
      sum = reduce(lambda x, y: x + y, self)
      return(sum / len(self))

>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()]

I would probably have done:

   hr, min, sec = map(int, timestamp.groups())

map() is like reduce, but it instead of returning one value (the list
reduced to one value), it applies the function to every element and
returns a list of the new values.  I mostly avoid the comprehension
syntax because I don't comprehend it.  ;-)

># 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())

How about:

   print '%d frames' % len(ftimes)
   print 'fastest: %s slowest: %s' % ( time_format(ftimes.minimum()),
         time_format(ftimes.maximum()) )
   print 'mean: %s\nstandard deviation: %s' % ( time_format(ftimes.mean()),
         time_format(ftimes.std_deviation()) )

or:

   print '%d frames' % len(ftimes)
   print 'fastest: %s slowest: %s\nmean: %s\nstandard deviation: %s' % \
         map(time_format, min(ftimes), max(ftimes), ftimes.mean(),
         ftimes.std_deviation())

Sean
-- 
 The structure of a system reflects the structure of the organization that
 built it.  -- Richard E. Fairley
Sean Reifschneider, Inimitably Superfluous <[EMAIL PROTECTED]>
tummy.com - Linux Consulting since 1995. Qmail, KRUD, Firewalls, Python

Reply via email to