Skip to content

Commit

Permalink
Refine the template processing in QuantiPhyError.render().
Browse files Browse the repository at this point in the history
  • Loading branch information
Ken Kundert authored and Ken Kundert committed Aug 14, 2019
1 parent 6a7e70e commit dfdc3c9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
14 changes: 14 additions & 0 deletions doc/user.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2094,4 +2094,18 @@ a format string that can access both the unnamed and named arguments:
... q = Quantity('tweed')
... except InvalidNumber as e:
... print(e.render('{}: no es un número valido.'))
... except UnknownScaleFactor as e:
... print(e.render('factor de escala desconocido.'))
... except UnknownConversion as e:
... if 'direction' in e.kwargs:
... direction = e.kwargs['direction']
... if direction == 'to':
... template = 'incapaz de convertir a {}'
... else: # direction must be 'from'
... template = 'incapaz de convertir de {}'
... else:
... template = 'incapaz de convertir entre {} y {}'
... print(e.render(template))
... except InvalidRecognizer as e:
... print(e.render("el reconocedor no contiene la clave 'val'"))
tweed: no es un número valido.
4 changes: 2 additions & 2 deletions quantiphy.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def render(self, template=None):
if msg == template and self.args:
break
return msg
except IndexError:
except (IndexError, KeyError):
continue
culprits = ', '.join(str(a) for a in self.args)
return '{}: {}'.format(culprits, template)
Expand Down Expand Up @@ -174,8 +174,8 @@ class UnknownConversion(QuantiPhyError, KeyError):
conversion was requested and there is no corresponding unit converter.
"""
_template = (
"unable to convert between '{}' and '{}'.",
"unable to convert {direction} '{}'.",
"unable to convert between '{}' and '{}'.",
)


Expand Down

0 comments on commit dfdc3c9

Please sign in to comment.