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

Nikos Alexandris nik at nikosalexandris.net
Tue May 14 13:24:19 PDT 2013


Hi Pietro,

thank you for taking the time to clarify this -- Perfect!
Note to self: use parentheses in complex if statements (and not only!).

Nikos



--%<---
On Tuesday 14 of May 2013 14:43:55 Pietro wrote:
> Hi Nikos,
> 
> On Tue, May 14, 2013 at 1:25 PM, Nikos Alexandris
> 
> z<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