Changeset 5758


Ignore:
Timestamp:
May 11, 2012 7:01:20 PM (7 years ago)
Author:
wehart
Message:

Replacing _ParamBase with Param.

This requires some class magic to replace the Param class with a subclass.

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

Legend:

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

    r5671 r5758  
    1919from numvalue import *
    2020from numvalue import ZeroConstant
    21 from param import _ParamBase
    2221from var import _VarData
    2322
  • coopr.pyomo/trunk/coopr/pyomo/base/param.py

    r5663 r5758  
    99#  _________________________________________________________________________
    1010
    11 __all__ = ['_ParamBase', 'Param']
     11__all__ = ['Param']
    1212
    1313import sys
     
    2929
    3030
    31 class _ParamBase(IndexedComponent):
     31class Param(IndexedComponent):
    3232    """A parameter value, which may be defined over a index"""
    3333
     
    4949                      developers - not modelers!
    5050    """
    51     def __init__(self, *args, **kwd):
     51
     52    alias("Param", doc="Parameter data that is used to define a model instance.", subclass=True)
     53
     54    def __new__(cls, *args, **kwds):
     55        if cls != Param:
     56            return super(Param, cls).__new__(cls)
     57        if args == ():
     58            return _ParamElement(*args, **kwds)
     59        else:
     60            return _ParamArray(*args, **kwds)
     61
     62    def _initialize_(self, *args, **kwd):
    5263        self._initialize = kwd.pop('initialize', None )
    5364        self._initialize = kwd.pop('rule', self._initialize )
     
    302313
    303314
    304 class _ParamElement(_ParamBase, _ParamData):
     315class _ParamElement(Param, _ParamData):
    305316
    306317    def __init__(self, *args, **kwds):
     
    308319        repn_name = kwds.get('repn', 'pyomo_dict')
    309320
    310         _ParamBase.__init__(self, *args, **kwds)
     321        self._initialize_( *args, **kwds)
    311322        _ParamData.__init__(self, kwds.get('name',None), kwds.get('within',Any), kwds.get('default',None))
    312323
     
    317328
    318329    def __getstate__(self):
    319         parambase_result = _ParamBase.__getstate__(self)
     330        parambase_result = Param.__getstate__(self)
    320331        paramvalue_result = _ParamData.__getstate__(self)
    321332        return dict(parambase_result.items() + paramvalue_result.items())   
     
    334345        if not key is None:
    335346            raise KeyError, "Undefined key %s for parameter %s" % (key, self.name)
    336         return _ParamBase.__getitem__(self,key)
     347        return Param.__getitem__(self,key)
    337348
    338349    def __setitem__(self, key, value):
    339350        if not key is None:
    340351            raise KeyError, "Undefined key %s for parameter %s" % (key, self.name)
    341         return _ParamBase.__setitem__(self,key,value)       
     352        return Param.__setitem__(self,key,value)       
    342353
    343354    def check_values(self):         #pragma:nocover
     
    355366
    356367
    357 class _ParamArray(_ParamBase):
     368class _ParamArray(Param):
    358369
    359370    def __init__(self, *args, **kwds):
    360         _ParamBase.__init__(self, *args, **kwds)
     371        self._initialize_(*args, **kwds)
    361372
    362373    def __float__(self):
     
    396407
    397408
    398 class Param(Component):
    399     """
    400     Data objects that are used to construct Pyomo models.
    401     """
    402 
    403     alias("Param", "Parameter data that is used to define a model instance.")
    404 
    405     def __new__(cls, *args, **kwds):
    406         if args == ():
    407             self = _ParamElement(*args, **kwds)
    408         else:
    409             self = _ParamArray(*args, **kwds)
    410         return self
Note: See TracChangeset for help on using the changeset viewer.