Changeset 747 for trunk/pg.py


Ignore:
Timestamp:
Jan 15, 2016, 6:06:51 AM (4 years ago)
Author:
cito
Message:

Improve the get/set_parameter methods

In addition to a list, also allow a set as parameter.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/pg.py

    r745 r747  
    469469        that is the current setting of the run-time parameter with that name.
    470470
    471         You can get several parameters at once by passing a list or tuple of
    472         parameter names.  The return value will then be a corresponding list
    473         of parameter settings.  If you pass a dict as parameter instead, its
    474         values will be set to the parameter settings corresponding to its keys.
     471        You can get several parameters at once by passing a list, set or dict.
     472        When passing a list of parameter names, the return value will be a
     473        corresponding list of parameter settings.  When passing a set of
     474        parameter names, a new dict will be returned, mapping these parameter
     475        names to their settings.  Finally, if you pass a dict as parameter,
     476        its values will be set to the current parameter settings corresponding
     477        to its keys.
    475478
    476479        By passing the special name 'all' as the parameter, you can get a dict
     
    482485        elif isinstance(parameter, (list, tuple)):
    483486            values = []
     487        elif isinstance(parameter, (set, frozenset)):
     488            values = {}
    484489        elif isinstance(parameter, dict):
    485490            values = parameter
    486491        else:
    487             raise TypeError('The parameter must be a dict, list or string')
     492            raise TypeError(
     493                'The parameter must be a string, list, set or dict')
    488494        if not parameter:
    489495            raise TypeError('No parameter has been specified')
     
    522528        then the run-time parameter will be restored to its default value.
    523529
    524         You can set several parameters at once by passing a list or tuple
    525         of parameter names, with a single value that all parameters should
    526         be set to or with a corresponding list or tuple of values.
    527 
    528         You can also pass a dict as parameters.  In this case, you should
    529         not pass a value, since the values will be taken from the dict.
     530        You can set several parameters at once by passing a list of parameter
     531        names, together with a single value that all parameters should be
     532        set to or with a corresponding list of values.  You can also pass
     533        the parameters as a set if you only provide a single value.
     534        Finally, you can pass a dict with parameter names as keys.  In this
     535        case, you should not pass a value, since the values for the parameters
     536        will be taken from the dict.
    530537
    531538        By passing the special name 'all' as the parameter, you can reset
     
    545552            else:
    546553                parameter = dict.fromkeys(parameter, value)
     554        elif isinstance(parameter, (set, frozenset)):
     555            if isinstance(value, (list, tuple, set, frozenset)):
     556                value = set(value)
     557                if len(value) == 1:
     558                    value = value.pop()
     559            if not(value is None or isinstance(value, basestring)):
     560                raise ValueError('A single value must be specified'
     561                    ' when parameter is a set')
     562            parameter = dict.fromkeys(parameter, value)
    547563        elif isinstance(parameter, dict):
    548564            if value is not None:
    549                 raise ValueError(
    550                     'A value must not be set when parameter is a dictionary')
    551         else:
    552             raise TypeError('The parameter must be a dict, list or string')
     565                raise ValueError('A value must not be specified'
     566                    ' when parameter is a dictionary')
     567        else:
     568            raise TypeError(
     569                'The parameter must be a string, list, set or dict')
    553570        if not parameter:
    554571            raise TypeError('No parameter has been specified')
Note: See TracChangeset for help on using the changeset viewer.