[GRASS-SVN] r55953 - grass/trunk/lib/python/pygrass/raster

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Apr 23 03:46:47 PDT 2013


Author: zarch
Date: 2013-04-23 03:46:47 -0700 (Tue, 23 Apr 2013)
New Revision: 55953

Modified:
   grass/trunk/lib/python/pygrass/raster/__init__.py
   grass/trunk/lib/python/pygrass/raster/abstract.py
Log:
Add the *with statement* to the Raster classes and add an example in the docstring.

Modified: grass/trunk/lib/python/pygrass/raster/__init__.py
===================================================================
--- grass/trunk/lib/python/pygrass/raster/__init__.py	2013-04-23 10:45:23 UTC (rev 55952)
+++ grass/trunk/lib/python/pygrass/raster/__init__.py	2013-04-23 10:46:47 UTC (rev 55953)
@@ -34,8 +34,6 @@
 from buffer import Buffer
 from segment import Segment
 from rowio import RowIO
-from category import Category
-from history import History
 
 
 class RasterRow(RasterAbstractBase):
@@ -101,7 +99,18 @@
          ('bare ground path', 10, None),
          ('grass', 11, None)]
 
+    Open a raster map using the *with statement*: ::
 
+        >>> with RasterRow('elevation') as elev:
+        ...     for row in elev[:3]:
+        ...         print row[:4]
+        ...
+        [ 141.99613953  141.27848816  141.37904358  142.29821777]
+        [ 142.90461731  142.39450073  142.68611145  143.59086609]
+        [ 143.81854248  143.54707336  143.83972168  144.59527588]
+        >>> elev.is_open()
+        False
+
     """
     def __init__(self, name, mapset='', *args, **kargs):
         super(RasterRow, self).__init__(name, mapset, *args, **kargs)

Modified: grass/trunk/lib/python/pygrass/raster/abstract.py
===================================================================
--- grass/trunk/lib/python/pygrass/raster/abstract.py	2013-04-23 10:45:23 UTC (rev 55952)
+++ grass/trunk/lib/python/pygrass/raster/abstract.py	2013-04-23 10:46:47 UTC (rev 55953)
@@ -4,19 +4,12 @@
 
 @author: pietro
 """
-
-
 import ctypes
 
-from numpy import isnan
-
 #
 # import GRASS modules
 #
-from grass.script import fatal, warning, gisenv
-from grass.script import core as grasscore
-#from grass.script import core
-#import grass.lib as grasslib
+from grass.script import fatal, gisenv
 import grass.lib.gis as libgis
 import grass.lib.raster as libraster
 
@@ -26,7 +19,7 @@
 from grass.pygrass import functions
 from grass.pygrass.gis.region import Region
 from grass.pygrass.errors import must_be_open
-from grass.pygrass.gis import Mapset
+
 #
 # import raster classes
 #
@@ -194,7 +187,13 @@
         if self.exist():
             self.info = Info(self.name, self.mapset)
 
+    def __enter__(self):
+        self.open('r')
+        return self
 
+    def __exit__(self, exc_type, exc_value, traceback):
+        self.close()
+
     def _get_mtype(self):
         """Private method to get the Raster type"""
         return self._mtype



More information about the grass-commit mailing list