Package topo :: Package base :: Module parameterclasses :: Class BooleanParameter
[hide private]
[frames] | no frames]

Class BooleanParameter

source code

                   object --+    
                            |    
parameterizedobject.Parameter --+
                                |
                               BooleanParameter

An attribute descriptor for declaring parameters.

Parameters are a special kind of class attribute. Setting a ParameterizedObject class attribute to be a Parameter instance causes that attribute of the class (and the class's instances) to be treated as a Parameter. This allows special behavior, including dynamically generated parameter values, documentation strings, constant and read-only parameters, and type or range checking at assignment time.

** Note that Parameters can only be used when set as class ** ** attributes of ParameterizedObjects. **

For example, suppose someone wants to define two new kinds of objects Foo and Bar, such that Bar has a parameter delta, Foo is a subclass of Bar, and Foo has parameters alpha, sigma, and gamma (and delta inherited from Bar). She would begin her class definitions with something like this:

class Bar(ParameterizedObject):
delta = Parameter(default=0.6, doc='The difference between steps.') ...
class Foo(Bar):
alpha = Parameter(default=0.1, doc='The starting value.') sigma = Parameter(default=0.5, doc='The standard deviation.', constant=True) gamma = Parameter(default=1.0, doc='The ending value.') ...

Class Foo would then have four parameters, with delta defaulting to 0.6.

Parameters have several advantages over plain attributes:

  1. Parameters can be set automatically when an instance is constructed: The default constructor for Foo (and Bar) will accept arbitrary keyword arguments, each of which can be used to specify the value of a Parameter of Foo (or any of Foo's superclasses). E.g., if a script does this:

    myfoo = Foo(alpha=0.5)

    myfoo.alpha will return 0.5, without the Foo constructor needing special code to set alpha.

    If Foo implements its own constructor, keyword arguments will still be accepted if the constructor accepts a dictionary of keyword arguments (as in def __init__(self,**params):), and then each class calls its superclass (as in super(Foo,self).__init__(**params)) so that the ParameterizedObject constructor will process the keywords.

  2. A ParameterizedObject need specify only the attributes of a Parameter whose values differ from those declared in superclasses of the ParameterizedObject; the other values will be inherited. E.g. if Foo declares

    delta = Parameter(default=0.2)

    the default value of 0.2 will override the 0.6 inherited from Bar, but the doc will be inherited from Bar.

  3. The Parameter descriptor class can be subclassed to provide more complex behavior, allowing special types of parameters that, for example, require their values to be numbers in certain ranges, generate their values dynamically from a random distribution, or read their values from a file or other external source.

  4. The attributes associated with Parameters provide enough information for automatically generating property sheets in graphical user interfaces, to allow ParameterizedObjects to be edited by users.



Nested Classes [hide private]

Inherited from parameterizedobject.Parameter: __metaclass__

Instance Methods [hide private]
 
__init__(self, default=False, bounds=(0, 1), **params)
Initialize a Boolean parameter, allowing values True or False.
source code
 
__set__(self, obj, val)
Set the value for this Parameter.
source code

Inherited from parameterizedobject.Parameter: __delete__, __get__, __getstate__, __setstate__

Inherited from object: __delattr__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __str__

Properties [hide private]
  bounds

Inherited from parameterizedobject.Parameter: constant, default, doc, instantiate, precedence, readonly

Inherited from object: __class__

Method Details [hide private]

__init__(self, default=False, bounds=(0, 1), **params)
(Constructor)

source code 
Initialize a Boolean parameter, allowing values True or False.
Overrides: parameterizedobject.Parameter.__init__

__set__(self, obj, val)

source code 

Set the value for this Parameter.

If called for a ParameterizedObject class, set that class's value (i.e. set this Parameter object's 'default' attribute).

If called for a ParameterizedObject instance, set the value of this Parameter on that instance (i.e. in the instance's __dict__, under the parameter's internal_name).

If the Parameter's constant attribute is True, only allows the value to be set for a ParameterizedObject class or on uninitialized ParameterizedObject instances.

If the Parameter's readonly attribute is True, only allows the value to be specified in the Parameter declaration inside the ParameterizedObject source code. A read-only parameter also cannot be set on a ParameterizedObject class.

Note that until we support some form of read-only object, it is still possible to change the attributes of the object stored in a constant or read-only Parameter (e.g. the left bound of a BoundingBox).

Overrides: parameterizedobject.Parameter.__set__
(inherited documentation)