[GRASS-dev] grass.info versus print
Vaclav Petras
wenzeslaus at gmail.com
Thu Apr 14 05:27:41 PDT 2016
On Thu, Apr 14, 2016 at 7:33 AM, Paulo van Breugel <p.vanbreugel at gmail.com>
wrote:
> On Thu, Apr 14, 2016 at 1:04 PM, Glynn Clements <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().
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.
>
> In my script (r.vif), both approaches have an issue with how output is
> displayed. Using 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()
> 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).
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/1fe1516f/attachment.html>
More information about the grass-dev
mailing list