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.7.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.7-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for mxboxutils-0.1.7.tar.gz
Algorithm Hash digest
SHA256 46be66fd427104e5165b723b5d2904dab6f0ac1f6abae10d16947853518a595f
MD5 b4976357ed1fc76e869bf3f8642e73e9
BLAKE2b-256 e134d676215d08264c9fbc044294d45f34b9848345d33080e1d72744bf025491

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for mxboxutils-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 6b6083e3d5d7e37a77989273648751cc794f604b670ef7ab6bf287c458eddc72
MD5 61133142299981b9c04b9f6a2cebeb2b
BLAKE2b-256 f778ffc38efcde6bda6518426afec9adfb5dd432a7244ee11aebd3b27f559a48

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