Cấu trúc thư mục trong phát triển phần mềm

Việc tổ chức thư mục hợp lý đóng vai trò quan trọng trong việc nâng cao khả năng đọc hiểu và duy trì mã nguồn. Một cấu trúc thư mục rõ ràng giúp các thành viên trong nhóm dễ dàng định vị và làm việc với các tệp tin, đồng thời tạo nền tảng vững chắc cho việc mở rộng dự án về lâu dài.

Cấu trúc thư mục được khuyến nghị sử dụng như sau:

BankingSystem/
|-- core/
|   |-- business.py  # Xử lý logic nghiệp vụ chính
|
|-- api/
|   |-- handlers.py  # Các endpoint API
|
|-- storage/
|   |-- data_manager.py  # Quản lý thao tác dữ liệu
|   |-- data.txt  # Tệp lưu trữ dữ liệu
|
|-- utils/
|   |-- helpers.py  # Các hàm tiện ích dùng chung
|
|-- config/
|   |-- configuration.py  # Thiết lập cấu hình hệ thống
|
|-- startup/
|   |-- launcher.py  # Tệp khởi động chương trình, thường đặt tại thư mục gốc để đảm bảo thư mục hiện tại được thêm vào sys.path đầu tiên, giúp đơn giản hóa việc cấu hình đường dẫn
|
|-- logs/
|   |-- system.log  # Tệp ghi nhận hoạt động
|
|-- requirements.txt # Liệt kê các thư viện bên ngoài cần thiết
|-- README  # Tài liệu mô tả dự án

Ví dụ về tệp khởi động:

# launcher.py

import sys
import os

PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, PROJECT_ROOT)
from core import business

if __name__ == '__main__':
    business.start()

Thiết lập cấu hình trong hệ thống:

# configuration.py

import os

PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

DATA_FILE_PATH = os.path.join(PROJECT_ROOT, 'storage', 'data.txt')
LOG_FILE_PATH = os.path.join(PROJECT_ROOT, 'logs', 'system.log')

# print(DATA_FILE_PATH)
# print(LOG_FILE_PATH)

Triển khai logic nghiệp vụ:

# business.py

from config import configuration
from utils import helpers


def authenticate_user():
    print('Xác thực người dùng')


def create_account():
    print('Tạo tài khoản mới')
    username = input('Tên đăng nhập: ')
    password = input('Mật khẩu: ')
    with open(configuration.DATA_FILE_PATH, mode='a', encoding='utf-8') as file:
        file.write('%s:%s\n' % (username, password))
        helpers.record_activity('%s đã tạo tài khoản thành công' % username)
        print('Tài khoản đã được tạo')


def process_transaction():
    print('Xử lý giao dịch')


def withdraw_money():
    print('Rút tiền')


def transfer_funds():
    print('Chuyển khoản')


operations = {
    '1': authenticate_user,
    '2': create_account,
    '3': process_transaction,
    '4': withdraw_money,
    '5': transfer_funds,
}


def start():
    while True:
        print("""
        1 Xác thực người dùng
        2 Tạo tài khoản
        3 Xử lý giao dịch
        4 Rút tiền
        5 Chuyển khoản
        6 Thoát
        """)
        user_choice = input('Lựa chọn: ').strip()
        if user_choice == '6': break
        if user_choice not in operations:
            print('Lệnh không hợp lệ, vui lòng thử lại')
            continue
        operations[user_choice]()

Các hàm tiện ích chung:

# helpers.py

import datetime
from config import configuration


def record_activity(message):
    timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    with open(configuration.LOG_FILE_PATH, mode='a', encoding='utf-8') as file:
        file.write('%s %s\n' % (timestamp, message))

Thẻ: python directory-structure project-organization software-architecture Best-Practices

Đăng vào ngày 7 tháng 7 lúc 18:19