🚀 Preparing Python Code for Production: My Go-To Tools 🚀

🚀 Preparing Python Code for Production: My Go-To Tools 🚀

🚀 Preparing Python Code for Production: My Go-To Tools 🚀

Last week, I was busy getting my Python code production-ready. And in order to do code cleaning automatically, I again used my favorite combination: Black and Pylint. These tools have become indispensable in my workflow, thanks to the great help and support from Jesús Virseda Jerez and Matteo Manca who tremendously helped in preparing demand forecasting model developed by our team at Lidl Digital for CI/CD pipeline.

In light of this experience, I wanted to share five amazing tools that might also be helpful in your coding journey. 🌟

  1. Black 🖤 Black is an uncompromising code formatter that ensures consistent code style across a project. It implements formatting rules easy to read and maintain! To see the changes before applying them, use the --diff option:

pip install black 
black --diff my_amazing_code.py        

otherwise do directly

black my_amazing_code.py        

2. Pylint ✅ is recognized as a comprehensive code analysis tool that checks for programming errors and enforces coding standards and returns the score + suggested lines with improvements on naming and not used libraries. Its detailed reports facilitate spotting and fixing issues as well. I particularly appreciate the ability to identify unused libraries, which can then be easily removed by user. This is especially useful in ML code, where it's common to end up with numerous unused libraries by the moment the code is production-ready.

pip install pylint 
pylint my_amazing_code.py        

3. isort 📚 - another tool which works with your imports. It automatically sorts and organizes imports, making code cleaner and more manageable. It integrates seamlessly with most editors and CI/CD pipelines. To see the changes before applying them, use the --diff option as with Black:

pip install isort
isort --diff my_amazing_code.py
#or go direcly by:
isort my_amazing_code.py        

4. Radon 📏 is another interesting tool to play with. It provides metrics for code complexity and maintainability. It helps you identify code that is overly complex and in need of refactoring. For example, a complexity rating of A indicates very low complexity, which is ideal. It helps identify complex code that may need refactoring to maintain a clean codebase.

  • A (1-5): Very low complexity, easy to understand and maintain.
  • B (6-10): Low complexity, still fairly easy to understand.
  • C (11-20): Moderate complexity, might need some attention.
  • D (21-30): High complexity, should be refactored.
  • E (31-40): Very high complexity, definitely needs refactoring.
  • F (41+): Extreme complexity, unmaintainable, must be refactored.

pip install radon 
radon cc my_amazing_code.py # checks code complexity 
radon mi my_amazing_code.py # checka maintainability index        

5. Coverage 📊 measures code coverage of your Python programs. It checks which parts of your code are being executed during tests, helping you ensure that your tests cover all parts of your application.

pip install coverage 
coverage run -m pytest # run tests and measure coverage 
coverage report -m        

Have fun! ❤️

Markus Heidt

Founder @ Crateflow | Data Science & Entrepreneurship

11mo

This is a very interesting list. I haven't heard about radon yet, but I'm keen to test it and see what comes out.

To view or add a comment, sign in

Insights from the community

Others also viewed

Explore topics