Skip to content
This repository was archived by the owner on Oct 10, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ build-backend = "hatchling.build"

[project]
name = "swankit"
version = "0.2.2"
version = "0.2.3"
dynamic = ["readme", "dependencies"]
description = "Base toolkit for SwanLab"
license = "Apache-2.0"
Expand Down
21 changes: 19 additions & 2 deletions swankit/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
swankit 为 swanlab 定制的配置类
"""
import os
import time
from datetime import datetime
from typing import Tuple, List, Optional


Expand Down Expand Up @@ -93,12 +95,27 @@ def __init__(self, logdir: str, run_id: str, version: str, should_save: bool) ->
LazySettings.__init__(self)
# ---------------------------------- 静态信息 ----------------------------------
self.__should_save = should_save
self.__run_id = run_id
self.__version = version
self.__run_id = run_id
# ----------------------- 检测运行文件夹的创建,确保文件夹在当前进程唯一占有 ----------
run_dir = None
while True:
run_dir is not None and time.sleep(1)
Copy link

Copilot AI Jun 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using and for control flow and side-effects here is subtle. Consider replacing this with an explicit if run_dir is not None: time.sleep(1) to improve readability.

Suggested change
run_dir is not None and time.sleep(1)
if run_dir is not None:
time.sleep(1)

Copilot uses AI. Check for mistakes.
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
run_name = "run-{}-{}".format(timestamp, run_id)
run_dir = os.path.join(logdir, run_name)
if not should_save:
break
try:
os.mkdir(run_dir)
break
except FileExistsError:
pass
assert run_dir is not None, "run_dir should not be None, please check the logdir and run_id"
# ---------------------------------- 文件夹信息 ----------------------------------
self.__root_dir = os.path.dirname(logdir)
self.__swanlog_dir = logdir
self.__run_dir = os.path.join(logdir, run_id)
self.__run_dir = run_dir
self.__console_dir = os.path.join(self.run_dir, "console")
self.__log_dir = os.path.join(self.run_dir, "logs")
self.__files_dir = os.path.join(self.run_dir, "files")
Expand Down
68 changes: 68 additions & 0 deletions test/unit/core/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
@description: 测试 settings
"""

import os.path
import time
from datetime import datetime

import pytest


Expand All @@ -26,3 +30,67 @@ def test_lazy_settings():
settings.exp_colors = ("green", "yellow")
with pytest.raises(ValueError, match="description can only be set once"):
settings.description = "test description 2"


def test_settings():
"""
主要测试 settings 初始化时遇见相同文件夹自动创建新文件夹的功能
"""
from swankit.core.settings import SwanLabSharedSettings
from tutils import TEMP_DIR

# 测试创建文件夹
# 尽量保持代码运行在一秒的开始
time.sleep(1 - (time.time() % 1)) # 确保时间戳在整秒
Copy link

Copilot AI Jun 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Relying on real time.sleep and datetime.now to align to the start of a second may lead to flaky tests. Consider injecting a deterministic clock or mocking datetime.now for more reliable assertions.

Copilot uses AI. Check for mistakes.
start = time.time()
run_id = "111111"
run_dir = os.path.join(TEMP_DIR, "run-{}-{}".format(datetime.now().strftime("%Y%m%d_%H%M%S"), run_id))
settings = SwanLabSharedSettings(logdir=TEMP_DIR, run_id=run_id, version="develop", should_save=True)
assert time.time() - start < 1 # 此时不应该等待一秒
assert settings.run_dir == run_dir
assert settings.run_id == run_id

# 测试创建同名文件夹
# 尽量保持代码运行在一秒的开始
time.sleep(1 - (time.time() % 1)) # 确保时间戳在整秒
start = time.time()
# 测试创建一个存在的文件夹
run_id = "123456"
run_dir = os.path.join(TEMP_DIR, "run-{}-{}".format(datetime.now().strftime("%Y%m%d_%H%M%S"), run_id))
os.mkdir(run_dir)
Copy link

Copilot AI Jun 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test creates directories under TEMP_DIR but does not clean them up. This can lead to side-effects across runs—consider using fixtures (e.g., tmp_path) or adding teardown steps to remove created folders.

Copilot uses AI. Check for mistakes.
settings = SwanLabSharedSettings(logdir=TEMP_DIR, run_id=run_id, version="develop", should_save=True)
assert 2 > time.time() - start >= 1 # 确保至少等待了一秒
assert settings.run_dir != run_dir # 确保没有使用原来的文件夹
assert settings.run_id == run_id # 确保run_id正确


def test_settings_no_save():
"""
测试不保存的情况,此时不会检查文件夹唯一性
"""
from swankit.core.settings import SwanLabSharedSettings
from tutils import TEMP_DIR

# 测试创建文件夹
# 尽量保持代码运行在一秒的开始
time.sleep(1 - (time.time() % 1)) # 确保时间戳在整秒
start = time.time()
run_id = "111111"
run_dir = os.path.join(TEMP_DIR, "run-{}-{}".format(datetime.now().strftime("%Y%m%d_%H%M%S"), run_id))
settings = SwanLabSharedSettings(logdir=TEMP_DIR, run_id=run_id, version="develop", should_save=False)
assert time.time() - start < 1 # 此时不应该等待一秒
assert settings.run_dir == run_dir
assert settings.run_id == run_id

# 测试创建同名文件夹
# 尽量保持代码运行在一秒的开始
time.sleep(1 - (time.time() % 1)) # 确保时间戳在整秒
start = time.time()
# 测试创建一个存在的文件夹
run_id = "123456"
run_dir = os.path.join(TEMP_DIR, "run-{}-{}".format(datetime.now().strftime("%Y%m%d_%H%M%S"), run_id))
os.mkdir(run_dir)
settings = SwanLabSharedSettings(logdir=TEMP_DIR, run_id=run_id, version="develop", should_save=False)
assert time.time() - start < 1 # 此时不应该等待一秒
assert settings.run_dir == run_dir
assert settings.run_id == run_id