forked from kyclark/tiny_python_projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cleanup on new.py/template.py, other stuff
- Loading branch information
Showing
5 changed files
with
33 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
out.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,4 @@ | ||
.PHONY: test | ||
|
||
test: | ||
pytest -xv test.py | ||
|
||
unit: | ||
pytest -xv unit.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from tictactoe import format_board, find_winner | ||
from itictactoe import format_board, find_winner | ||
import random | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
#!/usr/bin/env python3 | ||
""" | ||
Author : Ken Youens-Clark <[email protected]> | ||
Date : 24 October 2018 | ||
Purpose: Python program to write a Python program | ||
""" | ||
|
||
|
@@ -19,18 +18,13 @@ def get_args(): | |
"""Get arguments""" | ||
|
||
parser = argparse.ArgumentParser( | ||
description='Create Python argparse/simple program', | ||
description='Create Python argparse program', | ||
formatter_class=argparse.ArgumentDefaultsHelpFormatter) | ||
|
||
defaults = get_defaults() | ||
|
||
parser.add_argument('program', help='Program name', type=str) | ||
|
||
parser.add_argument('-s', | ||
'--simple', | ||
help='Use simple format', | ||
action='store_true') | ||
|
||
parser.add_argument('-n', | ||
'--name', | ||
type=str, | ||
|
@@ -59,7 +53,10 @@ def get_args(): | |
args.program = args.program.strip().replace('-', '_') | ||
|
||
if not args.program: | ||
parser.error('Not a usable filename "{}"'.format(args.program)) | ||
parser.error(f'Not a usable filename "{args.program}"') | ||
|
||
if args.email: | ||
args.email = f'<{args.email}>' | ||
|
||
return args | ||
|
||
|
@@ -72,22 +69,19 @@ def main(): | |
program = args.program | ||
|
||
if os.path.isfile(program) and not args.force: | ||
answer = input('"{}" exists. Overwrite? [yN] '.format(program)) | ||
answer = input(f'"{program}" exists. Overwrite? [yN] ') | ||
if not answer.lower().startswith('y'): | ||
print('Will not overwrite. Bye!') | ||
sys.exit() | ||
|
||
header = preamble(name=args.name, | ||
email=args.email, | ||
purpose=args.purpose, | ||
date=str(date.today())) | ||
text = simple() if args.simple else body(args.purpose) | ||
text = body(name=args.name, | ||
email=args.email, | ||
purpose=args.purpose, | ||
date=str(date.today())) | ||
|
||
out_fh = open(program, 'w') | ||
out_fh.write(header + text) | ||
out_fh.close() | ||
print(text, file=open(program, 'wt'), end='') | ||
subprocess.run(['chmod', '+x', program]) | ||
print('Done, see new script "{}."'.format(program)) | ||
print(f'Done, see new script "{program}."') | ||
|
||
|
||
# -------------------------------------------------- | ||
|
@@ -102,47 +96,25 @@ def preamble(**args): | |
|
||
|
||
# -------------------------------------------------- | ||
def simple(): | ||
return """ | ||
import os | ||
import sys | ||
# -------------------------------------------------- | ||
def main(): | ||
\"\"\"Make a jazz noise here\"\"\" | ||
args = sys.argv[1:] | ||
if len(args) != 1: | ||
print('Usage: {} ARG'.format(os.path.basename(sys.argv[0]))) | ||
sys.exit(1) | ||
arg = args[0] | ||
print('Arg is "{}"'.format(arg)) | ||
# -------------------------------------------------- | ||
if __name__ == '__main__': | ||
main() | ||
""" | ||
def body(**args): | ||
""" The program template """ | ||
|
||
return f"""#!/usr/bin/env python3 | ||
\"\"\" | ||
Author : {args['name']}{args['email']} | ||
Date : {args['date']} | ||
Purpose: {args['purpose']} | ||
\"\"\" | ||
# -------------------------------------------------- | ||
def body(purpose): | ||
text = """ | ||
import argparse | ||
import os | ||
import sys | ||
# -------------------------------------------------- | ||
def get_args(): | ||
\"\"\"Get command-line arguments\"\"\" | ||
parser = argparse.ArgumentParser( | ||
description='{}', | ||
description='{args["purpose"]}', | ||
formatter_class=argparse.ArgumentDefaultsHelpFormatter) | ||
parser.add_argument('positional', | ||
|
@@ -189,18 +161,18 @@ def main(): | |
flag_arg = args.on | ||
pos_arg = args.positional | ||
print('str_arg = "{{}}"'.format(str_arg)) | ||
print('int_arg = "{{}}"'.format(int_arg)) | ||
print(f'str_arg = "{{str_arg}}"') | ||
print(f'int_arg = "{{int_arg}}"') | ||
print('file_arg = "{{}}"'.format(file_arg.name if file_arg else '')) | ||
print('flag_arg = "{{}}"'.format(flag_arg)) | ||
print('positional = "{{}}"'.format(pos_arg)) | ||
print(f'flag_arg = "{{flag_arg}}"') | ||
print(f'positional = "{{pos_arg}}"') | ||
# -------------------------------------------------- | ||
if __name__ == '__main__': | ||
main() | ||
""" | ||
return text.format(purpose) | ||
|
||
|
||
# -------------------------------------------------- | ||
def get_defaults(): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters