Skip to main content

A Python decorator for logging, saving results, and tracking function source code.

Project description

YihanResultSaver Library

当我们做数值计算时,会面临代码修改次数多,数值结果保存得太多,最后分不清哪个是哪个的问题,不要担心,在你的main函数前加上YihanResultSaver修饰器,他会自动帮你用numpy.save保存计算结果、源代码和日志,方便后面查阅


特性

  • 运行日志记录:自动记录函数运行的时间、参数和描述信息。
  • 返回值保存:将函数的返回值保存为 .npy 文件,便于后续加载和分析。
  • 代码备份:将目标函数的源代码及其依赖的非公共模块的文件保存到指定目录。
  • 输出日志重定向:将函数的标准输出保存到文件,同时保留在终端输出。

安装

你可以直接将项目克隆到本地使用:

git clone https://github.com/YihanYu115/YihanResultSaver.git
cd YihanResultSaver

并将库添加到你的项目中。

pip install dist\YihanResultSaver-0.1.0-py3-none-any.whl

或者直接

pip install YihanResultSaver

使用方法

示例代码

import numpy as np
from YihanResultSaver import YihanResultSaver

# 为函数添加装饰器
@YihanResultSaver(description="This is a sample function for demo purposes.")
def sample_function(x, y):
    print("This is a sample function.")
    return np.array([x + y, x - y, x * y])

# 调用函数
result = sample_function(10, 5)
print("Function Result:", result)

执行后的结果

执行上面的代码后,将生成以下文件和目录结构:

Results/
├── run_log.txt  # 日志文件,记录函数的运行时间、参数等
├── sample_function_20240101_123456/
│   ├── source_code/
│      ├── sample_function.py  # 函数及依赖模块的源代码   ├── result.npy  # 函数返回值   ├── stdout.txt  # 函数的标准输出日志
  • run_log.txt:记录每次函数运行的信息,包括时间、函数名、参数和描述。

  • source_code/:包含目标函数及其所有依赖模块的备份代码。

  • result.npy:函数返回值保存为 .npy 文件。

  • stdout.txt:标准输出日志文件。

效果截图 alt text


函数装饰器参数

@YihanResultSaver(description="Your description here")

  • description:用于描述目标函数,记录在日志中,方便追溯。

API 文档

YihanResultSaver(description)

参数

  • description (str): 对目标函数的描述信息。

功能

  • 自动创建运行日志。

  • 保存目标函数的返回值到文件。

  • 保存目标函数和依赖模块的源代码。

  • 捕获目标函数的标准输出。


系统需求

  • Python 版本 :>= 3.6

  • 依赖库

    • numpy>=1.21.0(用于保存返回值)

开发者


许可证

本项目基于 MIT License 进行分发和开源。详情请参阅 LICENSE 文件。


贡献指南

欢迎对该项目进行贡献!如果您有新的想法或发现了问题,请通过以下方式参与:

  1. Fork 本项目。

  2. 创建您的功能分支:git checkout -b feature/AmazingFeature

  3. 提交更改:git commit -m 'Add some AmazingFeature'

  4. 推送到分支:git push origin feature/AmazingFeature

  5. 提交 Pull Request。


致谢

感谢所有使用和贡献该库的开发者!

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

YihanResultSaver-0.1.1.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

YihanResultSaver-0.1.1-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file YihanResultSaver-0.1.1.tar.gz.

File metadata

  • Download URL: YihanResultSaver-0.1.1.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for YihanResultSaver-0.1.1.tar.gz
Algorithm Hash digest
SHA256 aac4761d5063934078b993f3707cc844df6c0d737d29ade00884653a87af84ec
MD5 653aa1a62d26d12f0fca640caba72e11
BLAKE2b-256 c448ebadfbdf4e19c0dc4f78261c3dbd3c7b1e7459f207e9a6737d2b05a0206f

See more details on using hashes here.

File details

Details for the file YihanResultSaver-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for YihanResultSaver-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4c83889565ee5585568fde3e6f2038dbfe6866913c2cd32c3e325e299d0ca2ae
MD5 af48ebe0a0e87158779c71f1e279392d
BLAKE2b-256 6a9dd4efa9b7ece94b1c915d61a6ca60df633f833f6911105b8a7a2ad0e78bbc

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