The computer program cowsay generates ASCII pictures of a cow with a message.
+----------------------------+
| Moo may represent an idea, |
| but only the cow knows. |
+----------------------------+
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
It can also generate pictures using pre-made images of other animals such as Tux the Penguin1, the Linux mascot.
+----------------------------+
| Moo may represent an idea, |
| but only the cow knows. |
+----------------------------+
\
\
.--.
|^_^ |
|:_/ |
// \ \
(| | )
/'\_ _/`\
\___)=(___/
The computer program uses pre-made images that are stored in text files with the extension .cow, enabling it to produce different variants of "cows". Each such text file also contains two occurrences of a pair of braces ({}) enabling the program to produce different kinds of eyes. The program initially was a kind of inside joke within hacker culture, but has been around long enough that its use has become rather widespread.
Define a class Cowsay that can be used to generate ASCII pictures of a "cow" with a message. The objects of this class must at least support the following methods:
An initialization method __init__ that takes a series (list or tuple) of strings as its argument. The given series of strings are the successive lines of the message said by the "cow". After initialization of the object, the message is never changed anymore.
A method __repr__ that retuns a string representation of the object. This string representation contains the given series of strings that are centered across the width of the longest line of the message, with an additional space to the left and to the right. Use the string method center to center the lines of the message. A rectangular box must be drawn around the centered lines of the message. Determine the format of the box from the examples given below.
A method setEyes that can be used to set a specific pair of eyes for the "cow". The method takes a single argument. If the argument passed to the method is not a two-character string, an AssertionError must be raised with the message invalid eyes. In generating the ASCII picture, the first character of the argument must be used to replace the first pair of braces in the .cow file containing the pre-made image, and the second character to replace the second pair of braces. If an ASCII picture is generated for the object before this method is called a first time on the object, by default the lowercase letter o must be used twice to represent the eyes.
A method setImage that can be used to set a specific pre-made image that is used when generating an ASCII picture for the current object. The method takes the location of a .cow file. If an ASCII picture is generated for the object before this method is called a first time on the object, by default the pre-made image must be used that is stored in the text file cow.cow2. The source code may assume that this file is located in the current directory.
A method __str__ that retuns a string containing the generated ASCII picture of the object. This string representation is a multiline string, whose first few lines correspond to the string representation as generated by the method __repr__, followed by the lines of the last pre-made image set for the object (or the pre-made image stored in the file cow.cow3 in case no explicit image was set for the object). In the lines containing the pre-made image, the eyes must be replaced by the last pair of eyes set for the object (or twice the lowercase letter o in case no eyes were set explicitly for the object).
In the following interactive session we assume that the text files cow.cow4 and tux.cow5 are in the current directory. Note that a docstring is nothing but an ordinary string, which requires that each backslash that occurs literally in the docstring must be repeated twice. Click here to convert the following interactive session into a docstring.
>>> quote = Cowsay(['Moo may represent an idea,', 'but only the cow knows.'])
>>> quote
+----------------------------+
| Moo may represent an idea, |
| but only the cow knows. |
+----------------------------+
>>> print(quote)
+----------------------------+
| Moo may represent an idea, |
| but only the cow knows. |
+----------------------------+
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
>>> quote.setEyes(33)
Traceback (most recent call last):
AssertionError: invalid eyes
>>> quote.setEyes('***')
Traceback (most recent call last):
AssertionError: invalid eyes
>>> quote.setEyes('^^')
>>> print(quote)
+----------------------------+
| Moo may represent an idea, |
| but only the cow knows. |
+----------------------------+
\ ^__^
\ (^^)\_______
(__)\ )\/\
||----w |
|| ||
>>> quote.setImage('tux.cow')
>>> print(quote)
+----------------------------+
| Moo may represent an idea, |
| but only the cow knows. |
+----------------------------+
\
\
.--.
|^_^ |
|:_/ |
// \ \
(| | )
/'\_ _/`\
\___)=(___/
Traceback (most recent call last):
AssertionError: invalid eyes