[GRASS-dev] formatting expressions for r.mapcalc in GRASS6.4.3

Glynn Clements glynn at gclements.plus.com
Fri Feb 15 05:29:29 PST 2013


Helena Mitasova wrote:

> > The wxGUI command line has its own rules (see shlex.split()
> > in the Python library documentation).
> 
> running mapcalc through the wxGUI command line is where the students have problems in winGRASS 
> (but it works on Mac)
> 
> Helmut - can you please try this commands with data in nc_spm_08 
> just pasting them in command console in wingrass?
> - I had them originally in double quotes but that did not work for any expression,
> without quotes this works:
> 
> g.region rast=landuse96_28m -p
> r.mapcalc ndvi3=float(lsat7_2002_40-lsat7_2002_30)/float(lsat7_2002_40+lsat7_2002_30)
> 
> but the students complain that the ones with if statements don't
> (they say that they tried to put spaces around = and some other modifications but they keep getting syntax error):

Is this on Windows?

gui/wxpython/core/utils.py has:

	def split(s):
	    """!Platform spefic shlex.split"""
	    if sys.version_info >= (2, 6):
	        return shlex.split(s, posix = (sys.platform != "win32"))
	    elif sys.platform == "win32":
	        return shlex.split(s.replace('\\', r'\\'))
	    else:
	        return shlex.split(s)

However: shlex.split(..., posix=False) will retain any quotes, while
posix=True removes them:

	> shlex.split('a "b c"')
	['a', 'b c']
	> shlex.split('a "b c"', posix=False)
	['a', '"b c"']

With posix=False, there doesn't appear any way to prevent splitting on
whitespace without also adding quotes.

>From the above code, it appears that posix=False is used on Windows so
that backslash characters in filenames are treated as literal
backslashes rather than as escape characters. If that's the only
reason, then removing the "if" case may suffice.

-- 
Glynn Clements <glynn at gclements.plus.com>


More information about the grass-dev mailing list