[Dev OpenGP] [12] Class to create abstract methods

[ Thread Index | Date Index | More opengp.tuxfamily.org/development Archives ]


Revision: 12
Author:   alband85
Date:     2009-02-08 00:38:16 +0100 (Sun, 08 Feb 2009)

Log Message:
-----------
Class to create abstract methods

Added Paths:
-----------
    trunk/src/AbstractMethod.py


Added: trunk/src/AbstractMethod.py
===================================================================
--- trunk/src/AbstractMethod.py	                        (rev 0)
+++ trunk/src/AbstractMethod.py	2009-02-07 23:38:16 UTC (rev 12)
@@ -0,0 +1,48 @@
+# See http://code.activestate.com/recipes/266468/ for further details
+
+class AbstractMethod (object):
+	"""Defines a class to create abstract methods
+
+	@example:
+	class Foo:
+	foo = AbstractMethod('foo')
+	"""
+
+	def __init__(self, func):
+		"""Constructor
+
+		@params func: name of the function (used when raising an
+		exception).
+		@type func: str
+		"""
+		self._function = func
+
+	def __get__(self, obj, type):
+		"""Get callable object
+
+		@returns An instance of AbstractMethodHelper.
+
+		This trickery is needed to get the name of the class for which
+		an abstract method was requested, otherwise it would be
+		sufficient to include a __call__ method in the AbstractMethod
+		class itself.
+		"""
+		return self.AbstractMethodHelper(self._function, type)
+
+class AbstractMethodHelper (object):
+	"""Abstract method helper class
+
+	An AbstractMethodHelper instance is a callable object that
+	represents an abstract method.
+	"""
+	def __init__(self, func, cls):
+		self._function = func
+		self._class = cls
+
+	def __call__(self, *args, **kwargs):
+		"""Call abstract method
+
+		Raises a TypeError, because abstract methods can not be
+		called.
+		"""
+		raise TypeError('Abstract method `' + self._class.__name__ + '.' + self._function + '\' called')


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/