My background is C and C++. I like Python a lot, but there's one aspect of it (and other interpreted languages I guess) that is really hard to work with when you're used to compiled languages.
我的背景是C和C ++。我非常喜欢Python,但是当你习惯于编译语言时,它的一个方面(以及我猜的其他解释语言)真的很难用。
When I've written something in Python and come to the point where I can run it, there's still no guarantee that no language-specific errors remain. For me that means that I can't rely solely on my runtime defense (rigorous testing of input, asserts etc.) to avoid crashes, because in 6 months when some otherwise nice code finally gets run, it might crack due to some stupid typo.
当我用Python编写一些内容并且可以运行它时,仍然无法保证不会出现特定于语言的错误。对我而言,这意味着我不能完全依赖我的运行时防御(严格测试输入,断言等)以避免崩溃,因为在6个月内,当一些其他好的代码最终运行时,它可能会因为一些愚蠢的错误而破解。
Clearly a system should be tested enough to make sure all code has been run, but most of the time I use Python for in-house scripts and small tools, which ofcourse never gets the QA attention they need. Also, some code is so simple that (if your background is C/C++) you know it will work fine as long as it compiles (e.g. getter-methods inside classes, usually a simple return of a member variable).
显然,系统应该经过足够的测试,以确保所有代码都已运行,但大部分时间我都使用Python作为内部脚本和小工具,这些内容永远不会得到他们所需的QA关注。此外,一些代码非常简单(如果您的背景是C / C ++),您知道只要编译它就可以正常工作(例如,类中的getter-methods,通常是成员变量的简单返回)。
So, my question is the obvious - is there any way (with a special tool or something) I can make sure all the code in my Python script will "compile" and run?
所以,我的问题是显而易见的 - 有什么方法(使用特殊工具或其他东西)我可以确保我的Python脚本中的所有代码都将“编译”并运行?
5 个解决方案
#1
看看PyChecker和PyLint。
Here's example output from pylint, resulting from the trivial program:
这是pylint的示例输出,由琐碎的程序产生:
print a
As you can see, it detects the undefined variable, which py_compile won't (deliberately).
如您所见,它检测到未定义的变量,py_compile不会(故意)。
in foo.py:
************* Module foo
C: 1: Black listed name "foo"
C: 1: Missing docstring
E: 1: Undefined variable 'a'
...
|error |1 |1 |= |
Trivial example of why tests aren't good enough, even if they cover "every line":
为什么测试不够好的简单例子,即使它们涵盖“每一行”:
bar = "Foo"
foo = "Bar"
def baz(X):
return bar if X else fo0
print baz(input("True or False: "))
EDIT: PyChecker handles the ternary for me:
编辑:PyChecker为我处理三元组:
Processing ternary...
True or False: True
Foo
Warnings...
ternary.py:6: No global (fo0) found
ternary.py:8: Using input() is a security problem, consider using raw_input()
#2
Others have mentioned tools like PyLint which are pretty good, but the long and the short of it is that it's simply not possible to do 100%. In fact, you might not even want to do it. Part of the benefit to Python's dynamicity is that you can do crazy things like insert names into the local scope through a dictionary access.
其他人提到像PyLint这样的工具非常好,但是长期和短期是它根本不可能做到100%。事实上,你甚至可能不想这样做。 Python的动态性的一部分好处是你可以通过字典访问来做一些疯狂的事情,比如在本地范围内插入名称。
What it comes down to is that if you want a way to catch type errors at compile time, you shouldn't use Python. A language choice always involves a set of trade-offs. If you choose Python over C, just be aware that you're trading a strong type system for faster development, better string manipulation, etc.
它归结为如果你想要一种在编译时捕获类型错误的方法,你不应该使用Python。语言选择总是涉及一系列权衡。如果您选择Python over C,请注意您正在交易强类型系统,以便更快地开发,更好的字符串操作等。
#3
I think what you are looking for is code test line coverage. You want to add tests to your script that will make sure all of your lines of code, or as many as you have time to, get tested. Testing is a great deal of work, but if you want the kind of assurance you are asking for, there is no free lunch, sorry :( .
我认为你要找的是代码测试线覆盖。您希望在脚本中添加测试,以确保您的所有代码行或您有时间的代码都经过测试。测试是一项繁重的工作,但如果你想要的那种保证,就没有免费的午餐,对不起:(。
#4
If you are using Eclipse with Pydev as an IDE, it can flag many typos for you with red squigglies immediately, and has Pylint integration too. For example:
如果您将Eclipse与Pydev一起用作IDE,它可以立即标记许多拼写错误的红色波浪形,并且还具有Pylint集成。例如:
foo = 5
print food
will be flagged as "Undefined variable: food". Of course this is not always accurate (perhaps food was defined earlier using setattr or other exotic techniques), but it works well most of the time.
将被标记为“未定义的变量:食物”。当然,这并不总是准确的(可能早先使用setattr或其他奇特的技术来定义食物),但它在大多数时候都很有效。
In general, you can only statically analyze your code to the extent that your code is actually static; the more dynamic your code is, the more you really do need automated testing.
通常,只能在代码实际上是静态的情况下静态分析代码;代码越动态,您就越需要自动化测试。
#5
Your code actually gets compiled when you run it, the Python runtime will complain if there is a syntax error in the code. Compared to statically compiled languages like C/C++ or Java, it does not check whether variable names and types are correct – for that you need to actually run the code (e.g. with automated tests).
您的代码在运行时实际上已编译,如果代码中存在语法错误,Python运行时将会抱怨。与静态编译的语言(如C / C ++或Java)相比,它不会检查变量名称和类型是否正确 - 因为您需要实际运行代码(例如,使用自动化测试)。
#1
看看PyChecker和PyLint。
Here's example output from pylint, resulting from the trivial program:
这是pylint的示例输出,由琐碎的程序产生:
print a
As you can see, it detects the undefined variable, which py_compile won't (deliberately).
如您所见,它检测到未定义的变量,py_compile不会(故意)。
in foo.py:
************* Module foo
C: 1: Black listed name "foo"
C: 1: Missing docstring
E: 1: Undefined variable 'a'
...
|error |1 |1 |= |
Trivial example of why tests aren't good enough, even if they cover "every line":
为什么测试不够好的简单例子,即使它们涵盖“每一行”:
bar = "Foo"
foo = "Bar"
def baz(X):
return bar if X else fo0
print baz(input("True or False: "))
EDIT: PyChecker handles the ternary for me:
编辑:PyChecker为我处理三元组:
Processing ternary...
True or False: True
Foo
Warnings...
ternary.py:6: No global (fo0) found
ternary.py:8: Using input() is a security problem, consider using raw_input()
#2
Others have mentioned tools like PyLint which are pretty good, but the long and the short of it is that it's simply not possible to do 100%. In fact, you might not even want to do it. Part of the benefit to Python's dynamicity is that you can do crazy things like insert names into the local scope through a dictionary access.
其他人提到像PyLint这样的工具非常好,但是长期和短期是它根本不可能做到100%。事实上,你甚至可能不想这样做。 Python的动态性的一部分好处是你可以通过字典访问来做一些疯狂的事情,比如在本地范围内插入名称。
What it comes down to is that if you want a way to catch type errors at compile time, you shouldn't use Python. A language choice always involves a set of trade-offs. If you choose Python over C, just be aware that you're trading a strong type system for faster development, better string manipulation, etc.
它归结为如果你想要一种在编译时捕获类型错误的方法,你不应该使用Python。语言选择总是涉及一系列权衡。如果您选择Python over C,请注意您正在交易强类型系统,以便更快地开发,更好的字符串操作等。
#3
I think what you are looking for is code test line coverage. You want to add tests to your script that will make sure all of your lines of code, or as many as you have time to, get tested. Testing is a great deal of work, but if you want the kind of assurance you are asking for, there is no free lunch, sorry :( .
我认为你要找的是代码测试线覆盖。您希望在脚本中添加测试,以确保您的所有代码行或您有时间的代码都经过测试。测试是一项繁重的工作,但如果你想要的那种保证,就没有免费的午餐,对不起:(。
#4
If you are using Eclipse with Pydev as an IDE, it can flag many typos for you with red squigglies immediately, and has Pylint integration too. For example:
如果您将Eclipse与Pydev一起用作IDE,它可以立即标记许多拼写错误的红色波浪形,并且还具有Pylint集成。例如:
foo = 5
print food
will be flagged as "Undefined variable: food". Of course this is not always accurate (perhaps food was defined earlier using setattr or other exotic techniques), but it works well most of the time.
将被标记为“未定义的变量:食物”。当然,这并不总是准确的(可能早先使用setattr或其他奇特的技术来定义食物),但它在大多数时候都很有效。
In general, you can only statically analyze your code to the extent that your code is actually static; the more dynamic your code is, the more you really do need automated testing.
通常,只能在代码实际上是静态的情况下静态分析代码;代码越动态,您就越需要自动化测试。
#5
Your code actually gets compiled when you run it, the Python runtime will complain if there is a syntax error in the code. Compared to statically compiled languages like C/C++ or Java, it does not check whether variable names and types are correct – for that you need to actually run the code (e.g. with automated tests).
您的代码在运行时实际上已编译,如果代码中存在语法错误,Python运行时将会抱怨。与静态编译的语言(如C / C ++或Java)相比,它不会检查变量名称和类型是否正确 - 因为您需要实际运行代码(例如,使用自动化测试)。