[GRASS-SVN] r72315 - in grass/trunk/lib/python/pygrass/raster: . testsuite

svn_grass at osgeo.org svn_grass at osgeo.org
Sat Mar 3 03:19:47 PST 2018


Author: marisn
Date: 2018-03-03 03:19:47 -0800 (Sat, 03 Mar 2018)
New Revision: 72315

Modified:
   grass/trunk/lib/python/pygrass/raster/__init__.py
   grass/trunk/lib/python/pygrass/raster/abstract.py
   grass/trunk/lib/python/pygrass/raster/testsuite/test_raster.py
Log:
pygrass: Provide better error message when raster row index is out of range;
Raise IndexError (instead of fatal exit) when operating on a colsed map. This is a more Pyhtonic way.



Modified: grass/trunk/lib/python/pygrass/raster/__init__.py
===================================================================
--- grass/trunk/lib/python/pygrass/raster/__init__.py	2018-03-03 09:33:53 UTC (rev 72314)
+++ grass/trunk/lib/python/pygrass/raster/__init__.py	2018-03-03 11:19:47 UTC (rev 72315)
@@ -53,6 +53,7 @@
         * No mathematical operation like __add__ and stuff for the Raster
           object (only for rows), since r.mapcalc is more sophisticated and
           faster
+        * Rises IndexError if [row] is out of range
 
         Examples:
 

Modified: grass/trunk/lib/python/pygrass/raster/abstract.py
===================================================================
--- grass/trunk/lib/python/pygrass/raster/abstract.py	2018-03-03 09:33:53 UTC (rev 72314)
+++ grass/trunk/lib/python/pygrass/raster/abstract.py	2018-03-03 11:19:47 UTC (rev 72315)
@@ -11,7 +11,7 @@
 #
 # import GRASS modules
 #
-from grass.script import fatal, gisenv
+from grass.script import fatal, error, gisenv
 import grass.lib.gis as libgis
 import grass.lib.raster as libraster
 
@@ -334,11 +334,12 @@
             x, y = key
             return self.get(x, y)
         elif isinstance(key, int):
+            if not self.is_open():
+                raise IndexError("Can not operate on a closed map. Call open() first.")
             if key < 0:  # Handle negative indices
                 key += self._rows
             if key >= self._rows:
-                fatal(INDXOUTRANGE.format(key))
-                raise IndexError
+                raise IndexError("The row index {0} is out of range [0, {1}).".format(key, self._rows))
             return self.get_row(key)
         else:
             fatal("Invalid argument type.")

Modified: grass/trunk/lib/python/pygrass/raster/testsuite/test_raster.py
===================================================================
--- grass/trunk/lib/python/pygrass/raster/testsuite/test_raster.py	2018-03-03 09:33:53 UTC (rev 72314)
+++ grass/trunk/lib/python/pygrass/raster/testsuite/test_raster.py	2018-03-03 11:19:47 UTC (rev 72315)
@@ -16,13 +16,13 @@
         """Create test raster map and region"""
         cls.use_temp_region()
         cls.runModule("g.region", n=40, s=0, e=40, w=0, res=10)
-        cls.runModule("r.mapcalc", expression="%s = row() + (10.0 * col())"%(cls.name),
+        cls.runModule("r.mapcalc", expression="%s = row() + (10.0 * col())" % (cls.name),
                                    overwrite=True)
 
     @classmethod
     def tearDownClass(cls):
         """Remove the generated vector map, if exist"""
-        cls.runModule("g.remove", flags='f', type='raster', 
+        cls.runModule("g.remove", flags='f', type='raster',
                       name=cls.name)
         cls.del_temp_region()
 
@@ -29,7 +29,7 @@
     def test_type(self):
         r = RasterRow(self.name)
         r.open(mode='r')
-        self.assertTrue(r.mtype,'DCELL')
+        self.assertTrue(r.mtype, 'DCELL')
         r.close()
 
     def test_isopen(self):
@@ -77,6 +77,17 @@
         r.open(mode='w', mtype='DCELL', overwrite=True)
         self.assertTrue(r.mtype, 'DCELL')
         r.close()
+    
+    def test_row_range(self):
+        r = RasterRow(self.name)
+        with self.assertRaises(IndexError):
+            # Map is not open yet
+            r[1]
+        with self.assertRaises(IndexError):
+            # Index is out of range
+            r.open()
+            r[9999]
+        r.close()
 
 
 if __name__ == '__main__':



More information about the grass-commit mailing list