- Fix documentation for Python Abstract module

git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@353 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
tchernobog 2006-02-19 16:22:53 +00:00
parent e3b3deca11
commit 9b40d632eb
2 changed files with 49 additions and 49 deletions

View File

@ -1,59 +1,63 @@
## @brief Defines a class to create abstract methods
#
# @author Ivo Timmermans
# @date 2004/01/23
# @version 1.1
#
# Example:
# @code
# import Abstract;
# class Foo:
# __metaclass__ = Abstract.Metaclass
# foo = Abstract.AbstractMethod('foo')
# @endcode
class AbstractMethod (object): class AbstractMethod (object):
"""Defines a class to create abstract methods ## @brief Constructor
#
@example: # @param func name of the function (used when raising an
class Foo: # exception). Its type is str.
foo = AbstractMethod('foo')
"""
def __init__(self, func): def __init__(self, func):
"""Constructor
@params func: name of the function (used when raising an
exception).
@type func: str
"""
self._function = func self._function = func
## @brief Get callable object
#
# @return 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.
def __get__(self, obj, type): 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) return self.AbstractMethodHelper(self._function, type)
## @brief Abstract method helper class
#
# An AbstractMethodHelper instance is a callable object that
# represents an abstract method.
class AbstractMethodHelper (object): class AbstractMethodHelper (object):
"""Abstract method helper class
An AbstractMethodHelper instance is a callable object that
represents an abstract method.
"""
def __init__(self, func, cls): def __init__(self, func, cls):
self._function = func self._function = func
self._class = cls self._class = cls
## @brief Call abstract method
#
# Raises a TypeError, because abstract methods can not be
# called.
def __call__(self, *args, **kwargs): 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__ \ raise TypeError('Abstract method `' + self._class.__name__ \
+ '.' + self._function + '\' called') + '.' + self._function + '\' called')
## @brief Configure a new class to be abstract
#
# @author Ivo Timmermans
# @date 2004/01/23
# @version 1.1
class Metaclass (type): class Metaclass (type):
## Configure a new class
#
# @param cls Class object
# @param name Name of the class
# @param bases All base classes for cls
def __init__(cls, name, bases, *args, **kwargs): def __init__(cls, name, bases, *args, **kwargs):
"""Configure a new class
@param cls: Class object
@param name: Name of the class
@param bases: All base classes for cls
"""
super(Metaclass, cls).__init__(cls, name, bases, *args, **kwargs) super(Metaclass, cls).__init__(cls, name, bases, *args, **kwargs)
# Detach cls.new() from class Metaclass, and make it a method # Detach cls.new() from class Metaclass, and make it a method
@ -77,14 +81,12 @@ class Metaclass (type):
abstractmethods.sort() abstractmethods.sort()
setattr(cls, '__abstractmethods__', abstractmethods) setattr(cls, '__abstractmethods__', abstractmethods)
## @brief Allocator for class cls
#
# @param self Class object for which an instance should be
# created.
# @param cls Same as self.
def new(self, cls): def new(self, cls):
"""Allocator for class cls
@param self: Class object for which an instance should be
created.
@param cls: Same as self.
"""
if len(cls.__abstractmethods__): if len(cls.__abstractmethods__):
raise NotImplementedError('Can\'t instantiate class `' + \ raise NotImplementedError('Can\'t instantiate class `' + \
cls.__name__ + '\';\n' + \ cls.__name__ + '\';\n' + \

View File

@ -1,11 +1,9 @@
''' from sgpem import SchedulableQueue, PolicyParameters ''' # from sgpem import SchedulableQueue, PolicyParameters
from Abstract import * from Abstract import *
class Policy: class Policy:
''' ## @var Avoid instantiation of an abstract class
Avoid instantiation of an abstract class.
'''
__metaclass__ = Metaclass __metaclass__ = Metaclass
configure = AbstractMethod('configure') configure = AbstractMethod('configure')