Author: cito
Date: Fri Jan 15 06:06:51 2016
New Revision: 747

Log:
Improve the get/set_parameter methods

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

Modified:
   branches/4.x/docs/contents/pg/db_wrapper.rst
   branches/4.x/pg.py
   branches/4.x/tests/test_classic_dbwrapper.py
   trunk/docs/contents/pg/db_wrapper.rst
   trunk/pg.py
   trunk/tests/test_classic_dbwrapper.py

Modified: branches/4.x/docs/contents/pg/db_wrapper.rst
==============================================================================
--- branches/4.x/docs/contents/pg/db_wrapper.rst        Thu Jan 14 20:27:05 
2016        (r746)
+++ branches/4.x/docs/contents/pg/db_wrapper.rst        Fri Jan 15 06:06:51 
2016        (r747)
@@ -149,10 +149,13 @@
 If the parameter is a string, the return value will also be a string
 that is the current setting of the run-time parameter with that name.
 
-You can get several parameters at once by passing a list or tuple of
-parameter names.  The return value will then be a corresponding list
-of parameter settings.  If you pass a dict as parameter instead, its
-values will be set to the parameter settings corresponding to its keys.
+You can get several parameters at once by passing a list, set or dict.
+When passing a list of parameter names, the return value will be a
+corresponding list of parameter settings.  When passing a set of
+parameter names, a new dict will be returned, mapping these parameter
+names to their settings.  Finally, if you pass a dict as parameter,
+its values will be set to the current parameter settings corresponding
+to its keys.
 
 By passing the special name `'all'` as the parameter, you can get a dict
 of all existing configuration parameters.
@@ -175,12 +178,13 @@
 will be set to that value.  If no value or *None* is passed as a value,
 then the run-time parameter will be restored to its default value.
 
-You can set several parameters at once by passing a list or tuple
-of parameter names, with a single value that all parameters should
-be set to or with a corresponding list or tuple of values.
-
-You can also pass a dict as parameters.  In this case, you should
-not pass a value, since the values will be taken from the dict.
+You can set several parameters at once by passing a list of parameter
+names, together with a single value that all parameters should be
+set to or with a corresponding list of values.  You can also pass
+the parameters as a set if you only provide a single value.
+Finally, you can pass a dict with parameter names as keys.  In this
+case, you should not pass a value, since the values for the parameters
+will be taken from the dict.
 
 By passing the special name `'all'` as the parameter, you can reset
 all existing settable run-time parameters to their default values.

Modified: branches/4.x/pg.py
==============================================================================
--- branches/4.x/pg.py  Thu Jan 14 20:27:05 2016        (r746)
+++ branches/4.x/pg.py  Fri Jan 15 06:06:51 2016        (r747)
@@ -523,10 +523,13 @@
         If the parameter is a string, the return value will also be a string
         that is the current setting of the run-time parameter with that name.
 
-        You can get several parameters at once by passing a list or tuple of
-        parameter names.  The return value will then be a corresponding list
-        of parameter settings.  If you pass a dict as parameter instead, its
-        values will be set to the parameter settings corresponding to its keys.
+        You can get several parameters at once by passing a list, set or dict.
+        When passing a list of parameter names, the return value will be a
+        corresponding list of parameter settings.  When passing a set of
+        parameter names, a new dict will be returned, mapping these parameter
+        names to their settings.  Finally, if you pass a dict as parameter,
+        its values will be set to the current parameter settings corresponding
+        to its keys.
 
         By passing the special name 'all' as the parameter, you can get a dict
         of all existing configuration parameters.
@@ -536,10 +539,13 @@
             values = None
         elif isinstance(parameter, (list, tuple)):
             values = []
+        elif isinstance(parameter, (set, frozenset)):
+            values = {}
         elif isinstance(parameter, dict):
             values = parameter
         else:
-            raise TypeError('The parameter must be a dict, list or string')
+            raise TypeError(
+                'The parameter must be a string, list, set or dict')
         if not parameter:
             raise TypeError('No parameter has been specified')
         if isinstance(values, dict):
@@ -581,12 +587,13 @@
         will be set to that value.  If no value or None is passed as a value,
         then the run-time parameter will be restored to its default value.
 
-        You can set several parameters at once by passing a list or tuple
-        of parameter names, with a single value that all parameters should
-        be set to or with a corresponding list or tuple of values.
-
-        You can also pass a dict as parameters.  In this case, you should
-        not pass a value, since the values will be taken from the dict.
+        You can set several parameters at once by passing a list of parameter
+        names, together with a single value that all parameters should be
+        set to or with a corresponding list of values.  You can also pass
+        the parameters as a set if you only provide a single value.
+        Finally, you can pass a dict with parameter names as keys.  In this
+        case, you should not pass a value, since the values for the parameters
+        will be taken from the dict.
 
         By passing the special name 'all' as the parameter, you can reset
         all existing settable run-time parameters to their default values.
@@ -604,12 +611,22 @@
                 parameter = dict(zip(parameter, value))
             else:
                 parameter = dict.fromkeys(parameter, value)
+        elif isinstance(parameter, (set, frozenset)):
+            if isinstance(value, (list, tuple, set, frozenset)):
+                value = set(value)
+                if len(value) == 1:
+                    value = value.pop()
+            if not(value is None or isinstance(value, basestring)):
+                raise ValueError('A single value must be specified'
+                    ' when parameter is a set')
+            parameter = dict.fromkeys(parameter, value)
         elif isinstance(parameter, dict):
             if value is not None:
                 raise ValueError(
                     'A value must not be set when parameter is a dictionary')
         else:
-            raise TypeError('The parameter must be a dict, list or string')
+            raise TypeError(
+                'The parameter must be a string, list, set or dict')
         if not parameter:
             raise TypeError('No parameter has been specified')
         params = {}

Modified: branches/4.x/tests/test_classic_dbwrapper.py
==============================================================================
--- branches/4.x/tests/test_classic_dbwrapper.py        Thu Jan 14 20:27:05 
2016        (r746)
+++ branches/4.x/tests/test_classic_dbwrapper.py        Fri Jan 15 06:06:51 
2016        (r747)
@@ -425,19 +425,25 @@
         self.assertEqual(r, 'ISO, YMD')
         r = f('bytea_output')
         self.assertEqual(r, 'hex')
-        r = f(('bytea_output', 'lc_monetary'))
+        r = f(['bytea_output', 'lc_monetary'])
         self.assertIsInstance(r, list)
         self.assertEqual(r, ['hex', 'C'])
-        r = f(['standard_conforming_strings', 'datestyle', 'bytea_output'])
+        r = f(('standard_conforming_strings', 'datestyle', 'bytea_output'))
         self.assertEqual(r, ['on', 'ISO, YMD', 'hex'])
+        r = f(set(['bytea_output', 'lc_monetary']))
+        self.assertIsInstance(r, dict)
+        self.assertEqual(r, {'bytea_output': 'hex', 'lc_monetary': 'C'})
+        r = f(set(['Bytea_Output', ' LC_Monetary ']))
+        self.assertIsInstance(r, dict)
+        self.assertEqual(r, {'Bytea_Output': 'hex', ' LC_Monetary ': 'C'})
         s = dict.fromkeys(('bytea_output', 'lc_monetary'))
         r = f(s)
         self.assertIs(r, s)
         self.assertEqual(r, {'bytea_output': 'hex', 'lc_monetary': 'C'})
-        s = dict.fromkeys(('Bytea_Output', 'LC_Monetary'))
+        s = dict.fromkeys(('Bytea_Output', ' LC_Monetary '))
         r = f(s)
         self.assertIs(r, s)
-        self.assertEqual(r, {'Bytea_Output': 'hex', 'LC_Monetary': 'C'})
+        self.assertEqual(r, {'Bytea_Output': 'hex', ' LC_Monetary ': 'C'})
 
     def testGetParameterServerVersion(self):
         r = self.db.get_parameter('server_version_num')
@@ -466,21 +472,30 @@
         self.assertEqual(g('standard_conforming_strings'), 'off')
         f('datestyle', 'ISO, DMY')
         self.assertEqual(g('datestyle'), 'ISO, DMY')
-        f(('standard_conforming_strings', 'datestyle'), ('on', 'ISO, YMD'))
+        f(['standard_conforming_strings', 'datestyle'], ['on', 'ISO, DMY'])
         self.assertEqual(g('standard_conforming_strings'), 'on')
-        self.assertEqual(g('datestyle'), 'ISO, YMD')
-        f(['standard_conforming_strings', 'datestyle'], ['off', 'ISO, DMY'])
-        self.assertEqual(g('standard_conforming_strings'), 'off')
         self.assertEqual(g('datestyle'), 'ISO, DMY')
-        f({'standard_conforming_strings': 'on', 'datestyle': 'ISO, YMD'})
+        f(['default_with_oids', 'standard_conforming_strings'], 'off')
+        self.assertEqual(g('default_with_oids'), 'off')
+        self.assertEqual(g('standard_conforming_strings'), 'off')
+        f(('standard_conforming_strings', 'datestyle'), ('on', 'ISO, YMD'))
         self.assertEqual(g('standard_conforming_strings'), 'on')
         self.assertEqual(g('datestyle'), 'ISO, YMD')
         f(('default_with_oids', 'standard_conforming_strings'), 'off')
         self.assertEqual(g('default_with_oids'), 'off')
         self.assertEqual(g('standard_conforming_strings'), 'off')
-        f(['default_with_oids', 'standard_conforming_strings'], 'on')
+        f(set(['default_with_oids', 'standard_conforming_strings']), 'on')
         self.assertEqual(g('default_with_oids'), 'on')
         self.assertEqual(g('standard_conforming_strings'), 'on')
+        self.assertRaises(ValueError, f, set([ 'default_with_oids',
+            'standard_conforming_strings']), ['off', 'on'])
+        f(set(['default_with_oids', 'standard_conforming_strings']),
+            ['off', 'off'])
+        self.assertEqual(g('default_with_oids'), 'off')
+        self.assertEqual(g('standard_conforming_strings'), 'off')
+        f({'standard_conforming_strings': 'on', 'datestyle': 'ISO, YMD'})
+        self.assertEqual(g('standard_conforming_strings'), 'on')
+        self.assertEqual(g('datestyle'), 'ISO, YMD')
 
     def testResetParameter(self):
         db = DB()
@@ -504,6 +519,13 @@
         f('standard_conforming_strings', not_scs)
         self.assertEqual(g('default_with_oids'), not_dwi)
         self.assertEqual(g('standard_conforming_strings'), not_scs)
+        f(['default_with_oids', 'standard_conforming_strings'], None)
+        self.assertEqual(g('default_with_oids'), dwi)
+        self.assertEqual(g('standard_conforming_strings'), scs)
+        f('default_with_oids', not_dwi)
+        f('standard_conforming_strings', not_scs)
+        self.assertEqual(g('default_with_oids'), not_dwi)
+        self.assertEqual(g('standard_conforming_strings'), not_scs)
         f(('default_with_oids', 'standard_conforming_strings'))
         self.assertEqual(g('default_with_oids'), dwi)
         self.assertEqual(g('standard_conforming_strings'), scs)
@@ -511,7 +533,7 @@
         f('standard_conforming_strings', not_scs)
         self.assertEqual(g('default_with_oids'), not_dwi)
         self.assertEqual(g('standard_conforming_strings'), not_scs)
-        f(['default_with_oids', 'standard_conforming_strings'], None)
+        f(set(['default_with_oids', 'standard_conforming_strings']), None)
         self.assertEqual(g('default_with_oids'), dwi)
         self.assertEqual(g('standard_conforming_strings'), scs)
 

Modified: trunk/docs/contents/pg/db_wrapper.rst
==============================================================================
--- trunk/docs/contents/pg/db_wrapper.rst       Thu Jan 14 20:27:05 2016        
(r746)
+++ trunk/docs/contents/pg/db_wrapper.rst       Fri Jan 15 06:06:51 2016        
(r747)
@@ -167,10 +167,13 @@
 If the parameter is a string, the return value will also be a string
 that is the current setting of the run-time parameter with that name.
 
-You can get several parameters at once by passing a list or tuple of
-parameter names.  The return value will then be a corresponding list
-of parameter settings.  If you pass a dict as parameter instead, its
-values will be set to the parameter settings corresponding to its keys.
+You can get several parameters at once by passing a list, set or dict.
+When passing a list of parameter names, the return value will be a
+corresponding list of parameter settings.  When passing a set of
+parameter names, a new dict will be returned, mapping these parameter
+names to their settings.  Finally, if you pass a dict as parameter,
+its values will be set to the current parameter settings corresponding
+to its keys.
 
 By passing the special name `'all'` as the parameter, you can get a dict
 of all existing configuration parameters.
@@ -193,12 +196,13 @@
 will be set to that value.  If no value or *None* is passed as a value,
 then the run-time parameter will be restored to its default value.
 
-You can set several parameters at once by passing a list or tuple
-of parameter names, with a single value that all parameters should
-be set to or with a corresponding list or tuple of values.
-
-You can also pass a dict as parameters.  In this case, you should
-not pass a value, since the values will be taken from the dict.
+You can set several parameters at once by passing a list of parameter
+names, together with a single value that all parameters should be
+set to or with a corresponding list of values.  You can also pass
+the parameters as a set if you only provide a single value.
+Finally, you can pass a dict with parameter names as keys.  In this
+case, you should not pass a value, since the values for the parameters
+will be taken from the dict.
 
 By passing the special name `'all'` as the parameter, you can reset
 all existing settable run-time parameters to their default values.

Modified: trunk/pg.py
==============================================================================
--- trunk/pg.py Thu Jan 14 20:27:05 2016        (r746)
+++ trunk/pg.py Fri Jan 15 06:06:51 2016        (r747)
@@ -468,10 +468,13 @@
         If the parameter is a string, the return value will also be a string
         that is the current setting of the run-time parameter with that name.
 
-        You can get several parameters at once by passing a list or tuple of
-        parameter names.  The return value will then be a corresponding list
-        of parameter settings.  If you pass a dict as parameter instead, its
-        values will be set to the parameter settings corresponding to its keys.
+        You can get several parameters at once by passing a list, set or dict.
+        When passing a list of parameter names, the return value will be a
+        corresponding list of parameter settings.  When passing a set of
+        parameter names, a new dict will be returned, mapping these parameter
+        names to their settings.  Finally, if you pass a dict as parameter,
+        its values will be set to the current parameter settings corresponding
+        to its keys.
 
         By passing the special name 'all' as the parameter, you can get a dict
         of all existing configuration parameters.
@@ -481,10 +484,13 @@
             values = None
         elif isinstance(parameter, (list, tuple)):
             values = []
+        elif isinstance(parameter, (set, frozenset)):
+            values = {}
         elif isinstance(parameter, dict):
             values = parameter
         else:
-            raise TypeError('The parameter must be a dict, list or string')
+            raise TypeError(
+                'The parameter must be a string, list, set or dict')
         if not parameter:
             raise TypeError('No parameter has been specified')
         params = {} if isinstance(values, dict) else []
@@ -521,12 +527,13 @@
         will be set to that value.  If no value or None is passed as a value,
         then the run-time parameter will be restored to its default value.
 
-        You can set several parameters at once by passing a list or tuple
-        of parameter names, with a single value that all parameters should
-        be set to or with a corresponding list or tuple of values.
-
-        You can also pass a dict as parameters.  In this case, you should
-        not pass a value, since the values will be taken from the dict.
+        You can set several parameters at once by passing a list of parameter
+        names, together with a single value that all parameters should be
+        set to or with a corresponding list of values.  You can also pass
+        the parameters as a set if you only provide a single value.
+        Finally, you can pass a dict with parameter names as keys.  In this
+        case, you should not pass a value, since the values for the parameters
+        will be taken from the dict.
 
         By passing the special name 'all' as the parameter, you can reset
         all existing settable run-time parameters to their default values.
@@ -544,12 +551,22 @@
                 parameter = dict(zip(parameter, value))
             else:
                 parameter = dict.fromkeys(parameter, value)
+        elif isinstance(parameter, (set, frozenset)):
+            if isinstance(value, (list, tuple, set, frozenset)):
+                value = set(value)
+                if len(value) == 1:
+                    value = value.pop()
+            if not(value is None or isinstance(value, basestring)):
+                raise ValueError('A single value must be specified'
+                    ' when parameter is a set')
+            parameter = dict.fromkeys(parameter, value)
         elif isinstance(parameter, dict):
             if value is not None:
-                raise ValueError(
-                    'A value must not be set when parameter is a dictionary')
+                raise ValueError('A value must not be specified'
+                    ' when parameter is a dictionary')
         else:
-            raise TypeError('The parameter must be a dict, list or string')
+            raise TypeError(
+                'The parameter must be a string, list, set or dict')
         if not parameter:
             raise TypeError('No parameter has been specified')
         params = {}

Modified: trunk/tests/test_classic_dbwrapper.py
==============================================================================
--- trunk/tests/test_classic_dbwrapper.py       Thu Jan 14 20:27:05 2016        
(r746)
+++ trunk/tests/test_classic_dbwrapper.py       Fri Jan 15 06:06:51 2016        
(r747)
@@ -396,19 +396,25 @@
         self.assertEqual(r, 'ISO, YMD')
         r = f('bytea_output')
         self.assertEqual(r, 'hex')
-        r = f(('bytea_output', 'lc_monetary'))
+        r = f(['bytea_output', 'lc_monetary'])
         self.assertIsInstance(r, list)
         self.assertEqual(r, ['hex', 'C'])
-        r = f(['standard_conforming_strings', 'datestyle', 'bytea_output'])
+        r = f(('standard_conforming_strings', 'datestyle', 'bytea_output'))
         self.assertEqual(r, ['on', 'ISO, YMD', 'hex'])
+        r = f(set(['bytea_output', 'lc_monetary']))
+        self.assertIsInstance(r, dict)
+        self.assertEqual(r, {'bytea_output': 'hex', 'lc_monetary': 'C'})
+        r = f(set(['Bytea_Output', ' LC_Monetary ']))
+        self.assertIsInstance(r, dict)
+        self.assertEqual(r, {'Bytea_Output': 'hex', ' LC_Monetary ': 'C'})
         s = dict.fromkeys(('bytea_output', 'lc_monetary'))
         r = f(s)
         self.assertIs(r, s)
         self.assertEqual(r, {'bytea_output': 'hex', 'lc_monetary': 'C'})
-        s = dict.fromkeys(('Bytea_Output', 'LC_Monetary'))
+        s = dict.fromkeys(('Bytea_Output', ' LC_Monetary '))
         r = f(s)
         self.assertIs(r, s)
-        self.assertEqual(r, {'Bytea_Output': 'hex', 'LC_Monetary': 'C'})
+        self.assertEqual(r, {'Bytea_Output': 'hex', ' LC_Monetary ': 'C'})
 
     def testGetParameterServerVersion(self):
         r = self.db.get_parameter('server_version_num')
@@ -437,21 +443,30 @@
         self.assertEqual(g('standard_conforming_strings'), 'off')
         f('datestyle', 'ISO, DMY')
         self.assertEqual(g('datestyle'), 'ISO, DMY')
-        f(('standard_conforming_strings', 'datestyle'), ('on', 'ISO, YMD'))
+        f(['standard_conforming_strings', 'datestyle'], ['on', 'ISO, DMY'])
         self.assertEqual(g('standard_conforming_strings'), 'on')
-        self.assertEqual(g('datestyle'), 'ISO, YMD')
-        f(['standard_conforming_strings', 'datestyle'], ['off', 'ISO, DMY'])
-        self.assertEqual(g('standard_conforming_strings'), 'off')
         self.assertEqual(g('datestyle'), 'ISO, DMY')
-        f({'standard_conforming_strings': 'on', 'datestyle': 'ISO, YMD'})
+        f(['default_with_oids', 'standard_conforming_strings'], 'off')
+        self.assertEqual(g('default_with_oids'), 'off')
+        self.assertEqual(g('standard_conforming_strings'), 'off')
+        f(('standard_conforming_strings', 'datestyle'), ('on', 'ISO, YMD'))
         self.assertEqual(g('standard_conforming_strings'), 'on')
         self.assertEqual(g('datestyle'), 'ISO, YMD')
         f(('default_with_oids', 'standard_conforming_strings'), 'off')
         self.assertEqual(g('default_with_oids'), 'off')
         self.assertEqual(g('standard_conforming_strings'), 'off')
-        f(['default_with_oids', 'standard_conforming_strings'], 'on')
+        f(set(['default_with_oids', 'standard_conforming_strings']), 'on')
         self.assertEqual(g('default_with_oids'), 'on')
         self.assertEqual(g('standard_conforming_strings'), 'on')
+        self.assertRaises(ValueError, f, set([ 'default_with_oids',
+            'standard_conforming_strings']), ['off', 'on'])
+        f(set(['default_with_oids', 'standard_conforming_strings']),
+            ['off', 'off'])
+        self.assertEqual(g('default_with_oids'), 'off')
+        self.assertEqual(g('standard_conforming_strings'), 'off')
+        f({'standard_conforming_strings': 'on', 'datestyle': 'ISO, YMD'})
+        self.assertEqual(g('standard_conforming_strings'), 'on')
+        self.assertEqual(g('datestyle'), 'ISO, YMD')
 
     def testResetParameter(self):
         db = DB()
@@ -475,6 +490,13 @@
         f('standard_conforming_strings', not_scs)
         self.assertEqual(g('default_with_oids'), not_dwi)
         self.assertEqual(g('standard_conforming_strings'), not_scs)
+        f(['default_with_oids', 'standard_conforming_strings'], None)
+        self.assertEqual(g('default_with_oids'), dwi)
+        self.assertEqual(g('standard_conforming_strings'), scs)
+        f('default_with_oids', not_dwi)
+        f('standard_conforming_strings', not_scs)
+        self.assertEqual(g('default_with_oids'), not_dwi)
+        self.assertEqual(g('standard_conforming_strings'), not_scs)
         f(('default_with_oids', 'standard_conforming_strings'))
         self.assertEqual(g('default_with_oids'), dwi)
         self.assertEqual(g('standard_conforming_strings'), scs)
@@ -482,7 +504,7 @@
         f('standard_conforming_strings', not_scs)
         self.assertEqual(g('default_with_oids'), not_dwi)
         self.assertEqual(g('standard_conforming_strings'), not_scs)
-        f(['default_with_oids', 'standard_conforming_strings'], None)
+        f(set(['default_with_oids', 'standard_conforming_strings']))
         self.assertEqual(g('default_with_oids'), dwi)
         self.assertEqual(g('standard_conforming_strings'), scs)
 
_______________________________________________
PyGreSQL mailing list
[email protected]
https://mail.vex.net/mailman/listinfo.cgi/pygresql

Reply via email to