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

Dylan Baker baker.dylan.c at gmail.com
Thu Mar 6 13:54:50 PST 2014


Using pythons __metaclass__ = abc.ABCMeta allows us to set the abstract
Status class's __init__ to pass instead of raise NotImplementedError,
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



More information about the Piglit mailing list