如果声明太多了? [英] Too Many if Statements?

查看:75
本文介绍了如果声明太多了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 - 我在win32上运行Python 2.4.2(#67,2005年9月28日,12:41:11)[MSC v.1310

32位(英特尔)],并且有一个脚本可以对文本文件(配置文件)进行大量检查

,因此可以报告差异。

脚本工作正常,但似乎我可能有用

''if''语句撞墙。


由于脚本对文本文件进行了多次检查,

(超过500),脚本中有很多'if''语句,(超过

1150)。似乎当我添加任何额外的

''if''语句时,脚本将无法运行。没有错误产生 - 它只需返回python提示符,就像选择成功的

''Check Module''命令一样。如果我删除了其他一些''if''

语句,那么新的语句会起作用,因此它似乎已经达到了限制

''if''语句的数量。这已经阻止了对文本文件制作的脚本进一步检查




有没有人遇到过这类事情?

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?

推荐答案

slogging_away写道:
slogging_away wrote:
嗨 - 我正在运行Python 2.4.2(#67,2005年9月28日)在win32上有一个脚本可以对文本文件(配置文件)进行大量检查,所以12:41:11)[MSC v.1310
32位(英特尔)],所以差异可能是报告。
脚本工作正常,但似乎我可能已经用
''if''语句撞墙。

由于脚本支持的检查数量很多关于文本文件,
(超过500),脚本中有很多'if''语句,(通过1150)。看起来,当我添加任何额外的if语句时,脚本将无法运行。没有错误产生 - 它只是返回到python提示符,就像选择成功的检查模块命令一样。如果我删除了其他一些'if''
语句,那么新语句的工作方式似乎已经达到了''if''语句数量的限制。这已经阻碍了对文本文件的脚本进行任何进一步的检查。

有没有人遇到过这类事情?
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 - 但我从来没有看到任何带有1150+测试的代码块: - /


无论如何,闻起来像是一个设计问题。您是否考虑过重构

您的代码?

-

bruno desthuilliers

python -c" print' '@''。join([''。''。join([w [:: - 1] for p in p.split(''。'')])for

p in' 'o **** @ xiludom.gro''。split(''''')])"



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(''@'')])"


slogging_away写道:
slogging_away wrote:
嗨 - 我在win32上运行Python 2.4.2(#67,2005年9月28日,12:41:11)[MSC v.1310
32位(英特尔)],并且有一个脚本可以生成对文本文件,(配置文件)进行了大量检查,因此可以报告差异。
脚本工作正常,但看起来我可能已经用
''if''语句敲门了。

由于脚本对文本文件进行了多次检查,
(超过500),脚本中有很多'if''语句,(结束)
1150)。看起来,当我添加任何额外的if语句时,脚本将无法运行。没有错误产生 - 它只是返回到python提示符,就像选择成功的检查模块命令一样。如果我删除了其他一些'if''
语句,那么新语句的工作方式似乎已经达到了''if''语句数量的限制。这已经阻碍了对文本文件的脚本进行任何进一步的检查。

有没有人遇到过这类事情?
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?




我不能说我之前遇到过它,但你的描述让我想到了

你可以采取其他方法。将大的

数量的if语句分成单独的函数是否有帮助?

如果你正在检查一些文本,看看是否有很多静态字符串是

包含在其中,你可以将测试字符串放入一个列表并循环

通过......这样的事情(未经测试):


测试= [

''寻找这个字符串'',

''和这一个'',

''我在文本''

]#etc ...


用于测试中的测试:

如果测试不在文本中:

引发LookupError



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


我不认为自己是一名经验丰富的程序员,所以如果你的意思是

重新设计脚本以进行检查,从而减少''if''语句的数量,我不确定是否可以这样做。

脚本需要对配置文件中特定

字符串的存在进行大量检查。它还使用''if''语句来确定正在检查的文件类型等等。如果错误是

遇到它会向主人写入警告消息文件。我想我想要说的是为了对

配置文件进行多次检查,我不知道除了检查<之外的任何其他方式br />
特定陈述(字符串)的存在,然后报告

如果它们不正确或缺失 - 因此至少有一个''if''

每张支票的声明。


我很感激反馈!

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!


这篇关于如果声明太多了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆