Skip to content

Commit e86845e

Browse files
authored
Merge pull request #37 from lcongdon:tab
Tab
2 parents 1b35bad + ab303a7 commit e86845e

2 files changed

Lines changed: 50 additions & 3 deletions

File tree

‎19_wod/test_wod.py‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from subprocess import getstatusoutput
99

1010
prg = './wod.py'
11+
tab = 'inputs/bad-delimiter.tab'
1112
input1 = 'inputs/exercises.csv'
1213
input2 = 'inputs/silly-exercises.csv'
1314

@@ -49,6 +50,41 @@ def test_bad_file():
4950
assert re.search(f"No such file or directory: '{bad}'", out)
5051

5152

53+
# --------------------------------------------------
54+
def test_tab_delimiter():
55+
"""tab delimiter"""
56+
57+
expected = """
58+
Exercise Reps
59+
---------- ------
60+
Pushups 56
61+
Situps 88
62+
Crunches 27
63+
Burpees 35
64+
"""
65+
tab_flag = '-t' if random.choice([0, 1]) else '--tab'
66+
rv, out = getstatusoutput(f'{prg} -f {tab} -s 1 {tab_flag}')
67+
assert rv == 0
68+
assert out.strip() == expected.strip()
69+
70+
71+
# --------------------------------------------------
72+
def test_tab_delimiter_extension():
73+
"""tab delimiter based on file extension"""
74+
75+
expected = """
76+
Exercise Reps
77+
---------- ------
78+
Pushups 56
79+
Situps 88
80+
Crunches 27
81+
Burpees 35
82+
"""
83+
rv, out = getstatusoutput(f'{prg} -f {tab} -s 1')
84+
assert rv == 0
85+
assert out.strip() == expected.strip()
86+
87+
5288
# --------------------------------------------------
5389
def test_seed1():
5490
"""Runs OK"""

‎19_wod/wod.py‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"""
77

88
import sys
9+
import pathlib
910
import argparse
1011
import csv
1112
import random
@@ -44,17 +45,27 @@ def get_args():
4445
default=False,
4546
action='store_true',
4647
help='Halve the reps')
48+
49+
parser.add_argument('-t',
50+
'--tab',
51+
default=False,
52+
action='store_true',
53+
help='Tab delimiter in exercises')
4754

4855
args = parser.parse_args()
4956
if(args.num <= 0):
5057
parser.error(f'--num "{args.num}" must be greater than 0')
58+
args.delimiter = '\t' if args.tab else ','
59+
p = pathlib.Path(args.file.name)
60+
if (p.suffix == '.tab'):
61+
args.delimiter = '\t'
5162
return args
5263

5364

54-
def read_csv(fh):
65+
def read_csv(fh, delimiter=','):
5566
"""Read the CSV input"""
5667
exercises = []
57-
for rec in csv.DictReader(fh, delimiter=','):
68+
for rec in csv.DictReader(fh, delimiter=delimiter):
5869
if 'exercise' not in rec or 'reps' not in rec:
5970
continue
6071
name, reps = rec['exercise'], rec['reps']
@@ -74,7 +85,7 @@ def main():
7485

7586
args = get_args()
7687
random.seed(args.seed)
77-
exercises = read_csv(args.file)
88+
exercises = read_csv(args.file, args.delimiter)
7889

7990
if not exercises:
8091
sys.exit(f'No usable data in --file "{args.file.name}"')

0 commit comments

Comments
 (0)