在没有 -0 标志的情况下禁用 python 的 assert() [英] Disabling python's assert() without -0 flag

查看:36
本文介绍了在没有 -0 标志的情况下禁用 python 的 assert()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从不同的软件内部运行一个 python 脚本(它提供了一个 python 接口来操作它的数据结构).

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.

我无法使用 python -O.我还有什么其他选择,以编程方式禁用 python 代码中的所有断言?变量 __debug__(由 -O 标志清除)不能分配给 :(

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 :(

推荐答案

文档说

内置变量 [__debug__] 的值在解释器启动.

The value for the built-in variable [__debug__] is determined when the interpreter starts.

所以,如果你不能控制python解释器的启动方式,那么看起来你不能禁用assert.

So, if you can not control how the python interpreter is started, then it looks like you can not disable assert.

这里有一些其他选项:

  1. 最安全的方法是手动删除所有断言语句.
  2. 如果你所有的断言语句都单独出现在行上,那么也许你可以用

  1. The safest way is to manually remove all the assert statements.
  2. 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

请注意,如果在断言之后出现其他代码,这将破坏您的代码.例如,上面的 sed 命令会在如下一行中注释掉 return:

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.

可能有一种方法可以通过解析您的带有 tokenize 模块的脚本,但是编写这样的程序来删除断言可能比手动花费更多的时间删除断言,特别是如果这是一次性工作.

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.

如果其他软件接受 .pyc 文件,则有一个肮脏的把戏似乎在我的机器上工作,但请注意 a Python核心开发人员对此提出警告(参见 Éric Araujo 在 2011-09-17 上的评论).假设您的脚本名为 script.py.

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.

  • 制作一个名为 temp.py 的临时脚本:

  • Make a temporary script called, say, temp.py:

import script

  • 运行 python -O temp.py.这将创建 script.pyo.
  • script.pyscript.pyc(如果存在)移出 PYTHONPATH或其他软件正在读取的任何目录以找到您的脚本.
  • 重命名script.pyo --> script.pyc.
  • Run python -O temp.py. This creates script.pyo.
  • Move script.py and script.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.
  • 现在当其他软件尝试导入您的脚本时,它会只找到删除断言的 pyc 文件.

    Now when the other software tries to import your script, it will only find the pyc file, which has the asserts removed.

    例如,如果 script.py 看起来像这样:

    For example, if script.py looks like this:

    assert False
    print('Got here')
    

    然后运行 ​​python temp.py 现在将打印 Got here 而不是引发 AssertionError.

    then running python temp.py will now print Got here instead of raising an AssertionError.

    这篇关于在没有 -0 标志的情况下禁用 python 的 assert()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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