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.

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <janpeterr@freenet.de>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.031
X-Spam-Evidence '*H*': 0.94; '*S*': 0.00; 'python.': 0.02; 'elif': 0.05; 'output': 0.05; 'debugging': 0.07; 'function,': 0.09; 'logic': 0.09; 'statements': 0.09; 'troubleshoot': 0.09; 'jan': 0.12; 'thread': 0.14; '"getting': 0.16; "'-'": 0.16; 'brilliant': 0.16; 'evaluations': 0.16; 'hint': 0.16; 'minutes..': 0.16; 'optional': 0.16; 'year)': 0.16; 'ignore': 0.16; 'language': 0.16; 'wrote:': 0.18; 'seems': 0.21; 'example': 0.22; '(in': 0.22; 'handles': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'char': 0.24; 'month,': 0.24; 'connected': 0.24; 'query': 0.26; 'code:': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'generally': 0.29; 'evaluation': 0.30; 'code': 0.31; 'comments': 0.31; 'comparison': 0.31; 'loads': 0.31; 'option': 0.32; 'minimal': 0.33; 'skip:d 20': 0.34; 'problem': 0.35; 'something': 0.35; 'test': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'done': 0.36; 'possible': 0.36; 'should': 0.36; 'searching': 0.37; 'wrong': 0.37; 'expected': 0.38; 'to:addr :python-list': 0.38; 'issue': 0.38; 'short': 0.38; 'anything': 0.39; '(from': 0.39; 'flow': 0.39; 'itself': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'even': 0.60; 'read': 0.60; 'catch': 0.60; 'break': 0.61; 'mentioned': 0.61; 'took': 0.61; 'save': 0.62; 'name': 0.63; 'costs': 0.63; 'myself': 0.63; 'finally': 0.65; 'direct': 0.67; 'it!': 0.67; 'header:Reply-To:1': 0.67; 'results': 0.69; 'targeted': 0.69; 'user,': 0.69; 'reply- to:no real name:2**0': 0.71; '8bit%:100': 0.72; 'yourself': 0.78; '...and': 0.84; 'fails,': 0.84; 'subject:gets': 0.84; 'valid,': 0.84; 'received:89': 0.85; 'mistakes': 0.93; 'technique': 0.93
Date Wed, 19 Jun 2013 01:05:34 +0300
From Jan Riechers <janpeterr@freenet.de>
User-Agent Mozilla/5.0 (Windows NT 6.1; rv:17.0) Gecko/20130509 Thunderbird/17.0.6
MIME-Version 1.0
To python-list@python.org
Subject Re: A certainl part of an if() structure never gets executed.
References <2bc90d3b-09c2-4315-9357-ff7f039465e0@googlegroups.com> <pf2gr8ho0sp2v1o1ugknrhuit93nc6brar@4ax.com> <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>
In-Reply-To <kpcts5$21co$6@news.ntua.gr>
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 8bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
Reply-To janpeterr@freenet.de
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3561.1371594689.3114.python-list@python.org> (permalink)
Lines 67
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1371594689 news.xs4all.nl 15900 [2001:888:2000:d::a6]:41971
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:48672

Show key headers only | 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