Repository navigation
Expand file tree
/
Copy pathfoo.py
More file actions
executable file
·113 lines (83 loc) · 3.03 KB
/
Copy pathfoo.py
File metadata and controls
executable file
·113 lines (83 loc) · 3.03 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
#!/usr/bin/env python3
"""Create Workout Of (the) Day (WOD)"""
import argparse
import csv
import io
import os
import random
from itertools import starmap
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='str',
type=argparse.FileType('r'),
default='exercises.csv')
parser.add_argument('-s',
'--seed',
help='Random seed',
metavar='int',
type=int,
default=None)
parser.add_argument('-n',
'--num_exercises',
help='Number of exercises',
metavar='int',
type=int,
default=4)
parser.add_argument('-e',
'--easy',
help='Make it easy',
action='store_true')
return parser.parse_args()
# --------------------------------------------------
def main():
"""Make a jazz noise here"""
args = get_args()
random.seed(args.seed)
wod = []
for name, low, high in read_csv(args.file):
reps = random.randint(low, high)
if args.easy:
reps = int(reps / 2)
wod.append((name, reps))
wod = random.sample(wod, k=args.num_exercises)
print(tabulate(wod, headers=('Exercise', 'Reps')))
# --------------------------------------------------
def read_csv(fh):
"""Read the CSV input"""
exercises = []
for row in csv.DictReader(fh, delimiter=','):
low, high = row['reps'].split('-')
if low.isdigit() and high.isdigit():
exercises.append((row['exercise'], int(low), int(high)))
return exercises
# --------------------------------------------------
def test_read_csv():
"""Test read_csv"""
text = io.StringIO('exercise,reps\nfoo,10-20\nbar,30-40')
assert read_csv(text) == [('foo', 10, 20), ('bar', 30, 40)]
# --------------------------------------------------
def make_exercise(name, low, high, easy):
"""Make an exercise"""
if easy:
low, high = int(low / 2), int(high / 2)
return (name, random.randint(low, high))
# --------------------------------------------------
def test_make_exercise():
"""Test make_exercise"""
random.seed(1)
assert make_exercise('foo', 10, 20, False) == ('foo', 12)
assert make_exercise('bar', 5, 30, True) == ('bar', 11)
assert make_exercise('baz', 0, 50, False) == ('baz', 48)
assert make_exercise('quux', 0, 50, True) == ('quux', 2)
random.seed(None)
# --------------------------------------------------
if __name__ == '__main__':
main()