[GRASS-SVN] r50972 - grass/trunk/lib/python

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Mar 2 12:24:25 EST 2012


Author: glynn
Date: 2012-03-02 09:24:25 -0800 (Fri, 02 Mar 2012)
New Revision: 50972

Modified:
   grass/trunk/lib/python/core.py
Log:
Use KeyValue object for key-value data
 (allows use of e.g. region.rows as an alternative to region['rows'])


Modified: grass/trunk/lib/python/core.py
===================================================================
--- grass/trunk/lib/python/core.py	2012-03-02 16:55:37 UTC (rev 50971)
+++ grass/trunk/lib/python/core.py	2012-03-02 17:24:25 UTC (rev 50972)
@@ -486,6 +486,27 @@
     
     return tmp
 
+class KeyValue(dict):
+    """A general-purpose key-value store.
+
+    KeyValue is a subclass of dict, but also allows entries to be read and
+    written using attribute syntax. Example:
+
+    \code
+    >>> region = grass.region()
+    >>> region['rows']
+    477
+    >>> region.rows
+    477
+    \endcode
+    """
+
+    def __getattr__(self, key):
+        return self[key]
+
+    def __setattr__(self, key, value):
+        self[key] = value
+
 # key-value parsers
 
 def parse_key_val(s, sep = '=', dflt = None, val_type = None, vsep = None):
@@ -500,7 +521,7 @@
 
     @return parsed input (dictionary of keys/values)
     """
-    result = {}
+    result = KeyValue()
 
     if not s:
         return result



More information about the grass-commit mailing list