[GRASS-dev] how to represent an argument as a variable in a grass python command

I’m trying to do a script that calls g.copy

I want the user to be able to select which type of data to copy. So instead of specifying:

grass.run_command(‘g.copy’, rast=‘%s,%s’ % (input, output))

I’d like to something equivalent to:

grass.run_command(‘g.copy’, ‘%s=%s,%s’ % (datatype, input, output))

where datatype is the form of grass data to copy (eg, rast, vect, etc)

The way I’d like to do this doesn’t work. So what is the correct syntax here?

Thanks
Michael


C. Michael Barton
Director, Center for Social Dynamics & Complexity
Professor of Anthropology, School of Human Evolution & Social Change
Arizona State University

voice: 480-965-6262 (SHESC), 480-727-9746 (CSDC)
fax: 480-965-7671 (SHESC), 480-727-0709 (CSDC)

www: http://www.public.asu.edu/~cmbarton, http://csdc.asu.edu

On Sun, Sep 2, 2012 at 11:04 PM, Michael Barton <Michael.Barton@asu.edu> wrote:

I'm trying to do a script that calls g.copy

I want the user to be able to select which type of data to copy. So instead
of specifying:

grass.run_command('g.copy', rast='%s,%s' % (input, output))

I'd like to something equivalent to:

grass.run_command('g.copy', '%s=%s,%s' % (datatype, input, output))

where datatype is the form of grass data to copy (eg, rast, vect, etc)

The way I'd like to do this doesn't work. So what is the correct syntax
here?

Create a dictionary first:

dataType = 'rast'
params = {dataType: '%s,%s' % (input, output)}
grass.run_command('g.copy', **params)

Maybe someone comes up with something better but this works.

Anna

Thanks
Michael

____________________
C. Michael Barton
Director, Center for Social Dynamics & Complexity
Professor of Anthropology, School of Human Evolution & Social Change
Arizona State University

voice: 480-965-6262 (SHESC), 480-727-9746 (CSDC)
fax: 480-965-7671 (SHESC), 480-727-0709 (CSDC)
www: http://www.public.asu.edu/~cmbarton, http://csdc.asu.edu

_______________________________________________
grass-dev mailing list
grass-dev@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-dev

Thanks much!!!

Michael
____________________
C. Michael Barton
Director, Center for Social Dynamics & Complexity
Professor of Anthropology, School of Human Evolution & Social Change
Arizona State University

voice: 480-965-6262 (SHESC), 480-727-9746 (CSDC)
fax: 480-965-7671 (SHESC), 480-727-0709 (CSDC)
www: http://www.public.asu.edu/~cmbarton, http://csdc.asu.edu

On Sep 2, 2012, at 2:22 PM, Anna Kratochvílová <kratochanna@gmail.com>
wrote:

On Sun, Sep 2, 2012 at 11:04 PM, Michael Barton <Michael.Barton@asu.edu> wrote:

I'm trying to do a script that calls g.copy

I want the user to be able to select which type of data to copy. So instead
of specifying:

grass.run_command('g.copy', rast='%s,%s' % (input, output))

I'd like to something equivalent to:

grass.run_command('g.copy', '%s=%s,%s' % (datatype, input, output))

where datatype is the form of grass data to copy (eg, rast, vect, etc)

The way I'd like to do this doesn't work. So what is the correct syntax
here?

Create a dictionary first:

dataType = 'rast'
params = {dataType: '%s,%s' % (input, output)}
grass.run_command('g.copy', **params)

Maybe someone comes up with something better but this works.

Anna

Thanks
Michael

____________________
C. Michael Barton
Director, Center for Social Dynamics & Complexity
Professor of Anthropology, School of Human Evolution & Social Change
Arizona State University

voice: 480-965-6262 (SHESC), 480-727-9746 (CSDC)
fax: 480-965-7671 (SHESC), 480-727-0709 (CSDC)
www: http://www.public.asu.edu/~cmbarton, http://csdc.asu.edu

_______________________________________________
grass-dev mailing list
grass-dev@lists.osgeo.org
grass-dev Info Page

Michael Barton wrote:

I'm trying to do a script that calls g.copy

I want the user to be able to select which type of data to copy. So instead of specifying:

grass.run_command('g.copy', rast='%s,%s' % (input, output))

Note that it isn't necessary to format the argument yourself;
run_command will do this for you, so you just need;

  grass.run_command('g.copy', rast = (input, output))

I'd like to something equivalent to:

grass.run_command('g.copy', '%s=%s,%s' % (datatype, input, output))

where datatype is the form of grass data to copy (eg, rast, vect, etc)

The way I'd like to do this doesn't work. So what is the correct syntax here?

  grass.run_command('g.copy', **{datatype: (input, output)})

--
Glynn Clements <glynn@gclements.plus.com>