[GRASS-dev] Copying Landsat MTL file(s) inside the cell_misc directory -- Python scripting

Pietro peter.zamb at gmail.com
Tue May 14 06:43:55 PDT 2013


Hi Nikos,

On Tue, May 14, 2013 at 1:25 PM, Nikos Alexandris
<nik at nikosalexandris.net> wrote:
> I know the following is a pure python question -- still, can someone shed some
> light on why is there such a difference between
>
>     if 'DATE_ACQUIRED' or 'ACQUISITION_DATE' in line:
>
> and
>
>     if 'DATE_ACQUIRED' in line or 'ACQUISITION_DATE' in line:
>
> ?

Your error is due to the precedence of the operator, what it is really
written in the first row is:

if ( ('DATE_ACQUIRED') or ('ACQUISITION_DATE' in line) ):

Therefore the first statement 'DATE_ACQUIRED' is always True because
is a string that it is not empty.
Let me define a function like:

>>> def istrue(obj):
...     if obj:
...         return True
...     return False
...

Now If you try with an empty string you have:

>>> istrue('')
False

If you try with a string that contains something you get:

>>> istrue('DATE_ACQUIRED')
True

Therefore the second is correct because you check if the first string
is contained or the second string is contained in the line.

An easy way to debug this kind of problems is to use python debugger
[1] (possibly the ipython dubugger! [2]) in the line before, like:

import ipdb; ipdb.set_trace()  # or import pdb; pdb.set_trace()
# set_trace will open a terminal to debug your program from this point...
if 'DATE_ACQUIRED' in line or 'ACQUISITION_DATE' in line:
[etc]


Have fun with python! :-)

All the best.

Pietro


[0] http://www.isthe.com/chongo/tech/comp/c/c-precedence.html
[1] http://docs.python.org/2/library/pdb.html
[2] https://pypi.python.org/pypi/ipdb


More information about the grass-dev mailing list