Coding Methodology in Python
Acknowledgement: This post is based mainly on Dr. Thai Ha Nguyen’s presentation and slides and partly on on TA, STA, and peers’ responses from the course AIO2025 offered by AI VIETNAM.
Relevance: The audience of this lecture were those who have learned Python for AI 102, had some coding experience, and known a bit of AI knowledge. Absolute beginners are encouraged to read the material and repeatedly practice coding without AI assistance to strengthen their coding and code-reading skills.
Be always intentional when writing and managing code. Learn logics. Don’t overuse AI assistant when starting.
-Dr. Thai Ha Nguyen
In today’s post, we’ll explore clean code principles for Python and standard project structures. We’ll mainly mention common concepts without particular examples. Details may be updated in the future.
Roadmap for learning Data Science
Learners should follow the core process of managing a Machine Learning projects.
- Planning
- Proof of Concept (PoC)
- Implementation
- Deployment
Learners can work on some practical projects:
- Sales Forecasting and Demand Prediction and Applied XAI
- Inventory Optimization Tools
- Customer Segmentation for Marketing Strategy using Unsupervised Learning.
Introduction to Clean Code and PEP-8
Our goal of this section is to comprehend and be able to apply clean code principles and PEP-8 so that our source code is readable, efficient, and maintainable.
Writing code is like building a house. A house can still be completed by putting things together but it’s likely not to last long without a blueprint. The same goes for code: without a clear architecture, solid logic, and clean design, it’s prone to errors, hard to understand, and difficult to maintain.
Anything that can go wrong will go wrong.
Edsel Murphey’s Law
Clean Code
The core principles of clean code are:
- Readable: “future” we and others should easily understand its purpose and how it works.
- Maintainable: code should be easy to be modifed, extended, and reused without making errors.
- Testable: code should be easy to write and automate unit tests.
- Extensible: code should be easy to add new features without changing existing code.
You may wonder, How important is clean code in practice?
When working on a project with colleagues, like in a company, we must read each other’s code which can be thousands of lines. This is when clean code comes in useful. Clean code is not only favored in theory but a key factor to boost work efficiency and improve team collaboration, and lower long-term maintenance cost. To be specific:
- Minimize bugs: clean code helps identify and prevent potential errors and lower debug and repair time.
- Improve team work: clean code helps team members understand and work with each other’s code easily.
- Save time lone-term: clean code may take time and effort to build in the first place but will save time during maintenance and development. This is to avoid technical debt.
- Extend easily: clean code makes it esy to add new features without modification.
Some examples for clean code are:
- good format: space out for code to be clear
- meaningful names: variables, functions, and classes should have names meaningful to the program.
Nevertheless, there are situations where we don’t have to clean code:
- Short-live/Emergency scripts: when the system encounters a serious accident, we need to handle the situation first, and refactor afterwards.
- Exploratory code: clean code is unnecessay when learning a concept.
- Sample code: we can skip clean code when checking some idea in a short time and can always refactor if implementing the idea.
- Only-current-you code: Only current-you need it!
PEP-8
PEP-8 is short for Python Enhancement Proposal 8 introduced by Guido van Rossum—Python’s inventor. It is the official format for Python code, helps preserving code unity and eligibility for projects.
Naming Convention
| Variable/Function | Class | Constant |
|---|---|---|
| snake_case | PascalCase or CamelCase | UPPER_CASE |
Indentation and Whitespace
| Indentation | Whitespace |
|---|---|
| four white spaces | add whitespaces next to operations a = b + c instead of a=b+c |
no whitespace before a comma f(a, b) instead of f(a , b) | |
no whitespaces before and after parentheses, brackets, or braces list([1, 2, 3]) instead of list( [ 1, 2, 3 ] ) |
Standard Code Line
| Length of a Line | Line Break for Continuation |
|---|---|
| Source code: 79 characters | Explicit: backslash \ |
| Docstring/Comment: 72 characters | Implicit: parentheses (), brackets [], braces {} |
| Purpose: - Easier to scroll up/down than left/right - Easy to read on all screens | Before a comma or operator |
Standard Import
We should import libraries and modules in different blocks based on their origin. Commonly, there are three blocks in the following order:
- Python libraries:
os,sys,math - Third-party packages:
requests,numpy,pandas - Internal modules:
from myproject.models import User.
Line Break and Class/Function Structure
| Function | Class | |
|---|---|---|
| Structure |
|
|
| Usage |
|
|
Documentation: Docstring and Annotation Basics
| Docstring | Annotations and Type Hints | Documentation for Projects |
|---|---|---|
Docstring is surrounded by ''' or """. Some styles to follow:
| A annotation is an optional support to check data types and specify purposes. They are called type hints.
|
|
Checking Tools
flake8: check for redundancy black . : format all black file_name: format file_name pyplint: analyze more than black and grade our code mypy: check error
Standard Structure for Projects
Similar to writing Python code, it’s essential to organize a Python project in order to build a healthy development cycle. There may be a few ways to construct a standard repository structure. Below is one of them. Using files described in the root directory ensures a project is efficiently managed and straightforward for contributors and readers.
- Root directory
project_name/- README.md: outlines a project’s goal, setup instructions, usage examples, and contributions
- requirements.txt: lists project dependencies
- setup.py: defines and installs packages
- pyproject.toml (preferred): specifies build íntructions, project dependencies, and project metadata (license, author, descriptions, etc)
- .gitignore: contains files and folders Git should ignore/track such as temporary files, log files, or personalfiles.
- Source code
project_name/src/- Main modules
- Main packages
- Tests
project_name/tests/- unittest
- pytest
- Documentation
project_name/docs- API
- Usage
- Resources
project_name/resources/- Static resources
- Templates
- Assets
It’s adequate and fast to build our repository structure using Command-Line Interface generator (CLIgen) such as Cookiecutter Data Science.
Pythonic Style
Our goal of this section is to familiarize ourselves with standard Python programming and utilize its unique features efficiently.
There are 19 guiding principles for Python written by Tim Peters in PEP 20. You can check them out with import this.
We apply Pythonic style to utilize unique features and characteristics of Python. It not only preserves clean code and PEP-8 but adheres to Python’s philosophy. Let’s take a look at some Pythonic cases.
- Use list, dict, and set comprehensions to create a sequence.
- All on one line makes it faster to create.
- It’s very readable one you’re familiar with the syntax.
dict_comprehension = {i: i * i for i in range(7)}
- Context manager
withstatement to open and close a file automatically.-
withprevents memory leak, releases resources correctly, and is safer for exception handling. - It’s commonly used for file management, network database, lock thread, benchmark, transaction database, and so on.
with open("file_name.txt", "r") as file: content = file.read()
-
-
@contextmanageris a decorator.-
@contextmanagercomes fromcontextlib. - It temporarily changes the system configuration and recovers once done.
- It measures operation time.
- It manages network database, Redis, or API.
- It creates a temporaty environment for testing.
from contextlib import contextmanager @contextmanager def file_reader(filename): f = open(filename, "r") try: # perform with statement yield f finally: # clean up everything and close the file f.close() # use context manager with file_reader("file_name.txt") as file: print(file.read())
-
-
@propertyis a decorator, making a method become an attribute.- No need parentheses
()in the end if there is no argument.
- No need parentheses
- Comparison and condition:
| Technique | Pythonic | Non-Pythonic |
|---|---|---|
is None | Check an object's identity: w is not the None object.
| Check an object's value: w has value of None.
|
| Boolean | Utilize truthfulness.
| Too lengthy.
|
Emptiness:
| Utilize truthfulness.
| Too lengthy.
|
in | Check existence directly.
| Too complicated and time-consuming.
|
| Chain comparison | Logical.
| Too lengthy.
|
Principles for Writing Neat Code
Our goal of this section is to understand various rules and designs to build high-quality code.
| Principle | Benifits | Usage |
|---|---|---|
| DRY (Don't Repeat Yourself) |
|
|
| YAGNI (You Aren't Gonna Need It) |
|
|
| KISS (Keep It Simple, Stupid) |
|
|
| Defensive Programming |
|
|
| Error Handling |
|
|
| Separation of Concerns |
|
|
As mentioned logging can be used to catch bugs in the program. The purposes of using logging and print may be mistaken for each other. logging is a feature to journal code professionally with different levels while print is to catch errors quickly but have its own drawbacks.
| Logging | |
|---|---|
| Easy to use | Flexible configuration |
| No classified levels | Classified levels: DEBUG, INFO, WARNING, etc |
| Hard to switch off | Configurable on/off switch |
| No add-ons | Add-ons: time, file, line |
| Hard output management | Direct to file, email, etc |
SOLID Rule and Design Patterns (Advanced) - to be updated
Our goal of this section is to learn how to organize projects, divide into modules, and write highly extensible code.
Single Responsibility
Open/Closed
Liskov Substitution
Interface Segregation
Dependency Inversion
4
Pro tip: Use AI to create problems, with hint docstrings, and test cases. Export it to colab files and start practicing. Once you’ve done, ask AI to analyze your solutions and provide answer keys and learned lessons. You can use lecture slides as an attachment to your question.
A sample prompt: “Create twenty Python programming problems from easy to diffcult. They should adhere to the lesson content in slides attached. They should have hint docstrings and test cases for each problem and learners need to complete them. After that, provide solutions separately and give lessons learned from each problem.”
https://colab.research.google.com/drive/1hwdu33Mo1wxoOcNWV80qSy2YWVodtyeL?usp=sharing
FAQ
Here are some good questions asked during class. The answers to these questions came from either some admin, TA, STA, or members of AIO2025.
| Questions | Answers |
|---|---|
| Resources | Mathematics for Machine Learning, Pattern Recognition and Machine Learning, and https://colab.research.google.com/drive/14H2oCxe06k28z30SKhdXMGxvtNneXGhs?usp=drive_link#scrollTo=QaAlW_n1Qirb |
| What is CI/CD? | CI/CD is short for Continuous Integration and Continuous Delivery/Deployment, respectively. This is a DepOvs method to automate and speed up software development. |
| What is Cloud Computing? | Cloud Computing is the delivery of computing services including serves, storage, databases, networking over the Internet. |
| What is Explainable AI? | The process is input -> complex AI model -> explaining the model and its results -> users understand the decision -> output. Check out XAI Overview. |
| What is a data engineer, data analyst, and data scientist? | DE manages data, DA retrieves basic information using statistic models, and DS uses ML/DL to investigate larger datasets. |
| What does refactor mean? | Refactoring code is the process of changing structure of source code without changing the program’s primary behavior. |
| What do I use requirement.txt for? | You can remove or update a library by just updating this file and pushing to the repository. |
| Resources for more complex Pythonic techniques like tensor/dataframe/array | You can utilize vectorized operations of libraries. Documentation can be found athere. |
| What are Redis and API? | Redis is cache database, supporting quick computations when needed but storing them is inconvenient. API (Application Programming Interface) is a network/collection of rules, methods, and data formats a software or service provides so that other programs can call and interact with. |
| How do I check all code before pushing to github? | pre commit git hook |
Enjoy Reading This Article?
Here are some more articles you might like to read next: