[GRASS-SVN] r39902 - grass/trunk/lib/python
svn_grass at osgeo.org
svn_grass at osgeo.org
Sun Dec 6 03:19:38 EST 2009
Author: martinl
Date: 2009-12-06 03:19:38 -0500 (Sun, 06 Dec 2009)
New Revision: 39902
Modified:
grass/trunk/lib/python/vector.py
Log:
libpy: vector_columns() - get dictionary / list
Modified: grass/trunk/lib/python/vector.py
===================================================================
--- grass/trunk/lib/python/vector.py 2009-12-06 08:17:58 UTC (rev 39901)
+++ grass/trunk/lib/python/vector.py 2009-12-06 08:19:38 UTC (rev 39902)
@@ -88,22 +88,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 (None for all layers)
- @param args
+ @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 parsed output
+ @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