[GRASS-user] Silent commands in python

Hamish hamish_nospam at yahoo.com
Tue Dec 4 04:40:20 EST 2007


Daniel Victoria wrote:
> This might be more of a python problem but here it goes...
> I wrote a water balance script in python to run under GRASS. What I
> would like now is that the commands in the python script would be
> silent, that is, no return from stdout or stderr.
> For instance every time I call a r.mapcalc function I get the 0-100%
> count. I'd like to silence that and other commands.
> 
> In the shell I would silence command outputs using &> /dev/null, for exemple:
> echo "blabla=2*bla" | r.mapcalc &> /dev/null
> 
> But in python that does not work using the os.system() function, that is:
> os.system("echo 'blabla=2*bla' | r.mapcalc &> /dev/null")
> 
> Anyone has any tips on the matter?


If this is purely for cosmetic/usability reasons*, and not for technical ones**
 you've got some options in GRASS 6.3.

*  (hard to see the important messages with all the noise)
** (like tcl's anything to stderr is an error) 


The easiest is to pass the --quiet flag on the module command line. This works
for almost all modules. The big exception to that is r.mapcalc which doesn't
use the standard parser.

For r.mapcalc, or if you want to set a blanket --quiet directive for the whole
script, you can set the GRASS_VERBOSE environmental variable. Python's os
module has a putenv() function to do that. 

These are the levels you can use:
  (from lib/gis/verbose.c)
0 - module should print nothing but errors and warnings (G_fatal_error,
      G_warning)  Triggered by "--q" or "--quiet".
1 - module will print progress information (G_percent)
2 - module will print all messages (G_message)  [default level]
3 - module will be very verbose. Triggered by "--v" or "--verbose".

"--quiet" will set the level to 0.


shell script example
# for everything that follows
GRASS_VERBOSE=1
export GRASS_VEBOSE

# override for just one module
GRASS_VERBOSE=0 \
  r.mapcalc "only_set = for_this_process"


Another nice thing about using the enviro variable method is that it is
silently ignored for earlier versions of GRASS. For earlier versions of GRASS
an error happens when "--quiet" isn't recognized as a valid command line
option. (the new 6.2.3 knows to ignore it)


see "Verbosity levels" at 
  http://grass.gdf-hannover.de/wiki/Development_Specs


The advantage of these methods versus sending all stderr to /dev/null is that
warning and error messages are not lost, making prototyping and debugging a
whole lot easier.


Hamish



      ____________________________________________________________________________________
Looking for last minute shopping deals?  
Find them fast with Yahoo! Search.  http://tools.search.yahoo.com/newsearch/category.php?category=shopping


More information about the grass-user mailing list