[GRASS-SVN] r39903 - grass/branches/develbranch_6/lib/python

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Dec 6 03:25:33 EST 2009


Author: martinl
Date: 2009-12-06 03:25:31 -0500 (Sun, 06 Dec 2009)
New Revision: 39903

Modified:
   grass/branches/develbranch_6/lib/python/vector.py
Log:
libpy: vector_columns() - get dictionary / list
       (merge 39902 from trunk)


Modified: grass/branches/develbranch_6/lib/python/vector.py
===================================================================
--- grass/branches/develbranch_6/lib/python/vector.py	2009-12-06 08:19:38 UTC (rev 39902)
+++ grass/branches/develbranch_6/lib/python/vector.py	2009-12-06 08:25:31 UTC (rev 39903)
@@ -81,16 +81,40 @@
 
 # run "v.info -c ..." and parse output
 
-def vector_columns(map, layer = None, **args):
-    """!Return a dictionary of the columns for the database table connected to
-    a vector map (interface to `v.info -c').
+def vector_columns(map, layer = None, getDict = True, **args):
+    """!Return a dictionary (or a list) of the columns for the
+    database table connected to a vector map (interface to `v.info
+    -c').
+    
+    @code
+    >>> vector_columns(urbanarea, getDict = True)
+    {'UA_TYPE': {'index': 4, 'type': 'CHARACTER'}, 'UA': {'index': 2, 'type': 'CHARACTER'}, 'NAME': {'index': 3, 'type': 'CHARACTER'}, 'OBJECTID': {'index': 1, 'type': 'INTEGER'}, 'cat': {'index': 0, 'type': 'INTEGER'}}
+
+    >>> vector_columns(urbanarea, getDict = False)
+    ['cat', 'OBJECTID', 'UA', 'NAME', 'UA_TYPE']
+    @endcode
+    
+    @param map map name
+    @param layer layer number or name (None for all layers)
+    @param getDict True to return dictionary of columns otherwise list of column names is returned
+    @param args (v.info's arguments)
+    
+    @return dictionary/list of columns
     """
     s = read_command('v.info', flags = 'c', map = map, layer = layer, quiet = True, **args)
-    result = {}
+    if getDict:
+        result = dict()
+    else:
+        result = list()
+    i = 0
     for line in s.splitlines():
-	f = line.split('|')
-	if len(f) == 2:
-            result[f[1]] = f[0]
+	ctype, cname = line.split('|')
+        if getDict:
+            result[cname] = { 'type' : ctype,
+                              'index' : i }
+        else:
+            result.append(cname)
+        i+=1
     
     return result
 



More information about the grass-commit mailing list