fix: 统一文件编码为 utf-8 并将 benchmark 日志改为英文

This commit is contained in:
2026-04-27 17:09:27 +08:00
parent e9eea2e5f8
commit d010752184
3 changed files with 266 additions and 13 deletions

18
main.py
View File

@@ -17,7 +17,7 @@ def add_newproblem(num: int, name: str | None = None) -> None:
Path(f"solutions/{title}").mkdir(parents=True, exist_ok=True)
file_name = f"solutions/{title}/euler_{num}.py"
Path(file_name).touch()
with open(file_name, "w") as f:
with open(file_name, "w", encoding="utf-8") as f:
f.write("""'''
'''
@@ -29,14 +29,8 @@ F = TypeVar("F", bound=Callable[..., Any])
def benchmark(repeat: int = 1) -> Callable[[F], F]:
'''
重复运行目标函数并计算平均耗时。
平均耗时会被存储在 wrapper.avg_time 和 wrapper.total_time 中,
同时会打印到控制台。函数的返回值不受影响。
'''
if repeat < 1:
raise ValueError("repeat 必须 >= 1")
raise ValueError("repeat must >= 1")
def decorator(func: F) -> F:
@wraps(func)
@@ -54,18 +48,16 @@ def benchmark(repeat: int = 1) -> Callable[[F], F]:
wrapper.total_time = total # type: ignore[attr-defined]
print(
f"[Benchmark] {func.__name__} | 重复 {repeat} | "
f"平均: {wrapper.avg_time:.6f}s | 总计: {wrapper.total_time:.6f}s" # type: ignore[attr-defined]
f"[Benchmark] {func.__name__} | repeated {repeat} times | "
f"average: {wrapper.avg_time:.6f}s | total: {wrapper.total_time:.6f}s" # type: ignore[attr-defined]
)
return result
# 初始化属性,避免调用前访问报错
wrapper.avg_time = 0.0 # type: ignore[attr-defined]
wrapper.total_time = 0.0 # type: ignore[attr-defined]
return wrapper # type: ignore[return-value]
return decorator
""")
return decorator""")
@app.command("list", help="List all problems in the projecteuler repository.")