一个为 PyTorch 项目设计的直观的配置管理库。它通过简单的 YAML 文件实现层级化配置,支持 TEST, DEV, PROD 等多种运行模式,并可通过类继承方便地扩展。让管理不同环境和实验的配置变得轻松。 (注意:作为库使用时,请确保运行目录下存在 `temp/mode.txt` 文件和包含 YAML 文件的 `config` 目录以符合当前加载逻辑)。
Project description
Project Setup and Usage Guide
Environment Setup
This project uses the following environment:
- Micromamba as package manager
- Python 3.13
- PyTorch 2.6.0
- PyTorch Lightning
You can use setup.ps1 to install all required dependencies.
Mode Configuration
The project supports different running modes:
- Default mode: test
- Available modes: test, dev, prod
To change the mode, use the following command:
.\set_mode.ps1 -Mode [mode_name]
Example:
.\set_mode.ps1 -Mode prod
.\set_mode.ps1 -Mode dev
.\set_mode.ps1 -Mode test
Running and Debugging
The project is configured for both running and debugging in VS Code:
- Use the right-click context menu in the editor
- Select "Run Python File" to execute the current file
- Debug configurations are available in
.vscodesettings
Environment Information
The project includes an environment information script (src/env.py) that displays:
- Python version
- PyTorch version
- CUDA version and device information (if available)
- Python executable path
To run the environment check:
D:/dev/mamba/envs/pytorch/python.exe src/env.py
Temporary Files
When creating temporary files:
- All temporary files must be created in the
tempdirectory - The
tempdirectory is ignored by git (configured in.gitignore) - This rule applies to all AI-assisted code generation and temporary file creation
Unit Testing
The project includes a sample test file (tests/add_test.py) that demonstrates basic unittest setup and structure. Here's how to work with unit tests:
Running Tests
You can run tests in several ways:
-
In Cursor IDE:
- Open the Testing view in the sidebar
- All test files will be automatically discovered
- You can run individual tests or all tests from the Testing view
- When editing a test file (e.g.,
add_test.py), right-click in the editor and select "Run Python File" to run the test
-
In Editor:
- Open the test file (e.g.,
add_test.py) in the editor - Right-click in the editor
- Select "Run Python File"
- The test will run in the terminal
- Open the test file (e.g.,
Test Configuration
- Configure test settings using
Ctrl+Shift+P - Select "Python: Configure Tests"
- Choose "unittest" (not pytest) as the test framework
- Select the
testsfolder - Test files should follow the
*_test.pynaming convention
Example Test Structure
The add_test.py demonstrates:
- Basic unittest setup with
unittest.TestCase - Test case structure with
setUpandtearDownmethods - Simple assertion testing (1+2=3)
iget-config 包发布与使用
本节介绍如何将 src/config 目录下的代码打包成 iget-config 库,发布到 PyPI,以及如何在其他项目中使用这个库。
环境准备 (用于编译和上传)
确保您的 Python 环境 (例如 D:/dev/mamba/envs/pytorch/python.exe) 中安装了以下包:
# 如果没有安装,请运行:
D:/dev/mamba/envs/pytorch/python.exe -m pip install build twine
编译 (构建) 新版本
当您修改了 src/config 目录下的代码后,需要重新编译打包:
- 更新版本号:
- 修改
pyproject.toml文件中的version = "..."行,增加版本号 (例如从0.1.2改为0.1.3)。 - 同时修改
src/config/__init__.py文件中的__version__ = "..."行,使其与pyproject.toml中的版本号保持一致。
- 修改
- 清理旧构建 (重要): 在项目根目录 (
D:\iget) 运行以下命令,删除可能存在的旧构建文件:Remove-Item -Recurse -Force dist Remove-Item -Recurse -Force build Remove-Item -Recurse -Force src\*.egg-info # 清理 egg-info 文件夹
- 执行构建: 在项目根目录 (
D:\iget) 运行构建命令:D:/dev/mamba/envs/pytorch/python.exe -m build
构建成功后,会在dist/目录下生成新的.tar.gz和.whl文件 (例如iget_config-0.1.3.tar.gz和iget_config-0.1.3-py3-none-any.whl)。
上传新版本到 PyPI
使用 twine 将 dist/ 目录下的新版本文件上传到 PyPI:
- 获取 PyPI API 令牌:
- 登录 pypi.org。
- 双因素验证在手机上查看微软的app
- 进入 "Account settings" -> "API tokens"。
- 建议为每个项目创建单独的令牌,点击 "Add API token",名称随意 (如
iget-config uploader),范围 (Scope) 选择 "Project",项目名填入iget-config。 - 立即复制新生成的、以
pypi-开头的完整令牌。
- 执行上传: 在项目根目录 (
D:\iget) 的本地终端 (例如 PowerShell) 中运行以下命令:D:/dev/mamba/envs/pytorch/python.exe -m twine upload dist/*
- 输入令牌: 当提示
Enter your API token:时,粘贴您刚刚获取的 API 令牌,然后按 Enter。- 如果上传成功,会看到类似
View at https://pypi.org/project/iget-config/0.1.3/的信息。 - 如果遇到
403 Forbidden错误,通常是 API 令牌无效或输入错误。 - 如果遇到
File already exists错误,说明这个版本号已经上传过,您需要回到【编译】步骤增加版本号。
- 如果上传成功,会看到类似
在其他项目中使用 iget-config
-
安装: 在您想使用该库的项目环境中,通过
pip安装:pip install iget-config
或者安装特定版本:
pip install iget-config==0.1.2
-
准备环境 (重要提醒): 由于库的原始代码依赖特定的文件结构,在您运行 Python 脚本的当前工作目录下,必须满足以下条件:
- 存在一个名为
config的文件夹,其中包含您的项目所需的.yaml配置文件 (例如default.yaml,default_dev.yaml,cnn.yaml等)。库会根据模式自动加载这些文件。 - 存在一个名为
temp的文件夹,其中包含一个名为mode.txt的文件,文件内容为TEST,DEV, 或PROD(不区分大小写),用于指定运行模式。
示例目录结构:
/path/to/your_project/ ├── main.py # 您的主程序脚本 ├── config/ # 必须存在 │ ├── default.yaml │ ├── default_dev.yaml │ └── cnn.yaml │ └── ... (其他 .yaml 文件) └── temp/ # 必须存在 └── mode.txt # 必须存在,内容为 TEST/DEV/PROD - 存在一个名为
-
在代码中使用 (推荐方式): 虽然库会尝试读取
temp/mode.txt和./config,但更健壮和明确的方式是在代码中显式设置:import os from config import Config, Mode # 直接从安装的包导入 # --- 设置配置路径和模式 --- # 假设您的配置文件放在项目根目录下的 'my_configs' 文件夹 project_root = os.path.dirname(os.path.abspath(__file__)) # 获取当前脚本所在目录 config_directory = os.path.join(project_root, 'my_configs') # 替换为您实际的配置文件夹名 # 检查配置目录是否存在 if not os.path.isdir(config_directory): raise FileNotFoundError(f"配置目录未找到: {config_directory}") Config.set_config_folder(config_directory) Config.set_mode(Mode.DEV) # 或者 Mode.PROD, Mode.TEST print(f"配置目录已设置为: {Config.config_folder}") print(f"运行模式已设置为: {Config.mode}") # --- 实例化 Config --- # 使用 reload=True 确保加载您刚才设置的路径和模式对应的文件 cfg = Config(reload=True) print("Config 实例已创建。") # --- 使用配置 --- print(f"数据库路径: {cfg.db_path}") print(f"训练批次大小: {cfg.train.batch_size}") # 如果需要使用 CnnConfig (假设已在 __init__.py 中导出) # from config import CnnConfig # cnn_cfg = CnnConfig(reload=True) # print(f"CNN 模型名称: {cnn_cfg.model_name}") # 假设 CnnConfig 有 model_name 属性
注意: 上述推荐用法可以摆脱对特定 temp/mode.txt 和 ./config 目录结构的依赖,使您的代码更清晰、更易于部署。
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file iget_config-0.2.0.tar.gz.
File metadata
- Download URL: iget_config-0.2.0.tar.gz
- Upload date:
- Size: 20.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
654af21c119dfb8ce5b437b2ac77c34366aa36770d3be34621642809222b951d
|
|
| MD5 |
b451a6dc976988b36aae855c88f1dc54
|
|
| BLAKE2b-256 |
c78c1137f30254a7fca882e56a5ff520a6fb4f844680e0bcda8e549227ac2f73
|
File details
Details for the file iget_config-0.2.0-py3-none-any.whl.
File metadata
- Download URL: iget_config-0.2.0-py3-none-any.whl
- Upload date:
- Size: 16.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
85e551ecc34510586600da7a21bb35775d263a22156d59b6eb7c404fde430086
|
|
| MD5 |
1726aa84f5064b77c51aac5751dec231
|
|
| BLAKE2b-256 |
e433668535b8748ecd7dce9e1c67d9c90524742bccb57c0b57cd8ab0d2da5b9a
|