Selaa lähdekoodia

pygrass raster: Added iterators to iterate over raster info and history

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@66110 15284696-431f-4ddb-bdfa-cd5b030d7da7
Soeren Gebbert 9 vuotta sitten
vanhempi
commit
6b2dc88ebf

+ 10 - 0
lib/python/pygrass/raster/__init__.py

@@ -88,6 +88,16 @@ class RasterRow(RasterAbstractBase):
         '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.

+ 4 - 1
lib/python/pygrass/raster/abstract.py

@@ -189,7 +189,10 @@ class Info(object):
 
     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')

+ 8 - 7
lib/python/pygrass/raster/history.py

@@ -27,21 +27,19 @@ class History(object):
         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 @@ class History(object):
     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):