[GRASS-SVN] r33524 - in grass/trunk: lib/python scripts/v.rast.stats

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Sep 24 14:41:31 EDT 2008


Author: glynn
Date: 2008-09-24 14:41:31 -0400 (Wed, 24 Sep 2008)
New Revision: 33524

Modified:
   grass/trunk/lib/python/grass.py
   grass/trunk/scripts/v.rast.stats/v.rast.stats.py
Log:
Add, use parse_key_val


Modified: grass/trunk/lib/python/grass.py
===================================================================
--- grass/trunk/lib/python/grass.py	2008-09-24 15:38:20 UTC (rev 33523)
+++ grass/trunk/lib/python/grass.py	2008-09-24 18:41:31 UTC (rev 33524)
@@ -127,19 +127,45 @@
 def tempfile():
     return read_command("g.tempfile", pid = os.getpid()).strip()
 
+# key-value parsers
+
+def parse_key_val(s, sep = '=', dflt = None):
+    result = {}
+    for line in s.splitlines():
+	kv = line.split(sep, 1)
+	k = kv[0].strip()
+	if len(kv) > 1:
+	    v = kv[1]
+	else:
+	    v = dflt
+	result[k] = v
+    return result
+
+_kv_regex = None
+
+def parse_key_val2(s):
+    global _kv_regex
+    if _kv_regex == None:
+	_kv_regex = re.compile("([^=]+)='(.*)';?")
+    result = []
+    for line in s.splitlines():
+	m = _kv_regex.match(line)
+	if m != None:
+	    result.append(m.groups())
+	else:
+	    result.append(line.split('=', 1))
+    return dict(result)
+
 # interface to g.gisenv
 
-_kv_regex = re.compile("([^=]+)='(.*)';?")
-
 def gisenv():
-    lines = read_command("g.gisenv").splitlines()
-    return dict([_kv_regex.match(line).groups() for line in lines])
+    return parse_key_val2(read_command("g.gisenv"))
 
 # interface to g.region
 
 def region():
-    lines = read_command("g.region", flags='g').splitlines()
-    return dict([line.split('=',1) for line in lines])
+    s = read_command("g.region", flags='g')
+    return parse_key_val(s)
 
 def use_temp_region():
     name = "tmp.%s.%d" % (os.path.basename(sys.argv[0]), os.getpid())
@@ -157,15 +183,8 @@
 # interface to g.findfile
 
 def find_file(name, element = 'cell', mapset = None):
-    lines = read_command("g.findfile", element = element, file = name, mapset = mapset).splitlines()
-    result = []
-    for line in lines:
-	m = _kv_regex.match(line)
-	if m != None:
-	    result.append(m.groups())
-	else:
-	    result.append(line.split('='))
-    return dict(result)
+    s = read_command("g.findfile", element = element, file = name, mapset = mapset)
+    return parse_key_val2(s)
 
 # interface to g.list
 

Modified: grass/trunk/scripts/v.rast.stats/v.rast.stats.py
===================================================================
--- grass/trunk/scripts/v.rast.stats/v.rast.stats.py	2008-09-24 15:38:20 UTC (rev 33523)
+++ grass/trunk/scripts/v.rast.stats/v.rast.stats.py	2008-09-24 18:41:31 UTC (rev 33524)
@@ -137,12 +137,9 @@
     #get RASTER resolution of map which we want to query:
     #fetch separated to permit for non-square cells (latlong etc)
     s = grass.read_command('r.info', flags = 's', map = raster)
-    d = {}
-    for l in s.splitlines():
-	kv = l.split('=')
-	d[kv[0]] = float(kv[1])
-    nsres = d['nsres']
-    ewres = d['ewres']
+    kv = grass.parse_key_val(s)
+    nsres = float(kv['nsres'])
+    ewres = float(kv['ewres'])
 
     #save current settings:
     grass.use_temp_region()
@@ -171,9 +168,10 @@
     #check if DBF driver used, in this case cut to 10 chars col names:
     s = grass.read_command('v.db.connect', flags = 'g', map = vector, layer = layer)
     # we need this for non-DBF driver:
-    table = s.split()[1]
-    db_database = s.split()[3]
-    db_sqldriver = s.split()[4]
+    f = s.split()
+    table = f[1]
+    db_database = f[3]
+    db_sqldriver = f[4]
     dbfdriver = db_sqldriver == 'dbf'
 
     #Find out which table is linked to the vector map on the given layer
@@ -247,16 +245,7 @@
         #n, min, max, range, mean, stddev, variance, coeff_var, sum
 	# How to test r.univar $? exit status? using -e creates the real possibility of out-of-memory errors
 	s = grass.read_command('r.univar', flags = 'g' + extstat, map = raster, percentile = percentile)
-	vars = {}
-	for l in s.splitlines():
-	    kv = l.split('=')
-	    if len(kv) != 2:
-		continue
-	    var = kv[0]
-	    val = kv[1]
-	    if val.lower() == 'nan':
-		val = 'NULL'
-	    vars[var] = val
+	vars = grass.parse_key_val(s)
 
 	vars['cf_var'] = vars['coeff_var']
 
@@ -266,6 +255,8 @@
 
 	for var in basecols + extracols:
 	    value = vars[var]
+	    if value.lower() == 'nan':
+		value = 'NULL'
 	    colname = '%s_%s' % (colprefix, var)
 	    if dbfdriver:
 		colname = colname[:10]



More information about the grass-commit mailing list