Python Script control flow question
Hello, This should be terribly simple, but i just don't see how it should be. I have a python script, it handels the output of a SQL statement with a SELECT COUNT in it. It tests the case that the count is zero (and that there is no record in the SQLresult). This is my idea how it should be: SQLresult=container.SQL_statement() if (not SQLresult): resultcount=0 else: resultcount=SQLres[0][0] return resultcount This doesn't return a value if the resultcount is 0, nothing seem to happen during testing the python script. It works fine if the resultcount>0 This however is a working script SQLresult=container.SQL_statement() if (not SQLresult): resultcount=0 print resultcount return printed else: resultcount=SQLres[0][0] return resultcount It is ugly, but it works. Is it possible to remove the print resultatcount and the return printed somehow? Met vriendelijke groeten, Hans de Wit Stichting Farmaceutische Kengetallen Postbus 30460 2500 GL DEN HAAG Tel. 070-3737448 Fax 070-3737445
H.de.Wit@SFK.NL wrote:
if (not SQLresult): resultcount=0
This doesn't return a value if the resultcount is 0, nothing seem to happen during testing the python script.
if (not SQLresult): resultcount=0 print resultcount return printed
It is ugly, but it works. Is it possible to remove the print resultatcount and the return printed somehow?
Returning any false value (zero, empty string, None) from a Script will appear to do nothing when tested. By printing it, you convert it from the integer zero into the string "0". You could just as easily do: return str(resultcount) ...but I wouldn't, if you plan on calling this from other code and getting an integer back. Cheers, Evan @ Zope
H.de.Wit@SFK.NL writes:
This should be terribly simple, but i just don't see how it should be. I have a python script, it handels the output of a SQL statement with a SELECT COUNT in it. It tests the case that the count is zero (and that there is no record in the SQLresult).
This is my idea how it should be: SQLresult=container.SQL_statement() if (not SQLresult): This part will never be entered. You should always get a single row. resultcount=0 else: resultcount=SQLres[0][0] return resultcount
This doesn't return a value if the resultcount is 0, nothing seem to happen during testing the python script. It works fine if the resultcount>0 Apparently, a caller of your method does something like:
if result: do something else: do nothing As "0" is a Python false value, nothing happens.
This however is a working script SQLresult=container.SQL_statement() if (not SQLresult): resultcount=0 print resultcount return printed else: resultcount=SQLres[0][0] return resultcount The "print" converted the "0" into the string "'0'" which is not a Python false value.
It is ugly, but it works. Is it possible to remove the print resultatcount and the return printed somehow? You can use:
return `resultcount` This, too, converts the integer into a string. Dieter
participants (3)
-
Dieter Maurer -
Evan Simpson -
H.de.Wit@SFK.NL