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

Skip to content

Commit 489e4b7

Browse files
committed
ch04: initial setup
1 parent b2e641b commit 489e4b7

File tree

3 files changed

+79
-8
lines changed

3 files changed

+79
-8
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ tex2pdf*
1111
.coverage
1212
.idea
1313
.vscode
14-
04_jump_the_five/jump.py
1514
05_howler/howler.py
1615
06_wc/wc.py
1716
07_gashlycrumb/gashlycrumb.py

04_jump_the_five/jump.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Author : Anonymous <Anonymous@localhost>
4+
Date : 2023-01-22
5+
Purpose: Rock the Casbah
6+
"""
7+
8+
import argparse
9+
10+
11+
# --------------------------------------------------
12+
def get_args():
13+
"""Get command-line arguments"""
14+
15+
parser = argparse.ArgumentParser(
16+
description='Rock the Casbah',
17+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
18+
19+
parser.add_argument('positional',
20+
metavar='str',
21+
help='A positional argument')
22+
23+
parser.add_argument('-a',
24+
'--arg',
25+
help='A named string argument',
26+
metavar='str',
27+
type=str,
28+
default='')
29+
30+
parser.add_argument('-i',
31+
'--int',
32+
help='A named integer argument',
33+
metavar='int',
34+
type=int,
35+
default=0)
36+
37+
parser.add_argument('-f',
38+
'--file',
39+
help='A readable file',
40+
metavar='FILE',
41+
type=argparse.FileType('rt'),
42+
default=None)
43+
44+
parser.add_argument('-o',
45+
'--on',
46+
help='A boolean flag',
47+
action='store_true')
48+
49+
return parser.parse_args()
50+
51+
52+
# --------------------------------------------------
53+
def main():
54+
"""Make a jazz noise here"""
55+
56+
args = get_args()
57+
str_arg = args.arg
58+
int_arg = args.int
59+
file_arg = args.file
60+
flag_arg = args.on
61+
pos_arg = args.positional
62+
63+
print(f'str_arg = "{str_arg}"')
64+
print(f'int_arg = "{int_arg}"')
65+
print('file_arg = "{}"'.format(file_arg.name if file_arg else ''))
66+
print(f'flag_arg = "{flag_arg}"')
67+
print(f'positional = "{pos_arg}"')
68+
69+
70+
# --------------------------------------------------
71+
if __name__ == '__main__':
72+
main()

04_jump_the_five/test.py

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

7-
prg = './jump.py'
7+
prg = "jump.py"
88

99

1010
# --------------------------------------------------
@@ -18,19 +18,19 @@ def test_exists():
1818
def test_usage():
1919
"""usage"""
2020

21-
for flag in ['-h', '--help']:
22-
rv, out = getstatusoutput(f'{prg} {flag}')
21+
for flag in ["-h", "--help"]:
22+
rv, out = getstatusoutput(f"{prg} {flag}")
2323
assert rv == 0
24-
assert out.lower().startswith('usage')
24+
assert out.lower().startswith("usage")
2525

2626

2727
# --------------------------------------------------
2828
def test_01():
2929
"""test"""
3030

31-
rv, out = getstatusoutput(f'{prg} 123-456-7890')
31+
rv, out = getstatusoutput(f"{prg} 123-456-7890")
3232
assert rv == 0
33-
assert out == '987-604-3215'
33+
assert out == "987-604-3215"
3434

3535

3636
# --------------------------------------------------
@@ -39,4 +39,4 @@ def test_02():
3939

4040
rv, out = getstatusoutput(f'{prg} "That number to call is 098-765-4321."')
4141
assert rv == 0
42-
assert out.rstrip() == 'That number to call is 512-340-6789.'
42+
assert out.rstrip() == "That number to call is 512-340-6789."

0 commit comments

Comments
 (0)