Skip to content

Commit 90300a3

Browse files
committed
Problem 06 solution
1 parent d0bb5b0 commit 90300a3

File tree

2 files changed

+97
-24
lines changed

2 files changed

+97
-24
lines changed

06_wc/test.py

+26-24
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
import string
88
from subprocess import getstatusoutput
99

10-
prg = './wc.py'
11-
empty = './inputs/empty.txt'
12-
one_line = './inputs/one.txt'
13-
two_lines = './inputs/two.txt'
14-
fox = '../inputs/fox.txt'
15-
sonnet = '../inputs/sonnet-29.txt'
10+
prg = "./wc.py"
11+
empty = "./inputs/empty.txt"
12+
one_line = "./inputs/one.txt"
13+
two_lines = "./inputs/two.txt"
14+
fox = "../inputs/fox.txt"
15+
sonnet = "../inputs/sonnet-29.txt"
1616

1717

1818
# --------------------------------------------------
@@ -26,8 +26,8 @@ def test_exists():
2626
def test_usage():
2727
"""usage"""
2828

29-
for flag in ['-h', '--help']:
30-
rv, out = getstatusoutput(f'{prg} {flag}')
29+
for flag in ["-h", "--help"]:
30+
rv, out = getstatusoutput(f"python3 {prg} {flag}")
3131
assert rv == 0
3232
assert re.match("usage", out, re.IGNORECASE)
3333

@@ -37,15 +37,15 @@ def random_string():
3737
"""generate a random string"""
3838

3939
k = random.randint(5, 10)
40-
return ''.join(random.choices(string.ascii_letters + string.digits, k=k))
40+
return "".join(random.choices(string.ascii_letters + string.digits, k=k))
4141

4242

4343
# --------------------------------------------------
4444
def test_bad_file():
4545
"""bad_file"""
4646

4747
bad = random_string()
48-
rv, out = getstatusoutput(f'{prg} {bad}')
48+
rv, out = getstatusoutput(f"python3 {prg} {bad}")
4949
assert rv != 0
5050
assert re.search(f"No such file or directory: '{bad}'", out)
5151

@@ -54,46 +54,48 @@ def test_bad_file():
5454
def test_empty():
5555
"""Test on empty"""
5656

57-
rv, out = getstatusoutput(f'{prg} {empty}')
57+
rv, out = getstatusoutput(f"python3 {prg} {empty}")
5858
assert rv == 0
59-
assert out.rstrip() == ' 0 0 0 ./inputs/empty.txt'
59+
assert out.rstrip() == " 0 0 0 ./inputs/empty.txt"
6060

6161

6262
# --------------------------------------------------
6363
def test_one():
6464
"""Test on one"""
6565

66-
rv, out = getstatusoutput(f'{prg} {one_line}')
66+
rv, out = getstatusoutput(f"python3 {prg} {one_line}")
6767
assert rv == 0
68-
assert out.rstrip() == ' 1 1 2 ./inputs/one.txt'
68+
assert out.rstrip() == " 1 1 2 ./inputs/one.txt"
6969

7070

7171
# --------------------------------------------------
7272
def test_two():
7373
"""Test on two"""
7474

75-
rv, out = getstatusoutput(f'{prg} {two_lines}')
75+
rv, out = getstatusoutput(f"python3 {prg} {two_lines}")
7676
assert rv == 0
77-
assert out.rstrip() == ' 2 2 4 ./inputs/two.txt'
77+
assert out.rstrip() == " 2 2 4 ./inputs/two.txt"
7878

7979

8080
# --------------------------------------------------
8181
def test_fox():
8282
"""Test on fox"""
8383

84-
rv, out = getstatusoutput(f'{prg} {fox}')
84+
rv, out = getstatusoutput(f"python3 {prg} {fox}")
8585
assert rv == 0
86-
assert out.rstrip() == ' 1 9 45 ../inputs/fox.txt'
86+
assert out.rstrip() == " 1 9 45 ../inputs/fox.txt"
8787

8888

8989
# --------------------------------------------------
9090
def test_more():
9191
"""Test on more than one file"""
9292

93-
rv, out = getstatusoutput(f'{prg} {fox} {sonnet}')
94-
expected = (' 1 9 45 ../inputs/fox.txt\n'
95-
' 17 118 661 ../inputs/sonnet-29.txt\n'
96-
' 18 127 706 total')
93+
rv, out = getstatusoutput(f"python3 {prg} {fox} {sonnet}")
94+
expected = (
95+
" 1 9 45 ../inputs/fox.txt\n"
96+
" 17 118 661 ../inputs/sonnet-29.txt\n"
97+
" 18 127 706 total"
98+
)
9799
assert rv == 0
98100
assert out.rstrip() == expected
99101

@@ -102,6 +104,6 @@ def test_more():
102104
def test_stdin():
103105
"""Test on stdin"""
104106

105-
rv, out = getstatusoutput(f'{prg} < {fox}')
107+
rv, out = getstatusoutput(f"python3 {prg} < {fox}")
106108
assert rv == 0
107-
assert out.rstrip() == ' 1 9 45 <stdin>'
109+
assert out.rstrip() == " 1 9 45 <stdin>"

06_wc/wc.py

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Author : Luke McGuire <[email protected]>
4+
Date : 2024-06-21
5+
Purpose: Emulate wc (word count)
6+
"""
7+
8+
import argparse
9+
import sys
10+
import os
11+
12+
13+
# --------------------------------------------------
14+
def get_args():
15+
"""Get command-line arguments"""
16+
17+
parser = argparse.ArgumentParser(
18+
description="Emulate wc (word count)",
19+
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
20+
)
21+
22+
parser.add_argument(
23+
"file",
24+
help="Input file(s)",
25+
nargs="*",
26+
metavar="FILE",
27+
type=argparse.FileType("rt"),
28+
default=[sys.stdin],
29+
)
30+
31+
args = parser.parse_args()
32+
33+
# if args.file != [sys.stdin]:
34+
# for path in args.file:
35+
# if not os.path.isfile(path.name):
36+
# print(f"No such file or directory: '{path.name}'")
37+
# sys.exit()
38+
39+
return args
40+
41+
42+
# --------------------------------------------------
43+
def main():
44+
"""Make a jazz noise here"""
45+
46+
args = get_args()
47+
total_lines = 0
48+
total_words = 0
49+
total_bytes = 0
50+
51+
for fh in args.file:
52+
num_lines = 0
53+
num_words = 0
54+
num_bytes = 0
55+
for line in fh:
56+
num_lines += 1
57+
num_words += len(line.split())
58+
num_bytes += len(line)
59+
60+
print(f"{num_lines:>8}{num_words:>8}{num_bytes:>8} {fh.name}")
61+
total_lines += num_lines
62+
total_words += num_words
63+
total_bytes += num_bytes
64+
65+
if len(args.file) > 1:
66+
print(f"{total_lines:>8}{total_words:>8}{total_bytes:>8} total")
67+
68+
69+
# --------------------------------------------------
70+
if __name__ == "__main__":
71+
main()

0 commit comments

Comments
 (0)