[GRASS-dev] question about arguments to methods in GRASS Python library

Glynn Clements glynn at gclements.plus.com
Thu Jun 30 20:14:10 EDT 2011

Michael Barton wrote:

> When I call the following method, I get an error with the keyword
> "from". I suspect that it is because "from" is a reserved Python
> keyword. How do I specify it so that it work as a GRASS command
> keyword?
> grass.parse_command('v.distance', to = 'bugsites', from = 'archsites', column = 'temp', upload = 'dist', dmax = '1000', flags = 'pa')

Add a leading underscore:

	grass.parse_command('v.distance', to='bugsites', _from='archsites', ...)

grass.make_command (which deals with generating the argv list from a
"**kwargs" for all of the *_command functions) automatically strips
trailing underscores from the actual command-line arguments.

This provides a generalised workaroud for module arguments which are
Python keywords or which are meaningful to any of the *_command
functions (e.g. stdin/stdout/stderr, quiet/verbose/overwrite, env,
cwd, etc).

For Python keywords, another workaround is to pass a **kwargs
dictionary, e.g.:

	grass.parse_command('v.distance', to='bugsites', **{'from': 'archsites'})

But that won't work with arguments which are recognised by the
*_command functions (which includes all of the arguments which are
recognised by the Popen constructor).

Glynn Clements <glynn at gclements.plus.com>

More information about the grass-dev mailing list