Skip to content

Commit

Permalink
Bug out early once valid from unit is found and remainer is not a val…
Browse files Browse the repository at this point in the history
…id unit.

Makes error messages more understandable, as it now correctly identifies at least one unit.
  • Loading branch information
deanishe committed Nov 26, 2015
1 parent a6f2c2a commit 48db8ad
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
Binary file not shown.
11 changes: 9 additions & 2 deletions src/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,18 @@ def convert(query, decimal_places=2):
continue
try:
from_unit = ureg.Quantity(qty, q1)
to_unit = ureg.Quantity(1, q2)
except UndefinedUnitError: # Didn't make sense; try again
except UndefinedUnitError:
continue
else:
log.debug('From unit : %s', q1)
try:
to_unit = ureg.Quantity(1, q2)
except UndefinedUnitError: # Didn't make sense; try again
raise ValueError('Unknown unit : %s' % q2)

log.debug("from '%s' to '%s'", from_unit.units, to_unit.units)
break # Got something!

# Throw error if we arrive here with no units
if from_unit is None:
raise ValueError('Unknown unit : %s' % q1)
Expand Down
2 changes: 1 addition & 1 deletion src/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.2.1
2.3

0 comments on commit 48db8ad

Please sign in to comment.