[GRASS-dev] How to improve flags related error message in pygrass module?

Hi devs,

when accidentially using a wrong flag, the resulting error message is
"unhelpful":

user@fry ~ $ python grass_scripts/grass_dem.py
GRASSBIN: grass74
GISBASE: /home/user/source/grass-7.4.svn/dist.x86_64-pc-linux-gnu
{u'MAPSET': u"'PERMANENT';", u'GISDBASE': u"'/home/user/grassdata';",
u'LOCATION_NAME': u"'nrw';"}
Traceback (most recent call last):
  File "grass_scripts/grass_dem.py", line 17, in <module>

r.in_gdal(input="/home/user/geodaten/ASTGTM2_N50E007_dem.tif",output="dem_nrw",flags="e",overwrite="OVR")
  File
"/home/user/source/grass-7.4.svn/dist.x86_64-pc-linux-gnu/etc/python/grass/pygrass/modules/interface/module.py",
line 647, in __call__
    self.check()
  File
"/home/user/source/grass-7.4.svn/dist.x86_64-pc-linux-gnu/etc/python/grass/pygrass/modules/interface/module.py",
line 705, in check
    if flg and flg.suppress_required:
TypeError: __nonzero__ should return bool or int, returned str

This script was written by a PyGRASS newcomer who reused code from the
Wiki. The reason for the error is
overwrite="OVR"

which is incorrect (must be: overwrite=True or =False).

The related code is around l705 in
lib/python/pygrass/modules/interface/module.py

    def check(self):
        """Check the correctness of the provide parameters"""
        required = True
        for flg in self.flags.values():
            if flg and flg.suppress_required:
                required = False

Could a python expert here please suggest how to generate a more
reasonable error message?

thanks
Markus

* Markus Neteler <neteler@osgeo.org> [2018-01-15 11:12:46 +0100]:

Hi devs,

when accidentially using a wrong flag, the resulting error message is
"unhelpful":

user@fry ~ $ python grass_scripts/grass_dem.py
GRASSBIN: grass74
GISBASE: /home/user/source/grass-7.4.svn/dist.x86_64-pc-linux-gnu
{u'MAPSET': u"'PERMANENT';", u'GISDBASE': u"'/home/user/grassdata';",
u'LOCATION_NAME': u"'nrw';"}
Traceback (most recent call last):
File "grass_scripts/grass_dem.py", line 17, in <module>

r.in_gdal(input="/home/user/geodaten/ASTGTM2_N50E007_dem.tif",output="dem_nrw",flags="e",overwrite="OVR")
File
"/home/user/source/grass-7.4.svn/dist.x86_64-pc-linux-gnu/etc/python/grass/pygrass/modules/interface/module.py",
line 647, in __call__
   self.check()
File
"/home/user/source/grass-7.4.svn/dist.x86_64-pc-linux-gnu/etc/python/grass/pygrass/modules/interface/module.py",
line 705, in check
   if flg and flg.suppress_required:
TypeError: __nonzero__ should return bool or int, returned str

This script was written by a PyGRASS newcomer who reused code from the
Wiki. The reason for the error is
overwrite="OVR"

which is incorrect (must be: overwrite=True or =False).

Even like that, the error message is informative. It expects a boolean
(True or False) or an integer (say: 0 or 1). Instead a string was returned.

Nikos

The related code is around l705 in
lib/python/pygrass/modules/interface/module.py

   def check(self):
       """Check the correctness of the provide parameters"""
       required = True
       for flg in self.flags.values():
           if flg and flg.suppress_required:
               required = False

Could a python expert here please suggest how to generate a more
reasonable error message?

thanks
Markus
_______________________________________________
grass-dev mailing list
grass-dev@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-dev

--
Nikos Alexandris | Remote Sensing & Geomatics
GPG Key Fingerprint 6F9D4506F3CA28380974D31A9053534B693C4FB3

On Mon, Jan 15, 2018 at 1:38 PM, Nikos Alexandris
<nik@nikosalexandris.net> wrote:

* Markus Neteler <neteler@osgeo.org> [2018-01-15 11:12:46 +0100]:

r.in_gdal(input="/home/user/geodaten/ASTGTM2_N50E007_dem.tif",output="dem_nrw",flags="e",overwrite="OVR")

...

"/home/user/source/grass-7.4.svn/dist.x86_64-pc-linux-gnu/etc/python/grass/pygrass/modules/interface/module.py",
line 705, in check
   if flg and flg.suppress_required:
TypeError: __nonzero__ should return bool or int, returned str

...

Even like that, the error message is informative. It expects a boolean
(True or False) or an integer (say: 0 or 1). Instead a string was returned.

Right. My point is that it does not say _which_ flag is wrong.

My scope it to make it easy/obvious also for newcomers...

Markus

* Markus Neteler <neteler@osgeo.org> [2018-01-15 14:42:29 +0100]:

On Mon, Jan 15, 2018 at 1:38 PM, Nikos Alexandris
<nik@nikosalexandris.net> wrote:

* Markus Neteler <neteler@osgeo.org> [2018-01-15 11:12:46 +0100]:

r.in_gdal(input="/home/user/geodaten/ASTGTM2_N50E007_dem.tif",output="dem_nrw",flags="e",overwrite="OVR")

...

"/home/user/source/grass-7.4.svn/dist.x86_64-pc-linux-gnu/etc/python/grass/pygrass/modules/interface/module.py",
line 705, in check
   if flg and flg.suppress_required:
TypeError: __nonzero__ should return bool or int, returned str

...

Even like that, the error message is informative. It expects a boolean
(True or False) or an integer (say: 0 or 1). Instead a string was returned.

Right. My point is that it does not say _which_ flag is wrong.

My scope it to make it easy/obvious also for newcomers...

Markus

Flags are set/updated during the following piece of code (in `module.py`):

"""
#
# set/update args
#
for param, arg in zip(self.params_list, args):
    param.value = arg
for key, val in kargs.items():
    if key in self.inputs:
        self.inputs[key].value = val
    elif key in self.outputs:
        self.outputs[key].value = val

            elif key in self.flags:

        # we need to add this, because some parameters (overwrite,
        # verbose and quiet) work like parameters
        self.flags[key].value = val
    else:
        raise ParameterError('%s is not a valid parameter.' % key)
"""

and `check()`ing for required parameters, runs after when/if the `run_`
attribute is True.

Maybe adding a test before

self.flags[key].value = val

would help? And report the `flag[key]` in case `val` is not one expected?

Maybe adding a specific test for the overwrite, verbose, quiet parameters, that
exist for every grass module?

Nikos