[GRASS-dev] grass.info versus print
Paulo van Breugel
p.vanbreugel at gmail.com
Thu Apr 14 06:55:16 PDT 2016
On 14-04-16 14:27, Vaclav Petras wrote:
>
> On Thu, Apr 14, 2016 at 7:33 AM, Paulo van Breugel
> <p.vanbreugel at gmail.com <mailto:p.vanbreugel at gmail.com>> wrote:
>
> On Thu, Apr 14, 2016 at 1:04 PM, Glynn Clements
> <glynn at gclements.plus.com <mailto:glynn at gclements.plus.com>> wrote:
>
>
> In general, scripts should use the g.message wrappers rather than
> Python's "print" statement or os.write(), as that will honour any
> configuration options (GRASS_VERBOSE, GRASS_MESSAGE_FORMAT,
> --quiet/--verbose, etc).
>
>
> Thanks Glynn. It seems there are different opinions, see earlier
> comment from Vaclav.
>
>
> Perhaps I was not completely clear before. I agree with Glynn.
> grass.script.message() etc. for messages. print()/sys.stdout for text
> output (e.g. tables).
>
> Also in the examples on the Wiki (e.g., on
> https://grasswiki.osgeo.org/wiki/GRASS_and_Python) and various
> addons I have looked at, I can see both being used.
>
>
> I would have to go through the individual examples but sometimes it
> might be be clear what is a message to inform user about something and
> what is a text output of the module. A rule of thumb can be that
> modules which output maps, (usually) don't do text output, so they
> shouldn't use print().
In this case (r.vif) the output is not a map, but a list of values,
i.e., text output (see below). Based on the guidelines, I would assume I
should use print()?
All variables
----------------------------------------
variable vif sqrtvif
2000_01_precip at climate_1970_2012 2.30 1.52
2000_03_tempmean at climate_1970_2012 24.95 5.00
2000_11_precip at climate_1970_2012 3.54 1.88
2001_02_precip at climate_1970_2012 4.55 2.13
2001_04_tempmean at climate_1970_2012 25.01 5.00
Variable '2001_04_tempmean at climate_1970_2012' removed
----------------------------------------
variable vif sqrtvif
2000_01_precip at climate_1970_2012 2.30 1.52
2000_03_tempmean at climate_1970_2012 2.19 1.48
2000_11_precip at climate_1970_2012 3.48 1.87
2001_02_precip at climate_1970_2012 4.54 2.13
Final selected variables are: 2000_01_precip, 2000_03_tempmean, 2000_11_precip, 2001_02_precip
with as maximum VIF: 5.69787583189
>
> Also for you personal scripts, you are free to use any solution you
> want. That might be what the examples are showing. However, Python
> scripts which are modules, for example in Addons, should adhere to the
> submitting guidelines:
>
> https://trac.osgeo.org/grass/wiki/Submitting/Python#Messages
>
> Perhaps the most important thing is that one should avoid mixing
> the two, which is something that maybe could go somewhere in the wiki?
>
>
> Feel free to extent either wiki or guidelines if you think it is not
> clear enough.
I wouldn't mind doing so, but I probably should understand better myself
what exactly the issue is with using both print() and grass.info() in
one script. Based on what I read in the guidelines, I am not clear
whether using both is against the guidelines as such, but technically it
seems it can cause problems as you explained (and I experienced).
> In my script (r.vif), both approaches have an issue with how output
> is displayed. Using grass.info <http://grass.info>() results in
> columns not being printed with fixed width in the command output,
> while they are in the console. On the other hand, when using print()
> empty lines are not printed. I personally am not too much bothered by
> both, so if you think I should use grass.info <http://grass.info>() I
> am fine with that.
>
> First, always use the above guidelines to decide if print() or one of
> the g.message wrappers is the right solution. Second, look at the
> behavior in the command line (that's the canonical one). The GUI might
> get things wrong (and that might be bug).
OK, that is good to know
>
> Please, let me know what you think about it now,
> Vaclav
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/grass-dev/attachments/20160414/0c2d840f/attachment.html>
More information about the grass-dev
mailing list