Complementary groups

Complementary parameter groups consist of N parameters (members) where each member is accompanied by a rule that describes how this parameter's value can be computed from the values of all other parameters. This makes it possible to specify any (N-1) combination of the required parameters where the missing parameter will be computed from the remaining ones. For example:

from hanna import ComplementaryGroup, Configurable, Integer

class Rectangle(Configurable):
    size = ComplementaryGroup(
        [Integer('width'), lambda height, area: area // height],
        [Integer('height'), lambda width, area: area // width],
        [Integer('area'), lambda width, height: width * height]
    )
    
r = Rectangle(dict(width=2, height=3))
r.size  # {'width': 2, 'height': 3, 'area': 6}

r = Rectangle(dict(width=2, area=6))
r.size  # {'width': 2, 'height': 3, 'area': 6}

r = Rectangle(dict(height=3, area=6))
r.size  # {'width': 2, 'height': 3, 'area': 6}

In order to compute the missing parameter's value the values of the remaining members are passed by name to the completion function (using argument expansion, func(**member_values)).

Complementary groups support sections similar to "regular" parameter groups (see Groups).

Last updated