[GRASS-dev] Python equivalent of piping results from one function to another

What would be the python equivalent of:

r.category mymap | r.category ```mymap rules=-`

I have been looking at the pygrass interface, but I didn’t understand things well enough to see how to proceed.

Any pointers would be appreciated

Cheers,

Paulo

Dear Paulo,

On Sun, Oct 18, 2015 at 8:03 PM, Paulo van Breugel
<p.vanbreugel@gmail.com> wrote:

What would be the python equivalent of:

r.category mymap | r.category mymap rules=-

Let's split the problem in two step.

First capture the output of a module (the python equivalent of bash pipe):

{{{

from subprocess import PIPE # from the standard library import PIPE costant
from grass.pygrass.modules import Module # import the Module class from pygrass
rcat = Module('r.category', 'landuse', stdout_=PIPE) # use the spacial parameter stdout_
rcat.outputs.stdout # the stdout is an attribute of the instanced class

'1\tdeveloped\n2\tagriculture\n3\therbaceous\n4\tshrubland\n5\tforest\n6\twater\n7\tsediment\n'

print(rcat.outputs.stdout)

1 developed
2 agriculture
3 herbaceous
4 shrubland
5 forest
6 water
7 sediment
}}}

Second provide a string as stdinput of a module:

{{{

Module('r.category', 'mymap', rules='-', stdin_='1\tdeveloped\n2\tagriculture\n3\therbaceous\n4\tshrubland\n5\tforest\n6\twater\n7\tsediment\n')

Module('r.category')
}}}

or to do both things in one line:

{{{
Module('r.category', 'mymap', rules='-', stdin_=Module('r.category',
'landuse', stdout_=PIPE).outputs.stdout)
}}}

You can use a shourtcut to have a cleaner syntax close to bash:

{{{

from grass.pygrass.modules.shortcuts import raster as r
r.category('mymap', rules='-', stdin_=r.category('landuse', stdout_=PIPE).outputs.stdout)

Module('r.category')
}}}

There is an example in the documentation using r.colors but it is not
rendered corectly [0]

Have fun!

Pietro

[0] https://grass.osgeo.org/grass71/manuals/libpython/pygrass.modules.interface.html#pygrass.modules.interface.module.Module

On 19-10-15 07:12, Pietro wrote:

Dear Paulo,

On Sun, Oct 18, 2015 at 8:03 PM, Paulo van Breugel
<p.vanbreugel@gmail.com> wrote:

What would be the python equivalent of:

r.category mymap | r.category mymap rules=-

Let's split the problem in two step.

First capture the output of a module (the python equivalent of bash pipe):

{{{

from subprocess import PIPE # from the standard library import PIPE costant
from grass.pygrass.modules import Module # import the Module class from pygrass
rcat = Module('r.category', 'landuse', stdout_=PIPE) # use the spacial parameter stdout_
rcat.outputs.stdout # the stdout is an attribute of the instanced class

'1\tdeveloped\n2\tagriculture\n3\therbaceous\n4\tshrubland\n5\tforest\n6\twater\n7\tsediment\n'

print(rcat.outputs.stdout)

1 developed
2 agriculture
3 herbaceous
4 shrubland
5 forest
6 water
7 sediment
}}}

Second provide a string as stdinput of a module:

{{{

Module('r.category', 'mymap', rules='-', stdin_='1\tdeveloped\n2\tagriculture\n3\therbaceous\n4\tshrubland\n5\tforest\n6\twater\n7\tsediment\n')

Module('r.category')
}}}

or to do both things in one line:

{{{
Module('r.category', 'mymap', rules='-', stdin_=Module('r.category',
'landuse', stdout_=PIPE).outputs.stdout)
}}}

You can use a shourtcut to have a cleaner syntax close to bash:

{{{

from grass.pygrass.modules.shortcuts import raster as r
r.category('mymap', rules='-', stdin_=r.category('landuse', stdout_=PIPE).outputs.stdout)

Module('r.category')
}}}

Dear Pietro, this is brilliant, thanks a lot!

There is an example in the documentation using r.colors but it is not
rendered corectly [0]

I tried out the examples on that page, but I couldn't get it all to work. For example, there are some examples that use of "stdout_=PIPE" and stdin_=PIPE. I though that might be what I was looking for, but after trying (and failing) to run the examples I basically do not understand what is done there. The solution you describe above is actually more easy to intuitively understand. It would be a great example to include on the manual page.

About the manual pages, although very detailed, they are sometimes a bit hard to understand (perhaps mostly for non-programmers like me), and could benefit from worked out examples like you provide above. I would not mind providing input, but lack a proper understanding of the code. On the other hand, I have had feedback with great examples / explanations for two or three times now which I though would be great to include in the manual. What is the best place for such feedback / suggestions?

Have fun!

Pietro

[0] https://grass.osgeo.org/grass71/manuals/libpython/pygrass.modules.interface.html#pygrass.modules.interface.module.Module

I had seen the example, but to be honest even after rereading it isn't all clear to me