Skip to content

Commit 0001c8a

Browse files
committed
cleanup on new.py/template.py, other stuff
1 parent b4c51e2 commit 0001c8a

File tree

5 files changed

+33
-65
lines changed

5 files changed

+33
-65
lines changed

05_howler/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
out.txt

22_itictactoe/Makefile

-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
.PHONY: test
22

3-
test:
4-
pytest -xv test.py
5-
63
unit:
74
pytest -xv unit.py

22_itictactoe/unit.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from tictactoe import format_board, find_winner
1+
from itictactoe import format_board, find_winner
22
import random
33

44

bin/new.py

+26-54
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/usr/bin/env python3
22
"""
33
Author : Ken Youens-Clark <[email protected]>
4-
Date : 24 October 2018
54
Purpose: Python program to write a Python program
65
"""
76

@@ -19,18 +18,13 @@ def get_args():
1918
"""Get arguments"""
2019

2120
parser = argparse.ArgumentParser(
22-
description='Create Python argparse/simple program',
21+
description='Create Python argparse program',
2322
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
2423

2524
defaults = get_defaults()
2625

2726
parser.add_argument('program', help='Program name', type=str)
2827

29-
parser.add_argument('-s',
30-
'--simple',
31-
help='Use simple format',
32-
action='store_true')
33-
3428
parser.add_argument('-n',
3529
'--name',
3630
type=str,
@@ -59,7 +53,10 @@ def get_args():
5953
args.program = args.program.strip().replace('-', '_')
6054

6155
if not args.program:
62-
parser.error('Not a usable filename "{}"'.format(args.program))
56+
parser.error(f'Not a usable filename "{args.program}"')
57+
58+
if args.email:
59+
args.email = f'<{args.email}>'
6360

6461
return args
6562

@@ -72,22 +69,19 @@ def main():
7269
program = args.program
7370

7471
if os.path.isfile(program) and not args.force:
75-
answer = input('"{}" exists. Overwrite? [yN] '.format(program))
72+
answer = input(f'"{program}" exists. Overwrite? [yN] ')
7673
if not answer.lower().startswith('y'):
7774
print('Will not overwrite. Bye!')
7875
sys.exit()
7976

80-
header = preamble(name=args.name,
81-
email=args.email,
82-
purpose=args.purpose,
83-
date=str(date.today()))
84-
text = simple() if args.simple else body(args.purpose)
77+
text = body(name=args.name,
78+
email=args.email,
79+
purpose=args.purpose,
80+
date=str(date.today()))
8581

86-
out_fh = open(program, 'w')
87-
out_fh.write(header + text)
88-
out_fh.close()
82+
print(text, file=open(program, 'wt'), end='')
8983
subprocess.run(['chmod', '+x', program])
90-
print('Done, see new script "{}."'.format(program))
84+
print(f'Done, see new script "{program}."')
9185

9286

9387
# --------------------------------------------------
@@ -102,47 +96,25 @@ def preamble(**args):
10296

10397

10498
# --------------------------------------------------
105-
def simple():
106-
return """
107-
import os
108-
import sys
109-
110-
111-
# --------------------------------------------------
112-
def main():
113-
\"\"\"Make a jazz noise here\"\"\"
114-
115-
args = sys.argv[1:]
116-
117-
if len(args) != 1:
118-
print('Usage: {} ARG'.format(os.path.basename(sys.argv[0])))
119-
sys.exit(1)
120-
121-
arg = args[0]
122-
123-
print('Arg is "{}"'.format(arg))
124-
125-
126-
# --------------------------------------------------
127-
if __name__ == '__main__':
128-
main()
129-
"""
99+
def body(**args):
100+
""" The program template """
130101

102+
return f"""#!/usr/bin/env python3
103+
\"\"\"
104+
Author : {args['name']}{args['email']}
105+
Date : {args['date']}
106+
Purpose: {args['purpose']}
107+
\"\"\"
131108
132-
# --------------------------------------------------
133-
def body(purpose):
134-
text = """
135109
import argparse
136-
import os
137-
import sys
138110
139111
140112
# --------------------------------------------------
141113
def get_args():
142114
\"\"\"Get command-line arguments\"\"\"
143115
144116
parser = argparse.ArgumentParser(
145-
description='{}',
117+
description='{args["purpose"]}',
146118
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
147119
148120
parser.add_argument('positional',
@@ -189,18 +161,18 @@ def main():
189161
flag_arg = args.on
190162
pos_arg = args.positional
191163
192-
print('str_arg = "{{}}"'.format(str_arg))
193-
print('int_arg = "{{}}"'.format(int_arg))
164+
print(f'str_arg = "{{str_arg}}"')
165+
print(f'int_arg = "{{int_arg}}"')
194166
print('file_arg = "{{}}"'.format(file_arg.name if file_arg else ''))
195-
print('flag_arg = "{{}}"'.format(flag_arg))
196-
print('positional = "{{}}"'.format(pos_arg))
167+
print(f'flag_arg = "{{flag_arg}}"')
168+
print(f'positional = "{{pos_arg}}"')
197169
198170
199171
# --------------------------------------------------
200172
if __name__ == '__main__':
201173
main()
202174
"""
203-
return text.format(purpose)
175+
204176

205177
# --------------------------------------------------
206178
def get_defaults():

template/template.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
"""
77

88
import argparse
9-
import os
10-
import sys
119

1210

1311
# --------------------------------------------------
@@ -62,11 +60,11 @@ def main():
6260
flag_arg = args.on
6361
pos_arg = args.positional
6462

65-
print('str_arg = "{}"'.format(str_arg))
66-
print('int_arg = "{}"'.format(int_arg))
67-
print('file_arg = "{}"'.format(file_arg.name))
68-
print('flag_arg = "{}"'.format(flag_arg))
69-
print('positional = "{}"'.format(pos_arg))
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}"')
7068

7169

7270
# --------------------------------------------------

0 commit comments

Comments
 (0)