Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > A certainl part of an if() structure never gets executed.

Reply
Thread Tools

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

 
 
Chris Angelico
Guest
Posts: n/a
 
      06-12-2013
On Wed, Jun 12, 2013 at 11:57 AM, alex23 <(E-Mail Removed)> wrote:
> On Jun 12, 11:46 am, Rick Johnson <(E-Mail Removed)>
> wrote:
>> Utilizing the power of interactive sessions, for learning and debugging,
>> should be a cornerstone fundamental of your programming "work-flow" when
>> writing code in an interpreted language like Python.

>
> Unfortunately with Ferrous, the process is more like:
>
> if error:
> post_to_python_list()
> while error:
> if time_elapsed == one_hour:
> bump_post()


Not quite.

while people_paying_me_money():
while not error:
code_per_rfc_2795()
while error:
post_to_python_list()
while sucker_count < 3:
if time_elapsed >= one_hour: # level-triggered, not edge-triggered
bump_post()

You have to include the coding phase. How else would he get into an error state?

ChrisA
 
Reply With Quote
 
 
 
 
alex23
Guest
Posts: n/a
 
      06-12-2013
On Jun 12, 12:05*pm, Chris Angelico <(E-Mail Removed)> wrote:
> You have to include the coding phase. How else would he get into an errorstate?


Via copy & paste.
 
Reply With Quote
 
 
 
 
Rick Johnson
Guest
Posts: n/a
 
      06-12-2013
On Tuesday, June 11, 2013 9:14:38 PM UTC-5, alex23 wrote:
> On Jun 12, 12:05*pm, Chris Angelico <(E-Mail Removed)> wrote:
>
> > You have to include the coding phase.
> > How else would he get into an error state?

>
> Via copy & paste.


Now that's more like it Alex!

If you move to my side the Python world could experience a cataclysmic polar shift. Which may not necessarily be a bad thing, however, I've noticed that these folks stress easily -- hmm, maybe their undergarments are just tootight???
 
Reply With Quote
 
Rick Johnson
Guest
Posts: n/a
 
      06-12-2013
On Tuesday, June 11, 2013 10:37:39 PM UTC-5, Rick Johnson wrote:

> Now that's more like it Alex!


Opps, it seems i falsely interpreted Chris's post as directed towards me, and then with that false assumption in mind i went on to falsely interpreted reply to Chris. Folks if your not already ignoring me this might be a good time to start <|).

At this time i think we should just forget about my mistake and and return to the request for internship in his new "open sore"sss project.
 
Reply With Quote
 
Michael Torrie
Guest
Posts: n/a
 
      06-12-2013
On 06/11/2013 02:20 PM, wrote:
> [code]
> if not re.search( '=', name ) and not re.search( '=', month ) and not re.search( '=', year ):


What do each of these functions return? When you print out
re.search('=', name) what happens?

When you're debugging you should print each of these out. Also make a
standalone program (NOT A CGI SCRIPT) for debugging. You can test
various inputs by simply going:

name = "My test name"
month = "Thirteenth"
year = "2013"

One thing I do is to make my code into functions and then at the bottom
of each script I have something like this:

if __name__ == "__main__":
# run some test code here
# or alternatively do the CGI stuff depending on what
# mode you're in.

You could even put in debugging flags (apologies to Rick who recently
raved against needing to resort to this sort of thing).

Consider this code:
---------- do_something.py -----------------------
debug = False

def do_something(name, month, year):
if debug:
print re.re.search( '=', name )
print not re.search( '=', month )
print not re.search( '=', year )
if not re.search( '=', name ) and not re.search( '=', month ) and
not re.search( '=', year ):
print "gonna do query"
# code follows

if __name__ == "__main__"
debug = True
do_something("bob", None, 2012)
do_something(None, None, None)
do_something("big", "Octember", 2067)
# other use cases here

-------------------------------------------------

Then in your cgi script itself, you just do this:
----------- my_cgi_script.py -------------------
import do_something

# handle cgi stuff
# get name, month year
dosomething.dosomething(name, month, year)
------------------------------------------------
Now to test your code you can just run the module directly with python,
and once you've got it working, the CGI will also work, since it pulls
in the same code.

If this concept is new to you, I strongly urge you to stop development
and read through a good python tutorial or get a good python book.
 
Reply With Quote
 
Michael Torrie
Guest
Posts: n/a
 
      06-12-2013
On 06/11/2013 10:49 PM, Michael Torrie wrote:
> ----------- my_cgi_script.py -------------------
> import do_something
>
> # handle cgi stuff
> # get name, month year
> dosomething.dosomething(name, month, year)


Make that do_something.do_something
 
Reply With Quote
 
alex23
Guest
Posts: n/a
 
      06-12-2013
On Jun 12, 3:44*pm, Tim Roberts <(E-Mail Removed)> wrote:
> It seems silly to fire up a regular expression compiler to look for a
> single character.
> * * if name.find('=') < 0 and month.find('=') < 0 and year.find('=') < 0:


If truthiness is the only concern, I prefer using `in`:

if '=' in name and '=' in month ...
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      06-12-2013
On Wed, Jun 12, 2013 at 5:45 PM, <(E-Mail Removed)> wrote:
> First of all i have changed the code to the following because using a
> regex
> to detect a single char was an overkill.
>
> if '=' not in name and '=' not in month and '=' not in year:


It'd be courteous to acknowledge those who made that suggestion, most
notably alex23 who posted it in almost that exact form.

ChrisA
 
Reply With Quote
 
Denis McMahon
Guest
Posts: n/a
 
      06-12-2013
On Tue, 11 Jun 2013 13:20:52 -0700, Νικόλαος Κούρας wrote:

> The above if structure works correctly *only* if the user sumbits by
> form:
>
> name, month, year or month, year
>
> If, he just enter a year in the form and sumbit then, i get no error,
> but no results displayed back.
>
> Any ideas as to why this might happen?


Yes, I know exactly why this is happening.

It is for one of two reasons. You may determine which one using the
following secret code which I stole from the illuminati in 1836:

import random

reason = { 1: "Input data is not as expected by coder", 2: "Flow control
logic not performing as coder expects", 3: "Badger Badger Badger Badger
Badger Badger Badger Badger Mushroom", 4: "Please try again", 5:
"Snaaaaake", 6: "Grumpy cat says fix your own damn code", 7: "I'll be
back" }

print reason[ random.choice( reason.keys() ) ]

Note - this only has a small chance of actually doing what you want (ie
giving a possibly accurate answer), but it sounds as if that's a level of
precision you're used to working with anyway.

On a slightly more serious note, if you can't apply yourself to debugging
a case of "the program logic isn't doing what I expect" for some value of
program logic that you coded, that may be a hint that:

a) you don't actually understand what the program logic is doing

b) you shouldn't be writing logic so complex that you can't see how to
debug it

c) your logic is overly convoluted and complex

d) all of the above

So perhaps you need to scrub the logic altogether, and start again taking
smaller steps.

You could also perhaps do with a lesson in De Morgan's theorem:

not a and not b and not c = not ( a or b or c )

not a or not b or not c = not ( a and b and c )

and sometimes the alternative form is easier to understand

Now, looking at your code here are two important questions:

(1) What is the initial values of name, month and year?
(2) Which block is actually being executed?

Bear in mind that if a branch other than one you expect to be executed is
executing, the fail condition might not be what you think it is.
Specifically, is it possible that the code is executing the wrong branch
and tripping up when it tries to generate or perhaps execute the sql
statement empty strings, or just not getting any result from the sql
because of the empty strings?

Hint - take the code from the current file, apply a liberal smattering of
print statements, and test it for various values of month, year and name.

def test(name, month, year):
print "name, month, year ::", name, month, year
if not re.search( '=', name ) and not re.search( '=', month ) and
not re.search( '=', year ):
print "branch 1"
elif not re.search( '=', month ) and not re.search( '=', year ):
print "branch 2"
elif not re.search( '=', year ):
print "branch 3"
else:
print "branch 4"

# testing logic for 8 possible input conditions

test("=", "=", "=")
test("=", "=", "x")
test("=", "x", "=")
test("=", "x", "x")
test("x", "=", "=")
test("x", "=", "x")
test("x", "x", "=")
test("x", "x", "x")

--
Denis McMahon, http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Νικόλαος Κούρας
Guest
Posts: n/a
 
      06-12-2013
On 12/6/2013 11:27 πμ, Denis McMahon wrote:
> On Tue, 11 Jun 2013 13:20:52 -0700, Νικόλαος Κούρας wrote:
>
>> The above if structure works correctly *only* if the user sumbits by
>> form:
>>
>> name, month, year or month, year
>>
>> If, he just enter a year in the form and sumbit then, i get no error,
>> but no results displayed back.
>>
>> Any ideas as to why this might happen?

>
> Yes, I know exactly why this is happening.
>
> It is for one of two reasons. You may determine which one using the
> following secret code which I stole from the illuminati in 1836:
>
> import random
>
> reason = { 1: "Input data is not as expected by coder", 2: "Flow control
> logic not performing as coder expects", 3: "Badger Badger Badger Badger
> Badger Badger Badger Badger Mushroom", 4: "Please try again", 5:
> "Snaaaaake", 6: "Grumpy cat says fix your own damn code", 7: "I'll be
> back" }
>
> print reason[ random.choice( reason.keys() ) ]
>
> Note - this only has a small chance of actually doing what you want (ie
> giving a possibly accurate answer), but it sounds as if that's a level of
> precision you're used to working with anyway.
>
> On a slightly more serious note, if you can't apply yourself to debugging
> a case of "the program logic isn't doing what I expect" for some value of
> program logic that you coded, that may be a hint that:
>
> a) you don't actually understand what the program logic is doing
>
> b) you shouldn't be writing logic so complex that you can't see how to
> debug it
>
> c) your logic is overly convoluted and complex
>
> d) all of the above
>
> So perhaps you need to scrub the logic altogether, and start again taking
> smaller steps.
>
> You could also perhaps do with a lesson in De Morgan's theorem:
>
> not a and not b and not c = not ( a or b or c )
>
> not a or not b or not c = not ( a and b and c )
>
> and sometimes the alternative form is easier to understand
>
> Now, looking at your code here are two important questions:
>
> (1) What is the initial values of name, month and year?
> (2) Which block is actually being executed?
>
> Bear in mind that if a branch other than one you expect to be executed is
> executing, the fail condition might not be what you think it is.
> Specifically, is it possible that the code is executing the wrong branch
> and tripping up when it tries to generate or perhaps execute the sql
> statement empty strings, or just not getting any result from the sql
> because of the empty strings?
>
> Hint - take the code from the current file, apply a liberal smattering of
> print statements, and test it for various values of month, year and name.
>
> def test(name, month, year):
> print "name, month, year ::", name, month, year
> if not re.search( '=', name ) and not re.search( '=', month ) and
> not re.search( '=', year ):
> print "branch 1"
> elif not re.search( '=', month ) and not re.search( '=', year ):
> print "branch 2"
> elif not re.search( '=', year ):
> print "branch 3"
> else:
> print "branch 4"
>
> # testing logic for 8 possible input conditions
>
> test("=", "=", "=")
> test("=", "=", "x")
> test("=", "x", "=")
> test("=", "x", "x")
> test("x", "=", "=")
> test("x", "=", "x")
> test("x", "x", "=")
> test("x", "x", "x")
>


Thank you but i already foudn out what the problem was, i just don't
known how to fix it. Here is is again:


Here is the defines of those variables. as you can see are all tuples

# populate names, months, years
names.add( '==========' )
months = ( '==========', 'Ιανουάριος', 'Φεβρουάριος', 'Μάρτιος',
'Απρίλιος', 'Μάϊος', 'Ιούνιος',
'Ιούλιος', 'Αύγουστος', 'Σεπτ*μβριος', 'Οκτώβριος',
'Νο*μβριος',
'Δεκ*μβριος' )
years = ( '==========', 2010, 2011, 2012, 2013 )


========================

i used print( name, month, year ) and noticed that all values
returned as expected when selected fro drop-down menus and submitted.

But when it comes to select '==========' from month instead of
'==========' to be submitted a zero gets submitted and i think the
problem is the way i'm filling up months into the drop down menu which is:


for i, month in enumerate(months):
print('<option value="%s"> %s </option>' % (i, month) )


the if case does not execute because of the way it checks for None entry
which is: elif '=' not in year:

but if enumerate yields 0 instead of '==========' then elif '=' not in
year of course fails.

So, i must tell:

for i, month in enumerate(months):
print('<option value="%s"> %s </option>' % (i, month) )

to somehow return '==========' instead of 0 but don't know how.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
semple code accept never executed fakessh C Programming 11 09-15-2012 05:46 AM
Using callback, I fill dorpdownlist without postback but it gets empty when a postback is executed Fabio Mastria ASP .Net 5 01-30-2008 09:07 PM
server event never/always executed phil ASP .Net Web Controls 1 06-06-2006 12:00 PM
ORIGINAL CODE GETS EXECUTED NOT THE NEW CODE - ASCX sk ASP .Net 1 04-19-2006 11:50 AM
Page_Unload gets executed twice chefo@bulgaria.com ASP .Net 0 01-21-2005 01:44 PM



Advertisments