Skip to main content

Xulab Useful Toolkit in Python (xutk)

Maintenance PyPI - Python Version License: Apache-2.0

XuTK 是课题组泛用 Python 小工具。 来自于古法编程时代,旨在消除重复代码、提高小工具质量。

本仓库发布两个独立的包(同一个 uv workspace):

发行版 是什么 约束
xutk Python 库,import xutk 使用 Python >= 3.11,有第三方依赖
xutk-tools 命令行工具(s3-login、ants-init) 零依赖,Python >= 3.8,每个工具是能拷走即跑的单文件

导入项目

# 在 uv 项目下添加依赖安装
uv add xutk
# 或使用 pip 安装
pip install xutk

命令行工具(xutk-tools)

uvx --from xutk-tools s3-login --export rclone   # 登录 S3 并直接配好 rclone
uvx --from xutk-tools ants-init                  # 装 ANTs 到 ~/.ants
uv tool install xutk-tools                       # 装成常驻命令 s3-login / ants-init

不需要 uv 时:pip install xutk-tools,或 python -m xutk_tools install s3-login ~/.local/bin 把单文件拷出来直接 python3 跑(之后不再需要本包)。 详见 docs/reference/s3-login.md、docs/reference/ants-init.md。

快速开始

from xutk import verchk, log, mp_runner
from xutk.script_lib.ants import AntsRegistrator
from pathlib import Path

# 版本检查
verchk.check_version("0.2.0", "some_package")

# 日志记录
logger = log.CtxLogger("my_app")
logger.info({"caller": "some_module", "note_file": "data.csv"}, "Processing started")

# 多进程运行器
runner = mp_runner.mprunner_factory(2, log_file=Path("process.log")) # 进程队列,取 2 个运行
runner.batch_run((f"echo test{num}" for num in range(1, 25))) # 排队 25 个进程

# ANTs 配准
slurm_cfg = ...
ants = AntsRegistrator(Path("config/ants_registration.yaml"), slurm_cfg)
job_id = ants.submit(
    project_root=Path("/data/project"),
    fixed_image=Path("/data/project/templates/t1.nii.gz"),
    moving_image=Path("/data/project/subjects/001.nii.gz"),
    job_name="subj001",
    # 可选任务级参数(初始变换 / mask,不进 YAML):
    # initial_moving_transform="moments",   # 或变换文件、或二者混链(列表顺序=施加顺序)
    # masks=(Path(".../t1_mask.nii.gz"), Path(".../001_mask.nii.gz")),  # (fixed, moving)
)

建议在项目 __init__.py 创建 CtxLogger("my_app"),避免实例化顺序问题。

项目结构

xutk/
├── .agents/skills/      # 按需加载的模块指引(语义与坑)
├── .gitea/workflows/    # CI 工作流
├── docs/
│   ├── decision/        # ADR
│   └── reference/       # 实现事实与工具参考文档
├── examples/            # 使用案例
├── packages/
│   └── xutk-tools/      # 命令行工具发行版(零依赖单文件)
│       ├── src/xutk_tools/  # s3_login.py / ants_init.py / 注册表
│       └── test/            # 单文件契约 + 隐私闸门测试
├── src/
│   └── xutk/                # 主包
│       ├── __init__.py
│       ├── log.py           # 日志记录
│       ├── mp_runner/       # 多进程运行管理
│       ├── perf.py          # 资源追踪
│       ├── verchk.py        # 版本检查
│       └── script_lib/      # 脚本工具
│           └── ants/        # ANTs 配准工具
├── test/unit/           # 单元测试
├── AGENTS.md            # agent 工作指引(主干)
├── CHANGELOG.md         # 变更记录
├── pyproject.toml       # 项目配置
└── README.md            # 项目文档

开发

安装开发依赖

uv sync --all-packages --all-extras   # xutk 库 + xutk-tools 工具都装上

运行测试

uv run pytest

代码检查

uv run ruff check      # 代码风格检查(两个包各有一套规则)
uv run ty check        # 类型检查
uv run pytest          # 样例测试 + xutk-tools 契约测试
uv build --all-packages  # 产出两个发行版的 wheel/sdist

Metadata

Release files for xutk 0.5.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for xutk 0.5.3
File Size Uploaded Provenance Actions
xutk-0.5.3.tar.gz 36.7 kB Without provenance Details for xutk-0.5.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for xutk 0.5.3
File Interpreter ABI Platform Actions
xutk-0.5.3-py3-none-any.whl Python 3 none any Details for xutk-0.5.3-py3-none-any.whl

Total release size: 81.2 kB

Release files / xutk-0.5.3.tar.gz

Download URL xutk-0.5.3.tar.gz
Size 36.7 kB
Tags Source
SHA-256 checksum
How to use checksums
3d0f5d5d96dcf8d31317bd5993f38b6a4918b0d7322a7eeb2b35fb9b8f7cf922
BLAKE2b-256 checksum
How to use checksums
c87015aeee25f86333723d96ab24ea3f164ac58456bbbe6b6023c61e8f074624
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Gentoo","version":"2.18","id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / xutk-0.5.3-py3-none-any.whl

Download URL xutk-0.5.3-py3-none-any.whl
Size 44.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e1e2b8e323e6204f6f826ac0cab488b018774b144ef1dbdb0f6384a1d44d5723
BLAKE2b-256 checksum
How to use checksums
97fddca85dceeef85e491c84aeaf05c2fda23fd4051cbf384e631d889ca1fdba
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Gentoo","version":"2.18","id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for xutk 0.5.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page