禁用python的assert()而不带-0标志 [英] Disabling python's assert() without -0 flag
问题描述
我正在优化我的代码速度,并希望看到对我的断言有什么影响。
我无法使用 python -O
。我还有什么其他选择,以编程方式禁用所有的断言在Python代码?变量 __ debug __
(由 -O
标志清除)不能分配给:(
文档说,
内置变量[调试]的值在
解释器启动。
所以,如果你不能控制python解释器的启动方式,那么看起来你不能禁用assert。
这里有一些其他选项:
- 最安全的方式是手动删除所有的assert语句。
-
如果所有的断言语句自行发生,那么
也许你可以用sed -i's / assert / pass #assert / g'script.py
请注意,如果其他代码出现在例如,上面的sed命令将以这样的方式注释掉
return
:断言x;返回True
这将改变程序的逻辑。
如果你有这样的代码,最好手动删除断言。
-
可能通过使用
tokenize
脚本,以编程方式删除它们>模块,但写这样一个程序到
删除断言可能需要更多的时间比手动
删除断言,特别是如果这是一次性的工作。 -
如果另一块软件接受.pyc文件,那么有一个
的脏话,似乎在我的机器上工作,虽然注意一个Python
核心开发人员警告这一点(见ÉricAraujo对2011-09-17的评论)。假设您的脚本称为script.py
。
-
暂时脚本叫做temp.py:
导入脚本
- 运行
python -O temp.py
。这将创建script.pyo
。 - 移动
script.py
和script.pyc
(如果存在),从您的PYTHONPATH
或其他软件正在阅读的任何目录中找到您的
脚本。 - 重命名
script.pyo
- >script.pyc
。
现在当其他软件尝试导入脚本时,
只能找到pyc
文件,其中已被删除。
例如,如果
script.py
如下所示:assert False
print('Got here')
然后运行
python temp.py
现在将打印而不是在这里
提起AssertionError。 -
I'm running a python script from inside a different software (it provides a python interface to manipulate its data structures).
I'm optimizing my code for speed and would like to see what impact on performance my asserts have.
I'm unable to use python -O
. What other options do I have, to programatically disable all asserts in python code? The variable __debug__
(which is cleared by -O
flag) cannot be assigned to :(
The value for the built-in variable [debug] is determined when the interpreter starts.
So, if you can not control how the python interpreter is started, then it looks like you can not disable assert.
Here then are some other options:
- The safest way is to manually remove all the assert statements.
If all your assert statements occur on lines by themselves, then perhaps you could remove them with
sed -i 's/assert /pass #assert /g' script.py
Note that this will mangle your code if other code comes after the assert. For example, the sed command above would comment-out the
return
in a line like this:assert x; return True
which would change the logic of your program.
If you have code like this, it would probably be best to manually remove the asserts.
There might be a way to remove them programmatically by parsing your script with the
tokenize
module, but writing such a program to remove asserts may take more time than it would take to manually remove the asserts, especially if this is a one-time job.If the other piece of software accepts .pyc files, then there is a dirty trick which seems to work on my machine, though note a Python core developer warns against this (See Éric Araujo's comment on 2011-09-17). Suppose your script is called
script.py
.Make a temporary script called, say, temp.py:
import script
- Run
python -O temp.py
. This createsscript.pyo
. - Move
script.py
andscript.pyc
(if it exists) out of your PYTHONPATH or whatever directory the other software is reading to find your script. - Rename
script.pyo
-->script.pyc
.
Now when the other software tries to import your script, it will only find the
pyc
file, which has the asserts removed.For example, if
script.py
looks like this:assert False print('Got here')
then running
python temp.py
will now printGot here
instead of raising an AssertionError.
这篇关于禁用python的assert()而不带-0标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!