From e3cb9135830c6ba9aed4f319c9958116adcd7f26 Mon Sep 17 00:00:00 2001 From: Robert Mikolajczyk Date: Mon, 12 May 2025 11:29:09 +0200 Subject: [PATCH 1/2] completed 01_hello --- .gitignore | 1 + 01_hello/hello.py | 30 +++++++++++++++++++ 01_hello/hello2.py | 72 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100755 01_hello/hello.py create mode 100755 01_hello/hello2.py diff --git a/.gitignore b/.gitignore index 68ca39690..d3533f6ca 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ tex2pdf* .coverage .idea .vscode +.python-version 02_crowsnest/crowsnest.py 03_picnic/picnic.py 04_jump_the_five/jump.py diff --git a/01_hello/hello.py b/01_hello/hello.py new file mode 100755 index 000000000..3cb640d2f --- /dev/null +++ b/01_hello/hello.py @@ -0,0 +1,30 @@ +#! /usr/bin/env python3 +""" +Author: Robert Mikolajczyk +email: ronert1674@gmail.com +Purpose: Say hello +""" + +import argparse + + +# -------------------------------------------------- +def get_args(): + """Get command-line arguments""" + parser = argparse.ArgumentParser(description="Say hello") + parser.add_argument( + "-n", "--name", metavar="name", default="World", help="Name to greet" + ) + return parser.parse_args() + + +# -------------------------------------------------- +def main(): + """Make a jazz noise here""" + args = get_args() + print("Hello, " + args.name + "!") + + +# -------------------------------------------------- +if __name__ == "__main__": + main() diff --git a/01_hello/hello2.py b/01_hello/hello2.py new file mode 100755 index 000000000..5b4896dca --- /dev/null +++ b/01_hello/hello2.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python3 +""" +Author : robertmikolajczyk +Date : 2025-05-11 +Purpose: Rock the Casbah +""" + +import argparse + + +# -------------------------------------------------- +def get_args(): + """Get command-line arguments""" + + parser = argparse.ArgumentParser( + description='Rock the Casbah', + formatter_class=argparse.ArgumentDefaultsHelpFormatter) + + parser.add_argument('positional', + metavar='str', + help='A positional argument') + + parser.add_argument('-a', + '--arg', + help='A named string argument', + metavar='str', + type=str, + default='') + + parser.add_argument('-i', + '--int', + help='A named integer argument', + metavar='int', + type=int, + default=0) + + parser.add_argument('-f', + '--file', + help='A readable file', + metavar='FILE', + type=argparse.FileType('rt'), + default=None) + + parser.add_argument('-o', + '--on', + help='A boolean flag', + action='store_true') + + return parser.parse_args() + + +# -------------------------------------------------- +def main(): + """Make a jazz noise here""" + + args = get_args() + str_arg = args.arg + int_arg = args.int + file_arg = args.file + flag_arg = args.on + pos_arg = args.positional + + print(f'str_arg = "{str_arg}"') + print(f'int_arg = "{int_arg}"') + print('file_arg = "{}"'.format(file_arg.name if file_arg else '')) + print(f'flag_arg = "{flag_arg}"') + print(f'positional = "{pos_arg}"') + + +# -------------------------------------------------- +if __name__ == '__main__': + main() From ce5eaa6b267997a49937631428a876e2370fd988 Mon Sep 17 00:00:00 2001 From: Robert Mikolajczyk Date: Mon, 12 May 2025 13:47:31 +0200 Subject: [PATCH 2/2] completed 02_crowsnest --- .gitignore | 1 - 02_crowsnest/crowsnest.py | 46 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100755 02_crowsnest/crowsnest.py diff --git a/.gitignore b/.gitignore index d3533f6ca..a12e7b2dd 100644 --- a/.gitignore +++ b/.gitignore @@ -12,7 +12,6 @@ tex2pdf* .idea .vscode .python-version -02_crowsnest/crowsnest.py 03_picnic/picnic.py 04_jump_the_five/jump.py 05_howler/howler.py diff --git a/02_crowsnest/crowsnest.py b/02_crowsnest/crowsnest.py new file mode 100755 index 000000000..6bd7151ac --- /dev/null +++ b/02_crowsnest/crowsnest.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 +""" +Author : robertmikolajczyk +Date : 2025-05-12 +Purpose: Crow's Nest +""" + +import argparse + + +# -------------------------------------------------- +def get_args(): + """Get command-line arguments""" + + parser = argparse.ArgumentParser( + description="Crow's Nest -- choose the correct article", + formatter_class=argparse.ArgumentDefaultsHelpFormatter, + ) + + parser.add_argument("word", + metavar="word", + help="A word") + + parser.add_argument('--starboard', + help='A right side of the board', + action='store_true') + + return parser.parse_args() + + +# -------------------------------------------------- +def main(): + """Make a jazz noise here""" + + args = get_args() + word = args.word + side = args.starboard + + sideboard = "starboard" if side else "larboard" + article = "an" if word[0].lower() in "aeiou" else "a" + print(f"Ahoy, Captain, {article} {word} off the {sideboard} bow!") + + +# -------------------------------------------------- +if __name__ == "__main__": + main()