跳转到主要内容

逐字符显示所有Python进程输出

项目描述

让您的Python输出戏剧性 🎭

PyPI - Version License Tests Codecov

dramatic模块包含了一些工具,它们会导致所有文本输出逐字符显示(它以戏剧性的方式打印)。

dramatic printing within a terminal

注意:本项目基于Python Morsels练习。如果您目前正在做这个练习,请不要查看这个项目的源代码!😉

an adorable snake taking a bite out of a cookie with the words Python Morsels next to it (Python Morsels logo)

使用方法 🥁

dramatic模块可在PyPI上找到。您可以使用pip安装它

$ python3 -m pip install dramatic

使用dramatic模块中的工具有四种主要方式

  1. 作为上下文管理器,暂时使输出显示戏剧性
  2. 作为装饰器,暂时使输出显示戏剧性
  3. 使用dramatic.start()函数使输出显示戏剧性
  4. 使用dramatic.print函数显示特定的文本戏剧性

戏剧性上下文管理器 🚪

《dramatic.output》上下文管理器将会临时导致所有标准输出和标准错误显示得非常戏剧化

import dramatic

def main():
    print("This function prints")

with dramatic.output:
    main()

要改变打印速度,从默认的每秒75个字符改为其他值(例如,每秒30个字符),请使用at_speed方法

import dramatic

def main():
    print("This function prints")

with dramatic.output.at_speed(30):
    main()

示例上下文管理器用法

dramatic.output context manager demo

戏剧化装饰器 🎀

《dramatic.output》装饰器将会在装饰的函数运行期间导致所有标准输出和标准错误显示得非常戏剧化

import dramatic

@dramatic.output
def main():
    print("This function prints")

main()

at_speed方法也作为一个装饰器使用

import dramatic

@dramatic.output.at_speed(30)
def main():
    print("This function prints")

main()

示例装饰器用法

dramatic.output decorator demo

手动启动和停止 🔧

除了使用上下文管理器或装饰器临时启用戏剧化打印外,还可以使用dramatic.start函数来启用戏剧化打印

import dramatic

def main():
    print("This function prints")

dramatic.start()
main()

可以使用关键字参数speed来改变打印速度(以每秒字符数计)

import dramatic

def main():
    print("This function prints")

dramatic.start(speed=30)
main()

如果要使仅标准输出戏剧化(但不包括标准错误),请在start中传递stderr=False

import dramatic

def main():
    print("This function prints")

dramatic.start(stderr=False)
main()

要禁用戏剧化打印,可以使用dramatic.stop函数。以下是一个使用dramatic.startdramatic.stop的示例上下文管理器

import dramatic


class CustomContextManager:
    def __enter__(self):
        print("Printing will become dramatic now")
        dramatic.start()
    def __exit__(self):
        dramatic.stop()
        print("Dramatic printing has stopped")

示例startstop用法

dramatic.start decorator demo

戏剧化打印 🖨️

dramatic.print函数的行为就像内置的print函数,但它会以戏剧化的方式打印

import dramatic
dramatic.print("This will print some text dramatically")

戏剧化解释器 ⌨️

要启动一个戏剧化的Python REPL

$ python3 -m dramatic
>>>

dramatic printing within a terminal

要以戏剧化的方式运行一个Python模块

$ python3 -m dramatic -m this

要以戏剧化的方式运行一个Python文件

$ python3 -m dramatic hello_world.py

dramatic模块还接受一个--speed参数来设置每秒打印的字符数。在这个例子中,我们将速度从每秒75个字符增加到120个字符

dramatic module running demo

最大戏剧化(谨慎使用 ⚠️)

想要让Python解释器默认就戏剧化吗?

使用带有--max-drama参数的脚本运行dramatic模块,以修改Python,使得所有Python程序都会以戏剧化的方式打印

$ python3 -m dramatic --max-drama
This will cause all Python programs to run dramatically.
Running --min-drama will undo this operation.
Are you sure? [y/N]

示例

python3 -m dramatic --max-drama

如果戏剧化程度太高,可以再次运行模块,使用--min-drama参数来撤销

$ python3 -m dramatic --min-drama
Deleted file /home/trey/.local/lib/python3.12/site-packages/dramatic.pth
Deleted file /home/trey/.local/lib/python3.12/site-packages/_dramatic.py
No drama.

示例

python3 -m dramatic --min-drama

即使没有安装dramatic模块,这也能工作。只需下载dramatic.py并使用--max-drama运行它!要禁用戏剧化,需要运行python3 -m _dramatic --min-drama(注意_dramatic之前)。

警告:使用--max-drama可能是一个不好的主意。请谨慎使用。

其他功能 ✨

其他值得注意的功能

  • 在文本以戏剧化方式打印时按下Ctrl-C将立即打印剩余的文本。
  • 当输出流被重定向到文件时(例如,python3 my_script.py > output.txt),自动禁用戏剧化打印

鸣谢 💖

本包受到“戏剧化打印”Python Morsels练习的启发,该练习部分受到Brandon Rhodes的adventure Python端口的启发(该端口以1200波特率显示文本)。

项目详情


下载文件

下载您平台上的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分发

dramatic-0.4.1.post1.tar.gz (728.4 kB 查看散列值)

上传时间

构建分发

dramatic-0.4.1.post1-py3-none-any.whl (7.3 kB 查看散列值)

上传时间 Python 3

AWSAWS云计算和安全赞助商DatadogDatadog监控FastlyFastlyCDNGoogleGoogle下载分析MicrosoftMicrosoftPSF赞助商PingdomPingdom监控SentrySentry错误日志StatusPageStatusPage状态页面