Skip to main content

An utils collection for python called MxBoxUtils

Project description

MxBoxUtils

MxBoxUtils 是一个 Python 工具库,提供了文件操作、TOML 配置解析等实用功能,专注于提高开发效率和代码复用性。

目录结构

MxBoxUtils/
├── src/
│   └── mxboxutils/      # 核心功能模块
│       ├── __init__.py  # 包初始化文件
│       ├── file.py      # 文件操作工具
│       └── toml.py      # TOML 配置文件解析工具
├── tests/               # 测试文件目录
│   └── test_mxboxutils.py  # 单元测试文件
├── LICENSE              # 许可证文件
├── pyproject.toml       # 项目配置文件
└── README.md            # 项目说明文档

功能模块

1. 文件操作 (file.py)

提供丰富的文件和目录操作功能,包括文件查找、路径处理、哈希计算等。

files(f_path: str, f_ext: list[str]) -> list[str]

列出指定目录下的所有指定扩展名的文件名(不包含路径)。

  • 参数:
    • f_path - 要搜索的目录路径
    • f_ext - 要匹配的文件扩展名列表(不包含点号)
  • 返回值: 匹配的文件名列表

file_paths(f_path: str, f_ext: list[str]) -> list[str]

获取指定目录下所有指定扩展名的文件的完整路径。

  • 参数:
    • f_path - 要搜索的目录路径
    • f_ext - 要匹配的文件扩展名列表
  • 返回值: 完整文件路径列表

imgs(f_path: str, f_ext: list[str] = IMG_TYPE) -> list[str]

列出指定目录下的所有图像文件(默认支持 jpg、jpeg、png)。

  • 参数:
    • f_path - 要搜索的目录路径
    • f_ext - 要匹配的图像文件扩展名(可选)
  • 返回值: 图像文件名列表

img_paths(f_path: str, f_ext: list[str] = IMG_TYPE) -> list[str]

获取指定目录下所有图像文件的完整路径。

  • 参数:
    • f_path - 要搜索的目录路径
    • f_ext - 要匹配的图像文件扩展名(可选)
  • 返回值: 图像文件完整路径列表

file_hash(file_path: str, hash_type: str) -> str

计算文件的哈希值(支持 SHA256 和 MD5)。

  • 参数:
    • file_path - 要计算哈希值的文件路径
    • hash_type - 哈希算法类型,支持 "SHA256" 或 "MD5"
  • 返回值: 计算后的哈希字符串

2. TOML 配置解析 (toml.py)

提供简洁的 TOML 配置文件解析功能,便于加载和管理配置数据。

load_toml(filepath)

加载并解析 TOML 配置文件。

  • 参数:
    • filepath - TOML 文件的完整路径
  • 返回值: 解析后的配置数据字典,如果文件不存在则返回 None

安装说明

# 从源代码安装
pip install -e .

# 安装开发依赖(包括 pytest)
pip install -e .[dev]

使用示例

文件操作示例

from mxboxutils.file import files, file_paths, file_hash

# 查找所有 Python 文件
py_files = files("/path/to/code", ["py"])
print(f"找到 {len(py_files)} 个 Python 文件")

# 获取文件完整路径
py_file_paths = file_paths("/path/to/code", ["py"])

# 计算文件 SHA256 哈希
file_sha256 = file_hash("/path/to/file", "SHA256")

TOML 配置解析示例

from mxboxutils.toml import load_toml

config = load_toml("config.toml")
if config:
    print(f"配置加载成功: {config}")

测试

项目已集成完整的单元测试,确保所有功能正常工作。

# 运行所有测试
python -m pytest tests/test_mxboxutils.py -v

版本信息

当前版本: 0.1.6

系统要求

  • Python 3.9 或更高版本(推荐 3.12+)

依赖

  • 标准库,无需额外依赖
  • 可选依赖 (dev): pytest, setuptools, twine, build

开发

欢迎提交 Issue 和 Pull Request 来帮助改进这个项目。

许可证

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mxboxutils-0.1.6.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mxboxutils-0.1.6-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file mxboxutils-0.1.6.tar.gz.

File metadata

  • Download URL: mxboxutils-0.1.6.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.0

File hashes

Hashes for mxboxutils-0.1.6.tar.gz
Algorithm Hash digest
SHA256 f82496939010761cab5c615acc87fadc88b46601322b569a5cfcc214d9fd8e60
MD5 74838b715d22e1df49145bc9d0e62166
BLAKE2b-256 7b320f67049544cdbbd3b58ce72dd70d12a849035fc1075548e61cbbdf602594

See more details on using hashes here.

File details

Details for the file mxboxutils-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: mxboxutils-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.0

File hashes

Hashes for mxboxutils-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 f8fc495fc6d95cb9f432d19ebf6ae6c80ceda0e57d9a07bdc378047340cbcae6
MD5 d7b40fa7a8693280a61b075ef50df9f7
BLAKE2b-256 716861fb8335ce4d8b4d5e199190c2e5c7d4ff356c43338d7bfdf89a70da9e88

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page