forked from kyclark/tiny_python_projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution2.py
More file actions
executable file
·98 lines (74 loc) · 2.62 KB
/
solution2.py
File metadata and controls
executable file
·98 lines (74 loc) · 2.62 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
#!/usr/bin/env python3
"""Create Workout Of (the) Day (WOD)"""
import argparse
import csv
import io
import re
import random
import sys
from tabulate import tabulate
# --------------------------------------------------
def get_args():
"""Get command-line arguments"""
parser = argparse.ArgumentParser(
description='Create Workout Of (the) Day (WOD)',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-f',
'--file',
help='CSV input file of exercises',
metavar='FILE',
type=argparse.FileType('rt'),
default='inputs/exercises.csv')
parser.add_argument('-s',
'--seed',
help='Random seed',
metavar='seed',
type=int,
default=None)
parser.add_argument('-n',
'--num',
help='Number of exercises',
metavar='exercises',
type=int,
default=4)
parser.add_argument('-e',
'--easy',
help='Halve the reps',
action='store_true')
args = parser.parse_args()
if args.num < 1:
parser.error(f'--num "{args.num}" must be greater than 0')
return args
# --------------------------------------------------
def main():
"""Make a jazz noise here"""
args = get_args()
random.seed(args.seed)
exercises = read_csv(args.file)
if not exercises:
sys.exit(f'No usable data in --file "{args.file.name}"')
num_exercises = len(exercises)
if args.num > num_exercises:
sys.exit(f'--num "{args.num}" > exercises "{num_exercises}"')
wod = []
for name, low, high in random.sample(exercises, k=args.num):
reps = random.randint(low, high)
if args.easy:
reps = int(reps / 2)
wod.append((name, reps))
print(tabulate(wod, headers=('Exercise', 'Reps')))
# --------------------------------------------------
def read_csv(fh):
"""Read the CSV input"""
exercises = []
for row in csv.DictReader(fh, delimiter=','):
name, reps = row.get('exercise'), row.get('reps')
if name and reps:
match = re.match(r'(\d+)-(\d+)', reps)
if match:
low, high = map(int, match.groups())
exercises.append((name, low, high))
return exercises
# --------------------------------------------------
if __name__ == '__main__':
main()