diff --git a/.gitignore b/.gitignore index 68ca39690..b62f38439 100644 --- a/.gitignore +++ b/.gitignore @@ -11,23 +11,24 @@ 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 -07_gashlycrumb/gashlycrumb.py -08_apples_and_bananas/apples.py -09_abuse/abuse.py -10_telephone/telephone.py -11_bottles_of_beer/bottles.py -12_ransom/ransom.py -13_twelve_days/twelve_days.py -14_rhymer/rhymer.py -15_kentucky_friar/friar.py -16_scrambler/scrambler.py -17_mad_libs/mad.py -18_gematria/gematria.py -19_wod/wod.py -20_password/password.py -21_tictactoe/tictactoe.py +# 02_crowsnest/crowsnest.py +# 03_picnic/picnic.py +# 04_jump_the_five/jump.py +# 05_howler/howler.py +# 06_wc/wc.py +# 07_gashlycrumb/gashlycrumb.py +# 08_apples_and_bananas/apples.py +# 09_abuse/abuse.py +# 10_telephone/telephone.py +# 11_bottles_of_beer/bottles.py +# 12_ransom/ransom.py +# 13_twelve_days/twelve_days.py +# 14_rhymer/rhymer.py +# 15_kentucky_friar/friar.py +# 16_scrambler/scrambler.py +# 17_mad_libs/mad.py +# 18_gematria/gematria.py +# 19_wod/wod.py +# 20_password/password.py +# 21_tictactoe/tictactoe.py +Ken Youens-Clark - Tiny Python Projects - 2020.pdf diff --git a/01_hello/hello.py b/01_hello/hello.py new file mode 100644 index 000000000..6babccc57 --- /dev/null +++ b/01_hello/hello.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python +""" +Author: Tanneeru Deepak +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" + ) + return parser.parse_args() + + +# Print Hello, world! on system output with input argument +def main(): + """Says Hello with the -name as input argument""" + args = get_args() + print("Hello, " + args.name + "!") + + +if __name__ == "__main__": + main() diff --git a/01_hello/test.py b/01_hello/test.py index d0cedd1c2..0836ca647 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' # -------------------------------------------------- @@ -16,9 +16,9 @@ def test_exists(): # -------------------------------------------------- def test_runnable(): - """Runs using python3""" + """Runs using python""" - out = getoutput(f'python3 {prg}') + out = getoutput(f'python {prg}') assert out.strip() == 'Hello, World!' diff --git a/02_crowsnest/README.md b/02_crowsnest/README.md index a07d9b5aa..e9d9253b2 100644 --- a/02_crowsnest/README.md +++ b/02_crowsnest/README.md @@ -1,25 +1,25 @@ # Crow's Nest -https://www.youtube.com/playlist?list=PLhOuww6rJJNPBqIwfD-0RedqsitBliLhT + Write a program that will announce the appearance of something "off the larboard bow" to the captain of the ship. Note that you need to "a" before a word starting with a consonant: -``` +```powershell $ ./crowsnest.py narwhal Ahoy, Captain, a narwhal off the larboard bow! ``` Or "an" before a word starting with a vowel: -``` +```powershell $ ./crowsnest.py octopus Ahoy, Captain, an octopus off the larboard bow! ``` Given no arguments, the program should print a brief usage: -``` +```powershell $ ./crowsnest.py usage: crowsnest.py [-h] str crowsnest.py: error: the following arguments are required: str @@ -27,7 +27,7 @@ crowsnest.py: error: the following arguments are required: str It should print a longer usage for `-h` and `--help`: -``` +```powershell $ ./crowsnest.py -h usage: crowsnest.py [-h] str @@ -42,7 +42,7 @@ optional arguments: A passing test suite looks like this: -``` +```powershell $ make test pytest -xv test.py ============================= test session starts ============================== diff --git a/02_crowsnest/crowsnest.py b/02_crowsnest/crowsnest.py new file mode 100644 index 000000000..605779ba0 --- /dev/null +++ b/02_crowsnest/crowsnest.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python3 +""" +Author : Tanneeru Deepak +Date : 03-08-2022 +Purpose: Chapter-2 Solution +""" + +import argparse + + +# -------------------------------------------------- +def get_args(): + """Get command-line arguments""" + + parser = argparse.ArgumentParser( + description="Shout out to captain about incoming threat", + formatter_class=argparse.ArgumentDefaultsHelpFormatter, + ) + + parser.add_argument( + "word", metavar="threat", help="Incoming threat to ship", type=str + ) + + return parser.parse_args() + + +# -------------------------------------------------- +def main(): + """Shout out about incoming threat to the ship""" + + args = get_args() + str_arg = args.word + + if str_arg[0].lower() in "aeiou": + article = "an" + else: + article = "a" + + print(f"Ahoy, Captain, {article} {str_arg} off the larboard bow!") + + +# -------------------------------------------------- +if __name__ == "__main__": + main() diff --git a/02_crowsnest/test.py b/02_crowsnest/test.py index d4e25c6c7..b4b771f6b 100755 --- a/02_crowsnest/test.py +++ b/02_crowsnest/test.py @@ -4,7 +4,7 @@ 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', diff --git a/03_picnic/picnic.py b/03_picnic/picnic.py new file mode 100644 index 000000000..780d00dd1 --- /dev/null +++ b/03_picnic/picnic.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python3 +""" +Author : Tanneeru Deepak +Date : today +Purpose: Chapter- Solution +""" + +import argparse + + +# -------------------------------------------------- +def get_args(): + """Get command-line arguments""" + + parser = argparse.ArgumentParser( + description='List out the items bought for picnic', + formatter_class=argparse.ArgumentDefaultsHelpFormatter) + + parser.add_argument('item', + metavar='str', + nargs='+', + help='List of item(s) to bring') + + parser.add_argument('-s', + '--sorted', + help='Sorts items', + action='store_true') + + args = parser.parse_args() + + return args + + +# -------------------------------------------------- +def main(): + """Make a jazz noise here""" + + args = get_args() + items_list = args.item + + if args.sorted: + items_list.sort() + + if len(items_list) == 1: + print(f'You are bringing {items_list[0]}.') + elif len(items_list) == 2: + print(f'You are bringing {items_list[0]} and {items_list[1]}.') + else: + print(f'You are bringing {", ".join(items_list[:-1])}, and {items_list[-1]}.') + + +# -------------------------------------------------- +if __name__ == '__main__': + main() diff --git a/03_picnic/test.py b/03_picnic/test.py index 281fab552..35227040f 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' # -------------------------------------------------- diff --git a/04_jump_the_five/jump.py b/04_jump_the_five/jump.py new file mode 100644 index 000000000..7db6f3576 --- /dev/null +++ b/04_jump_the_five/jump.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python3 +""" +Author : Tanneeru Deepak +Date : today +Purpose: Chapter-4 Solution +""" + +import argparse + + +# -------------------------------------------------- +def get_args(): + """Get command-line arguments""" + + parser = argparse.ArgumentParser( + description="Jump the five algorithm", + formatter_class=argparse.ArgumentDefaultsHelpFormatter, + ) + + parser.add_argument("ctext", metavar="str", help="cryptic text") + + parser.add_argument( + "-a", + "--arg", + help="A named string argument", + metavar="str", + type=str, + default="", + ) + + return parser.parse_args() + + +# -------------------------------------------------- +def main(): + """Make a jazz noise here""" + + args = get_args() + str_arg = args.ctext + + num_jump = { + "1": "9", + "2": "8", + "3": "7", + "4": "6", + "5": "0", + "6": "4", + "7": "3", + "8": "2", + "9": "1", + "0": "5", + } + + decode_text = "" + + for char in str_arg: + if char.isnumeric(): + new_char = num_jump[char] + decode_text += new_char + else: + decode_text += char + + print(f"{decode_text}") + + +# -------------------------------------------------- +if __name__ == "__main__": + main() diff --git a/04_jump_the_five/test.py b/04_jump_the_five/test.py index 5dc89e311..0fb13734b 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' # -------------------------------------------------- diff --git a/05_howler/howler.py b/05_howler/howler.py new file mode 100644 index 000000000..cacf2fb53 --- /dev/null +++ b/05_howler/howler.py @@ -0,0 +1,67 @@ +#!C:\Python311\python3.exe +""" +Author : Tanneeru Deepak +Date : today +Purpose: Chapter-5 Solution +""" + +import argparse +import os + + +# -------------------------------------------------- +def get_args(): + """Get command-line arguments""" + + parser = argparse.ArgumentParser( + description="Howler - Upper case conversion", + formatter_class=argparse.ArgumentDefaultsHelpFormatter, + ) + + parser.add_argument( + "infile", + help="Input howler text or file name contains hower text", + metavar="text", + type=str, + ) + + parser.add_argument( + "-o", + "--outfile", + help="Out filename", + metavar="str", + type=str, + default=None, + ) + + return parser.parse_args() + + +def check_out(out_arg, howlers): + + if out_arg is None: + print(howlers.upper()) + else: + with open(out_arg, "w") as o: + o.write(howlers.upper() + "\n") + + +# -------------------------------------------------- +def main(): + """Make a jazz noise here""" + + args = get_args() + infile = args.infile + outfile = args.outfile + + if os.path.isfile(infile): + with open(infile, "r") as g: + howlers = g.read() + check_out(outfile, howlers=howlers) + else: + check_out(outfile, howlers=infile) + + +# -------------------------------------------------- +if __name__ == "__main__": + main() diff --git a/05_howler/test.py b/05_howler/test.py index 1c04934c8..5360e4274 100755 --- a/05_howler/test.py +++ b/05_howler/test.py @@ -7,7 +7,7 @@ import string from subprocess import getstatusoutput, getoutput -prg = './howler.py' +prg = ".\\howler.py" # -------------------------------------------------- @@ -15,14 +15,14 @@ def random_string(): """generate a random string""" k = random.randint(5, 10) - return ''.join(random.choices(string.ascii_letters + string.digits, k=k)) + return "".join(random.choices(string.ascii_letters + string.digits, k=k)) # -------------------------------------------------- def out_flag(): """Either -o or --outfile""" - return '-o' if random.randint(0, 1) else '--outfile' + return "-o" if random.randint(0, 1) else "--outfile" # -------------------------------------------------- @@ -36,8 +36,8 @@ 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 re.match("usage", out, re.IGNORECASE) @@ -47,7 +47,7 @@ def test_text_stdout(): """Test STDIN/STDOUT""" out = getoutput(f'{prg} "foo bar baz"') - assert out.strip() == 'FOO BAR BAZ' + assert out.strip() == "FOO BAR BAZ" # -------------------------------------------------- @@ -60,10 +60,10 @@ def test_text_outfile(): try: out = getoutput(f'{prg} {out_flag()} {out_file} "foo bar baz"') - assert out.strip() == '' + assert out.strip() == "" assert os.path.isfile(out_file) text = open(out_file).read().rstrip() - assert text == 'FOO BAR BAZ' + assert text == "FOO BAR BAZ" finally: if os.path.isfile(out_file): os.remove(out_file) @@ -73,19 +73,18 @@ def test_text_outfile(): def test_file(): """Test file in/out""" - for expected_file in os.listdir('test-outs'): + for expected_file in os.listdir("test-outs"): + out_file = random_string() try: - out_file = random_string() if os.path.isfile(out_file): os.remove(out_file) basename = os.path.basename(expected_file) - in_file = os.path.join('../inputs', basename) - out = getoutput(f'{prg} {out_flag()} {out_file} {in_file}') - assert out.strip() == '' + in_file = os.path.join("../inputs", basename) + out = getoutput(f"{prg} {out_flag()} {out_file} {in_file}") + assert out.strip() == "" produced = open(out_file).read().rstrip() - expected = open(os.path.join('test-outs', - expected_file)).read().strip() + expected = open(os.path.join("test-outs", expected_file)).read().strip() assert expected == produced finally: if os.path.isfile(out_file): diff --git a/06_wc/test.py b/06_wc/test.py index f9ebc9dea..19d651078 100755 --- a/06_wc/test.py +++ b/06_wc/test.py @@ -7,7 +7,7 @@ import string from subprocess import getstatusoutput -prg = './wc.py' +prg = '.\\wc.py' empty = './inputs/empty.txt' one_line = './inputs/one.txt' two_lines = './inputs/two.txt' diff --git a/07_gashlycrumb/test.py b/07_gashlycrumb/test.py index b7bd700eb..9076d2ceb 100755 --- a/07_gashlycrumb/test.py +++ b/07_gashlycrumb/test.py @@ -7,7 +7,7 @@ import string from subprocess import getstatusoutput -prg = './gashlycrumb.py' +prg = '.\\gashlycrumb.py' # -------------------------------------------------- diff --git a/08_apples_and_bananas/test.py b/08_apples_and_bananas/test.py index e82161f9b..8d1214919 100755 --- a/08_apples_and_bananas/test.py +++ b/08_apples_and_bananas/test.py @@ -5,7 +5,7 @@ import os from subprocess import getstatusoutput, getoutput -prg = './apples.py' +prg = '.\\apples.py' fox = '../inputs/fox.txt' diff --git a/09_abuse/test.py b/09_abuse/test.py index ff31fc90a..5837b66e8 100755 --- a/09_abuse/test.py +++ b/09_abuse/test.py @@ -7,7 +7,7 @@ import string from subprocess import getstatusoutput, getoutput -prg = './abuse.py' +prg = '.\\abuse.py' # -------------------------------------------------- diff --git a/10_telephone/test.py b/10_telephone/test.py index ea263af1b..c10e42b76 100755 --- a/10_telephone/test.py +++ b/10_telephone/test.py @@ -7,7 +7,7 @@ import re import string -prg = "./telephone.py" +prg = ".\\telephone.py" fox = '../inputs/fox.txt' now = '../inputs/now.txt' diff --git a/11_bottles_of_beer/test.py b/11_bottles_of_beer/test.py index d9487cc82..594071dab 100755 --- a/11_bottles_of_beer/test.py +++ b/11_bottles_of_beer/test.py @@ -8,7 +8,7 @@ import string from subprocess import getstatusoutput -prg = './bottles.py' +prg = '.\\bottles.py' # -------------------------------------------------- diff --git a/12_ransom/test.py b/12_ransom/test.py index d1c06de4f..fed8e3815 100755 --- a/12_ransom/test.py +++ b/12_ransom/test.py @@ -6,7 +6,7 @@ import random from subprocess import getstatusoutput -prg = './ransom.py' +prg = '.\\ransom.py' fox = '../inputs/fox.txt' now = '../inputs/now.txt' diff --git a/13_twelve_days/test.py b/13_twelve_days/test.py index b7fdb9836..ffe030cc5 100755 --- a/13_twelve_days/test.py +++ b/13_twelve_days/test.py @@ -7,7 +7,7 @@ import string from subprocess import getstatusoutput, getoutput -prg = './twelve_days.py' +prg = '.\\twelve_days.py' day_one = '\n'.join([ 'On the first day of Christmas,', 'My true love gave to me,', 'A partridge in a pear tree.' diff --git a/14_rhymer/test.py b/14_rhymer/test.py index 5946c448b..6250df085 100755 --- a/14_rhymer/test.py +++ b/14_rhymer/test.py @@ -5,7 +5,7 @@ import random from subprocess import getoutput -prg = './rhymer.py' +prg = '.\\rhymer.py' # -------------------------------------------------- diff --git a/15_kentucky_friar/test.py b/15_kentucky_friar/test.py index cc1ae9a49..bed4a70b4 100755 --- a/15_kentucky_friar/test.py +++ b/15_kentucky_friar/test.py @@ -5,7 +5,7 @@ import re from subprocess import getstatusoutput, getoutput -prg = './friar.py' +prg = '.\\friar.py' # -------------------------------------------------- diff --git a/16_scrambler/test.py b/16_scrambler/test.py index 544c7e445..46a253ded 100755 --- a/16_scrambler/test.py +++ b/16_scrambler/test.py @@ -5,7 +5,7 @@ import re from subprocess import getstatusoutput, getoutput -prg = './scrambler.py' +prg = '.\\scrambler.py' fox = '../inputs/fox.txt' bustle = '../inputs/the-bustle.txt' spiders = '../inputs/spiders.txt' diff --git a/17_mad_libs/test.py b/17_mad_libs/test.py index b6843a6f6..99f1ea435 100755 --- a/17_mad_libs/test.py +++ b/17_mad_libs/test.py @@ -7,7 +7,7 @@ import string from subprocess import getstatusoutput -prg = './mad.py' +prg = '.\\mad.py' no_blanks = 'inputs/no_blanks.txt' fox = 'inputs/fox.txt' hlp = 'inputs/help.txt' diff --git a/18_gematria/test.py b/18_gematria/test.py index b585f6c74..9c0a16a47 100755 --- a/18_gematria/test.py +++ b/18_gematria/test.py @@ -5,7 +5,7 @@ import re from subprocess import getstatusoutput, getoutput -prg = './gematria.py' +prg = '.\\gematria.py' spiders = '../inputs/spiders.txt' fox = '../inputs/fox.txt' sonnet = '../inputs/sonnet-29.txt' diff --git a/19_wod/test.py b/19_wod/test.py index 8aff68721..f63e015af 100755 --- a/19_wod/test.py +++ b/19_wod/test.py @@ -7,7 +7,7 @@ import string from subprocess import getstatusoutput -prg = './wod.py' +prg = '.\\wod.py' input1 = 'inputs/exercises.csv' input2 = 'inputs/silly-exercises.csv' diff --git a/20_password/test.py b/20_password/test.py index bc085fb3d..98899759c 100755 --- a/20_password/test.py +++ b/20_password/test.py @@ -7,7 +7,7 @@ import string from subprocess import getstatusoutput -prg = './password.py' +prg = '.\\password.py' words = '../inputs/words.txt' diff --git a/21_tictactoe/test.py b/21_tictactoe/test.py index 5c63bf9ab..2cefa52dd 100755 --- a/21_tictactoe/test.py +++ b/21_tictactoe/test.py @@ -7,7 +7,7 @@ import re import string -prg = './tictactoe.py' +prg = '.\\tictactoe.py' # -------------------------------------------------- diff --git a/template/my_template.py b/template/my_template.py new file mode 100644 index 000000000..da7fd7662 --- /dev/null +++ b/template/my_template.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 +""" +Author : Tanneeru Deepak +Date : today +Purpose: Chapter- Solution +""" + +import argparse + + +# -------------------------------------------------- +def get_args(): + """Get command-line arguments""" + + parser = argparse.ArgumentParser( + description='Arguments extraction', + 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='') + + return parser.parse_args() + + +# -------------------------------------------------- +def main(): + """Make a jazz noise here""" + + args = get_args() + str_arg = args.arg + + print(f'str_arg = "{str_arg}"') + + +# -------------------------------------------------- +if __name__ == '__main__': + main()