Skip to content

Commit 2fb13f8

Browse files
committed
Chapter 1 Completed
1 parent bcf4b24 commit 2fb13f8

4 files changed

Lines changed: 87 additions & 13 deletions

File tree

.flake8

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[flake8]
2+
max-line-length = 88
3+
extend-ignore = E203, E704

01_hello/hello.orig

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env python3
2+
# Purpose: Say Hello
3+
4+
"""
5+
Author: Oak Hopper
6+
Purpose: Say Hello
7+
"""
8+
9+
import argparse
10+
11+
12+
def get_args():
13+
"""Get the command-line arguments"""
14+
parser = argparse.ArgumentParser(description="Say hello")
15+
parser.add_argument(
16+
"-n", "--name", metavar="name", default="World", help="Name to greet"
17+
)
18+
return parser.parse_args()
19+
20+
21+
def main():
22+
"""main"""
23+
args = get_args()
24+
name = args.name
25+
print(f"Hello, {name}!")
26+
27+
28+
if __name__ == "__main__":
29+
main()

01_hello/hello.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Author : Oak Hopper
4+
Date : 2024-06-17
5+
Purpose: Say Hello
6+
"""
7+
8+
import argparse
9+
10+
11+
# --------------------------------------------------
12+
def get_args():
13+
"""Get command-line arguments"""
14+
15+
parser = argparse.ArgumentParser(
16+
description="Say hello",
17+
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
18+
)
19+
parser.add_argument(
20+
"-n", "--name", help="Name to greet", metavar="name", type=str, default="World"
21+
)
22+
return parser.parse_args()
23+
24+
25+
# --------------------------------------------------
26+
def main():
27+
"""Make a jazz noise here"""
28+
29+
args = get_args()
30+
print(f"Hello, {args.name}!")
31+
32+
33+
# --------------------------------------------------
34+
if __name__ == "__main__":
35+
main()

01_hello/test.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,55 @@
44
import os
55
from subprocess import getstatusoutput, getoutput
66

7-
prg = './hello.py'
7+
PRG = "./hello.py"
88

99

1010
# --------------------------------------------------
1111
def test_exists():
1212
"""exists"""
1313

14-
assert os.path.isfile(prg)
14+
assert os.path.isfile(PRG)
1515

1616

1717
# --------------------------------------------------
1818
def test_runnable():
1919
"""Runs using python3"""
2020

21-
out = getoutput(f'python3 {prg}')
22-
assert out.strip() == 'Hello, World!'
21+
out = getoutput(f"python3 {PRG}")
22+
assert out.strip() == "Hello, World!"
2323

2424

2525
# --------------------------------------------------
2626
def test_executable():
2727
"""Says 'Hello, World!' by default"""
2828

29-
out = getoutput(prg)
30-
assert out.strip() == 'Hello, World!'
29+
out = getoutput(f"python3 {PRG}")
30+
assert out.strip() == "Hello, World!"
31+
32+
33+
# # This won't work on windows apparently
34+
# out = getoutput(prg)
35+
# assert out.strip() == 'Hello, World!'
3136

3237

3338
# --------------------------------------------------
3439
def test_usage():
3540
"""usage"""
3641

37-
for flag in ['-h', '--help']:
38-
rv, out = getstatusoutput(f'{prg} {flag}')
42+
for flag in ["-h", "--help"]:
43+
# rv, out = getstatusoutput(f'{prg} {flag}')
44+
rv, out = getstatusoutput(f"python3 {PRG} {flag}")
3945
assert rv == 0
40-
assert out.lower().startswith('usage')
46+
assert out.lower().startswith("usage")
4147

4248

4349
# --------------------------------------------------
4450
def test_input():
4551
"""test for input"""
4652

47-
for val in ['Universe', 'Multiverse']:
48-
for option in ['-n', '--name']:
49-
rv, out = getstatusoutput(f'{prg} {option} {val}')
53+
for val in ["Universe", "Multiverse"]:
54+
for option in ["-n", "--name"]:
55+
# rv, out = getstatusoutput(f'{prg} {option} {val}')
56+
rv, out = getstatusoutput(f"python3 {PRG} {option} {val}")
5057
assert rv == 0
51-
assert out.strip() == f'Hello, {val}!'
58+
assert out.strip() == f"Hello, {val}!"

0 commit comments

Comments
 (0)