From 9d1728c0e0312a127c3c22b75e0bde563884ff5e Mon Sep 17 00:00:00 2001 From: theFippo Date: Sun, 1 Jan 2023 17:48:43 +0100 Subject: [PATCH 01/22] ch01: create hello world file --- 01_hello/hello.py | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 01_hello/hello.py diff --git a/01_hello/hello.py b/01_hello/hello.py new file mode 100644 index 000000000..cae95c454 --- /dev/null +++ b/01_hello/hello.py @@ -0,0 +1,2 @@ +print("Hello, World!") + From b44b044d5047951056ab194b9b4d409dc8f7a38c Mon Sep 17 00:00:00 2001 From: theFippo Date: Sun, 1 Jan 2023 19:31:44 +0100 Subject: [PATCH 02/22] Ch01: add first comment --- 01_hello/hello.py | 1 + 1 file changed, 1 insertion(+) diff --git a/01_hello/hello.py b/01_hello/hello.py index cae95c454..8bddc5e73 100644 --- a/01_hello/hello.py +++ b/01_hello/hello.py @@ -1,2 +1,3 @@ +# Purpose: Say hello print("Hello, World!") From 5f598948e17a5defadfe17ae388bd1f1c00cd0af Mon Sep 17 00:00:00 2001 From: theFippo Date: Sun, 1 Jan 2023 20:27:14 +0100 Subject: [PATCH 03/22] ch01 fix test executable --- 01_hello/test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/01_hello/test.py b/01_hello/test.py index d0cedd1c2..6d05ffeb0 100755 --- a/01_hello/test.py +++ b/01_hello/test.py @@ -4,7 +4,7 @@ import os from subprocess import getstatusoutput, getoutput -prg = './hello.py' +prg = 'hello.py' # -------------------------------------------------- From f34180880c4d187c251aa3b7911225b67216d873 Mon Sep 17 00:00:00 2001 From: theFippo Date: Sun, 1 Jan 2023 20:30:03 +0100 Subject: [PATCH 04/22] ch01: add shebang --- 01_hello/hello.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/01_hello/hello.py b/01_hello/hello.py index 8bddc5e73..d17b6df72 100644 --- a/01_hello/hello.py +++ b/01_hello/hello.py @@ -1,3 +1,3 @@ +#!/usr/bin/env python3 # Purpose: Say hello print("Hello, World!") - From ff354b40b978155dbeb5adfa354c9906b0f2e528 Mon Sep 17 00:00:00 2001 From: theFippo Date: Mon, 2 Jan 2023 11:37:15 +0100 Subject: [PATCH 05/22] ch01: add positional argument and help doc --- 01_hello/hello.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/01_hello/hello.py b/01_hello/hello.py index d17b6df72..c95e24c77 100644 --- a/01_hello/hello.py +++ b/01_hello/hello.py @@ -1,3 +1,9 @@ #!/usr/bin/env python3 # Purpose: Say hello -print("Hello, World!") + +import argparse + +parser = argparse.ArgumentParser(description="Say hello") +parser.add_argument("name", help="Name to greet") +args = parser.parse_args() +print("Hello, " + args.name + "!") From e070255edd8e0243ea91fe9e1458557c6a2ed953 Mon Sep 17 00:00:00 2001 From: theFippo Date: Mon, 2 Jan 2023 13:38:03 +0100 Subject: [PATCH 06/22] ch01: make name argument optional --- 01_hello/hello.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/01_hello/hello.py b/01_hello/hello.py index c95e24c77..b42c854b7 100644 --- a/01_hello/hello.py +++ b/01_hello/hello.py @@ -1,9 +1,11 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python # Purpose: Say hello import argparse parser = argparse.ArgumentParser(description="Say hello") -parser.add_argument("name", help="Name to greet") +parser.add_argument( + "-n", "--name", metavar="name", default="World", help="Name to greet" +) args = parser.parse_args() print("Hello, " + args.name + "!") From 733c30b7be9ee7d034ef7f3339229bd58ff64566 Mon Sep 17 00:00:00 2001 From: theFippo Date: Mon, 2 Jan 2023 13:42:33 +0100 Subject: [PATCH 07/22] ch01: wrap program into a main function --- 01_hello/hello.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/01_hello/hello.py b/01_hello/hello.py index b42c854b7..23007f23a 100644 --- a/01_hello/hello.py +++ b/01_hello/hello.py @@ -3,9 +3,15 @@ import argparse -parser = argparse.ArgumentParser(description="Say hello") -parser.add_argument( - "-n", "--name", metavar="name", default="World", help="Name to greet" -) -args = parser.parse_args() -print("Hello, " + args.name + "!") + +def main(): + parser = argparse.ArgumentParser(description="Say hello") + parser.add_argument( + "-n", "--name", metavar="name", default="World", help="Name to greet" + ) + args = parser.parse_args() + print("Hello, " + args.name + "!") + + +if __name__ == "__main__": + main() From ca2b9c62c0e8d09e392d06818ea0f41eef762c85 Mon Sep 17 00:00:00 2001 From: theFippo Date: Mon, 2 Jan 2023 13:46:09 +0100 Subject: [PATCH 08/22] ch01: separate getting arguments from main --- 01_hello/hello.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/01_hello/hello.py b/01_hello/hello.py index 23007f23a..df6b7b41d 100644 --- a/01_hello/hello.py +++ b/01_hello/hello.py @@ -4,12 +4,16 @@ import argparse -def main(): +def get_args(): parser = argparse.ArgumentParser(description="Say hello") parser.add_argument( "-n", "--name", metavar="name", default="World", help="Name to greet" ) - args = parser.parse_args() + return parser.parse_args() + + +def main(): + args = get_args() print("Hello, " + args.name + "!") From c8d7c9b64b360885ae5306f9d1770b6063690f85 Mon Sep 17 00:00:00 2001 From: theFippo Date: Mon, 2 Jan 2023 13:52:16 +0100 Subject: [PATCH 09/22] ch01: add docstrings --- 01_hello/hello.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/01_hello/hello.py b/01_hello/hello.py index df6b7b41d..964bf37cb 100644 --- a/01_hello/hello.py +++ b/01_hello/hello.py @@ -1,10 +1,14 @@ #!/usr/bin/env python -# Purpose: Say hello +""" +Author: Jeffrey Schmid-Paz +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" @@ -13,6 +17,7 @@ def get_args(): def main(): + """Run the program""" args = get_args() print("Hello, " + args.name + "!") From 6616852d1840d0f1f281a24c8cb4054ca1e2c6bc Mon Sep 17 00:00:00 2001 From: theFippo Date: Mon, 2 Jan 2023 15:14:10 +0100 Subject: [PATCH 10/22] ch02: fix usage test and formatting --- 02_crowsnest/test.py | 53 +++++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/02_crowsnest/test.py b/02_crowsnest/test.py index d4e25c6c7..5ac387931 100755 --- a/02_crowsnest/test.py +++ b/02_crowsnest/test.py @@ -4,15 +4,32 @@ import os from subprocess import getstatusoutput, getoutput -prg = './crowsnest.py' +prg = "crowsnest.py" consonant_words = [ - 'brigantine', 'clipper', 'dreadnought', 'frigate', 'galleon', 'haddock', - 'junk', 'ketch', 'longboat', 'mullet', 'narwhal', 'porpoise', 'quay', - 'regatta', 'submarine', 'tanker', 'vessel', 'whale', 'xebec', 'yatch', - 'zebrafish' + "brigantine", + "clipper", + "dreadnought", + "frigate", + "galleon", + "haddock", + "junk", + "ketch", + "longboat", + "mullet", + "narwhal", + "porpoise", + "quay", + "regatta", + "submarine", + "tanker", + "vessel", + "whale", + "xebec", + "yatch", + "zebrafish", ] -vowel_words = ['aviso', 'eel', 'iceberg', 'octopus', 'upbound'] -template = 'Ahoy, Captain, {} {} off the larboard bow!' +vowel_words = ["aviso", "eel", "iceberg", "octopus", "upbound"] +template = "Ahoy, Captain, {} {} off the larboard bow!" # -------------------------------------------------- @@ -26,10 +43,10 @@ def test_exists(): def test_usage(): """usage""" - for flag in ['-h', '--help']: - rv, out = getstatusoutput(f'{prg} {flag}') + for flag in ["-h", "--help"]: + rv, out = getstatusoutput(f"{prg} {flag}") assert rv == 0 - assert out.lower().startswith('usage') + assert out.lower().startswith("usage") # -------------------------------------------------- @@ -37,8 +54,8 @@ def test_consonant(): """brigantine -> a brigantine""" for word in consonant_words: - out = getoutput(f'{prg} {word}') - assert out.strip() == template.format('a', word) + out = getoutput(f"{prg} {word}") + assert out.strip() == template.format("a", word) # -------------------------------------------------- @@ -46,8 +63,8 @@ def test_consonant_upper(): """brigantine -> a Brigatine""" for word in consonant_words: - out = getoutput(f'{prg} {word.title()}') - assert out.strip() == template.format('a', word.title()) + out = getoutput(f"{prg} {word.title()}") + assert out.strip() == template.format("a", word.title()) # -------------------------------------------------- @@ -55,8 +72,8 @@ def test_vowel(): """octopus -> an octopus""" for word in vowel_words: - out = getoutput(f'{prg} {word}') - assert out.strip() == template.format('an', word) + out = getoutput(f"{prg} {word}") + assert out.strip() == template.format("an", word) # -------------------------------------------------- @@ -64,5 +81,5 @@ def test_vowel_upper(): """octopus -> an Octopus""" for word in vowel_words: - out = getoutput(f'{prg} {word.upper()}') - assert out.strip() == template.format('an', word.upper()) + out = getoutput(f"{prg} {word.upper()}") + assert out.strip() == template.format("an", word.upper()) From dd275b5e602de82f792650f409ce523ba92dd909 Mon Sep 17 00:00:00 2001 From: theFippo Date: Mon, 2 Jan 2023 16:17:00 +0100 Subject: [PATCH 11/22] ch02: create initial program --- .gitignore | 1 - 02_crowsnest/crowsnest.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 02_crowsnest/crowsnest.py diff --git a/.gitignore b/.gitignore index 68ca39690..c78b21b50 100644 --- a/.gitignore +++ b/.gitignore @@ -11,7 +11,6 @@ tex2pdf* .coverage .idea .vscode -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 100644 index 000000000..8ab0918fe --- /dev/null +++ b/02_crowsnest/crowsnest.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python +""" +Author : Jeffrey Schmid-Paz +Date : 2023-01-02 +Purpose: Warn captain for a something near the ship +""" + +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(): + """Run the program""" + + args = get_args() + pos_arg = args.positional + + print(f'positional = "{pos_arg}"') + + +# -------------------------------------------------- +if __name__ == "__main__": + main() From c388fe8dfe90a7ec874829fa60f275bead7381e5 Mon Sep 17 00:00:00 2001 From: theFippo Date: Mon, 2 Jan 2023 16:19:57 +0100 Subject: [PATCH 12/22] ch02: create output for argument --- 02_crowsnest/crowsnest.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/02_crowsnest/crowsnest.py b/02_crowsnest/crowsnest.py index 8ab0918fe..0723c0805 100644 --- a/02_crowsnest/crowsnest.py +++ b/02_crowsnest/crowsnest.py @@ -27,9 +27,9 @@ def main(): """Run the program""" args = get_args() - pos_arg = args.positional + word = args.word - print(f'positional = "{pos_arg}"') + print(word) # -------------------------------------------------- From cf294871ce29b0fc1cdac7f8df47c96212f29d20 Mon Sep 17 00:00:00 2001 From: theFippo Date: Mon, 2 Jan 2023 16:29:47 +0100 Subject: [PATCH 13/22] ch02: modify output to concatenated phrase --- 02_crowsnest/crowsnest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/02_crowsnest/crowsnest.py b/02_crowsnest/crowsnest.py index 0723c0805..06cecf9d4 100644 --- a/02_crowsnest/crowsnest.py +++ b/02_crowsnest/crowsnest.py @@ -29,7 +29,7 @@ def main(): args = get_args() word = args.word - print(word) + print("Ahoy, Captain, a " + word + " off the larboard bow!") # -------------------------------------------------- From ffa532bfb4a13f65f7c9353b2dc601b210fc211e Mon Sep 17 00:00:00 2001 From: theFippo Date: Mon, 2 Jan 2023 19:32:46 +0100 Subject: [PATCH 14/22] ch02: make article dependent on first letter --- 02_crowsnest/crowsnest.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/02_crowsnest/crowsnest.py b/02_crowsnest/crowsnest.py index 06cecf9d4..6c533dfd3 100644 --- a/02_crowsnest/crowsnest.py +++ b/02_crowsnest/crowsnest.py @@ -28,8 +28,9 @@ def main(): args = get_args() word = args.word + article = "an" if word[0].lower() in "aeiou" else "a" - print("Ahoy, Captain, a " + word + " off the larboard bow!") + print(f"Ahoy, Captain, {article} {word} off the larboard bow!") # -------------------------------------------------- From 6dc56915d8e24ccb37dadb5234eeda82ff369837 Mon Sep 17 00:00:00 2001 From: theFippo Date: Thu, 5 Jan 2023 20:14:07 +0100 Subject: [PATCH 15/22] ch03: create initial program --- .gitignore | 1 - 03_picnic/picnic.py | 74 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 03_picnic/picnic.py diff --git a/.gitignore b/.gitignore index c78b21b50..4bd4b80b0 100644 --- a/.gitignore +++ b/.gitignore @@ -11,7 +11,6 @@ tex2pdf* .coverage .idea .vscode -03_picnic/picnic.py 04_jump_the_five/jump.py 05_howler/howler.py 06_wc/wc.py diff --git a/03_picnic/picnic.py b/03_picnic/picnic.py new file mode 100644 index 000000000..656e1c853 --- /dev/null +++ b/03_picnic/picnic.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python3 +""" +Author : Jeffrey Schmid-Paz +Date : 2023-01-05 +Purpose: Create a list of items to bring along a picnic +""" + +import argparse + + +# -------------------------------------------------- +def get_args(): + """Get command-line arguments""" + + parser = argparse.ArgumentParser( + description="Picnic game", + 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 2d8bf0ef2400c0630b6e48a082a26ed13e50144f Mon Sep 17 00:00:00 2001 From: theFippo Date: Thu, 5 Jan 2023 20:14:30 +0100 Subject: [PATCH 16/22] ch03: fix test and reformat --- 03_picnic/test.py | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/03_picnic/test.py b/03_picnic/test.py index 281fab552..f70b1ad7f 100755 --- a/03_picnic/test.py +++ b/03_picnic/test.py @@ -4,7 +4,7 @@ import os from subprocess import getoutput -prg = './picnic.py' +prg = "picnic.py" # -------------------------------------------------- @@ -18,17 +18,17 @@ def test_exists(): def test_usage(): """usage""" - for flag in ['', '-h', '--help']: - out = getoutput(f'{prg} {flag}') - assert out.lower().startswith('usage') + for flag in ["", "-h", "--help"]: + out = getoutput(f"{prg} {flag}") + assert out.lower().startswith("usage") # -------------------------------------------------- def test_one(): """one item""" - out = getoutput(f'{prg} chips') - assert out.strip() == 'You are bringing chips.' + out = getoutput(f"{prg} chips") + assert out.strip() == "You are bringing chips." # -------------------------------------------------- @@ -36,7 +36,7 @@ def test_two(): """two items""" out = getoutput(f'{prg} soda "french fries"') - assert out.strip() == 'You are bringing soda and french fries.' + assert out.strip() == "You are bringing soda and french fries." # -------------------------------------------------- @@ -44,9 +44,10 @@ def test_more_than_two(): """more than two items""" arg = '"potato chips" coleslaw cupcakes "French silk pie"' - out = getoutput(f'{prg} {arg}') - expected = ('You are bringing potato chips, coleslaw, ' - 'cupcakes, and French silk pie.') + out = getoutput(f"{prg} {arg}") + expected = ( + "You are bringing potato chips, coleslaw, " "cupcakes, and French silk pie." + ) assert out.strip() == expected @@ -54,15 +55,15 @@ def test_more_than_two(): def test_two_sorted(): """two items sorted output""" - out = getoutput(f'{prg} -s soda candy') - assert out.strip() == 'You are bringing candy and soda.' + out = getoutput(f"{prg} -s soda candy") + assert out.strip() == "You are bringing candy and soda." # -------------------------------------------------- def test_more_than_two_sorted(): """more than two items sorted output""" - arg = 'bananas apples dates cherries' - out = getoutput(f'{prg} {arg} --sorted') - expected = ('You are bringing apples, bananas, cherries, and dates.') + arg = "bananas apples dates cherries" + out = getoutput(f"{prg} {arg} --sorted") + expected = "You are bringing apples, bananas, cherries, and dates." assert out.strip() == expected From 36572e312366991f65da3e834daa79fbb9b1e6bb Mon Sep 17 00:00:00 2001 From: theFippo Date: Mon, 9 Jan 2023 18:27:24 +0100 Subject: [PATCH 17/22] ch03: modify program to align help message --- 03_picnic/picnic.py | 45 ++++++--------------------------------------- 1 file changed, 6 insertions(+), 39 deletions(-) diff --git a/03_picnic/picnic.py b/03_picnic/picnic.py index 656e1c853..8b111965b 100644 --- a/03_picnic/picnic.py +++ b/03_picnic/picnic.py @@ -17,36 +17,9 @@ def get_args(): formatter_class=argparse.ArgumentDefaultsHelpFormatter, ) - parser.add_argument("positional", metavar="str", help="A positional argument") + parser.add_argument("positional", metavar="str", help="Item(s) to bring") - 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") + parser.add_argument("-s", "--sorted", help="Sort the items", action="store_true") return parser.parse_args() @@ -56,17 +29,11 @@ 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 + sort_flag = args.on + items = args.str - 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}"') + print(f'flag_arg = "{sort_flag}"') + print(f'positional = "{items}"') # -------------------------------------------------- From de3e87ebfbe4d34c3d6b6be23b6ba4aa5b4cac3b Mon Sep 17 00:00:00 2001 From: theFippo Date: Mon, 9 Jan 2023 18:57:10 +0100 Subject: [PATCH 18/22] ch03: valid solution --- 03_picnic/picnic.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/03_picnic/picnic.py b/03_picnic/picnic.py index 8b111965b..b871e4a79 100644 --- a/03_picnic/picnic.py +++ b/03_picnic/picnic.py @@ -17,7 +17,7 @@ def get_args(): formatter_class=argparse.ArgumentDefaultsHelpFormatter, ) - parser.add_argument("positional", metavar="str", help="Item(s) to bring") + parser.add_argument("items", metavar="str", help="Item(s) to bring", nargs="+") parser.add_argument("-s", "--sorted", help="Sort the items", action="store_true") @@ -29,11 +29,21 @@ def main(): """Make a jazz noise here""" args = get_args() - sort_flag = args.on - items = args.str + sort_flag = args.sorted + items = args.items - print(f'flag_arg = "{sort_flag}"') - print(f'positional = "{items}"') + if sort_flag == True: + items.sort() + + if len(items) >= 3: + items.insert(-1, "and ") + items_to_bring = ", ".join(items[:-1]) + items[-1] + elif len(items) == 2: + items_to_bring = " and ".join(items) + else: + items_to_bring = items[0] + + print("You are bringing " + items_to_bring + ".") # -------------------------------------------------- From c3007e107305ed6b00bf534ec0884954c3db630c Mon Sep 17 00:00:00 2001 From: theFippo Date: Sun, 22 Jan 2023 17:20:24 +0100 Subject: [PATCH 19/22] ch03: cleanup --- 03_picnic/picnic.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/03_picnic/picnic.py b/03_picnic/picnic.py index b871e4a79..74d2ee086 100644 --- a/03_picnic/picnic.py +++ b/03_picnic/picnic.py @@ -17,7 +17,7 @@ def get_args(): formatter_class=argparse.ArgumentDefaultsHelpFormatter, ) - parser.add_argument("items", metavar="str", help="Item(s) to bring", nargs="+") + parser.add_argument("item", metavar="str", help="Item(s) to bring", nargs="+") parser.add_argument("-s", "--sorted", help="Sort the items", action="store_true") @@ -29,12 +29,12 @@ def main(): """Make a jazz noise here""" args = get_args() - sort_flag = args.sorted - items = args.items + items = args.item - if sort_flag == True: + if args.sorted == True: items.sort() + items_to_bring = "" if len(items) >= 3: items.insert(-1, "and ") items_to_bring = ", ".join(items[:-1]) + items[-1] @@ -43,7 +43,7 @@ def main(): else: items_to_bring = items[0] - print("You are bringing " + items_to_bring + ".") + print(f"You are bringing {items_to_bring}.") # -------------------------------------------------- From 489e4b7ad75937bb511b57e26be8f96bea8971c3 Mon Sep 17 00:00:00 2001 From: theFippo Date: Sun, 22 Jan 2023 17:51:29 +0100 Subject: [PATCH 20/22] ch04: initial setup --- .gitignore | 1 - 04_jump_the_five/jump.py | 72 ++++++++++++++++++++++++++++++++++++++++ 04_jump_the_five/test.py | 14 ++++---- 3 files changed, 79 insertions(+), 8 deletions(-) create mode 100644 04_jump_the_five/jump.py diff --git a/.gitignore b/.gitignore index 4bd4b80b0..8d9f40edd 100644 --- a/.gitignore +++ b/.gitignore @@ -11,7 +11,6 @@ tex2pdf* .coverage .idea .vscode -04_jump_the_five/jump.py 05_howler/howler.py 06_wc/wc.py 07_gashlycrumb/gashlycrumb.py diff --git a/04_jump_the_five/jump.py b/04_jump_the_five/jump.py new file mode 100644 index 000000000..9b0338306 --- /dev/null +++ b/04_jump_the_five/jump.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python3 +""" +Author : Anonymous +Date : 2023-01-22 +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/04_jump_the_five/test.py b/04_jump_the_five/test.py index 5dc89e311..c19dfac42 100755 --- a/04_jump_the_five/test.py +++ b/04_jump_the_five/test.py @@ -4,7 +4,7 @@ import os from subprocess import getstatusoutput -prg = './jump.py' +prg = "jump.py" # -------------------------------------------------- @@ -18,19 +18,19 @@ def test_exists(): def test_usage(): """usage""" - for flag in ['-h', '--help']: - rv, out = getstatusoutput(f'{prg} {flag}') + for flag in ["-h", "--help"]: + rv, out = getstatusoutput(f"{prg} {flag}") assert rv == 0 - assert out.lower().startswith('usage') + assert out.lower().startswith("usage") # -------------------------------------------------- def test_01(): """test""" - rv, out = getstatusoutput(f'{prg} 123-456-7890') + rv, out = getstatusoutput(f"{prg} 123-456-7890") assert rv == 0 - assert out == '987-604-3215' + assert out == "987-604-3215" # -------------------------------------------------- @@ -39,4 +39,4 @@ def test_02(): rv, out = getstatusoutput(f'{prg} "That number to call is 098-765-4321."') assert rv == 0 - assert out.rstrip() == 'That number to call is 512-340-6789.' + assert out.rstrip() == "That number to call is 512-340-6789." From abdbe66d235fc636b182f3d2f259dd70b2e8ba9e Mon Sep 17 00:00:00 2001 From: theFippo Date: Mon, 23 Jan 2023 18:39:25 +0100 Subject: [PATCH 21/22] ch04: valid solution --- 04_jump_the_five/jump.py | 68 +++++++++++++++------------------------- 1 file changed, 25 insertions(+), 43 deletions(-) diff --git a/04_jump_the_five/jump.py b/04_jump_the_five/jump.py index 9b0338306..042a875bb 100644 --- a/04_jump_the_five/jump.py +++ b/04_jump_the_five/jump.py @@ -1,8 +1,8 @@ #!/usr/bin/env python3 """ -Author : Anonymous +Author : Jeffrey Paz-Schmid Date : 2023-01-22 -Purpose: Rock the Casbah +Purpose: Encode Phone-Numbers """ import argparse @@ -13,38 +13,11 @@ def get_args(): """Get command-line arguments""" parser = argparse.ArgumentParser( - description='Rock the Casbah', - formatter_class=argparse.ArgumentDefaultsHelpFormatter) + description="Jump the Five", + 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') + parser.add_argument("positional", metavar="str", help="Input text") return parser.parse_args() @@ -54,19 +27,28 @@ 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}"') + jumper = { + "1": "9", + "2": "8", + "3": "7", + "4": "6", + "5": "0", + "6": "4", + "7": "3", + "8": "2", + "9": "1", + "0": "5", + } + + for char in pos_arg: + if char in jumper: + print(jumper[char], end="") + else: + print(char, end="") # -------------------------------------------------- -if __name__ == '__main__': +if __name__ == "__main__": main() From 36fd2d10e1ede7b09e3fa024b306f3e2b20ae122 Mon Sep 17 00:00:00 2001 From: theFippo Date: Mon, 23 Jan 2023 19:01:48 +0100 Subject: [PATCH 22/22] ch04: use list comprehension --- 04_jump_the_five/jump.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/04_jump_the_five/jump.py b/04_jump_the_five/jump.py index 042a875bb..a0f3b8890 100644 --- a/04_jump_the_five/jump.py +++ b/04_jump_the_five/jump.py @@ -17,7 +17,7 @@ def get_args(): formatter_class=argparse.ArgumentDefaultsHelpFormatter, ) - parser.add_argument("positional", metavar="str", help="Input text") + parser.add_argument("text", metavar="str", help="Input text") return parser.parse_args() @@ -27,7 +27,6 @@ def main(): """Make a jazz noise here""" args = get_args() - pos_arg = args.positional jumper = { "1": "9", @@ -42,11 +41,7 @@ def main(): "0": "5", } - for char in pos_arg: - if char in jumper: - print(jumper[char], end="") - else: - print(char, end="") + print("".join([jumper.get(char, char) for char in args.text])) # --------------------------------------------------