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

svn_grass at osgeo.org svn_grass at osgeo.org
Sat Sep 5 05:47:33 PDT 2015


Author: huhabla
Date: 2015-09-05 05:47:33 -0700 (Sat, 05 Sep 2015)
New Revision: 66110

Modified:
   grass/trunk/lib/python/pygrass/raster/__init__.py
   grass/trunk/lib/python/pygrass/raster/abstract.py
   grass/trunk/lib/python/pygrass/raster/history.py
Log:
pygrass raster: Added iterators to iterate over raster info and history


Modified: grass/trunk/lib/python/pygrass/raster/__init__.py
===================================================================
--- grass/trunk/lib/python/pygrass/raster/__init__.py	2015-09-05 09:00:50 UTC (rev 66109)
+++ grass/trunk/lib/python/pygrass/raster/__init__.py	2015-09-05 12:47:33 UTC (rev 66110)
@@ -88,6 +88,16 @@
         'A test map'
         >>> elev.hist.keyword
         'This is a test map'
+        
+        >>> attrs = list(elev.hist)
+        >>> attrs[0]
+        ('name', u'Raster_test_map')
+        >>> attrs[1]
+        ('mapset', 'user1')
+        >>> attrs[2]
+        ('mtype', '')
+        >>> attrs[3]
+        ('creator', 'soeren')
 
         Each Raster map have an attribute call ``cats`` that allow user
         to interact with the raster categories.

Modified: grass/trunk/lib/python/pygrass/raster/abstract.py
===================================================================
--- grass/trunk/lib/python/pygrass/raster/abstract.py	2015-09-05 09:00:50 UTC (rev 66109)
+++ grass/trunk/lib/python/pygrass/raster/abstract.py	2015-09-05 12:47:33 UTC (rev 66110)
@@ -189,7 +189,10 @@
 
     def items(self):
         return [(k, self.__getattribute__(k)) for k in self.keys()]
-
+        
+    def __iter__(self):
+        return ((k, self.__getattribute__(k)) for k in self.keys())
+        
     def _repr_html_(self):
         return dict2html(dict(self.items()), keys=self.keys(),
                          border='1', kdec='b')

Modified: grass/trunk/lib/python/pygrass/raster/history.py
===================================================================
--- grass/trunk/lib/python/pygrass/raster/history.py	2015-09-05 09:00:50 UTC (rev 66109)
+++ grass/trunk/lib/python/pygrass/raster/history.py	2015-09-05 12:47:33 UTC (rev 66110)
@@ -27,21 +27,19 @@
         self.keyword = keyword
         self.date = date
         self.title = title
+        self.attrs = ['name', 'mapset', 'mtype', 'creator', 'src1', 'src2',
+                      'keyword', 'date', 'title']
 
     def __repr__(self):
-        attrs = ['name', 'mapset', 'mtype', 'creator', 'src1', 'src2',
-                 'keyword', 'date', 'title']
-        return "History(%s)" % ', '.join(["%s=%r" % (attr, getattr(self, attr))
-                                          for attr in attrs])
+        return "History(%s)" % ', '.join(["%s=%r" % (self.attr, getattr(self, attr))
+                                          for attr in self.attrs])
 
     def __del__(self):
         """Rast_free_history"""
         pass
         
     def __eq__(self, hist):
-        attrs = ['name', 'mapset', 'mtype', 'creator', 'src1', 'src2',
-                 'keyword', 'date', 'title']
-        for attr in attrs:
+        for attr in self.attrs:
            if getattr(self, attr) != getattr(hist, attr):
                 return False
         return True
@@ -49,6 +47,9 @@
     def __len__(self):
         return self.length()
 
+    def __iter__(self):
+        return ((attr, getattr(self, attr)) for attr in self.attrs)
+
     #----------------------------------------------------------------------
     #libraster.HIST_CREATOR
     def _get_creator(self):



More information about the grass-commit mailing list