Changeset 747 for trunk


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.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/docs/contents/pg/db_wrapper.rst

    r745 r747  
    168168that is the current setting of the run-time parameter with that name.
    169169
    170 You can get several parameters at once by passing a list or tuple of
    171 parameter names.  The return value will then be a corresponding list
    172 of parameter settings.  If you pass a dict as parameter instead, its
    173 values will be set to the parameter settings corresponding to its keys.
     170You can get several parameters at once by passing a list, set or dict.
     171When passing a list of parameter names, the return value will be a
     172corresponding list of parameter settings.  When passing a set of
     173parameter names, a new dict will be returned, mapping these parameter
     174names to their settings.  Finally, if you pass a dict as parameter,
     175its values will be set to the current parameter settings corresponding
     176to its keys.
    174177
    175178By passing the special name `'all'` as the parameter, you can get a dict
     
    194197then the run-time parameter will be restored to its default value.
    195198
    196 You can set several parameters at once by passing a list or tuple
    197 of parameter names, with a single value that all parameters should
    198 be set to or with a corresponding list or tuple of values.
    199 
    200 You can also pass a dict as parameters.  In this case, you should
    201 not pass a value, since the values will be taken from the dict.
     199You can set several parameters at once by passing a list of parameter
     200names, together with a single value that all parameters should be
     201set to or with a corresponding list of values.  You can also pass
     202the parameters as a set if you only provide a single value.
     203Finally, you can pass a dict with parameter names as keys.  In this
     204case, you should not pass a value, since the values for the parameters
     205will be taken from the dict.
    202206
    203207By passing the special name `'all'` as the parameter, you can reset
  • 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')
  • trunk/tests/test_classic_dbwrapper.py

    r745 r747  
    397397        r = f('bytea_output')
    398398        self.assertEqual(r, 'hex')
    399         r = f(('bytea_output', 'lc_monetary'))
     399        r = f(['bytea_output', 'lc_monetary'])
    400400        self.assertIsInstance(r, list)
    401401        self.assertEqual(r, ['hex', 'C'])
    402         r = f(['standard_conforming_strings', 'datestyle', 'bytea_output'])
     402        r = f(('standard_conforming_strings', 'datestyle', 'bytea_output'))
    403403        self.assertEqual(r, ['on', 'ISO, YMD', 'hex'])
     404        r = f(set(['bytea_output', 'lc_monetary']))
     405        self.assertIsInstance(r, dict)
     406        self.assertEqual(r, {'bytea_output': 'hex', 'lc_monetary': 'C'})
     407        r = f(set(['Bytea_Output', ' LC_Monetary ']))
     408        self.assertIsInstance(r, dict)
     409        self.assertEqual(r, {'Bytea_Output': 'hex', ' LC_Monetary ': 'C'})
    404410        s = dict.fromkeys(('bytea_output', 'lc_monetary'))
    405411        r = f(s)
    406412        self.assertIs(r, s)
    407413        self.assertEqual(r, {'bytea_output': 'hex', 'lc_monetary': 'C'})
    408         s = dict.fromkeys(('Bytea_Output', 'LC_Monetary'))
     414        s = dict.fromkeys(('Bytea_Output', ' LC_Monetary '))
    409415        r = f(s)
    410416        self.assertIs(r, s)
    411         self.assertEqual(r, {'Bytea_Output': 'hex', 'LC_Monetary': 'C'})
     417        self.assertEqual(r, {'Bytea_Output': 'hex', ' LC_Monetary ': 'C'})
    412418
    413419    def testGetParameterServerVersion(self):
     
    438444        f('datestyle', 'ISO, DMY')
    439445        self.assertEqual(g('datestyle'), 'ISO, DMY')
     446        f(['standard_conforming_strings', 'datestyle'], ['on', 'ISO, DMY'])
     447        self.assertEqual(g('standard_conforming_strings'), 'on')
     448        self.assertEqual(g('datestyle'), 'ISO, DMY')
     449        f(['default_with_oids', 'standard_conforming_strings'], 'off')
     450        self.assertEqual(g('default_with_oids'), 'off')
     451        self.assertEqual(g('standard_conforming_strings'), 'off')
    440452        f(('standard_conforming_strings', 'datestyle'), ('on', 'ISO, YMD'))
    441         self.assertEqual(g('standard_conforming_strings'), 'on')
    442         self.assertEqual(g('datestyle'), 'ISO, YMD')
    443         f(['standard_conforming_strings', 'datestyle'], ['off', 'ISO, DMY'])
    444         self.assertEqual(g('standard_conforming_strings'), 'off')
    445         self.assertEqual(g('datestyle'), 'ISO, DMY')
    446         f({'standard_conforming_strings': 'on', 'datestyle': 'ISO, YMD'})
    447453        self.assertEqual(g('standard_conforming_strings'), 'on')
    448454        self.assertEqual(g('datestyle'), 'ISO, YMD')
     
    450456        self.assertEqual(g('default_with_oids'), 'off')
    451457        self.assertEqual(g('standard_conforming_strings'), 'off')
    452         f(['default_with_oids', 'standard_conforming_strings'], 'on')
     458        f(set(['default_with_oids', 'standard_conforming_strings']), 'on')
    453459        self.assertEqual(g('default_with_oids'), 'on')
    454460        self.assertEqual(g('standard_conforming_strings'), 'on')
     461        self.assertRaises(ValueError, f, set([ 'default_with_oids',
     462            'standard_conforming_strings']), ['off', 'on'])
     463        f(set(['default_with_oids', 'standard_conforming_strings']),
     464            ['off', 'off'])
     465        self.assertEqual(g('default_with_oids'), 'off')
     466        self.assertEqual(g('standard_conforming_strings'), 'off')
     467        f({'standard_conforming_strings': 'on', 'datestyle': 'ISO, YMD'})
     468        self.assertEqual(g('standard_conforming_strings'), 'on')
     469        self.assertEqual(g('datestyle'), 'ISO, YMD')
    455470
    456471    def testResetParameter(self):
     
    476491        self.assertEqual(g('default_with_oids'), not_dwi)
    477492        self.assertEqual(g('standard_conforming_strings'), not_scs)
     493        f(['default_with_oids', 'standard_conforming_strings'], None)
     494        self.assertEqual(g('default_with_oids'), dwi)
     495        self.assertEqual(g('standard_conforming_strings'), scs)
     496        f('default_with_oids', not_dwi)
     497        f('standard_conforming_strings', not_scs)
     498        self.assertEqual(g('default_with_oids'), not_dwi)
     499        self.assertEqual(g('standard_conforming_strings'), not_scs)
    478500        f(('default_with_oids', 'standard_conforming_strings'))
    479501        self.assertEqual(g('default_with_oids'), dwi)
     
    483505        self.assertEqual(g('default_with_oids'), not_dwi)
    484506        self.assertEqual(g('standard_conforming_strings'), not_scs)
    485         f(['default_with_oids', 'standard_conforming_strings'], None)
     507        f(set(['default_with_oids', 'standard_conforming_strings']))
    486508        self.assertEqual(g('default_with_oids'), dwi)
    487509        self.assertEqual(g('standard_conforming_strings'), scs)
Note: See TracChangeset for help on using the changeset viewer.