Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 263e041

Browse files
committed
05_howler
1 parent b412ce7 commit 263e041

File tree

7 files changed

+96
-73
lines changed

7 files changed

+96
-73
lines changed

05_howler/README.md

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +0,0 @@
1-
# Howler
2-
3-
https://www.youtube.com/playlist?list=PLhOuww6rJJNNzo5zqtx0388myQkUKyrQz
4-
5-
Write a program that uppercases the given text:
6-
7-
```
8-
$ ./howler.py 'The quick brown fox jumps over the lazy dog.'
9-
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.
10-
```
11-
12-
If the text names a file, uppercase the contents of the file:
13-
14-
```
15-
$ ./howler.py ../inputs/fox.txt
16-
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.
17-
```
18-
19-
If given no arguments, print a brief usage:
20-
21-
```
22-
$ ./howler.py
23-
usage: howler.py [-h] [-o str] str
24-
howler.py: error: the following arguments are required: str
25-
```
26-
27-
If the `-o` or `--outfile` option is present, write the output to the given file and print nothing:
28-
29-
```
30-
$ ./howler.py ../inputs/fox.txt -o out.txt
31-
```
32-
33-
There should now be an `out.txt` file with the contents:
34-
35-
```
36-
$ cat out.txt
37-
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.
38-
```
39-
40-
Respond to `-h` or `--help` with a longer usage:
41-
42-
```
43-
$ ./howler.py -h
44-
usage: howler.py [-h] [-o str] str
45-
46-
Howler (upper-cases input)
47-
48-
positional arguments:
49-
str Input string or file
50-
51-
optional arguments:
52-
-h, --help show this help message and exit
53-
-o str, --outfile str
54-
Output filename (default: )
55-
```
56-
57-
Run the test suite to ensure your program is working correctly:
58-
59-
```
60-
$ make test
61-
pytest -xv test.py
62-
============================= test session starts ==============================
63-
...
64-
collected 5 items
65-
66-
test.py::test_exists PASSED [ 20%]
67-
test.py::test_usage PASSED [ 40%]
68-
test.py::test_text_stdout PASSED [ 60%]
69-
test.py::test_text_outfile PASSED [ 80%]
70-
test.py::test_file PASSED [100%]
71-
72-
============================== 5 passed in 0.40s ===============================
73-
```

05_howler/hagrid.txt

Whitespace-only changes.

05_howler/howler.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Author : Johan Runesson <[email protected]>
4+
Date : 2020-08-04
5+
Purpose: Howler (upper-cases input)
6+
"""
7+
8+
import argparse
9+
import os
10+
11+
12+
# --------------------------------------------------
13+
def get_args():
14+
"""Get command-line arguments"""
15+
16+
parser = argparse.ArgumentParser(
17+
description='Howler (upper-cases input)',
18+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
19+
20+
parser.add_argument('text',
21+
metavar='text',
22+
type=str,
23+
help='Input string or file')
24+
25+
parser.add_argument('-o',
26+
'--outfile',
27+
help='Output filename',
28+
metavar='str',
29+
type=str,
30+
default='')
31+
32+
return parser.parse_args()
33+
34+
35+
# --------------------------------------------------
36+
def main():
37+
"""Start things up"""
38+
39+
args = get_args()
40+
text = args.text
41+
outfile = args.outfile
42+
output = text.upper()
43+
44+
# validate arguments inside get_args (see solution1.py)
45+
if os.path.isfile(text):
46+
fh_in = open(text)
47+
output = fh_in.read().upper()
48+
fh_in.close()
49+
50+
if outfile:
51+
fh_out = open(outfile, 'wt')
52+
fh_out.write(output)
53+
fh_out.close()
54+
else:
55+
print(output)
56+
57+
58+
# --------------------------------------------------
59+
if __name__ == '__main__':
60+
main()

05_howler/test.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hej hej he

05_howler/test2.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
HEJ HEJ HE

NOTES.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,35 @@ Get help in repl:
6262
python3
6363
help(str)
6464
```
65+
66+
Read a file (reads the entire file into memory):
67+
68+
```
69+
fh = open(text)
70+
text = fh.read()
71+
fh.close()
72+
```
73+
74+
Read a file (without reading the entire file into memory):
75+
76+
```
77+
fh = open(text)
78+
for line in fh:
79+
print(line.upper())
80+
fh.close()
81+
```
82+
83+
Handle text that might be a file of a string:
84+
85+
```
86+
if os.path.isfile(args.text):
87+
args.text = open(args.text)
88+
else:
89+
args.text = io.StringIO(args.text + '\n')
90+
```
91+
92+
Handle output to file if specified else to print:
93+
94+
```
95+
out_fh = open(args.outfile, 'wt') if args.outfile else sys.stdout
96+
```

out.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
this is some text
2+
this is some more text

0 commit comments

Comments
 (0)