Skip to content

Commit 4657495

Browse files
committed
Minor code refactoring
1 parent ce3204d commit 4657495

File tree

1 file changed

+25
-30
lines changed

1 file changed

+25
-30
lines changed

EulerPy/euler.py

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,10 @@ def verify(p, filename=None, exit=True):
9090
click.secho('Error calling "{0}".'.format(filename), fg='red')
9191
click.secho(time_info, fg='cyan')
9292

93-
# Exit here if appropriate, otherwise return None (for --verify-all)
94-
if exit:
95-
sys.exit(1)
96-
else:
97-
return None
93+
# Return None if option is not --verify-all, otherwise exit
94+
return sys.exit(1) if exit else None
9895

99-
# Python 3 returns bytes; use a valid encoding like ASCII
96+
# Decode output if returned as bytes (Python 3)
10097
if isinstance(stdout, bytes):
10198
output = stdout.decode('ascii')
10299

@@ -117,15 +114,12 @@ def verify(p, filename=None, exit=True):
117114

118115
click.secho(time_info, fg='cyan')
119116

120-
# Exit here if answer was incorrect
121-
if exit and not is_correct:
122-
sys.exit(1)
123-
else:
124-
# Remove any suffix from the filename if its solution is correct
125-
if is_correct and filename != p.filename:
126-
rename_file(filename, p.filename)
117+
# Remove any suffix from the filename if its solution is correct
118+
if is_correct and filename != p.filename:
119+
rename_file(filename, p.filename)
127120

128-
return is_correct
121+
# Exit here if answer was incorrect, otherwise return is_correct value
122+
return sys.exit(1) if exit and not is_correct else is_correct
129123

130124

131125
# --verify-all
@@ -135,6 +129,15 @@ def verify_all(current_p):
135129
prints an overview of the status of each problem.
136130
"""
137131

132+
# Define various problem statuses
133+
status = {
134+
'correct': click.style('C', fg='green', bold=True),
135+
'incorrect': click.style('I', fg='red', bold=True),
136+
'error': click.style('E', fg='yellow', bold=True),
137+
'skipped': click.style('S', fg='cyan', bold=True),
138+
'missing': click.style('.', fg='white', bold=True),
139+
}
140+
138141
overview = {}
139142

140143
# Search through problem files using glob module
@@ -146,14 +149,14 @@ def verify_all(current_p):
146149
try:
147150
is_correct = verify(p, filename=filename, exit=False)
148151
except KeyboardInterrupt:
149-
overview[p.num] = click.style('S', fg='cyan')
152+
overview[p.num] = status['skipped']
150153
else:
151154
if is_correct is None: # error was returned by problem file
152-
overview[p.num] = click.style('E', fg='yellow')
155+
overview[p.num] = status['error']
153156
elif is_correct:
154-
overview[p.num] = click.style('C', fg='green')
157+
overview[p.num] = status['correct']
155158
elif not is_correct:
156-
overview[p.num] = click.style('I', fg='red')
159+
overview[p.num] = status['incorrect']
157160

158161
# Attempt to add "skipped" suffix to the filename if the
159162
# problem file is not the current problem. This is useful
@@ -171,19 +174,11 @@ def verify_all(current_p):
171174
sys.exit(1)
172175

173176
# Print overview of the status of each problem
174-
click.echo('-' * 63)
175-
176-
legend = ', '.join(
177-
'{0} = {1}'.format(click.style(symbol, bold=True, fg=colour), name)
178-
for symbol, name, colour in (
179-
('C', 'correct', 'green'),
180-
('I', 'incorrect', 'red'),
181-
('E', 'error', 'yellow'),
182-
('S', 'skipped', 'cyan'),
183-
('.', 'missing', 'white'),
184-
)
177+
legend = ', '.join('{0} = {1}'.format(status[key], key) for key in
178+
('correct', 'incorrect', 'error', 'skipped', 'missing')
185179
)
186180

181+
click.echo('-' * 63)
187182
click.echo(legend + '\n')
188183

189184
# Rows needed for overview is based on the current problem number
@@ -201,7 +196,7 @@ def verify_all(current_p):
201196
spacer = ' ' if (problem % 5 == 0) else ' '
202197

203198
# Start a new line at the end of each row
204-
click.secho(status + spacer, bold=True, nl=(problem % 20 == 0))
199+
click.secho(status + spacer, nl=(problem % 20 == 0))
205200

206201
click.echo()
207202

0 commit comments

Comments
 (0)