Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #48672

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

Date 2013-06-19 01:05 +0300
From Jan Riechers <janpeterr@freenet.de>
Subject Re: A certainl part of an if() structure never gets executed.
References (2 earlier) <kpa155$qhk$1@reader1.panix.com> <b1rgf6Fo4b0U1@mid.individual.net> <mailman.3181.1371124494.3114.python-list@python.org> <b1tshpF9297U1@mid.individual.net> <kpcts5$21co$6@news.ntua.gr>
Newsgroups comp.lang.python
Message-ID <mailman.3561.1371594689.3114.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Re: A certainl part of an if() structure never gets executed. Zero Piraeus <schesis@gmail.com> - 2013-06-12 11:20 -0400
  Re: A certainl part of an if() structure never gets executed. rusi <rustompmody@gmail.com> - 2013-06-13 05:30 -0700
    Re: A certainl part of an if() structure never gets executed. Roy Smith <roy@panix.com> - 2013-06-13 09:01 -0400
  Re: A certainl part of an if() structure never gets executed. Neil Cerutti <neilc@norwich.edu> - 2013-06-13 12:34 +0000
    Re: A certainl part of an if() structure never gets executed. Νικόλαος Κούρας <support@superhost.gr> - 2013-06-13 20:00 +0300
      Re: A certainl part of an if() structure never gets executed. Jan Riechers <janpeterr@freenet.de> - 2013-06-19 01:05 +0300

csiph-web