Changeset 2649


Ignore:
Timestamp:
Jun 8, 2010 2:20:00 PM (11 years ago)
Author:
khunter
Message:

Refactor Contraint init function

  • Use more standard kwargs instead of kwd
  • Replace kwarg parsing with more standard kwargs.pop handling
  • Add a slightly more informative doc string.
  • Maintain 80-char width standard
File:
1 edited

Legend:

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

    r2638 r2649  
    249249    alias("Constraint", "Constraint expressions a model.")
    250250
    251     def __init__(self, *args, **kwd):
     251    def __init__(self, *args, **kwargs):
    252252        """Construct an objective expression with rule to construct the
    253253           expression
     254           
     255           keyword arguments:
     256           name: name of this object
     257           rule: function or rule definition of constraint
     258           expr: same as rule
     259           doc:  documentation string for constraint
    254260        """
    255         tkwd = {'ctype':Constraint}
     261        tkwd = { 'ctype' : Constraint }
    256262        IndexedComponent.__init__(self, *args, **tkwd)
    257         tmpname="unknown"
    258         tmprule=None
     263
     264        tmpname  = kwargs.pop('name', 'unknown')
     265        tmprule  = kwargs.pop('rule', None )
     266        tmprule  = kwargs.pop('expr', tmprule )
     267        self.doc = kwargs.pop('doc', None )
     268        self._no_rule_init = kwargs.pop('noruleinit', None )
     269
     270        if ( kwargs ): # if dict not empty, there's an error.  Let user know.
     271            msg = "Creating constraint '%s': unknown option(s)\n\t%s"
     272            msg = msg % ( tmpname, ', '.join(kwargs.keys()) )
     273            raise ValueError, msg
     274       
     275        # _no_rule_init is a flag specified by the user to indicate that no
     276        # construction rule will be specified, and that constraints will be
     277        # explicitly added by the user. set via the "noruleinit" keyword. value
     278        # doesn't matter, as long as it isn't "None".
     279
    259280        self._data = {}
    260         # a flag specified by the user to indicate that no construction
    261         # rule will be specified, and that constraints will be explicitly
    262         # added by the user. set via the "noruleinit" keyword. value doesn't
    263         # matter, as long as it isn't "None".
    264         self._no_rule_init = None
    265         #if args == ():
    266         #   self._data[None]=ConstraintData()
    267         for key in kwd.keys():
    268           if key == "name":
    269              tmpname=kwd[key]
    270           elif key == "doc":
    271              self.doc=kwd[key]
    272           elif key == "rule" or key == 'expr':
    273              tmprule = kwd[key]
    274           elif key == "noruleinit":
    275              self._no_rule_init = kwd[key]
    276           else:
    277              raise ValueError, "Constraint constructor: unknown keyword - "+key
     281
    278282        NumericValue.__init__(self,name=tmpname)
    279283        if None in self._data:
     284            # As far as I can tell, this never executes? 2010 Jun 08
    280285            self._data[None].name=tmpname
    281286        self.rule = tmprule
Note: See TracChangeset for help on using the changeset viewer.