Skip to content

Commit 5ec7566

Browse files
committed
07
1 parent 7ba3e74 commit 5ec7566

3 files changed

Lines changed: 109 additions & 1 deletion

File tree

06_wc/wc.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,52 @@
1-
The quick brown fox jumps over the lazy dog.
1+
#!/usr/bin/env python3
2+
"""Emulate wc (word count)"""
3+
4+
import argparse
5+
import sys
6+
7+
8+
# --------------------------------------------------
9+
def get_args():
10+
"""Get command-line arguments"""
11+
12+
parser = argparse.ArgumentParser(
13+
description='Emulate wc (word count)',
14+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
15+
16+
parser.add_argument('file',
17+
metavar='FILE',
18+
nargs='*',
19+
default=[sys.stdin],
20+
type=argparse.FileType('rt'),
21+
help='Input file(s)')
22+
23+
return parser.parse_args()
24+
25+
26+
# --------------------------------------------------
27+
def main():
28+
"""Start things up"""
29+
30+
args = get_args()
31+
32+
total_lines, total_bytes, total_words = 0, 0, 0
33+
for fh in args.file:
34+
num_lines, num_words, num_bytes = 0, 0, 0
35+
for line in fh:
36+
num_lines += 1
37+
num_bytes += len(line)
38+
num_words += len(line.split())
39+
40+
total_lines += num_lines
41+
total_bytes += num_bytes
42+
total_words += num_words
43+
44+
print(f'{num_lines:8}{num_words:8}{num_bytes:8} {fh.name}')
45+
46+
if len(args.file) > 1:
47+
print(f'{total_lines:8}{total_words:8}{total_bytes:8} total')
48+
49+
50+
# --------------------------------------------------
51+
if __name__ == '__main__':
52+
main()

07_gashlycrumb/gashlycrumb.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Author : Johan Runesson <info@johanrunesson.se>
4+
Date : 2020-08-04
5+
Purpose: Gashlycrumb
6+
"""
7+
8+
import argparse
9+
10+
11+
# --------------------------------------------------
12+
def get_args():
13+
"""Get command-line arguments"""
14+
15+
parser = argparse.ArgumentParser(
16+
description='Gashlycrumb',
17+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
18+
19+
parser.add_argument('letter',
20+
metavar='letter',
21+
nargs='+',
22+
help='Letter(s)')
23+
24+
parser.add_argument('-f',
25+
'--file',
26+
help='Input file',
27+
metavar='FILE',
28+
type=argparse.FileType('rt'),
29+
default='gashlycrumb.txt')
30+
31+
return parser.parse_args()
32+
33+
34+
# --------------------------------------------------
35+
def main():
36+
"""Start things up"""
37+
38+
args = get_args()
39+
dictionary = { line[0].lower(): line.rstrip() for line in args.file }
40+
# dictionary = {}
41+
42+
# for line in args.file:
43+
# dictionary[line[0].lower()] = line.rstrip()
44+
45+
for letter in args.letter:
46+
print(dictionary.get(letter.lower(), f'I do not know "{letter}".'))
47+
48+
49+
# --------------------------------------------------
50+
if __name__ == '__main__':
51+
main()

NOTES.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,9 @@ Handle output to file if specified else to print:
100100
```
101101
out_fh = open(args.outfile, 'wt') if args.outfile else sys.stdout
102102
```
103+
104+
Create a dictionary with a loop:
105+
106+
```
107+
lookup = { line[0].upper(): line.rstrip() for line in fh }
108+
```

0 commit comments

Comments
 (0)