cowsay-让你的python脚本也有大佬内味

以前经常看到互联网大佬的Linux shell脚本启动的时候显示一副炫酷的字符画面,逼格瞬间提升一个档次,按说字符拼画也不是什么难事,只要有时间、有耐心(然鹅大部分人这两样都没有)。

网上流传着一个很老的库cawsay牛说。cowsay是一个生成ASCII图片的程序,显示一头牛的消息。它也可以使用预先制作的图像,以生成其他动物的图片,如Linux的吉祥物企鹅。由于它是用Perl编写的,它也适用于其他系统,如微软的Windows。

cawsay的风格看起来是酱婶儿的

$ fortune | cowsay
 ________________________________________
/ You have Egyptian flu: you're going to \
\ be a mummy.                            /
 ----------------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

这里要安利的是python的cawsay版本,代码在这里:
https://github.com/jeffbuttars/cowpy
使用很简单,一键引入python程序,执行即可,效果与Linux下的cawsay一样样的

安装

pip install cowpy

使用

#!/usr/bin/env python
# encoding: utf-8

from cowpy import cow

# Create a Cow
cheese = cow.Moose()

# Get a cowsay message by milking the cow
msg = cheese.milk("My witty mesage")

# do something with the message
print(msg)

# Create a Cow with a thought bubble
cheese = cow.Moose(thoughts=True)
msg = cheese.milk("My witty mesage, with thought")
print(msg)

# Create a Cow with a tongue
cheese = cow.Moose(tongue=True)
msg = cheese.milk("My witty mesage, with tongue")
print(msg)

# Create a Cow with dead eyes
cheese = cow.Moose(eyes='dead')
msg = cheese.milk("my witty mesage, i'm dead")
print(msg)

# Get a cow by name
cow_cls = cow.get_cow('moose')
cheese = cow_cls()
msg = cheese.milk("Cow by name is moose")
print(msg)

# Create a Cow with a thought bubble, a tongue, and dead eyes
cheese = cow.Moose(thoughts=True, tongue=True, eyes='dead')
msg = cheese.milk("My witty mesage with several attributes")
print(msg)

这个函数可以随机抽一个风格的牛生成图像:

# Create a random cow with a message
msg = cow.milk_random_cow("A random message for fun")
print(msg)

这个方法可以列举所有备选图像:

# all the eye options
eye_options = cow.eye_options()
print(eye_options)

# all the cowacter options
cow_options = cow.cow_options()
print(cow_options)

也可以引入fortune的语料库随机抽文字给cawsay,如此简单两步就可以让你的python脚本瞬间拥有大佬内味儿了。

   __________________________________________________________________
/ Do not argue with an idiot.                                      \
\ He will drag you down to his level and beat you with experience. /
 ------------------------------------------------------------------
  o
     o
                  _ _
       | \__/|  .~    ~.
       /$$ `./      .'
      {o__,   \    {
        / .  . )    \
        `-` '-' \    }
       .(   _(   )_.'
      '---.~_ _ _|
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 关系数据库入门 关系数据库概述 数据持久化 - 将数据保存到能够长久保存数据的存储介质中,在掉电的情况下数据也不会...
    d4d98020ef88阅读 715评论 0 0
  • 一,Python介绍 1. python的出生与应用 python的创始人为吉多·范罗苏姆(Guido van R...
    曹元_阅读 213评论 0 8
  • 计算机的硬件组成 现代计算机的基本组成部分其实主要由三部分组成:CPU,内存,主板。 你撰写的程序,打开的任何PC...
    HikariCP阅读 2,919评论 1 4
  • 之前Python就已经学过了,Flask也玩的一溜一溜的。但是间隔时间有些长,所以就找个一个项目重新温习一下,顺便...
    d4d98020ef88阅读 1,689评论 1 0
  • 我是黑夜里大雨纷飞的人啊 1 “又到一年六月,有人笑有人哭,有人欢乐有人忧愁,有人惊喜有人失落,有的觉得收获满满有...
    陌忘宇阅读 8,592评论 28 53