Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
.venv/
1 change: 1 addition & 0 deletions implement-cowsay/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.venv/
51 changes: 51 additions & 0 deletions implement-cowsay/cow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env python3

import argparse
import cowsay
import inspect
import sys

def get_supported_animals():
animal_list = []
for name, func in inspect.getmembers(cowsay, inspect.isfunction):
if not name.startswith('_') and name not in ['cowsay', 'cowthink']:
animal_list.append(name)
return sorted(animal_list)


def main():
animals = get_supported_animals()

parser = argparse.ArgumentParser(
prog='cowsay',
description='Make animals say things'
)

parser.add_argument(
'message',
nargs='+',
help='The message to say.'
)

parser.add_argument(
'--animal',
choices=animals,
default='cow',
help=f'The animal to be saying things.'
)

args = parser.parse_args()
message = ' '.join(args.message)

# Dynamically call the animal function (like cowsay.turtle("hello"))
try:
animal_func = getattr(cowsay, args.animal)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can check out an alternative get_output_string method here to avoid getattr (not the best choice to use if there is alternative - it make code more readable).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted, Thank you 🙏

output = animal_func(message)
if output is not None:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So can you explain in which cases the output is None here and when it is not None?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Output is None only if the animal function doesn't return anything. In the official cowsay module, it always returns a string.

The output is not None in normal cases because standard cowsay animal functions return a string of ASCII art.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does it print None when you remove if output is not None:? What is considered to be not a normal case?

print(output)
except AttributeError:
print(f"Error: '{args.animal}' is not a valid animal.", file=sys.stderr)
sys.exit(1)

if __name__ == '__main__':
main()
1 change: 1 addition & 0 deletions implement-cowsay/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cowsay