Changeset 2652


Ignore:
Timestamp:
Jun 10, 2010 11:17:35 AM (11 years ago)
Author:
prsteel
Message:

Changed error checking logic for all derived classes of Component.

Before, IndexedComponent? would perform error checking to ensure that a
ctype was being passed to the Component constructor. Now that error
checking occurs in Component; this is to allow classes to derive from
Component without also deriving from IndexedComponent?.

N.B. If you inherit from Component via a 'diamond' inheritance
structure, e.g., class foo(IndexedComponent?, DerivedComponent?), you
need to pass ctype arguments to both IndexedComponent?.init and
DerivedComponent?.init to avoid Component throwing an exception. I
believe this could be avoided by using super() calls, but I haven't
looked into it.

N.B. Created an exception class DeveloperError?, which is used to
indicate an internal Pyomo error that a user *should* never see. In
this case it was used to throw an exception if Component.init does
not receive a non-None ctype argument. This exception should be thrown
due to internal Pyomo code, not user modeling.

Location:
coopr.pyomo/trunk/coopr/pyomo/base
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • coopr.pyomo/trunk/coopr/pyomo/base/component.py

    r2592 r2652  
    1010    implements(IModelComponent)
    1111
    12     def __init__(self, ctype):
     12    def __init__(self, ctype=None):
     13
     14        # Error check for ctype
     15        if ctype is None:
     16            raise DeveloperError, "Must specify a class for the component type!"
     17
    1318        self.active=True
    1419        self._type=ctype
     
    2833        pass
    2934
     35class DeveloperError(Exception):
     36    """
     37    Exception class used to throw errors stemming from Pyomo programming
     38    errors, rather than user modeling errors (e.g., a component not declaring
     39    a 'ctype')
     40    """
     41
     42    def __init__(self, val):
     43        self.parameter = val
     44
     45    def __str__(self):
     46        return repr(self.parameter)
  • coopr.pyomo/trunk/coopr/pyomo/base/indexed_component.py

    r2359 r2652  
    77
    88    def __init__(self, *args, **kwds):
    9         if 'ctype' not in kwds:
    10             raise ValueError, "Must specify a class for the component type!"
    11         Component.__init__(self, kwds['ctype'])
     9
     10        # Don't be afraid to pass None, correct 'ctype' can be passed via
     11        # other routes. Error handling now in Component
     12        component_type = kwds.get('ctype', None)
     13        Component.__init__(self, component_type)
     14       
    1215        self._ndim=0
    1316        self._index_set=None
Note: See TracChangeset for help on using the changeset viewer.