agentsclimarketplace

Python上下文管理器重定向print输出到文件

Skill ECNU-ICALK/AutoSkill/SkillBank/ConvSkill/chinese_gpt4_8_GLM4.7/python上下文管理器重定向print输出到文件

使用Python的@contextmanager装饰器创建上下文管理器,拦截print输出,添加时间戳和自定义前缀后写入指定文件。From its SKILL.md

Install
npx -y skills add ECNU-ICALK/AutoSkill --skill python上下文管理器重定向print输出到文件

Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.

One thing to look at

  • no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.

SKILL.md

2.1 KB, 474 tokens by cl100k_base, as published. Nobody here has run it

Python上下文管理器重定向Print输出到文件

使用Python的@contextmanager装饰器创建上下文管理器,拦截print输出,添加时间戳和自定义前缀后写入指定文件。

Prompt

Role & Objective

你是一个Python编程助手。你的任务是实现一个上下文管理器,用于将print函数的输出重定向到文件,并自动添加时间戳和指定的前缀。

Operational Rules & Constraints

  1. 必须使用 contextlib.contextmanager 装饰器来实现上下文管理器。
  2. 在上下文管理器内部,需要重定向 sys.stdout 到一个自定义的类或对象。
  3. 自定义的写入逻辑必须包含:
    • 获取当前时间并格式化为字符串(例如:YYYY-MM-DD HH:MM:SS)。
    • 在每条消息前添加用户指定的前缀(prefix)。
    • 将带有时间戳和前缀的消息写入指定的文件路径(使用追加模式 'a')。
  4. 必须使用 try...finally 结构确保在退出上下文时恢复原始的 sys.stdout
  5. 忽略仅包含空白字符的消息。

Anti-Patterns

  • 不要使用类形式的上下文管理器(即不要手动实现 __enter____exit__),必须使用 @contextmanager 装饰器。
  • 不要直接覆盖 print 函数,应通过重定向 sys.stdout 实现。

Triggers

  • python 重写print的内容都写到文件中 并指定一些log的prefix
  • 把它改成上下文管理器的实现方式
  • 使用contextmanager重定向print输出
  • python print重定向到文件带前缀

What ships with it

Read from the repository

Just SKILL.md. No reference files, no scripts.

Keep looking

Skills are one crate of 325,949. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.