pyflakes: The Passive Checker of Python Programs

jieforest發表於2012-06-19
There are several code analysis tools for Python. The most well known is pylint. Then there’s pychecker and now we’re moving on to pyflakes. The pyflakes project is a part of something known as the Divmod Project. Pyflakes doesn’t actually execute the code it checks, unlike pychecker. Of course, pylint also doesn’t execute the code. Regardless, we’ll take a quick look at it and see how pyflakes works and if it’s better than the competition.

Getting Started

As you have probably guessed, pyflakes is not a part of the Python distribution. You will need to download it from PyPI or from the project’s launchpad page. Once you have it installed, you can run it against some of your own code. Or you can follow along and see how it works with our test script.

Running pyflakes


We’ll be using a super simple and pretty silly example script. In fact, it’s the same one we used for the pylint and pychecker articles. Here it is again for your viewing pleasure:

CODE:

01.import sys
02.
03.########################################################################
04.class CarClass:
05.""""""
06.
07.#----------------------------------------------------------------------
08.def __init__(self, color, make, model, year):
09."""Constructor"""
10.self.color = color
11.self.make = make
12.self.model = model
13.self.year = year
14.
15.if "Windows" in platform.platform():
16.print "You're using Windows!"
17.
18.self.weight = self.getWeight(1, 2, 3)
19.
20.#----------------------------------------------------------------------
21.def getWeight(this):
22.""""""
23.return "2000 lbs"As was noted in the other articles, this dumb code has 4 issues, 3 of which would stop the programming from running. Let’s see what pyflakes can find! Try running the following command and you’ll see the following output:

CODE:

C:\Users\mdriscoll\Desktop>pyflakes crummy_code.py
crummy_code.py:1: 'sys' imported but unused
crummy_code.py:15: undefined name 'platform'While pyflakes was super fast at returning this output, it didn’t find all the errors. The getWeight method call is passing too many arguments and getWeight method itself is defined incorrectly as it doesn’t have a “self” argument. If you fixed your code according to what pyflakes told you, you’re code still wouldn’t work.




來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-733242/,如需轉載,請註明出處,否則將追究法律責任。

相關文章