逐字符显示所有Python进程输出
项目描述
让您的Python输出戏剧性 🎭
dramatic
模块包含了一些工具,它们会导致所有文本输出逐字符显示(它以戏剧性的方式打印)。
注意:本项目基于Python Morsels练习。如果您目前正在做这个练习,请不要查看这个项目的源代码!😉
使用方法 🥁
dramatic
模块可在PyPI上找到。您可以使用pip
安装它
$ python3 -m pip install dramatic
使用dramatic
模块中的工具有四种主要方式
- 作为上下文管理器,暂时使输出显示戏剧性
- 作为装饰器,暂时使输出显示戏剧性
- 使用
dramatic.start()
函数使输出显示戏剧性 - 使用
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》装饰器将会在装饰的函数运行期间导致所有标准输出和标准错误显示得非常戏剧化
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.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.start
和dramatic.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")
示例start
和stop
用法
戏剧化打印 🖨️
dramatic.print
函数的行为就像内置的print
函数,但它会以戏剧化的方式打印
import dramatic
dramatic.print("This will print some text dramatically")
戏剧化解释器 ⌨️
要启动一个戏剧化的Python REPL
$ python3 -m dramatic
>>>
要以戏剧化的方式运行一个Python模块
$ python3 -m dramatic -m this
要以戏剧化的方式运行一个Python文件
$ python3 -m dramatic hello_world.py
dramatic
模块还接受一个--speed
参数来设置每秒打印的字符数。在这个例子中,我们将速度从每秒75个字符增加到120个字符
最大戏剧化(谨慎使用 ⚠️)
想要让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]
示例
如果戏剧化程度太高,可以再次运行模块,使用--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.
示例
即使没有安装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的散列值
算法 | 散列摘要 | |
---|---|---|
SHA256 | 97a2aea21280ac6bd81b015a6f509ad2d742da503e7b3ff1fd62e5e1c21bac31 |
|
MD5 | 846da597b84af24f9b510ff957a17912 |
|
BLAKE2b-256 | 0af0ccdb4a063882cc3896bf96791e8b988e99b9416c4ddb015e5d130f3f27a5 |
dramatic-0.4.1.post1-py3-none-any.whl的散列值
算法 | 散列摘要 | |
---|---|---|
SHA256 | 171caabeb333665f72de873ef7964c0297b4e5abd907d430614dd01b180fa434 |
|
MD5 | 2b2a4ee59044264c1ae35a3971cf6708 |
|
BLAKE2b-256 | 6028ec1da6c7f83bb2a5896352b43d7fc9ab8070cf331cb4818c19278c41cffa |