Too Many if Statements?
snoe
case.nelson at gmail.com
Tue Feb 7 12:06:09 EST 2006
slogging_away wrote:
> Hi - I'm running Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310
> 32 bit (Intel)] on win32, and have a script that makes numerous checks
> on text files, (configuration files), so discrepancies can be reported.
> The script works fine but it appears that I may have hit a wall with
> 'if' statements.
>
> Due to the number of checks perfromed by the script on the text files,
> (over 500), there are quite a few 'if' statements in the script, (over
> 1150). It seems that it is at the point that when I add any additional
> 'if' statements the script will not run. No error is produced - it
> just returns to the python prompt much the same as when a successful
> 'Check Module' command is selected. If I delete some other 'if'
> statements the new ones work so it appears that it has hit a limit on
> the number of 'if' statements. This has stunted any further checks for
> the script to make on the text files.
>
> Hs anyone ever run into this sort of thing?
I can't say I've run into it before but your description makes me think
there are other approaches you could take. Does splitting the large
number of if statements into separate functions help at all?
If you're checking some text to see if a number of static strings are
contained therein, you could put the test strings into a list and loop
through...something like this (untested):
tests = [
'looking for this string',
'and this one',
'am i in the text'
] # etc...
for test in tests:
if test not in text:
raise LookupError
More information about the Python-list
mailing list