Checking Python code correctness
Asked Answered
V

10

6

In C++ I have compiler that tell me if something wrong with my code after refactoring. How to make sure that Python code is at least correct after changes? There may be some stupid error like wrong function name etc. that pretty easy to find in compile time.

Thanks

Veliz answered 11/9, 2009 at 11:56 Comment(0)
G
8

Looks like PyChecker or pylint are what you're looking for

Grantley answered 11/9, 2009 at 12:0 Comment(2)
Eclipse/PyDev. That's what I use, and I am a satisfied customer.Revamp
I think IDLE might have one of those built in (can't remember exactly).Normand
M
3
  1. use editor / IDE that supports code highlighting. E.g., Notepad++ has word-highlighting feature that I find very useful.

  2. use unit tests

stupid errors will be weeded out first, so I wouldn't worry to much about this type of errors. it's "smart" error you should be afraid of.

Mancilla answered 11/9, 2009 at 11:58 Comment(0)
P
3
  1. Use tools such as pylint or PyChecker.

  2. Write unit tests.

Pattiepattin answered 11/9, 2009 at 12:0 Comment(0)
C
3

Unit test. http://docs.python.org/library/unittest.html

If your tests are written at a reasonable level of granularity, it can be as fast to unit test as it is to run lint or a compiler.

Cyanamide answered 11/9, 2009 at 13:40 Comment(0)
J
2
  • Static analysis (as from the IDE, or from tools like pyLint and pyChecker) is a very quick and effective way to check simple errors, and enforce a common style.
  • Unit tests are a great way to ensure the code stands for its contract.
  • Code reviews and pair programming are one of the best ways to find errors of all sorts, and to spread knowledge in a team.

All of the options require some time, to setup and to execute. However, the gains are tremendous, and far higher than the investment.

Jessicajessie answered 11/9, 2009 at 14:39 Comment(0)
V
0

Eclipse has a good python plugin for doing the syntax highlighting and debugging.

Visible answered 11/9, 2009 at 12:0 Comment(0)
T
0

Pylint is almost doing what you are looking for.

You can also force the compilation of your python files. That will show some basic syntax error (it doesn't have all the capability of a c++ compiler)

I've read this article and decided to make an automated build system with pyDev and ant. It does the compilation of the python files and is running the unit tests. Next step is to integrate pylint to that process.

I hope it helps

Tehee answered 11/9, 2009 at 12:55 Comment(0)
C
0

As with other languages, you should use assertions liberally throughout your code. Use assertions when you must rely on the predicate to be true for the program to run, not as exception/error handling. An assertion should be used to check for irrecoverable errors and force the program to crash. More on assertions (and python error checking in general)

Crwth answered 12/3, 2012 at 22:36 Comment(0)
E
0

You may need this:

python -m py_compile script.py
Elfriedeelfstan answered 25/3, 2015 at 5:51 Comment(0)
B
0

You might also want to check out PEP8 as a style guide for Python Code.

Barred answered 28/5, 2022 at 23:48 Comment(1)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewSmoothspoken

© 2022 - 2024 — McMap. All rights reserved.