{"id":2185,"date":"2025-03-04T07:02:29","date_gmt":"2025-03-04T07:02:29","guid":{"rendered":"https:\/\/mailitics.com\/index.php\/2025\/03\/04\/data-science-from-school-to-work-part-ii\/"},"modified":"2025-03-04T07:02:29","modified_gmt":"2025-03-04T07:02:29","slug":"data-science-from-school-to-work-part-ii","status":"publish","type":"post","link":"https:\/\/mailitics.com\/index.php\/2025\/03\/04\/data-science-from-school-to-work-part-ii\/","title":{"rendered":"Data Science: From School to Work, Part II"},"content":{"rendered":"<p>    Data Science: From School to Work, Part II<br \/>\n \t<BR><br \/>\n<BR><\/BR><br \/>\n    <!-- no image --><br \/>\n \t<BR><br \/>\n<BR><\/BR><\/p>\n<div>\n<p class=\"wp-block-paragraph\">In my previous article, I highlighted the importance of effective project management in Python development. Now, let\u2019s shift our focus to the code itself and explore how to write clean, maintainable code\u200a\u2014\u200aan essential practice in professional and collaborative environments.\u00a0<\/p>\n<ul class=\"wp-block-list\">\n<li class=\"wp-block-list-item\">\n<strong>Readability &amp; Maintainability:<\/strong> Well-structured code is easier to read, understand, and modify. Other developers\u200a\u2014\u200aor even your future self\u200a\u2014\u200acan quickly grasp the logic without struggling to decipher messy code.<\/li>\n<li class=\"wp-block-list-item\">\n<strong>Debugging &amp; Troubleshooting:<\/strong> Organized code with clear variable names and structured functions makes it easier to identify and fix bugs efficiently.<\/li>\n<li class=\"wp-block-list-item\">\n<strong>Scalability &amp; Reusability:<\/strong> Modular, well-organized code can be reused across different projects, allowing for seamless scaling without disrupting existing functionality.<\/li>\n<\/ul>\n<p class=\"wp-block-paragraph\">So, as you work on your next Python project, remember:\u00a0<\/p>\n<p class=\"wp-block-paragraph\">Half of good code is <a href=\"https:\/\/towardsdatascience.com\/tag\/clean-code\/\" title=\"Clean Code\">Clean Code<\/a>.<\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-dotted\">\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n<p class=\"wp-block-paragraph\">Python is one of the most popular and versatile <a href=\"https:\/\/towardsdatascience.com\/tag\/programming\/\" title=\"Programming\">Programming<\/a> languages, appreciated for its simplicity, comprehensibility and large community. Whether web development, data analysis, artificial intelligence or automation of tasks\u200a\u2014\u200aPython offers powerful and flexible tools that are suitable for a wide range of areas.<\/p>\n<p class=\"wp-block-paragraph\">However, the efficiency and maintainability of a <a href=\"https:\/\/towardsdatascience.com\/tag\/python\/\" title=\"Python\">Python<\/a> project depends heavily on the practices used by the developers. Poor structuring of the code, a lack of conventions or even a lack of documentation can quickly turn a promising project into a maintenance and development-intensive puzzle. It is precisely this point that makes the difference between student code and professional code.<\/p>\n<p class=\"wp-block-paragraph\">This article is intended to present the most important best practices for writing high-quality Python code. By following these recommendations, developers can create scripts and applications that are not only functional, but also readable, performant and easily maintainable by third parties.<\/p>\n<p class=\"wp-block-paragraph\">Adopting these best practices right from the start of a project not only ensures better collaboration within teams, but also prepares your code to evolve with future needs. Whether you\u2019re a beginner or an experienced developer, this guide is designed to support you in all your Python developments.<\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-dotted\">\n<h2 class=\"wp-block-heading\">The code structuration<\/h2>\n<p class=\"wp-block-paragraph\">Good code structuring in Python is essential. There are two main project layouts: <strong>flat layout<\/strong> and <strong>src layout<\/strong>.<\/p>\n<p class=\"wp-block-paragraph\">The <strong>flat layout<\/strong> places the source code directly in the project root without an additional folder. This approach simplifies the structure and is well-suited for small scripts, quick prototypes, and projects that do not require complex packaging. However, it may lead to unintended import issues when running tests or scripts.<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f4c2.png?ssl=1\" alt=\"\ud83d\udcc2\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> my_project\/\n\u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f4c2.png?ssl=1\" alt=\"\ud83d\udcc2\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> my_project\/                  # Directly in the root\n\u2502   \u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f40d.png?ssl=1\" alt=\"\ud83d\udc0d\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> __init__.py\n\u2502   \u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f40d.png?ssl=1\" alt=\"\ud83d\udc0d\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> main.py                   # Main entry point (if needed)\n\u2502   \u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f40d.png?ssl=1\" alt=\"\ud83d\udc0d\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> module1.py             # Example module\n\u2502   \u2514\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f40d.png?ssl=1\" alt=\"\ud83d\udc0d\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> utils.py\n\u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f4c2.png?ssl=1\" alt=\"\ud83d\udcc2\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> tests\/                            # Unit tests\n\u2502   \u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f40d.png?ssl=1\" alt=\"\ud83d\udc0d\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> test_module1.py\n\u2502   \u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f40d.png?ssl=1\" alt=\"\ud83d\udc0d\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> test_utils.py\n\u2502   \u2514\u2500\u2500 ...\n\u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f4c4.png?ssl=1\" alt=\"\ud83d\udcc4\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> .gitignore                      # Git ignored files\n\u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f4c4.png?ssl=1\" alt=\"\ud83d\udcc4\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> pyproject.toml              # Project configuration (Poetry, setuptools)\n\u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f4c4.png?ssl=1\" alt=\"\ud83d\udcc4\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> uv.lock                         # UV file\n\u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f4c4.png?ssl=1\" alt=\"\ud83d\udcc4\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> README.md               # Main project documentation\n\u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f4c4.png?ssl=1\" alt=\"\ud83d\udcc4\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> LICENSE                     # Project license\n\u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f4c4.png?ssl=1\" alt=\"\ud83d\udcc4\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> Makefile                       # Automates common tasks\n\u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f4c4.png?ssl=1\" alt=\"\ud83d\udcc4\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> DockerFile                   # Automates common tasks\n\u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f4c2.png?ssl=1\" alt=\"\ud83d\udcc2\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> .github\/                        # GitHub Actions workflows (CI\/CD)\n\u2502   \u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f4c2.png?ssl=1\" alt=\"\ud83d\udcc2\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> actions\/               \n\u2502   \u2514\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f4c2.png?ssl=1\" alt=\"\ud83d\udcc2\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> workflows\/<\/code><\/pre>\n<p class=\"wp-block-paragraph\">On the other hand, the <strong>src layout<\/strong> (src is the contraction of source) organizes the source code inside a dedicated <code>src\/<\/code> directory, preventing accidental imports from the working directory and ensuring a clear separation between source files and other project components like tests or configuration files. This layout is ideal for large projects, libraries, and production-ready applications as it enforces proper package installation and avoids import conflicts.<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f4c2.png?ssl=1\" alt=\"\ud83d\udcc2\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> my-project\/\n\u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f4c2.png?ssl=1\" alt=\"\ud83d\udcc2\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> src\/                              # Main source code\n\u2502   \u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f4c2.png?ssl=1\" alt=\"\ud83d\udcc2\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> my_project\/            # Main package\n\u2502   \u2502   \u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f40d.png?ssl=1\" alt=\"\ud83d\udc0d\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> __init__.py        # Makes the folder a package\n\u2502   \u2502   \u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f40d.png?ssl=1\" alt=\"\ud83d\udc0d\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> main.py             # Main entry point (if needed)\n\u2502   \u2502   \u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f40d.png?ssl=1\" alt=\"\ud83d\udc0d\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> module1.py       # Example module\n\u2502   \u2502   \u2514\u2500\u2500 ...\n\u2502   \u2502   \u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f4c2.png?ssl=1\" alt=\"\ud83d\udcc2\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> utils\/                  # Utility functions\n\u2502   \u2502   \u2502   \u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f40d.png?ssl=1\" alt=\"\ud83d\udc0d\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> __init__.py     \n\u2502   \u2502   \u2502   \u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f40d.png?ssl=1\" alt=\"\ud83d\udc0d\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> data_utils.py  # data functions\n\u2502   \u2502   \u2502   \u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f40d.png?ssl=1\" alt=\"\ud83d\udc0d\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> io_utils.py      # Input\/output functions\n\u2502   \u2502   \u2502   \u2514\u2500\u2500 ...\n\u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f4c2.png?ssl=1\" alt=\"\ud83d\udcc2\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> tests\/                             # Unit tests\n\u2502   \u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f40d.png?ssl=1\" alt=\"\ud83d\udc0d\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> test_module1.py     \n\u2502   \u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f40d.png?ssl=1\" alt=\"\ud83d\udc0d\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> test_module2.py     \n\u2502   \u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f40d.png?ssl=1\" alt=\"\ud83d\udc0d\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> conftest.py              # Pytest configurations\n\u2502   \u2514\u2500\u2500 ...\n\u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f4c2.png?ssl=1\" alt=\"\ud83d\udcc2\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> docs\/                            # Documentation\n\u2502   \u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f4c4.png?ssl=1\" alt=\"\ud83d\udcc4\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> index.md                \n\u2502   \u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f4c4.png?ssl=1\" alt=\"\ud83d\udcc4\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> architecture.md         \n\u2502   \u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f4c4.png?ssl=1\" alt=\"\ud83d\udcc4\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> installation.md         \n\u2502   \u2514\u2500\u2500 ...                     \n\u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f4c2.png?ssl=1\" alt=\"\ud83d\udcc2\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> notebooks\/                   # Jupyter Notebooks for exploration\n\u2502   \u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f4c4.png?ssl=1\" alt=\"\ud83d\udcc4\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> exploration.ipynb       \n\u2502   \u2514\u2500\u2500 ...                     \n\u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f4c2.png?ssl=1\" alt=\"\ud83d\udcc2\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> scripts\/                         # Standalone scripts (ETL, data processing)\n\u2502   \u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f40d.png?ssl=1\" alt=\"\ud83d\udc0d\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> run_pipeline.py         \n\u2502   \u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f40d.png?ssl=1\" alt=\"\ud83d\udc0d\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> clean_data.py           \n\u2502   \u2514\u2500\u2500 ...                     \n\u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f4c2.png?ssl=1\" alt=\"\ud83d\udcc2\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> data\/                            # Raw or processed data (if applicable)\n\u2502   \u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f4c2.png?ssl=1\" alt=\"\ud83d\udcc2\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> raw\/                    \n\u2502   \u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f4c2.png?ssl=1\" alt=\"\ud83d\udcc2\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> processed\/\n\u2502   \u2514\u2500\u2500 ....                                 \n\u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f4c4.png?ssl=1\" alt=\"\ud83d\udcc4\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> .gitignore                      # Git ignored files\n\u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f4c4.png?ssl=1\" alt=\"\ud83d\udcc4\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> pyproject.toml              # Project configuration (Poetry, setuptools)\n\u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f4c4.png?ssl=1\" alt=\"\ud83d\udcc4\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> uv.lock                         # UV file\n\u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f4c4.png?ssl=1\" alt=\"\ud83d\udcc4\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> README.md               # Main project documentation\n\u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f40d.png?ssl=1\" alt=\"\ud83d\udc0d\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> setup.py                       # Installation script (if applicable)\n\u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f4c4.png?ssl=1\" alt=\"\ud83d\udcc4\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> LICENSE                     # Project license\n\u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f4c4.png?ssl=1\" alt=\"\ud83d\udcc4\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> Makefile                       # Automates common tasks\n\u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f4c4.png?ssl=1\" alt=\"\ud83d\udcc4\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> DockerFile                   # To create Docker image\n\u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f4c2.png?ssl=1\" alt=\"\ud83d\udcc2\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> .github\/                        # GitHub Actions workflows (CI\/CD)\n\u2502   \u251c\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f4c2.png?ssl=1\" alt=\"\ud83d\udcc2\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> actions\/               \n\u2502   \u2514\u2500\u2500 <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/1f4c2.png?ssl=1\" alt=\"\ud83d\udcc2\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\"> workflows\/<\/code><\/pre>\n<p class=\"wp-block-paragraph\">Choosing between these layouts depends on the project\u2019s complexity and long-term goals. For production-quality code, the <code>src\/<\/code> layout is often recommended, whereas the flat layout works well for simple or short-lived projects.<\/p>\n<p class=\"wp-block-paragraph\">You can imagine different templates that are better adapted to your use case. It is important that you maintain the modularity of your project. Do not hesitate to create subdirectories and to group together scripts with similar functionalities and separate those with different uses. A good code structure ensures readability, maintainability, scalability and reusability and helps to identify and correct errors efficiently.<\/p>\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.cookiecutter.io\/\">Cookiecutter<\/a> is an open-source tool for generating preconfigured project structures from templates. It is particularly useful for ensuring the coherence and organization of projects, especially in Python, by applying good practices from the outset. The flat layout and src layout can be initiate using a UV <a href=\"https:\/\/github.com\/fpgmaas\/cookiecutter-uv\">tool<\/a>. <\/p>\n<\/blockquote>\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-dotted\">\n<h2 class=\"wp-block-heading\">The SOLID principles<\/h2>\n<p class=\"wp-block-paragraph\">SOLID programming is an essential approach to software development based on five basic principles for improving code quality, maintainability and scalability. These principles provide a clear framework for developing robust, flexible systems. By following the <a href=\"https:\/\/towardsdatascience.com\/tag\/solid-principles\/\" title=\"Solid Principles\">Solid Principles<\/a>, you reduce the risk of complex dependencies, make testing easier and ensure that applications can evolve more easily in the face of change. Whether you are working on a single project or a large-scale application, mastering SOLID is an important step towards adopting object-oriented programming best practices.<\/p>\n<h3 class=\"wp-block-heading\"><strong>S\u200a\u2014\u200aSingle Responsibility Principle (SRP)<\/strong><\/h3>\n<p class=\"wp-block-paragraph\">The principle of single responsibility means that a class\/function can only manage one thing. This means that it only has one reason to change. This makes the code more maintainable and easier to read. A class\/function with multiple responsibilities is difficult to understand and often a source of errors.<\/p>\n<p class=\"wp-block-paragraph\"><strong>Example:<\/strong><\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\"># Violates SRP\nclass MLPipeline:\n\u00a0 \u00a0 def __init__(self, df: pd.DataFrame, target_column: str):\n\u00a0 \u00a0 \u00a0 \u00a0 self.df = df\n\u00a0 \u00a0 \u00a0 \u00a0 self.target_column = target_column\n\u00a0 \u00a0 \u00a0 \u00a0 self.scaler = StandardScaler()\n\u00a0 \u00a0 \u00a0 \u00a0 self.model = RandomForestClassifier()\n\u00a0 \u00a0 \u00a0 \u00a0 def preprocess_data(self):\n\u00a0 \u00a0 \u00a0 \u00a0 self.df.fillna(self.df.mean(), inplace=True)\u00a0 # Handle missing values\n\u00a0 \u00a0 \u00a0 \u00a0 X = self.df.drop(columns=[self.target_column])\n\u00a0 \u00a0 \u00a0 \u00a0 y = self.df[self.target_column]\n\u00a0 \u00a0 \u00a0 \u00a0 X_scaled = self.scaler.fit_transform(X)\u00a0 # Feature scaling\n\u00a0 \u00a0 \u00a0 \u00a0 return X_scaled, y\n\u00a0 \u00a0 \u00a0 \u00a0 def train_model(self):\n\u00a0 \u00a0 \u00a0 \u00a0 X, y = self.preprocess_data()\u00a0 # Data preprocessing inside model training\n\u00a0 \u00a0 \u00a0 \u00a0 self.model.fit(X, y)\n\u00a0 \u00a0 \u00a0 \u00a0 print(\"Model training complete.\")<\/code><\/pre>\n<p class=\"wp-block-paragraph\">Here, the Report class has two responsibilities: Generate content and save the file.<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\"># Follows SRP\nclass DataPreprocessor:\n\u00a0 \u00a0 def __init__(self):\n\u00a0 \u00a0 \u00a0 \u00a0 self.scaler = StandardScaler()\n\u00a0 \u00a0 \u00a0 \u00a0 def preprocess(self, df: pd.DataFrame, target_column: str):\n\u00a0 \u00a0 \u00a0 \u00a0 df = df.copy()\n\u00a0 \u00a0 \u00a0 \u00a0 df.fillna(df.mean(), inplace=True)\u00a0 # Handle missing values\n\u00a0 \u00a0 \u00a0 \u00a0 X = df.drop(columns=[target_column])\n\u00a0 \u00a0 \u00a0 \u00a0 y = df[target_column]\n\u00a0 \u00a0 \u00a0 \u00a0 X_scaled = self.scaler.fit_transform(X)\u00a0 # Feature scaling\n\u00a0 \u00a0 \u00a0 \u00a0 return X_scaled, y\n\n\nclass ModelTrainer:\n\u00a0 \u00a0 def __init__(self, model):\n\u00a0 \u00a0 \u00a0 \u00a0 self.model = model\n\u00a0 \u00a0 \u00a0 \u00a0 def train(self, X, y):\n\u00a0 \u00a0 \u00a0 \u00a0 self.model.fit(X, y)\n\u00a0 \u00a0 \u00a0 \u00a0 print(\"Model training complete.\")<\/code><\/pre>\n<h3 class=\"wp-block-heading\"><strong>O\u200a\u2014\u200aOpen\/Closed Principle (OCP)<\/strong><\/h3>\n<p class=\"wp-block-paragraph\">The open\/close principle means that a class\/function must be open to extension, but closed to modification. This makes it possible to add functionality without the risk of breaking existing code.<\/p>\n<p class=\"wp-block-paragraph\">It is not easy to develop with this principle in mind, but a good indicator for the main developer is to see more and more additions (+) and fewer and fewer removals (-) in the merge requests during project development.<\/p>\n<h3 class=\"wp-block-heading\"><strong>L\u200a\u2014\u200aLiskov Substitution Principle (LSP)<\/strong><\/h3>\n<p class=\"wp-block-paragraph\">The Liskov substitution principle states that a subordinate class can replace its parent class without changing the behavior of the program, ensuring that the subordinate class meets the expectations defined by the base class. It limits the risk of unexpected errors.<\/p>\n<p class=\"wp-block-paragraph\"><strong>Example :<\/strong><\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\"># Violates LSP\nclass Rectangle:\n    def __init__(self, width, height):\n        self.width = width\n        self.height = height\n\n    def area(self):\n        return self.width * self.height\n\n\nclass Square(Rectangle):\n    def __init__(self, side):\n        super().__init__(side, side)\n# Changing the width of a square violates the idea of a square.<\/code><\/pre>\n<p class=\"wp-block-paragraph\">To respect the LSP, it is better to avoid this hierarchy and use independent classes:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">class Shape:\n    def area(self):\n        raise NotImplementedError\n\n\nclass Rectangle(Shape):\n    def __init__(self, width, height):\n        self.width = width\n        self.height = height\n\n    def area(self):\n        return self.width * self.height\n\n\nclass Square(Shape):\n    def __init__(self, side):\n        self.side = side\n\n    def area(self):\n        return self.side * self.side<\/code><\/pre>\n<h3 class=\"wp-block-heading\"><strong>I\u200a\u2014\u200aInterface Segregation Principle (ISP)<\/strong><\/h3>\n<p class=\"wp-block-paragraph\">The principle of interface separation states that several small classes should be built instead of one with methods that cannot be used in certain cases. This reduces unnecessary dependencies.<\/p>\n<p class=\"wp-block-paragraph\"><strong>Example:<\/strong><\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\"># Violates ISP\nclass Animal:\n    def fly(self):\n        raise NotImplementedError\n\n    def swim(self):\n        raise NotImplementedError<\/code><\/pre>\n<p class=\"wp-block-paragraph\">It is better to split the class <code>Animal<\/code> into several classes:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\"># Follows ISP\nclass CanFly:\n\u00a0 \u00a0 def fly(self):\n\u00a0 \u00a0 \u00a0 \u00a0 raise NotImplementedError\n\n\nclass CanSwim:\n\u00a0 \u00a0 def swim(self):\n\u00a0 \u00a0 \u00a0 \u00a0 raise NotImplementedError\n\n\nclass Bird(CanFly):\n\u00a0 \u00a0 def fly(self):\n\u00a0 \u00a0 \u00a0 \u00a0 print(\"Flying\")\n\n\nclass Fish(CanSwim):\n\u00a0 \u00a0 def swim(self):\n\u00a0 \u00a0 \u00a0 \u00a0 print(\"Swimming\")<\/code><\/pre>\n<h3 class=\"wp-block-heading\">D\u200a\u2014\u200aDependency Inversion Principle (DIP)<\/h3>\n<p class=\"wp-block-paragraph\">The Dependency Inversion Principle means that a class must depend on an abstract class and not on a concrete class. This reduces the connections between the classes and makes the code more modular.<\/p>\n<p class=\"wp-block-paragraph\"><strong>Example:<\/strong><\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\"># Violates DIP\nclass Database:\n    def connect(self):\n        print(\"Connecting to database\")\n\n\nclass UserService:\n    def __init__(self):\n        self.db = Database()\n\n    def get_users(self):\n        self.db.connect()\n        print(\"Getting users\")<\/code><\/pre>\n<p class=\"wp-block-paragraph\">Here, the attribute db of UserService depends on the class Database. To respect the DIP, db has to depend on an abstract class.<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-\"># Follows DIP\nclass DatabaseInterface:\n\u00a0 \u00a0 def connect(self):\n\u00a0 \u00a0 \u00a0 \u00a0 raise NotImplementedError\n\n\nclass MySQLDatabase(DatabaseInterface):\n\u00a0 \u00a0 def connect(self):\n\u00a0 \u00a0 \u00a0 \u00a0 print(\"Connecting to MySQL database\")\n\n\nclass UserService:\n\u00a0 \u00a0 def __init__(self, db: DatabaseInterface):\n\u00a0 \u00a0 \u00a0 \u00a0 self.db = db\n\n\u00a0 \u00a0 def get_users(self):\n\u00a0 \u00a0 \u00a0 \u00a0 self.db.connect()\n\u00a0 \u00a0 \u00a0 \u00a0 print(\"Getting users\")\n\n\n# We can easily change the used database.\ndb = MySQLDatabase()\nservice = UserService(db)\nservice.get_users()<\/code><\/pre>\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-dotted\">\n<h2 class=\"wp-block-heading\"><strong>PEP standards<\/strong><\/h2>\n<p class=\"wp-block-paragraph\">PEPs (Python Enhancement Proposals) are technical and informative documents that describe new features, language improvements or guidelines for the Python community. Among them, PEP 8, which defines style conventions for Python code, plays a fundamental role in promoting readability and consistency in projects.<\/p>\n<p class=\"wp-block-paragraph\">Adopting the PEP standards, especially PEP 8, not only ensures that the code is understandable to other developers, but also that it conforms to the standards set by the community. This facilitates collaboration, re-reads and long-term maintenance.<\/p>\n<p class=\"wp-block-paragraph\">In this article, I present the most important aspects of the PEP standards, including:<\/p>\n<ul class=\"wp-block-list\">\n<li class=\"wp-block-list-item\">Style Conventions (PEP 8): Indentations, variable names and import organization.<\/li>\n<li class=\"wp-block-list-item\">Best practices for documenting code (PEP 257).<\/li>\n<li class=\"wp-block-list-item\">Recommendations for writing typed, maintainable code (PEP 484 and PEP 563).<\/li>\n<\/ul>\n<p class=\"wp-block-paragraph\">Understanding and applying these standards is essential to take full advantage of the Python ecosystem and contribute to professional quality projects.<\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-dotted\">\n<h3 class=\"wp-block-heading\"><strong>PEP 8<\/strong><\/h3>\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/peps.python.org\/pep-0008\/\">This documentation<\/a> is about coding conventions to standardize the code, and there exists a lot of documentation about the PEP 8. I will not show all recommendation in this posts, only those that I judge essential when I review a code<\/p>\n<h4 class=\"wp-block-heading\"><strong>Naming conventions<\/strong><\/h4>\n<p class=\"wp-block-paragraph\">Variable, function and module names should be in lower case, and use underscore to separate words. This typographical convention is called snake_case.<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">my_variable\nmy_new_function()\nmy_module<\/code><\/pre>\n<p class=\"wp-block-paragraph\">Constances are written in capital letters and set at the beginning of the script (after the imports):<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">LIGHT_SPEED\nMY_CONSTANT<\/code><\/pre>\n<p class=\"wp-block-paragraph\">Finally, class names and exceptions use the CamelCase format (a capital letter at the beginning of each word). Exceptions must contain an Error at the end.<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">MyGreatClass\nMyGreatError<\/code><\/pre>\n<p class=\"wp-block-paragraph\">Remember to give your variables names <strong>that make sense<\/strong>! Don\u2019t use variable names like v1, v2, func1, i, toto\u2026<\/p>\n<p class=\"wp-block-paragraph\">Single-character variable names are permitted for loops and indexes:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">my_list = [1, 3, 5, 7, 9, 11]\nfor i in range(len(my_liste)):\n\u00a0 \u00a0 print(my_list[i])<\/code><\/pre>\n<p class=\"wp-block-paragraph\">A more \u201cpythonic\u201d way of writing, to be preferred to the previous example, gets rid of the i index:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">my_list = [1, 3, 5, 7, 9, 11]\nfor element in my_list:\n    print(element )<\/code><\/pre>\n<h3 class=\"wp-block-heading\">Spaces management<\/h3>\n<p class=\"wp-block-paragraph\">It is recommended surrounding operators (+, -, *, \/, \/\/, %, ==, !=, &gt;, not, in, and, or, \u2026) with a space before AND after:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\"># recommended code:\nmy_variable = 3 + 7\nmy_text = \"mouse\"\nmy_text == my_variable\n\n# not recommended code:\nmy_variable=3+7\nmy_text=\"mouse\"\nmy_text== ma_variable<\/code><\/pre>\n<p class=\"wp-block-paragraph\">You can\u2019t add several spaces around an operator. On the other hand, there are no spaces inside square brackets, braces or parentheses:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\"># recommended code:\nmy_list[1]\nmy_dict{\"key\"}\nmy_function(argument)\n\n# not recommended code:\nmy_list[ 1 ]\nmy_dict{ \"key\" }\nmy_function( argument )<\/code><\/pre>\n<p class=\"wp-block-paragraph\">A space is recommended after the characters \u201c:\u201d and \u201c,\u201d, but not before:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\"># recommended code:\nmy_list= [1, 2, 3]\nmy_dict= {\"key1\": \"value1\", \"key2\": \"value2\"}\nmy_function(argument1, argument2)\n\n# not recommended code:\nmy_list= [1 , 2 , 3]\nmy_dict= {\"key1\":\"value1\", \"key2\":\"value2\"}\nmy_function(argument1 , argument2)<\/code><\/pre>\n<p class=\"wp-block-paragraph\">However, when indexing lists, we don\u2019t put a space after the \u201c:\u201d:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">my_list= [1, 3, 5, 7, 9, 1]\n\n# recommended code:\nmy_list[1:3]\nmy_list[1:4:2]\nmy_list[::2]\n\n# not recommended code:\nmy_list[1 : 3]\nmy_list[1: 4:2 ]\nmy_list[ : :2]<\/code><\/pre>\n<p class=\"wp-block-paragraph\"><strong>Line length<\/strong><\/p>\n<p class=\"wp-block-paragraph\">For the sake of readability, we recommend writing lines of code no longer than 80 characters long. However, in certain circumstances this rule can be broken, especially if you are working on a Dash project, it may be complicated to respect this recommendation\u00a0<\/p>\n<p class=\"wp-block-paragraph\">The <code><\/code> character can be used to cut lines that are too long.<\/p>\n<p class=\"wp-block-paragraph\">For example:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">my_variable = 3\nif my_variable &gt; 1 and my_variable &lt; 10 \n    and my_variable % 2 == 1 and my_variable % 3 == 0:\n    print(f\"My variable is equal to {my_variable }\")<\/code><\/pre>\n<p class=\"wp-block-paragraph\">Within a parenthesis, you can return to the line without using the  character. This can be useful for specifying the arguments of a function or method when defining or using it:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">def my_function(argument_1, argument_2,\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 argument_3, argument_4):\n\u00a0 \u00a0 return argument_1 + argument_2<\/code><\/pre>\n<p class=\"wp-block-paragraph\">It is also possible to create multi-line lists or dictionaries by skipping a line after a comma:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">my_list = [1, 2, 3,\n          4, 5, 6,\n          7, 8, 9]\nmy_dict = {\"key1\": 13,\n          \"key2\": 42,\n          \"key2\": -10}<\/code><\/pre>\n<h3 class=\"wp-block-heading\"><strong>Blank lines<\/strong><\/h3>\n<p class=\"wp-block-paragraph\">In a script, blank lines are useful for visually separating different parts of the code. It is recommended to leave two blank lines before the definition of a function or class, and to leave a single blank line before the definition of a method (in a class). You can also leave a blank line in the body of a function to separate the logical sections of the function, but this should be used sparingly.<\/p>\n<h3 class=\"wp-block-heading\"><strong>Comments<\/strong><\/h3>\n<p class=\"wp-block-paragraph\">Comments always begin with the # symbol followed by a space. They give clear explanations of the purpose of the code and must be synchronized with the code, i.e. if the code is modified, the comments must be too (if applicable). They are on the same indentation level as the code they comment on. Comments are complete sentences, with a capital letter at the beginning (unless the first word is a variable, which is written without a capital letter) and a period at the end.I strongly recommend writing comments in English and it is important to be consistent between the language used for comments and the language used to name variables. Finally, Comments that follow the code on the same line should be avoided wherever possible, and should be separated from the code by at least two spaces.<\/p>\n<p class=\"wp-block-paragraph\"><strong>Tool to help you<\/strong><\/p>\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/docs.astral.sh\/ruff\/\">Ruff<\/a> is a linter (code analysis tool) and formatter for Python code written in Rust. It combines the advantages of the <strong>flake8 <\/strong>linter and <strong>black <\/strong>and <strong>isort <\/strong>formatting while being faster.<\/p>\n<p class=\"wp-block-paragraph\">Ruff has an extension on the VS Code editor.<\/p>\n<p class=\"wp-block-paragraph\">To check your code you can type:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">ruff check my_modul.py<\/code><\/pre>\n<p class=\"wp-block-paragraph\">But, it is also possible to correct it with the following command:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">ruff format my_modul.py<\/code><\/pre>\n<h3 class=\"wp-block-heading\"><strong>PEP 20<\/strong><\/h3>\n<p class=\"has-text-align-left wp-block-paragraph\"><a href=\"https:\/\/peps.python.org\/pep-0020\/\">PEP 20: The Zen of Python<\/a> is a set of 19 principles written in poetic form. They are more a way of coding than actual guidelines.<\/p>\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"has-text-align-center wp-block-paragraph\">Beautiful is better than ugly.<br \/>Explicit is better than implicit.<br \/>Simple is better than complex.<br \/>Complex is better than complicated.<br \/>Flat is better than nested.<br \/>Sparse is better than dense.<br \/>Readability counts.<br \/>Special cases aren\u2019t special enough to break the rules.<br \/>Although practicality beats purity.<br \/>Errors should never pass silently.<br \/>Unless explicitly silenced.<br \/>In the face of ambiguity, refuse the temptation to guess.<br \/>There should be one\u2013 and preferably only one \u2013obvious way to do it.<br \/>Although that way may not be obvious at first unless you\u2019re Dutch.<br \/>Now is better than never.<br \/>Although never is often better than *right* now.<br \/>If the implementation is hard to explain, it\u2019s a bad idea.<br \/>If the implementation is easy to explain, it may be a good idea.<br \/>Namespaces are one honking great idea \u2014 let\u2019s do more of those!<\/p>\n<\/blockquote>\n<h3 class=\"wp-block-heading\"><strong>PEP 257<\/strong><\/h3>\n<p class=\"wp-block-paragraph\">The aim of <a href=\"https:\/\/peps.python.org\/pep-0257\/\">PEP 257<\/a> is to standardize the use of docstrings.<\/p>\n<p class=\"wp-block-paragraph\"><strong>What is a docstring?<\/strong><\/p>\n<p class=\"wp-block-paragraph\">A docstring is a string that appears as the first instruction after the definition of a function, class or method. A docstring becomes the output of the <code>__doc__<\/code> special attribute of this object.<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">def my_function():\n\u00a0 \u00a0 \"\"\"This is a doctring.\"\"\"\n\u00a0 \u00a0 pass<\/code><\/pre>\n<p class=\"wp-block-paragraph\">And we have:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-\">&gt;&gt;&gt; my_function.__doc__\n&gt;&gt;&gt; 'This is a doctring.'<\/code><\/pre>\n<p class=\"wp-block-paragraph\">We always write a docstring between triple double quote <code>\"\"\"<\/code>.<\/p>\n<h3 class=\"wp-block-heading\"><strong>Docstring on a line<\/strong><\/h3>\n<p class=\"wp-block-paragraph\">Used for simple functions or methods, it must fit on a single line, with no blank line at the beginning or end. The closing quotes are on the same line as opening quotes and there are no blank lines before or after the docstring.<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-\">def add(a, b):\n    \"\"\"Return the sum of a and b.\"\"\"\n    return a + b<\/code><\/pre>\n<p class=\"wp-block-paragraph\">Single-line docstring MUST NOT reintegrate function\/method parameters. Do not do:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-\">def my_function(a, b):\n    \"\"\" my_function(a, b) -&gt; list\"\"\"<\/code><\/pre>\n<h3 class=\"wp-block-heading\"><strong>Docstring on several lines<\/strong><\/h3>\n<p class=\"wp-block-paragraph\">The first line should be a summary of the object being documented. An empty line follows, followed by more detailed explanations or clarifications of the arguments.<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-\">def divide(a, b):\n\u00a0 \u00a0 \"\"\"Divide a byb.\n\n\u00a0 \u00a0 Returns the result of the division. Raises a ValueError if b equals 0.\n\u00a0 \u00a0 \"\"\"\n\u00a0 \u00a0 if b == 0:\n\u00a0 \u00a0 \u00a0 \u00a0 raise ValueError(\"Only Chuck Norris can divide by 0\") return a \/ b<\/code><\/pre>\n<h3 class=\"wp-block-heading\"><strong>Complete Docstring<\/strong><\/h3>\n<p class=\"wp-block-paragraph\">A complete docstring is made up of several parts (in this case, based on the numpydoc standard).<\/p>\n<ol class=\"wp-block-list\">\n<li class=\"wp-block-list-item\">Short description: Summarizes the main functionality.<\/li>\n<li class=\"wp-block-list-item\">Parameters: Describes the arguments with their type, name and role.<\/li>\n<li class=\"wp-block-list-item\">Returns: Specifies the type and role of the returned value.<\/li>\n<li class=\"wp-block-list-item\">Raises: Documents exceptions raised by the function.<\/li>\n<li class=\"wp-block-list-item\">Notes (optional): Provides additional explanations.<\/li>\n<li class=\"wp-block-list-item\">Examples (optional): Contains illustrated usage examples with expected results or exceptions.<\/li>\n<\/ol>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">def calculate_mean(numbers: list[float]) -&gt; float:\n\u00a0 \u00a0 \"\"\"\n\u00a0 \u00a0 Calculate the mean of a list of numbers.\n\n\u00a0 \u00a0 Parameters\n\u00a0 \u00a0 ----------\n\u00a0 \u00a0 numbers : list of float\n\u00a0 \u00a0 \u00a0 \u00a0 A list of numerical values for which the mean is to be calculated.\n\n\u00a0 \u00a0 Returns\n\u00a0 \u00a0 -------\n\u00a0 \u00a0 float\n\u00a0 \u00a0 \u00a0 \u00a0 The mean of the input numbers.\n\n\u00a0 \u00a0 Raises\n\u00a0 \u00a0 ------\n\u00a0 \u00a0 ValueError\n\u00a0 \u00a0 \u00a0 \u00a0 If the input list is empty.\n\n\u00a0 \u00a0 Notes\n\u00a0 \u00a0 -----\n\u00a0 \u00a0 The mean is calculated as the sum of all elements divided by the number of elements.\n\n\u00a0 \u00a0 Examples\n\u00a0 \u00a0 --------\n\u00a0 \u00a0 Calculate the mean of a list of numbers:\n\u00a0 \u00a0 &gt;&gt;&gt; calculate_mean([1.0, 2.0, 3.0, 4.0])\n\u00a0 \u00a0 2.5<\/code><\/pre>\n<h4 class=\"wp-block-heading\"><strong>Tool to help you<\/strong><\/h4>\n<p class=\"wp-block-paragraph\">VsCode\u2019s <a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=njpwerner.autodocstring\">autoDocstring <\/a>extension lets you automatically create a docstring template.<\/p>\n<h3 class=\"wp-block-heading\"><strong>PEP 484<\/strong><\/h3>\n<p class=\"wp-block-paragraph\">In some programming languages, typing is mandatory when declaring a variable. In Python, typing is optional, but strongly recommended. <a href=\"https:\/\/peps.python.org\/pep-0484\/\">PEP 484<\/a> introduces a typing system for Python, annotating the types of variables, function arguments and return values. This PEP provides a basis for improving code readability, facilitating static analysis and reducing errors.<\/p>\n<h3 class=\"wp-block-heading\"><strong>What is typing?<\/strong><\/h3>\n<p class=\"wp-block-paragraph\">Typing consists in explicitly declaring the type (float, string, etc.) of a variable. The typing module provides standard tools for defining generic types, such as Sequence, List, Union, Any, etc.<\/p>\n<p class=\"wp-block-paragraph\">To type function attributes, we use \u201c:\u201d for function arguments and \u201c-&gt;\u201d for the type of what is returned.<\/p>\n<p class=\"wp-block-paragraph\">Here a list of none typing functions:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">def show_message(message):\n\u00a0 \u00a0 print(f\"Message : {message}\")\n\ndef addition(a, b):\n\u00a0 \u00a0 return a + b\n\ndef is_even(n):\n\u00a0 \u00a0 return n % 2 == 0\n\ndef list_square(numbers):\n\u00a0 \u00a0 \u00a0 return [x**2 for x in numbers]\n\ndef reverse_dictionary(d):\n\u00a0 \u00a0 return {v: k for k, v in d.items()}\n\ndef add_element(ensemble, element):\n\u00a0 \u00a0 ensemble.add(element)\n\u00a0 return ensemble<\/code><\/pre>\n<p class=\"wp-block-paragraph\">Now here\u2019s how they should look:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">from typing import List, Tuple, Dict, Set, Any\n\ndef show _message(message: str) -&gt; None:\n\u00a0 \u00a0 print(f\"Message : {message}\")\n\ndef addition(a: int, b: int) -&gt; int:\n\u00a0 \u00a0 return a + b\n\ndef is_even(n: int) -&gt; bool:\n\u00a0 \u00a0 return n % 2 == 0\n\ndef list_square (numbers: List[int]) -&gt; List[int]:\n\u00a0 \u00a0 return [x**2 for x in numbers]\n\ndef reverse_dictionary (d: Dict[str, int]) -&gt; Dict[int, str]:\n\u00a0 \u00a0 return {v: k for k, v in d.items()}\n\ndef add_element(ensemble: Set[int], element: int) -&gt; Set[int]:\n\u00a0 \u00a0 ensemble.add(element)\n\u00a0 \u00a0 return ensemble<\/code><\/pre>\n<h4 class=\"wp-block-heading\"><strong>Tool to help you<\/strong><\/h4>\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=ms-python.mypy-type-checker\">The MyPy extension<\/a> automatically checks whether the use of a variable corresponds to the declared type. For example, for the following function:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-\">def my_function(x: float) -&gt; float:\n    return x.mean()<\/code><\/pre>\n<p class=\"wp-block-paragraph\">The editor will point out that a float has no \u201cmean\u201d attribute.<\/p>\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" data-dominant-color=\"f3ecec\" data-has-transparency=\"true\" style=\"--dominant-color: #f3ecec;\" loading=\"lazy\" decoding=\"async\" width=\"817\" height=\"204\" src=\"https:\/\/i0.wp.com\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/image-25.png?resize=817%2C204&#038;ssl=1\" alt=\"\" class=\"wp-image-598613 has-transparency\" srcset=\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/image-25.png 817w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/image-25-300x75.png 300w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/image-25-768x192.png 768w\" sizes=\"auto, (max-width: 817px) 100vw, 817px\"><figcaption class=\"wp-element-caption\">Image from author<\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\">The benefit is twofold: you\u2019ll know whether the declared type is the right one and whether the use of this variable corresponds to its type.<\/p>\n<p class=\"wp-block-paragraph\">In the above example, x must be of a type that has a mean() method (e.g. np.array).<\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-dotted\">\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n<p class=\"wp-block-paragraph\">In this article, we have looked at the most important principles for creating clean Python production code. A solid architecture, adherence to SOLID principles, and compliance with PEP recommendations (at least the four discussed here) are essential for ensuring code quality. The desire for beautiful code is not (just) coquetry. It standardizes development practices and makes teamwork and maintenance much easier. There\u2019s nothing more frustrating than spending hours (or even days) reverse-engineering a program, deciphering poorly written code before you\u2019re finally able to fix the bugs. By applying these best practices, you ensure that your code remains clear, scalable, and easy for any developer to work with in the future.<\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-dotted\">\n<h2 class=\"wp-block-heading\"><strong>References<\/strong><\/h2>\n<p class=\"wp-block-paragraph\">1. <a href=\"https:\/\/packaging.python.org\/en\/latest\/discussions\/src-layout-vs-flat-layout\/\">src layout vs flat layout<\/a><\/p>\n<p class=\"wp-block-paragraph\">2. <a href=\"https:\/\/realpython.com\/solid-principles-python\/\">SOLID principles<\/a><\/p>\n<p class=\"wp-block-paragraph\">3. <a href=\"https:\/\/peps.python.org\/pep-0000\/\">Python Enhancement Proposals index<\/a><\/p>\n<p>The post <a href=\"https:\/\/towardsdatascience.com\/data-science-from-school-to-work-part-ii\/\">Data Science: From School to Work, Part II<\/a> appeared first on <a href=\"https:\/\/towardsdatascience.com\/\">Towards Data Science<\/a>.<\/p>\n<\/div>\n<p> \t<BR><br \/>\n <BR><\/BR><br \/>\n    Vincent Margot<br \/>\n \t<BR><br \/>\n<BR><\/BR><br \/>\n<a href=\"https:\/\/towardsdatascience.com\/data-science-from-school-to-work-part-ii\/\">Go to original source<\/a><br \/>\n \t<BR><br \/>\n <BR><\/BR><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Data Science: From School to Work, Part II In my previous article, I highlighted the importance of effective project management in Python development. Now, let\u2019s shift our focus to the code itself and explore how to write clean, maintainable code\u200a\u2014\u200aan essential practice in professional and collaborative environments.\u00a0 Readability &amp; Maintainability: Well-structured code is easier to [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[62,1908,83,160,157,1909,158],"tags":[368,544,102],"class_list":["post-2185","post","type-post","status-publish","format-standard","hentry","category-aimldsaimlds","category-clean-code","category-data-science","category-programming","category-python","category-solid-principles","category-tips-and-tricks","tag-code","tag-project","tag-python"],"_links":{"self":[{"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/posts\/2185"}],"collection":[{"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/comments?post=2185"}],"version-history":[{"count":0,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/posts\/2185\/revisions"}],"wp:attachment":[{"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/media?parent=2185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/categories?post=2185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/tags?post=2185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}