[GRASS-SVN] r61182 - in grass/trunk/lib/python/pygrass/modules/interface: . testsuite

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Jul 8 00:10:51 PDT 2014


Author: zarch
Date: 2014-07-08 00:10:51 -0700 (Tue, 08 Jul 2014)
New Revision: 61182

Modified:
   grass/trunk/lib/python/pygrass/modules/interface/parameter.py
   grass/trunk/lib/python/pygrass/modules/interface/testsuite/test_parameter.py
Log:
pygrass: modify the parameter check function to be less stringent with strings parameters

Modified: grass/trunk/lib/python/pygrass/modules/interface/parameter.py
===================================================================
--- grass/trunk/lib/python/pygrass/modules/interface/parameter.py	2014-07-08 03:45:45 UTC (rev 61181)
+++ grass/trunk/lib/python/pygrass/modules/interface/parameter.py	2014-07-08 07:10:51 UTC (rev 61182)
@@ -18,7 +18,6 @@
     """
     must_val = 'The Parameter <%s>, must be one of the following values: %r'
     req = 'The Parameter <%s>, require: %s, get: %s instead: %r\n%s'
-    string = (str, unicode)
 
     def raiseexcpet(exc, param, ptype, value):
         """Function to modifa the error message"""
@@ -33,10 +32,14 @@
 
     def check_string(value):
         """Function to check that a string parameter is already a string"""
-        if param.type in string and type(value) not in string:
-            msg = ("The Parameter <%s> require a string,"
-                   " %s instead is provided: %r")
-            raise ValueError(msg % (param.name, type(value), value))
+        string = (str, unicode)
+        if param.type in string:
+            if type(value) in (int, float):
+                value = str(value)
+            if type(value) not in string:
+                msg = ("The Parameter <%s> require a string,"
+                       " %s instead is provided: %r")
+                raise ValueError(msg % (param.name, type(value), value))
         return value
 
     # return None if None

Modified: grass/trunk/lib/python/pygrass/modules/interface/testsuite/test_parameter.py
===================================================================
--- grass/trunk/lib/python/pygrass/modules/interface/testsuite/test_parameter.py	2014-07-08 03:45:45 UTC (rev 61181)
+++ grass/trunk/lib/python/pygrass/modules/interface/testsuite/test_parameter.py	2014-07-08 07:10:51 UTC (rev 61182)
@@ -189,7 +189,6 @@
             _check_value(param, "elev")
         with self.assertRaises(TypeError):
             _check_value(param, (1, 2))
-        #import ipdb; ipdb.set_trace()
         with self.assertRaises(ValueError):
             _check_value(param, 3)
 
@@ -199,12 +198,14 @@
                                        multiple='no', type=ptype))
             value = u'elev'
             self.assertTupleEqual((value, value), _check_value(param, value))
+            value = 10
+            self.assertTupleEqual((str(value), value),
+                                  _check_value(param, value))
+            value = 12.5
+            self.assertTupleEqual((str(value), value),
+                                  _check_value(param, value))
 
             # test errors
-            with self.assertRaises(ValueError):
-                _check_value(param, 1)
-            with self.assertRaises(ValueError):
-                _check_value(param, 1.0)
             with self.assertRaises(TypeError):
                 _check_value(param, ('abc', 'def'))
 
@@ -217,10 +218,16 @@
         self.assertTupleEqual((list(value), value), _check_value(param, value))
         value = ['1.3', '2.3', '4.5']
         self.assertTupleEqual((value, value), _check_value(param, value))
+        value = [1.3, 2.3, 4.5]
+        self.assertTupleEqual(([str(v) for v in value], value),
+                              _check_value(param, value))
+        value = (1, 2, 3)
+        self.assertTupleEqual(([str(v) for v in value], value),
+                              _check_value(param, value))
 
         # test errors
         with self.assertRaises(ValueError):
-            _check_value(param, (1, 2, 3))
+            _check_value(param, ({}, {}, {}))
 
     def test_choice_string(self):
         values = ["elev", "asp", "slp"]



More information about the grass-commit mailing list