From 49a39fc253a9529b547461e456c2cc3b0c2719f0 Mon Sep 17 00:00:00 2001 From: Ernesto Rodriguez Ortiz Date: Fri, 29 Sep 2017 15:29:25 -0400 Subject: [PATCH] Allow call to other convertes as to_currency, to_year There are at least to issues related with questions about how to use other convertes. This changes should allow the use of this converters --- num2words/__init__.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/num2words/__init__.py b/num2words/__init__.py index 69ad8db..a53456a 100644 --- a/num2words/__init__.py +++ b/num2words/__init__.py @@ -63,7 +63,10 @@ CONVERTER_CLASSES = { 'vi_VN': lang_VN.Num2Word_VN() } -def num2words(number, ordinal=False, lang='en'): +CONVERTES_TYPES = ['cardinal', 'ordinal', 'year', 'currency'] + + +def num2words(number, ordinal=False, lang='en', to='cardinal'): # We try the full language first if lang not in CONVERTER_CLASSES: # ... and then try only the first 2 letters @@ -71,7 +74,11 @@ def num2words(number, ordinal=False, lang='en'): if lang not in CONVERTER_CLASSES: raise NotImplementedError() converter = CONVERTER_CLASSES[lang] + # backwards compatible if ordinal: return converter.to_ordinal(number) - else: - return converter.to_cardinal(number) + + if to not in CONVERTES_TYPES: + raise NotImplementedError() + + return getattr(converter, 'to_{}'.format(to))(number)