Skip to main content

A Cowsay clone in Python

Project description

Python Cowsay

A rewrite of cowsay in python. Allows for parsing of existing .cow files.

Install

pip install python-cowsay

Usage

The classic cowsay can be generated by the cowsay or cowthink functions:

from cowsay import cowsay

message = """
The most remarkable thing about my mother is that for thirty years she served
the family nothing but leftovers.  The original meal has never been found.
		-- Calvin Trillin
""".strip()
print(cowsay(message))

Will yield:

 __________________________________________ 
/ The most remarkable thing about my       \
| mother is that for thirty years she      |
| served the family nothing but leftovers. |
| The original meal has never been found.  |
|                                          |
\ -- Calvin Trillin                        /
 ------------------------------------------ 
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

The parameters for these functions are:

  • message – a string to wrap in the text bubble
  • cow='default' – the name of the cow (valid names from list_cows)
  • preset=None – the original cowsay presets: -bgpstwy
  • eyes=Option.eyes – A custom eye string
  • tongue=Option.tongue – A custom tongue string
  • width=40 – The width of the text bubble
  • wrap_text=True – Whether text should be wrapped in the bubble
  • cowfile=None – A custom string representing a cow

Other Functions

The available builtin cows can be found with list_cows. A cow can be chosen randomly from this list with get_random_cow.

Using Your Own Cows

A custom .cow file can be parsed using the read_dot_cow function which takes a TextIO stream. I.e., You can either create a TextIO from a string or read a file.

The read_dot_cow will look for the first heredoc in the steam and extract the heredoc contents. If no heredoc exists, the whole stream is used instead. Escape characters are then escaped. The default escape characters can be changed by passing in an optional escape dictionary parameter mapping escape codes to their chars.

For example:

from io import StringIO

from cowsay import read_dot_cow, cowthink

cow = read_dot_cow(StringIO("""
$the_cow = <<EOC;
         $thoughts
          $thoughts
           ___
          (o o)
         (  V  )
        /--m-m-
EOC
"""))
message = """
Nothing is illegal if one hundred businessmen decide to do it.
        -- Andrew Young
""".strip()
print(cowthink(message, cowfile=cow))

Will yield:

 ___________________________________ 
( Nothing is illegal if one hundred )
( businessmen decide to do it.      )
(                                   )
( -- Andrew Young                   )
 ----------------------------------- 
         o
          o
           ___
          (o o)
         (  V  )
        /--m-m-

Full-Width Characters

A bit of a hack at the moment, but if any full-width characters are found in the message string, all characters in the thought bubble are converted to full-width. For example:

 ____________ 
( 喵喵喵。我是一只猫。 )
 ------------ 
   o
    o

     |\_/|
     |o o|__
     --*--__\
     C_C_(___)

This works fine when all characters in the message are full-width, but does not work so well when there is a mix of full- and neutral-width characters:

 _________________ 
( 喵喵喵。I am a cat. )
 ----------------- 
   o
    o

     |\_/|
     |o o|__
     --*--__\
     C_C_(___)

Each full-width character still only counts as one character when setting the text width. For example:

from cowsay import cowthink

message = "喵喵喵。我是一只猫。我想吃鱼和喝牛奶。"
print(cowthink(message, cow="kitten", width=10))

Will yield:

 ____________ 
( 喵喵喵。我是一只猫。 )
( 我想吃鱼和喝牛奶。  )
 ------------ 
   o
    o

     |\_/|
     |o o|__
     --*--__\
     C_C_(___)

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

python-cowsay-1.1.0.tar.gz (18.7 kB view details)

Uploaded Source

Built Distribution

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

python_cowsay-1.1.0-py3-none-any.whl (27.4 kB view details)

Uploaded Python 3

File details

Details for the file python-cowsay-1.1.0.tar.gz.

File metadata

  • Download URL: python-cowsay-1.1.0.tar.gz
  • Upload date:
  • Size: 18.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for python-cowsay-1.1.0.tar.gz
Algorithm Hash digest
SHA256 02936c4e543e185a4408eba212a526309ed8abe6413e87d4d316a6d0f2dc8ff1
MD5 51dc64529a0ec51d2f59c63fa932ce06
BLAKE2b-256 30c17d3a8ad816682b65a36c707dbe2d3bcbc43e5d4eb5f3c6bd69a53462add6

See more details on using hashes here.

File details

Details for the file python_cowsay-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: python_cowsay-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 27.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for python_cowsay-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0deae0b0d2ce084057a254b1491a9e8b66697832bc3fbe1d63dd3a2b2b76a2fa
MD5 af1279629cba7c0142759fe41d883522
BLAKE2b-256 5dcb167c0eb7a6e7397b03c0667b64a32b3aaab13eb6cd7dbe5e13b6c060c946

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