-
-
Notifications
You must be signed in to change notification settings - Fork 42
London | 25-SDC-July | Eyuel Abraham | Sprint 4 | Implementing cowsay #157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
5af07ad
e448fe5
96c0d12
687e4d8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| node_modules | ||
| .venv/ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| .venv/ |
| 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) | ||
| output = animal_func(message) | ||
| if output is not None: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So can you explain in which cases the output is
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does it print |
||
| print(output) | ||
| except AttributeError: | ||
| print(f"Error: '{args.animal}' is not a valid animal.", file=sys.stderr) | ||
| sys.exit(1) | ||
|
|
||
| if __name__ == '__main__': | ||
| main() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| cowsay |
There was a problem hiding this comment.
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_stringmethod here to avoidgetattr(not the best choice to use if there is alternative - it make code more readable).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noted, Thank you 🙏