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
CLI
Installing python-cowsay
adds cowsay
and cowthink
terminal programs.
These work similarly to the classic cowsay CLIs.
See cowsay --help
or cowthink --help
for more info.
Programmatic Execution
The classic cowsay text 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 bubblecow='default'
– the name of the cow (valid names fromlist_cows
)preset=None
– the original cowsay presets:-bgpstwy
eyes=Option.eyes
– A custom eye stringtongue=Option.tongue
– A custom tongue stringwidth=40
– The width of the text bubblewrap_text=True
– Whether text should be wrapped in the bubblecowfile=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-
Parsing .cow
Files
As discussed in this issue,
.cow
files are just files containing Perl code. This causes some issues for
more advanced .cow
files that do things like define additional variables
that get used in the cow heredoc. Most notably, this happens when using tools
like Charc0al's cowsay file converter.
python-cowsay
does not fully support Perl .cow
files but has accounted for
this one case. When parsing .cow
files, python-cowsay
will look for any
string variable declarations at the start of each line and the cow heredoc.
If any string variables are found, these are inlined in the resulting cow.
Changing the Cows
python-cowsay
will attempt to retrieve a COWPATH
environment variable, if
found, the path this variable references will be used instead of the default
that is installed with python-cowsay
.
If you wish to change the default cows, set the COWPATH
environment variable
in your shell profile:
export COWPATH=path/to/cows
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_(___)
Related projects
- The original cowsay repository
- Cowexcept to jazz up Python exceptions
- fortune-python — a rewrite of fortune in Python
- This collection of additional cow files
Notes
The cow files that are installed by default in this package are taken from the original cowsay repository Please see that repo for their corresponding license.
The cows provided in this package, save for minor edits to resolve issues with parsing cow files, are otherwise provided as they were in the original cowsay repository. I do not take responsibility for the content of these cow files.
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
Built Distribution
File details
Details for the file python-cowsay-1.2.0.tar.gz
.
File metadata
- Download URL: python-cowsay-1.2.0.tar.gz
- Upload date:
- Size: 19.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | eae49ea3abc00dba38f166e92f2e8f103a75182a944eb7b79aab5b515ca0363d |
|
MD5 | 3b8355542115c79fb2882559f92cde41 |
|
BLAKE2b-256 | 083d4e2891d47b4d9653f3c3aafd844877644ae95a2e8821058adbcf59696ba5 |
File details
Details for the file python_cowsay-1.2.0-py3-none-any.whl
.
File metadata
- Download URL: python_cowsay-1.2.0-py3-none-any.whl
- Upload date:
- Size: 26.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 69aa91072e36ff27ceb8261718323c9ab275b3470b2260841c5b9ad5d83e8ff3 |
|
MD5 | 14a702c04c37698bf3f479b62e235295 |
|
BLAKE2b-256 | b34fb80170bcd5fbbc86d7608002a3056a610426fe7ca4605d8354528639ac48 |