On Mon, 11 Mar 2002, 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 ?
> 
The UserList class encapsulates a list that can then have additional
methods added to it.

You need to import it before you can inherit from it.

from UserList import UserList

class myfoo(UserList):
        pass


> Ralph
> ---
> # python
> Python 1.5.2 (#1, Jul  5 2001, 03:02:19)  [GCC 2.96 20000731 (Red Hat Linux
> 7.1 2 on linux-i386
> Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
> >>> class t(list): pass
> ...
> Traceback (innermost last):
>   File "<stdin>", line 1, in ?
> TypeError: base is not a class object
> >>>
> >>> class t(UserList.UserList): pass
> ...
> Traceback (innermost last):
>   File "<stdin>", line 1, in ?
> NameError: UserList
> >>>
> 
> At 09:02 PM 3/10/02 Sean Reifschneider <[EMAIL PROTECTED]> wrote:
> >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
> >
> 

http://www.efn.org/~laprice        ( Community, Cooperation, Consensus
http://www.opn.org                 ( Openness to serendipity, make mistakes
http://www.efn.org/~laprice/poems  ( but learn from them.(carpe fructus ludi)

Reply via email to