473,809 Members | 2,769 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Too Many if Statements?

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?

Feb 7 '06 #1
39 6863
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?


Nope - but I never saw any code block with 1150+ tests in it neither :-/

Smells like a design problem anyway. Have you considered refactoring
your code ?
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom. gro'.split('@')])"
Feb 7 '06 #2
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...somet hing 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

Feb 7 '06 #3
I don't consider myself to be a seasoned programmer so if you mean
redesigning the script to make the checks and therefore reduce the
number of 'if' statements, I'm not sure if that can be done. The
script needs to make numerous checks for the existence of particular
strings within the configuration file. It also uses 'if' statements to
determine what type of file is being examined, etc.. If an error is
encounterd it writes warning messages to a master file. I guess what I
am trying to say is that in order to make the many checks on the
configuration files I do not know of any other way than to check for
the existance of particular statements, (strings), and then report on
those if they are incorrect or missing - hence at least one 'if'
statement for every check.

I appreciate the feedback though!

Feb 7 '06 #4
Ah! I see what you are saying snoe, (and most likely what bruno at
modulix was recommending). That technique should provide a workaround
to the direct 'if' approach currently used and also offer some
modularity to the logic as well.

Thank you for pointing me in the right direction - I'll give it a go.

Feb 7 '06 #5
slogging_away wrote:
I don't consider myself to be a seasoned programmer
nor do I.
so if you mean
redesigning the script to make the checks and therefore reduce the
number of 'if' statements, I'm not sure if that can be done.
I strongly doubt it could *not* be done !-)
The
script needs to make numerous checks for the existence of particular
strings within the configuration file. It also uses 'if' statements to
determine what type of file is being examined, etc.. If an error is
encounterd it writes warning messages to a master file.
Yeps, that's pretty common with this kind of scripts. I recently had a
script doing thousands of regexp substitutions, image resizing, file
moves, database inserts etc, and of course a fair amount of logging. And
I can tell you that there many few "if" in this code.
I guess what I
am trying to say is that in order to make the many checks on the
configuration files I do not know of any other way than to check for
the existance of particular statements, (strings), and then report on
those if they are incorrect or missing - hence at least one 'if'
statement for every check.
Suppose you have to match a line against a list of regexp and log if it
doesn't match. You could of course repeat the whole code for each
regexp, ie:

if not re.match(r'a/regexp/here', line):
log('a first message')

if not re.match(r'anot her/regexp/here', line):
log('another message')

(... 150 regexps later ...)

if not re.match(r'150/regexps/later', line):
log('pfww, getting tired of copy/pasting')

etc...

But you could also factor much of it:

def checkMatch(line , regexp, msg):
if not re.match(regexp , line):
log(msg)

then have a list of regexps/messages pairs and:
for exp, msg in regexps:
checkMatch(line , exp, msg)

And now, you can add as many thousands regexps you want, you still have
one (and only one) if in the code (well, in this snippet at least...).
I appreciate the feedback though!


You're welcome !-)

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom. gro'.split('@')])"
Feb 7 '06 #6
In article <11************ **********@z14g 2000cwz.googleg roups.com>,
slogging_away <hi***@yahoo.co m> 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 generated files with 10000, 25000, and 50000 simple if statements and ran
them. 10000 was okay, 25000 gave a bizarre internal error, and 50000 segfaulted
and died. My system has plenty of memory and it isn't obvious to me why python
should be so bothered about this. I'm not sure why I can have 10x the number of
if statements that cause you trouble. There might be some overall limitation
on the number of statements in a file.

Alan
--
Defendit numerus
Feb 7 '06 #7
Try the state(s) pattern!

"slogging_a way" <hi***@yahoo.co m> 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?

--
Regards,
Casey
Feb 7 '06 #8
Alan Morgan wrote:
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 generated files with 10000, 25000, and 50000 simple if statements and ran
them. 10000 was okay, 25000 gave a bizarre internal error, and 50000 segfaulted
and died. My system has plenty of memory and it isn't obvious to me why python
should be so bothered about this. I'm not sure why I can have 10x the number of
if statements that cause you trouble. There might be some overall limitation
on the number of statements in a file.


I made a script with 100,000 if's, (code below) and it appears
to work on a couple systems, including Python 2.4.2 on Win32-XP.
So at first cut, it doesn't seem to be just the if-count that
triggers the bug.

Code that does *not* demo the error on my systems:

#! /usr/bin/env python

lines = ["""#! /usr/bin/env python

import random
c = 0
n = random.randrang e(10L**10)
"""]

for i in range(100000):
lines.append('i f n % random.randrang e(2, 1000) == 0: c += 1')
lines.append('p rint c')
lines.append('# #############')
progtext = '\n'.join(lines )

f = file('manyifs.p y', 'w')
f.write(progtex t)
f.close()

exec progtext

--
--Bryan
Feb 7 '06 #9
Hmmm - good responses all around. Thank you all for your valued
feedback.

Perhaps it's too may 'if' statements under the for XXX in range(x,x,x)
statement as most of the 'if' statements appear there. It could be
something entirely else. I'm afraid its a bug with Python, (if I try
and run it several times it keeps going to the IDLE console prompt and
it eventually crashes out of Python entirely).

Some useful suggestions were provided in terms of better design so that
may be my route at this point. Thanks again for all of your help!

Feb 7 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2514
by: Bart Nessux | last post by:
Should an if statement have a corresponding else statement? Or, is it OK to have an if statement by itself. For completeness, it seems the two should be together, but from experience I know that a if statement by itself works just fine. Below is an example: if x >=0: DO SOMETHING Would it be better, or perhaps more complete, written like this:
9
2638
by: Jaime Wyant | last post by:
I know I've seen this somewhere, but can't seem to google it. Is there a way to use an alternate statement separator, other than the default ';'? jw
1
3815
by: Tom D | last post by:
I'm rewriting a database interface that our company currently has. Currently it's using the Pear::DB interface, but we found that that was introducing a bit too much overhead. I'm rewriting the interface to use mysqli. Most of what the interface does is to simplify getting results in the form of arrays (ordered and associative). Most of the code using the interface used sql queries with placeholders and parameters. For that reason I'd...
0
1861
by: Fuzzyman | last post by:
Hello all, The following is a copy of a blog entry. It's asking a question about future statements and the built in compile function. I'd appreciate any pointers or comments about possible approaches. `Movable Python <http://www.voidspace.org.uk/python/movpy/>`_ supports running both Python scripts and ``.pyc`` bytecode files. It does this by compiling scripts to bytecode, or extracting the code object from bytecode files, and then...
20
2025
by: Neroku | last post by:
Hello, i would like to know what the serious definition of statements and expressions is: i know an expression are evaluated to a value, i.e: 1 == 2 5+7 foo( 1,2) and a statement is executed: break;
2
2739
by: ojorus | last post by:
Hi! Some questions regarding the mysqli-extension (php5) 1) Prepared statements: If I understand things right, prepared statements will give better performance if you make several similar querys. (where you only change the parameters) But what if you do only ONE query; will it then be usefull to use prepared statements? Can it actuelly give better performance NOT to use prepared statements in that case? 2) Are there any DISadvantages...
3
6604
by: Dmitri | last post by:
Hello! I have a developer that is playing around with some SQL statements using VB.NET. He has a test table in a SQL 2000 database, and he has about 2000 generated INSERT statements. When the 2000 INSERT statements are run in SQL query analyzer, all 2000 rows are added to the table. When he tries to send the 2000 statements to SQL Server through his app., a random number of statements do not get executed. But, SQL Profiler shows that...
0
1933
by: Gary Herron | last post by:
Ohad Frand wrote: There is no way you can consider 'elif', 'else', 'except', and 'from' statements. However, as someone pointed out, the kwlist from the keyword module is the closest thing we can think of to the list you are asking for. On the other hand, what's wrong with constructing the list as you did in your example above?
0
1452
by: Ohad Frand | last post by:
Hi Thanks a lot for your reply I think the main uses for it is to study the language and to see that I didn't miss anything else or that something is changed from one version to another. The keyword module will help me Thanks again Ohad Frand
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10633
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10375
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9198
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7651
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6880
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5686
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.