Changeset 747 for branches/4.x


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:
branches/4.x
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/4.x/docs/contents/pg/db_wrapper.rst

    r745 r747  
    150150that is the current setting of the run-time parameter with that name.
    151151
    152 You can get several parameters at once by passing a list or tuple of
    153 parameter names.  The return value will then be a corresponding list
    154 of parameter settings.  If you pass a dict as parameter instead, its
    155 values will be set to the parameter settings corresponding to its keys.
     152You can get several parameters at once by passing a list, set or dict.
     153When passing a list of parameter names, the return value will be a
     154corresponding list of parameter settings.  When passing a set of
     155parameter names, a new dict will be returned, mapping these parameter
     156names to their settings.  Finally, if you pass a dict as parameter,
     157its values will be set to the current parameter settings corresponding
     158to its keys.
    156159
    157160By passing the special name `'all'` as the parameter, you can get a dict
     
    176179then the run-time parameter will be restored to its default value.
    177180
    178 You can set several parameters at once by passing a list or tuple
    179 of parameter names, with a single value that all parameters should
    180 be set to or with a corresponding list or tuple of values.
    181 
    182 You can also pass a dict as parameters.  In this case, you should
    183 not pass a value, since the values will be taken from the dict.
     181You can set several parameters at once by passing a list of parameter
     182names, together with a single value that all parameters should be
     183set to or with a corresponding list of values.  You can also pass
     184the parameters as a set if you only provide a single value.
     185Finally, you can pass a dict with parameter names as keys.  In this
     186case, you should not pass a value, since the values for the parameters
     187will be taken from the dict.
    184188
    185189By passing the special name `'all'` as the parameter, you can reset
  • branches/4.x/pg.py

    r745 r747  
    524524        that is the current setting of the run-time parameter with that name.
    525525
    526         You can get several parameters at once by passing a list or tuple of
    527         parameter names.  The return value will then be a corresponding list
    528         of parameter settings.  If you pass a dict as parameter instead, its
    529         values will be set to the parameter settings corresponding to its keys.
     526        You can get several parameters at once by passing a list, set or dict.
     527        When passing a list of parameter names, the return value will be a
     528        corresponding list of parameter settings.  When passing a set of
     529        parameter names, a new dict will be returned, mapping these parameter
     530        names to their settings.  Finally, if you pass a dict as parameter,
     531        its values will be set to the current parameter settings corresponding
     532        to its keys.
    530533
    531534        By passing the special name 'all' as the parameter, you can get a dict
     
    537540        elif isinstance(parameter, (list, tuple)):
    538541            values = []
     542        elif isinstance(parameter, (set, frozenset)):
     543            values = {}
    539544        elif isinstance(parameter, dict):
    540545            values = parameter
    541546        else:
    542             raise TypeError('The parameter must be a dict, list or string')
     547            raise TypeError(
     548                'The parameter must be a string, list, set or dict')
    543549        if not parameter:
    544550            raise TypeError('No parameter has been specified')
     
    582588        then the run-time parameter will be restored to its default value.
    583589
    584         You can set several parameters at once by passing a list or tuple
    585         of parameter names, with a single value that all parameters should
    586         be set to or with a corresponding list or tuple of values.
    587 
    588         You can also pass a dict as parameters.  In this case, you should
    589         not pass a value, since the values will be taken from the dict.
     590        You can set several parameters at once by passing a list of parameter
     591        names, together with a single value that all parameters should be
     592        set to or with a corresponding list of values.  You can also pass
     593        the parameters as a set if you only provide a single value.
     594        Finally, you can pass a dict with parameter names as keys.  In this
     595        case, you should not pass a value, since the values for the parameters
     596        will be taken from the dict.
    590597
    591598        By passing the special name 'all' as the parameter, you can reset
     
    605612            else:
    606613                parameter = dict.fromkeys(parameter, value)
     614        elif isinstance(parameter, (set, frozenset)):
     615            if isinstance(value, (list, tuple, set, frozenset)):
     616                value = set(value)
     617                if len(value) == 1:
     618                    value = value.pop()
     619            if not(value is None or isinstance(value, basestring)):
     620                raise ValueError('A single value must be specified'
     621                    ' when parameter is a set')
     622            parameter = dict.fromkeys(parameter, value)
    607623        elif isinstance(parameter, dict):
    608624            if value is not None:
     
    610626                    'A value must not be set when parameter is a dictionary')
    611627        else:
    612             raise TypeError('The parameter must be a dict, list or string')
     628            raise TypeError(
     629                'The parameter must be a string, list, set or dict')
    613630        if not parameter:
    614631            raise TypeError('No parameter has been specified')
  • branches/4.x/tests/test_classic_dbwrapper.py

    r745 r747  
    426426        r = f('bytea_output')
    427427        self.assertEqual(r, 'hex')
    428         r = f(('bytea_output', 'lc_monetary'))
     428        r = f(['bytea_output', 'lc_monetary'])
    429429        self.assertIsInstance(r, list)
    430430        self.assertEqual(r, ['hex', 'C'])
    431         r = f(['standard_conforming_strings', 'datestyle', 'bytea_output'])
     431        r = f(('standard_conforming_strings', 'datestyle', 'bytea_output'))
    432432        self.assertEqual(r, ['on', 'ISO, YMD', 'hex'])
     433        r = f(set(['bytea_output', 'lc_monetary']))
     434        self.assertIsInstance(r, dict)
     435        self.assertEqual(r, {'bytea_output': 'hex', 'lc_monetary': 'C'})
     436        r = f(set(['Bytea_Output', ' LC_Monetary ']))
     437        self.assertIsInstance(r, dict)
     438        self.assertEqual(r, {'Bytea_Output': 'hex', ' LC_Monetary ': 'C'})
    433439        s = dict.fromkeys(('bytea_output', 'lc_monetary'))
    434440        r = f(s)
    435441        self.assertIs(r, s)
    436442        self.assertEqual(r, {'bytea_output': 'hex', 'lc_monetary': 'C'})
    437         s = dict.fromkeys(('Bytea_Output', 'LC_Monetary'))
     443        s = dict.fromkeys(('Bytea_Output', ' LC_Monetary '))
    438444        r = f(s)
    439445        self.assertIs(r, s)
    440         self.assertEqual(r, {'Bytea_Output': 'hex', 'LC_Monetary': 'C'})
     446        self.assertEqual(r, {'Bytea_Output': 'hex', ' LC_Monetary ': 'C'})
    441447
    442448    def testGetParameterServerVersion(self):
     
    467473        f('datestyle', 'ISO, DMY')
    468474        self.assertEqual(g('datestyle'), 'ISO, DMY')
     475        f(['standard_conforming_strings', 'datestyle'], ['on', 'ISO, DMY'])
     476        self.assertEqual(g('standard_conforming_strings'), 'on')
     477        self.assertEqual(g('datestyle'), 'ISO, DMY')
     478        f(['default_with_oids', 'standard_conforming_strings'], 'off')
     479        self.assertEqual(g('default_with_oids'), 'off')
     480        self.assertEqual(g('standard_conforming_strings'), 'off')
    469481        f(('standard_conforming_strings', 'datestyle'), ('on', 'ISO, YMD'))
    470         self.assertEqual(g('standard_conforming_strings'), 'on')
    471         self.assertEqual(g('datestyle'), 'ISO, YMD')
    472         f(['standard_conforming_strings', 'datestyle'], ['off', 'ISO, DMY'])
    473         self.assertEqual(g('standard_conforming_strings'), 'off')
    474         self.assertEqual(g('datestyle'), 'ISO, DMY')
    475         f({'standard_conforming_strings': 'on', 'datestyle': 'ISO, YMD'})
    476482        self.assertEqual(g('standard_conforming_strings'), 'on')
    477483        self.assertEqual(g('datestyle'), 'ISO, YMD')
     
    479485        self.assertEqual(g('default_with_oids'), 'off')
    480486        self.assertEqual(g('standard_conforming_strings'), 'off')
    481         f(['default_with_oids', 'standard_conforming_strings'], 'on')
     487        f(set(['default_with_oids', 'standard_conforming_strings']), 'on')
    482488        self.assertEqual(g('default_with_oids'), 'on')
    483489        self.assertEqual(g('standard_conforming_strings'), 'on')
     490        self.assertRaises(ValueError, f, set([ 'default_with_oids',
     491            'standard_conforming_strings']), ['off', 'on'])
     492        f(set(['default_with_oids', 'standard_conforming_strings']),
     493            ['off', 'off'])
     494        self.assertEqual(g('default_with_oids'), 'off')
     495        self.assertEqual(g('standard_conforming_strings'), 'off')
     496        f({'standard_conforming_strings': 'on', 'datestyle': 'ISO, YMD'})
     497        self.assertEqual(g('standard_conforming_strings'), 'on')
     498        self.assertEqual(g('datestyle'), 'ISO, YMD')
    484499
    485500    def testResetParameter(self):
     
    505520        self.assertEqual(g('default_with_oids'), not_dwi)
    506521        self.assertEqual(g('standard_conforming_strings'), not_scs)
     522        f(['default_with_oids', 'standard_conforming_strings'], None)
     523        self.assertEqual(g('default_with_oids'), dwi)
     524        self.assertEqual(g('standard_conforming_strings'), scs)
     525        f('default_with_oids', not_dwi)
     526        f('standard_conforming_strings', not_scs)
     527        self.assertEqual(g('default_with_oids'), not_dwi)
     528        self.assertEqual(g('standard_conforming_strings'), not_scs)
    507529        f(('default_with_oids', 'standard_conforming_strings'))
    508530        self.assertEqual(g('default_with_oids'), dwi)
     
    512534        self.assertEqual(g('default_with_oids'), not_dwi)
    513535        self.assertEqual(g('standard_conforming_strings'), not_scs)
    514         f(['default_with_oids', 'standard_conforming_strings'], None)
     536        f(set(['default_with_oids', 'standard_conforming_strings']), None)
    515537        self.assertEqual(g('default_with_oids'), dwi)
    516538        self.assertEqual(g('standard_conforming_strings'), scs)
Note: See TracChangeset for help on using the changeset viewer.