Skip to content

Commit 37ce190

Browse files
committed
Implement table formatting
1 parent 1b35bad commit 37ce190

2 files changed

Lines changed: 88 additions & 1 deletion

File tree

‎19_wod/test_wod.py‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,63 @@ def test_seed4_num3_input2():
132132
assert out.strip() == expected.strip()
133133

134134

135+
# --------------------------------------------------
136+
def test_bad_tablefmt():
137+
"""Dies on bad --tablefmt"""
138+
139+
bad = 'badformat'
140+
rv, out = getstatusoutput(f'{prg} -tf {bad}')
141+
assert rv != 0
142+
assert re.search(f"argument -tf/--tablefmt: invalid choice: '{bad}'", out)
143+
144+
# --------------------------------------------------
145+
146+
147+
def test_seed2_num8_tablefmt_plain():
148+
"""Runs OK, plain formatting"""
149+
150+
expected = """
151+
Exercise Reps
152+
Burpees 39
153+
Situps 42
154+
Crunches 29
155+
Pushups 68
156+
Plank 35
157+
Hand-stand pushups 18
158+
Pullups 30
159+
Lunges 32
160+
"""
161+
162+
seed_flag = '-s' if random.choice([0, 1]) else '--seed'
163+
num_flag = '-n' if random.choice([0, 1]) else '--num'
164+
format_flag = '-tf' if random.choice([0, 1]) else '--tablefmt'
165+
cmd = f'{prg} {num_flag} 8 {seed_flag} 2 -f {input1} {format_flag} plain'
166+
rv, out = getstatusoutput(cmd)
167+
assert rv == 0
168+
assert out.strip() == expected.strip()
169+
170+
171+
# --------------------------------------------------
172+
def test_seed4_num3_input2_tablefmt_github():
173+
"""Runs OK, github formatting"""
174+
175+
expected = """
176+
| Exercise | Reps |
177+
|-------------------|--------|
178+
| Hanging Chads | 86 |
179+
| Red Barchettas | 50 |
180+
| Squatting Chinups | 35 |
181+
"""
182+
183+
seed_flag = '-s' if random.choice([0, 1]) else '--seed'
184+
num_flag = '-n' if random.choice([0, 1]) else '--num'
185+
format_flag = '-tf' if random.choice([0, 1]) else '--tablefmt'
186+
rv, out = getstatusoutput(
187+
f'{prg} {num_flag} 3 {seed_flag} 4 -f {input2} {format_flag} github')
188+
assert rv == 0
189+
assert out.strip() == expected.strip()
190+
191+
135192
# --------------------------------------------------
136193
def random_string():
137194
"""generate a random string"""

‎19_wod/wod.py‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,36 @@ def get_args():
4545
action='store_true',
4646
help='Halve the reps')
4747

48+
table_formats = ("plain",
49+
"simple",
50+
"github",
51+
"grid",
52+
"fancy_grid",
53+
"pipe",
54+
"orgtbl",
55+
"jira",
56+
"presto",
57+
"pretty",
58+
"psql",
59+
"rst",
60+
"mediawiki",
61+
"moinmoin",
62+
"youtrack",
63+
"html",
64+
"unsafehtml",
65+
"latex",
66+
"latex_raw",
67+
"latex_booktabs",
68+
"latex_longtable",
69+
"textile",
70+
"tsv")
71+
72+
parser.add_argument('-tf',
73+
'--tablefmt',
74+
default='simple',
75+
choices=table_formats,
76+
help='Output formatting')
77+
4878
args = parser.parse_args()
4979
if(args.num <= 0):
5080
parser.error(f'--num "{args.num}" must be greater than 0')
@@ -89,7 +119,7 @@ def main():
89119
if args.easy:
90120
reps = int(reps/2)
91121
workout.append((name, reps))
92-
print(tabulate(workout, headers=('Exercise', 'Reps')))
122+
print(tabulate(workout, headers=('Exercise', 'Reps'), tablefmt=args.tablefmt))
93123

94124

95125
if __name__ == '__main__':

0 commit comments

Comments
 (0)