Changeset 747


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.

Files:
6 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)
  • 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.