Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 55d3c4b

Browse files
committed
cleanup
1 parent ee39c3f commit 55d3c4b

20 files changed

+62
-141
lines changed

19_wod/foo.py

Lines changed: 0 additions & 113 deletions
This file was deleted.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

19_wod/manual1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from pprint import pprint
44

5-
with open('exercises.csv') as fh:
5+
with open('inputs/exercises.csv') as fh:
66
headers = fh.readline().rstrip().split(',')
77
records = []
88
for line in fh:

19_wod/manual2.py renamed to 19_wod/manual2_list_comprehension.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from pprint import pprint
44

5-
with open('exercises.csv') as fh:
5+
with open('inputs/exercises.csv') as fh:
66
headers = fh.readline().rstrip().split(',')
77
records = [dict(zip(headers, line.rstrip().split(','))) for line in fh]
88
pprint(records)

19_wod/manual3.py renamed to 19_wod/manual3_map.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from pprint import pprint
44

5-
with open('exercises.csv') as fh:
5+
with open('inputs/exercises.csv') as fh:
66
headers = fh.readline().rstrip().split(',')
77
records = list(
88
map(lambda line: dict(zip(headers,

19_wod/manual4_map2.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env python3
2+
3+
from pprint import pprint
4+
5+
with open('inputs/exercises.csv') as fh:
6+
headers = fh.readline().rstrip().split(',')
7+
mk_rec = lambda line: dict(zip(headers, line.rstrip().split(',')))
8+
records = map(mk_rec, fh)
9+
pprint(list(records))

19_wod/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
csv
2+
csvkit
3+
tabulate

19_wod/solution1.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,21 @@ def get_args():
1919
parser.add_argument('-f',
2020
'--file',
2121
help='CSV input file of exercises',
22-
metavar='str',
22+
metavar='FILE',
2323
type=argparse.FileType('r'),
24-
default='exercises.csv')
24+
default='inputs/exercises.csv')
2525

2626
parser.add_argument('-s',
2727
'--seed',
2828
help='Random seed',
29-
metavar='int',
29+
metavar='seed',
3030
type=int,
3131
default=None)
3232

3333
parser.add_argument('-n',
3434
'--num',
3535
help='Number of exercises',
36-
metavar='int',
36+
metavar='exercises',
3737
type=int,
3838
default=4)
3939

@@ -57,8 +57,9 @@ def main():
5757
args = get_args()
5858
random.seed(args.seed)
5959
wod = []
60+
exercises = read_csv(args.file)
6061

61-
for name, low, high in random.sample(read_csv(args.file), k=args.num):
62+
for name, low, high in random.sample(exercises, k=args.num):
6263
reps = random.randint(low, high)
6364
if args.easy:
6465
reps = int(reps / 2)

19_wod/solution2.py

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import io
77
import re
88
import random
9+
import sys
910
from tabulate import tabulate
1011

1112

@@ -20,21 +21,21 @@ def get_args():
2021
parser.add_argument('-f',
2122
'--file',
2223
help='CSV input file of exercises',
23-
metavar='str',
24+
metavar='FILE',
2425
type=argparse.FileType('r'),
25-
default='exercises.csv')
26+
default='inputs/exercises.csv')
2627

2728
parser.add_argument('-s',
2829
'--seed',
2930
help='Random seed',
30-
metavar='int',
31+
metavar='seed',
3132
type=int,
3233
default=None)
3334

3435
parser.add_argument('-n',
3536
'--num',
3637
help='Number of exercises',
37-
metavar='int',
38+
metavar='exercises',
3839
type=int,
3940
default=4)
4041

@@ -46,7 +47,7 @@ def get_args():
4647
args = parser.parse_args()
4748

4849
if args.num < 1:
49-
parser.error('--num "{args.num}" must be greater than 0')
50+
parser.error(f'--num "{args.num}" must be greater than 0')
5051

5152
return args
5253

@@ -59,16 +60,29 @@ def main():
5960
random.seed(args.seed)
6061
exercises = read_csv(args.file)
6162

62-
if exercises:
63-
for name, low, high in random.sample(exercises, k=args.num):
64-
reps = random.randint(low, high)
65-
if args.easy:
66-
reps = int(reps / 2)
67-
wod.append((name, reps))
63+
if not exercises:
64+
die(f'No usable data in --file "{args.file.name}"')
6865

69-
print(tabulate(wod, headers=('Exercise', 'Reps')))
70-
else:
71-
print(f'No usable data in --file "{args.file.name}"')
66+
num_exercises = len(exercises)
67+
if args.num > num_exercises:
68+
die(f'--num "{args.num}" greater than exercises "{num_exercises}"')
69+
70+
wod = []
71+
for name, low, high in random.sample(exercises, k=args.num):
72+
reps = random.randint(low, high)
73+
if args.easy:
74+
reps = int(reps / 2)
75+
wod.append((name, reps))
76+
77+
print(tabulate(wod, headers=('Exercise', 'Reps')))
78+
79+
80+
# --------------------------------------------------
81+
def die(msg):
82+
"""Print message to STDERR and exit with an error"""
83+
84+
print(msg, file=sys.stderr)
85+
sys.exit(1)
7286

7387

7488
# --------------------------------------------------

19_wod/test.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
from subprocess import getstatusoutput
99

1010
prg = './wod.py'
11-
input1 = 'exercises.csv'
12-
input2 = 'silly-exercises.csv'
11+
input1 = 'inputs/exercises.csv'
12+
input2 = 'inputs/silly-exercises.csv'
1313

1414

1515
# --------------------------------------------------
@@ -107,7 +107,8 @@ def test_seed2_num8():
107107

108108
seed_flag = '-s' if random.choice([0, 1]) else '--seed'
109109
num_flag = '-n' if random.choice([0, 1]) else '--num'
110-
rv, out = getstatusoutput(f'{prg} {num_flag} 8 {seed_flag} 2 -f {input1}')
110+
cmd = f'{prg} {num_flag} 8 {seed_flag} 2 -f {input1}'
111+
rv, out = getstatusoutput(cmd)
111112
assert rv == 0
112113
assert out.strip() == expected.strip()
113114

19_wod/using_csv1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import csv
44
from pprint import pprint
55

6-
with open('exercises.csv') as fh:
6+
with open('inputs/exercises.csv') as fh:
77
reader = csv.DictReader(fh, delimiter=',')
88
records = []
99
for rec in reader:

19_wod/using_csv2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import csv
44
from pprint import pprint
55

6-
with open('exercises.csv') as fh:
6+
with open('inputs/exercises.csv') as fh:
77
reader = csv.DictReader(fh, delimiter=',')
88
records = list(reader)
99
pprint(records)

19_wod/using_csv3.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import csv
44
from pprint import pprint
55

6-
with open('exercises.csv') as fh:
6+
with open('inputs/exercises.csv') as fh:
77
reader = csv.DictReader(fh, delimiter=',')
88
exercises = []
99
for rec in reader:

19_wod/using_pandas.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env python3
2+
3+
import pandas as pd
4+
5+
df = pd.read_csv('inputs/exercises.csv')
6+
print(df)

0 commit comments

Comments
 (0)