SoFunction
Updated on 2025-04-08

How to extract public modules and avoid duplication of code

Modular programming is the key to improving code reusability and maintainability. In Python, a module is a file containing Python code (usually.pyending). By organizing the code into the module reasonably, duplication of code can be avoided, making the project easier to manage and scale.

1. Create and use modules

Define module:A module is a file containing function, class, or variable definitions. For example, create amy_utils.pydocument:

# my_utils.py
def add(x, y):
  """
   Add two numbers together.
   """
  return x + y

def multiply(x, y):
  """
   Multiply two numbers.
   """
  return x * y

Import module:In other Python files, useimportStatements to use functions in the module:

# 
import my_utils

result_add = my_utils.add(5, 3)
result_multiply = my_utils.multiply(5, 3)

print(f"5 + 3 = {result_add}") # Output: 5 + 3 = 8print(f"5 * 3 = {result_multiply}") # Output: 5 * 3 = 15

Can also be usedfrom ... import ...Statement imports specific functions or classes in modules:

# 
from my_utils import add

result = add(5, 3)
print(f"5 + 3 = {result}") # Output: 5 + 3 = 8

2. Organization module structure

Folder structure:Use folder organization modules to make the project structure clearer. For example:

my_project/
├──
├── utils/
│   ├── __init__.py
│   ├── string_utils.py
│   └── math_utils.py

: Main program file

utils/: Folder that stores common modules

  • __init__.py: letutilsThe folder becomes a Python package. Can be empty, or contain some initialization code.
  • string_utils.py: A module containing string processing functions.
  • math_utils.py: A module containing mathematical calculation functions.

Avoid circular dependencies:A circular dependency refers to two or more modules that depend on each other, causing problems during import. Try to avoid this situation. If both module A and module B need to use each other's functions, the common part can be extracted into an independent module C, so that both A and B depend on C.

3. DRY Principle (Don't Repeat Yourself)

The core idea of ​​the DRY principle is to avoid code duplication.

Package common functions:Encapsulate duplicate blocks of code into functions or classes. For example, if data cleaning is required in multiple places, you can create adata_cleaning.pyModule:

# data_cleaning.py
def clean_data(data):
    """
     Clean the data and remove missing values ​​and outliers.
     """
    # Remove missing values    data = ()
    # Remove outliers (assuming outliers are data greater than 100)    data = data[data < 100]
    return data

Then import and use where you need it:

# 
import pandas as pd
from utils.data_cleaning import clean_data

# Sample datadata = ([1, 5, 2, 105, 3, None, 88])
print(f"Raw data:\n{data}")

cleaned_data = clean_data(data)
print(f"Cleaned data:\n{cleaned_data}")

Output result:

Raw data:
0      1.0
1      5.0
2      2.0
3    105.0
4      3.0
5      NaN
6     88.0
dtype: float64
Cleaned data:
0     1.0
1     5.0
2     2.0
4     3.0
6    88.0
dtype: float64

Use inheritance and combination:In object-oriented programming, inheritance and composition can be used to avoid code duplication.

4. Other best practices

  • Clear interface:The module's public functions and classes are placed at the top of the file, and the private helper functions are placed at the bottom, using_The beginning represents a private function.
  • Consistent import convention:Always import modules at the top of the file, using absolute import (e.g.from my_project.utils import my_function) instead of relative import (e.g.from ..utils import my_function)。
  • Appropriately named:Use descriptive names for modules, functions, and variables to improve code readability. For example, usecalculate_averageInsteadcalc_avg

5. Practical application examples

Web crawler:Extract common functions of crawlers (such as sending HTTP requests and parsing HTML) into independent modules for use by different crawler programs.

Data Analysis:Extract common functions such as data preprocessing, feature engineering, model training, etc. into independent modules, making it easier to reuse in different data analysis projects.

Web Development:Extract general functions such as user authentication, permission management, and database operations into independent modules to improve the development efficiency of web applications.

6. Numerical indicators

Modular programming can improve the codeMaintainabilityandTestability. Some studies have shown that a good modular design can reduce the number of bugs by up to 20% and improve development efficiency by 15%. In addition, modularity can also reduce the codeCoupling degree, improve the codeCohesion. Generally speaking, the lower the coupling degree between modules, the better, and the higher the cohesion inside the module, the better. Some tools can help evaluate the modularity of your code, such as SonarQube.

This is the article about how Python extracts public modules and avoids duplication of code. For more related content of Python extracting public modules, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!