A certainl part of an if() structure never gets executed.

Jan Riechers janpeterr at freenet.de
Tue Jun 18 18:05:34 EDT 2013


On 13.06.2013 20:00, Νικόλαος Κούρας wrote:
>          if '-' not in name + month + year:
>              cur.execute( '''SELECT * FROM works WHERE clientsID =
> (SELECT id FROM clients WHERE name = %s) and MONTH(lastvisit) = %s and
> YEAR(lastvisit) = %s ORDER BY lastvisit ASC''', (name, month, year) )
>          elif '-' not in name + year:
>              cur.execute( '''SELECT * FROM works WHERE clientsID =
> (SELECT id FROM clients WHERE name = %s) and YEAR(lastvisit) = %s ORDER
> BY lastvisit ASC''', (name, year) )
>          elif '-' not in month + year:
>              cur.execute( '''SELECT * FROM works WHERE MONTH(lastvisit)
> = %s and YEAR(lastvisit) = %s ORDER BY lastvisit ASC''', (month, year) )
>          elif '-' not in year:
>              cur.execute( '''SELECT * FROM works WHERE YEAR(lastvisit) =
> %s ORDER BY lastvisit ASC''', year )
>
>
> This finally worked!


I spared myself to read the whole thread cause its super long and I find 
it admirable that some people took time to troubleshoot the issue and 
(even) added optimizations of what can be done differently/better.. even 
so it seems there was only a wrong char used in the ifs?..

And as a short note - you should add code which handles the worst-case 
scenario (everthing fails, nothing valid, avoid into code which might 
require variables/data which is not present and so forth..)


But generally speaking:
As a best practice in any language not just Python.
If you have the option to print out evaluations and values, use it!


As rule of thumb for debugging code:
1. Print out what values you get in (from user, in(to) a function)
2a. Test statements and logic either by setting a print (in your case 
inside each if) with something like "if1", "if2", ... "else fired" or 
what you prefer OR do a direct comparison if the evaluation statements 
are not super long (someone mentioned that..)
2b. In case of longer statements/calculations, break them into chunks, 
simplify the problem to smaller ones and try to verify the results as 
possible (being "far apart" and "getting closer" after each time is 
already a good hint in calculations..)
3. If you catch data from a database and you see now results -> print 
out if your search/query even can return anything at all or if your 
query itself has a flaw or similar.

Optional but also useful:
4. On the end of a function, print if the output of the function is as 
expected

This small technique costs minimal time, but brings a brilliant ability:
1. Understand the code flow
2. Understand mistakes
3. Save yourself a big time by searching targeted for code parts which 
are executed..

..and ignore not directly connected code - for example when looking at 
other peoples code without documentation, comments or introduction.
This can spare loads of headaches.

And all this can be done in a couple of minutes..

Jan




More information about the Python-list mailing list