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

bocco
  • 924
  • 1
  • 10
  • 15

10 Answers10

8

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

oggy
  • 3,483
  • 1
  • 20
  • 24
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.

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
3
  1. Use tools such as pylint or PyChecker.

  2. Write unit tests.

Bastien Léonard
  • 60,478
  • 20
  • 78
  • 95
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.

S.Lott
  • 384,516
  • 81
  • 508
  • 779
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.

rob
  • 36,896
  • 2
  • 55
  • 65
0

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

Kieveli
  • 10,944
  • 6
  • 56
  • 81
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

luc
  • 41,928
  • 25
  • 127
  • 172
0

You may need this:

python -m py_compile script.py
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
0

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

smeric
  • 71
  • 7
  • 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 Review](/review/late-answers/31888333) – Emi OB Jun 01 '22 at 07:59
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)

Community
  • 1
  • 1
Morten Jensen
  • 5,818
  • 3
  • 43
  • 55