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/
50 changes: 50 additions & 0 deletions implement-cowsay/cow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/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).

output = animal_func(message)
print(output)

Choose a reason for hiding this comment

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

You program always prints none in the end, please debug why this is and fix (should be a simple fix :) )

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