[Piglit] [PATCH 3/7] framework/status.py: Convert from NotImplementedError to ABCMeta

Dylan Baker baker.dylan.c at gmail.com
Thu Mar 6 14:37:17 PST 2014


On Thursday, March 06, 2014 05:24:57 PM Ilia Mirkin wrote:
> On Thu, Mar 6, 2014 at 4:54 PM, Dylan Baker <baker.dylan.c at gmail.com> wrote:
> > Using pythons __metaclass__ = abc.ABCMeta allows us to set the abstract
> > Status class's __init__ to pass instead of raise NotImplementedError,
> 
> I haven't investigated the reason for this, but can't come up with
> anything sane off-hand... why are Pass/etc subclasses of Status, and
> not just instances of it? i.e. why not make a constructor that takes
> the name/value/fraction, and then just say
> 
> Fail = Status('fail', 35)
> 
> and be done with it? Then you also avoid having the tons of little
> identical objects (the cost of which is minimized by using slots, but
> still). And you could probably do some sort of cleverness to make it
> immutable too.
> 
> But like I said, I haven't actually looked that carefully, so perhaps
> there's a good reason why they're separate objects.
> 
>   -ilia

I remember investigating that route when I first created this, and deciding to 
use slots instead, I also played with borg classes, but Threw that way too. I 
don't remember the reason I went the way I did, but I remember having some 
concern about passing a single instance around. 

> 
> > but still raise an exception if there is an attempt initialize it. This
> > reduces boilerplate by removing the need for each subclass to override
> > the __init__ method.
> > 
> > Signed-off-by: Dylan Baker <baker.dylan.c at gmail.com>
> > ---
> > 
> >  framework/status.py | 32 +++++++-------------------------
> >  1 file changed, 7 insertions(+), 25 deletions(-)
> > 
> > diff --git a/framework/status.py b/framework/status.py
> > index 2c6b692..d682531 100644
> > --- a/framework/status.py
> > +++ b/framework/status.py
> > 
> > @@ -62,6 +62,8 @@ The formula for determining fixes is:
> >  """
> > 
> > +import abc
> > +
> > 
> >  def status_lookup(status):
> >      """ Provided a string return a status object instance
> > 
> > @@ -115,12 +117,16 @@ class Status(object):
> >      # the memory consumed for creating tens of thousands of these
> >      objects.
> >      __slots__ = ['name', 'value', 'fraction']
> > 
> > +    # setting the __metaclass__ allows Status to be an abstract class,
> > and
> > +    # doesn't require children to overwrite __init__ reducing boilerplate
> > +    __metaclass__ = abc.ABCMeta
> > +
> > 
> >      name = None
> >      value = None
> >      fraction = (0, 1)
> > 
> >      def __init__(self):
> > -        raise NotImplementedError
> > +        pass
> > 
> >      def __repr__(self):
> >          return self.name
> > 
> > @@ -158,63 +164,39 @@ class NotRun(Status):
> >      value = 0
> >      fraction = (0, 0)
> > 
> > -    def __init__(self):
> > -        pass
> > -
> > 
> >  class Skip(Status):
> >      name = 'skip'
> >      value = 5
> >      fraction = (0, 0)
> > 
> > -    def __init__(self):
> > -        pass
> > -
> > 
> >  class Pass(Status):
> >      name = 'pass'
> >      value = 10
> >      fraction = (1, 1)
> > 
> > -    def __init__(self):
> > -        pass
> > -
> > 
> >  class DmesgWarn(Status):
> >      name = 'dmesg-warn'
> >      value = 20
> > 
> > -    def __init__(self):
> > -        pass
> > -
> > 
> >  class Warn(Status):
> >      name = 'warn'
> >      value = 25
> > 
> > -    def __init__(self):
> > -        pass
> > -
> > 
> >  class DmesgFail(Status):
> >      name = 'dmesg-fail'
> >      value = 30
> > 
> > -    def __init__(self):
> > -        pass
> > -
> > 
> >  class Fail(Status):
> >      name = 'fail'
> >      value = 35
> > 
> > -    def __init__(self):
> > -        pass
> > -
> > 
> >  class Crash(Status):
> >      name = 'crash'
> >      value = 40
> > 
> > -
> > -    def __init__(self):
> > -        pass
> > --
> > 1.9.0
> > 
> > _______________________________________________
> > Piglit mailing list
> > Piglit at lists.freedesktop.org
> > http://lists.freedesktop.org/mailman/listinfo/piglit
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 490 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.freedesktop.org/archives/piglit/attachments/20140306/40ff63b1/attachment.pgp>


More information about the Piglit mailing list