diff --git a/.gitignore b/.gitignore index 68ca39690..4bd4b80b0 100644 --- a/.gitignore +++ b/.gitignore @@ -11,8 +11,6 @@ tex2pdf* .coverage .idea .vscode -02_crowsnest/crowsnest.py -03_picnic/picnic.py 04_jump_the_five/jump.py 05_howler/howler.py 06_wc/wc.py diff --git a/01_hello/hello.py b/01_hello/hello.py new file mode 100755 index 000000000..9250aa78f --- /dev/null +++ b/01_hello/hello.py @@ -0,0 +1,28 @@ +#!/Users/yida.wang/miniconda3/envs/py39/bin/python3 +""" +Author: Yida Wang +Purpose: Say hello +""" + +import argparse + + +def get_args(): + """Get the command-line arguments""" + + parser = argparse.ArgumentParser(description='Say hello') + parser.add_argument('-n', '--name', metavar='name', + default='World', help='Name to greet') + args = parser.parse_args() + return 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..85642e0a1 --- /dev/null +++ b/01_hello/hello2.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python3 +""" +Author : Yida Wang +Date : 2021-06-27 +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() diff --git a/02_crowsnest/crowsnest.py b/02_crowsnest/crowsnest.py new file mode 100755 index 000000000..dc7e81dac --- /dev/null +++ b/02_crowsnest/crowsnest.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 +""" +Author : Yida Wang +Date : 2021-07-01 +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') + + return parser.parse_args() + + +# -------------------------------------------------- +def main(): + """Make a jazz noise here""" + + args = get_args() + word = args.word + article = 'an' if word[0].lower() in 'aeiou' else 'a' + + print(f'Ahoy, Captain, {article} {word} off the larboard bow!') + + +# -------------------------------------------------- +if __name__ == '__main__': + main() diff --git a/03_picnic/picnic.py b/03_picnic/picnic.py new file mode 100755 index 000000000..a054b143e --- /dev/null +++ b/03_picnic/picnic.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 +""" +Author : Yida Wang +Date : 2021-07-02 +Purpose: Picnic game +""" + +import argparse + + +# -------------------------------------------------- +def get_args(): + """Get command-line arguments""" + + parser = argparse.ArgumentParser( + description='Picnic game', + formatter_class=argparse.ArgumentDefaultsHelpFormatter) + + parser.add_argument('items', + metavar='str', + nargs='+', + help='Item(s) to bring') + + parser.add_argument('-s', + '--sorted', + help='Sort the items', + action='store_true') + + return parser.parse_args() + + +# -------------------------------------------------- +def main(): + """Make a jazz noise here""" + + args = get_args() + items = args.items + to_sort = args.sorted + + num = len(items) + + if to_sort: + items.sort() + + bringing = '' + if num == 1: + bringing = items[0] + elif num == 2: + bringing = items[0] + ' and ' + items[1] + else: + bringing = ', '.join(items[:-1]) + ', and ' + items[-1] + + print(f'You are bringing {bringing}.') + + +# -------------------------------------------------- +if __name__ == '__main__': + main()