-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargparseboilerplate.py
More file actions
168 lines (127 loc) · 3.74 KB
/
argparseboilerplate.py
File metadata and controls
168 lines (127 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env python3
"""
Calculate all prime numbers from 1 to N (with N being a number of choice to input).
"""
import sys
import argparse
from pathlib import Path
import pathlib
from typing import NamedTuple, TextIO
# -------------------------------------
class Args(NamedTuple):
"""Define data types for each commandline argument"""
number: int
infile: TextIO
outfile: Path
verbosity: int
# -------------------------------------
def cmdline_args() -> Args:
"""Get arguments from command line"""
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument(
"-n",
"--number",
metavar="<N>",
type=int,
help="provide an integer input",
# default="100",
)
parser.add_argument(
"-f",
"--file_input",
metavar="<INPUT_FILE>",
type=argparse.FileType("r"),
help="provide a path/to/file containing an integer input",
)
parser.add_argument(
"-o",
"--output_file",
metavar="<OUTPUT_FILE>",
type=pathlib.Path,
# type=str,
help="output results to path/to/file",
)
parser.add_argument(
"-v",
"--verbosity",
type=int,
choices=[0, 1],
default=0,
help="increase output verbosity (default: %(default)s)",
)
# print help if no arguments are given
if len(sys.argv) == 1:
parser.print_help()
sys.exit()
# assign args shorthand
args = parser.parse_args()
# returns object with previously defined data type
return Args(
number=args.number,
infile=args.file_input,
outfile=args.output_file,
verbosity=args.verbosity,
)
# -------------------------------------
def process_input() -> int:
"""Convert text from input file to int for primes() function"""
# get command line arguments
args = cmdline_args()
# define input for primes() function
n: int = 1
# get input from command line arguments
if args.number:
n = args.number
elif args.infile:
with args.infile as f:
text_input = f.read().rstrip()
n = int(text_input)
return n
# ---------------------------------------
def process_output() -> None | Path:
"""Create output file (and directory to file if specified)"""
# get command line arguments
args = cmdline_args()
# define output
outdir = None
# create output file and parent directory if cmdline_arg was given
if args.outfile:
outdir = args.outfile
if not outdir.exists():
outdir.parent.mkdir(parents=True, exist_ok=True)
outdir.touch()
return outdir
# ---------------------------------------
def primes(n: int) -> list[int]:
"""Return a list of the first n primes"""
sieve: list[bool] = [True] * n
result: list[int] = []
for i in range(2, n):
if sieve[i]:
result.append(i)
for j in range(i * i, n, i):
sieve[j] = False
return result
# --------------------------------------------
def main() -> None:
"""Main function"""
# call cmdline functions
n: int = process_input()
outdir: None | Path = process_output()
# call primes function
result: list = primes(n)
# OUTPUT
# write result of primtes() to output file if command line arg was given
if outdir is not None:
with open(outdir, "w") as f:
f.write(str(result))
print(f"Output file created at {outdir}")
else:
# print result of primes() to stdout
print(result)
# --------------------------------------------
if __name__ == "__main__":
main()