Python代码似乎无序执行
在工作中,我有一种编程语言编码在数据库记录中。我试图在python中编写一个打印函数来显示记录包含的内容。Python代码似乎无序执行
这是我遇到的麻烦的代码:
# Un-indent the block if necessary.
if func_option[row.FRML_FUNC_OPTN] in ['Endif', 'Else']:
self.indent = self.indent - 1
# if this is a new line, indent it.
if len(self.formulatext) <> 0 and self.formulatext[len(self.formulatext) - 1] == '\n':
for i in range(1,self.indent):
rowtext = ' ' + rowtext
# increase indent for 'then', 'else'
if func_option[row.FRML_FUNC_OPTN] in ['Then', 'Else']:
self.indent = self.indent + 1
当row.FRML ____ FUNC____OPTN等于“别人的,我期待它首先取消缩进,然后再次缩进,使“其他”在较低的缩进级别打印,然后代码的其余部分在内。这实际上是缩进我得到的类型:
IfThen
IfThen
Else
EndifComment
IfThen
Endif
IfThen
Else
Endif
Else
Endif
正如你可以看到“别人的比的if/endif仍然缩进更高。任何想法,为什么会发生这种情况?
我曾尝试用洒调试语句,其结果是代码:
row: Else
row.FRML_FUNC_OPTN is : Elsedecrementing indent
row.FRML_FUNC_OPTN is : Elseincrementing indent
这意味着缩进改变,如果确实被输入的...
从您的调试日志:
row: Else
row.FRML_FUNC_OPTN is : Elsedecrementing indent
row.FRML_FUNC_OPTN is : Elseincrementing indent
我怀疑在输入提供的代码片段时,您在“Else”之前已经有缩进。
尝试增加:
rowtext = rowtext.strip()
之前只是第一个,如果
或者,如果rowtext是空白的,你将它添加到别的东西以后,尝试是调用带。
事实证明,我正在阅读的表中的公式有一个如果没有'If',那么只需添加'Then'即可。我有代码来消除Thens没有'如果是在其他地方打印,在我的功能,但我没有消除'如果'。谢谢你的帮助。非常感激 :) 。 – Alterlife 2009-09-25 11:45:01
仅仅因为它是一个“脚本语言”并不意味着你必须没有一个完整的调试器与断点生活!
- 安装eric3
- 加载代码
- 按 “调试”)
此外,您还像是新的Python,所以这里有一些提示:
- 你可以乘以字符串,比循环要快得多
- 读取数组访问的工作方式,使用[-1]为最后的e字元素
- 读的字符串方法,使用.endswith()
- 使用的元组静态unmutable数据,更快
>
# Un-indent the block if necessary.
op = func_option[row.FRML_FUNC_OPTN]
if op in ('Endif', 'Else'):
self.indent -= 1
# if this is a new line, indent it.
if self.formulatext.endswith('\n'):
rowtext = ("\t" * indent) + rowtext
# increase indent for 'then', 'else'
if op in ('Then', 'Else'):
self.indent += 1
非常感谢您的建议。 – Alterlife 2009-09-26 13:39:29
把一些日志记录在你的代码来跟踪它。例如,你确定func_option是'Else'吗?也许这是'其他',或者其他一些排列?你可能根本不会触及unindent/indent代码。 – 2009-09-25 11:16:07
我确实在调试语句中添加了代码。 row:否则 row.FRML_FUNC_OPTN是:其他的缩进缩进 row.FRML_FUNC_OPTN是:Else增量缩进 – Alterlife 2009-09-25 11:20:46
rowtext是否已经在开始处包含空格? – 2009-09-25 11:26:13