Skip to content

Commit

Permalink
Merge branch 'master' into Feat/add_MX_to_lang_EU
Browse files Browse the repository at this point in the history
  • Loading branch information
temoctzin committed Sep 20, 2018
2 parents 9c4d629 + c71d99c commit be77739
Show file tree
Hide file tree
Showing 32 changed files with 736 additions and 410 deletions.
4 changes: 4 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[report]
omit =
*/.tox/*
*/tests/*
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
include CHANGES.rst
include COPYING
include tests/*
include bin/num2words
16 changes: 14 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,20 @@ The test suite in this library is new, so it's rather thin, but it can be run wi

Usage
-----

There's only one function to use::
Command line::

$ num2words 10001
ten thousand and one
$ num2words 10123123 --lang es
diez millones ciento veintitrés mil ciento veintitrés
$ num2words 24,120.10
twenty-four thousand, one hundred and twenty point one
$ num2words 24,120.10 -l es
veinticuatro mil ciento veinte punto uno
$num2words 2.14 -l es --to currency
dos euros con catorce centimos

In code there's only one function to use::

>>> from num2words import num2words
>>> num2words(42)
Expand Down
81 changes: 81 additions & 0 deletions bin/num2words
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""num2words: convert numbers into words.
Usage:
num2words [options] <number>
num2words --list-languages
num2words --list-converters
num2words --help
Arguments:
<number> Number you want to convert into words
Options:
-L --list-languages Show all languages.
-C --list-converters Show all converters.
-l --lang=<lang> Output language [default: en].
-t --to=<to> Output converter [default: cardinal].
-h --help Show this message.
-v --version Show version.
Examples:
$ num2words 10001
ten thousand and one
$ num2words 10123123 --lang es
diez millones ciento veintitrés mil ciento veintitrés
$ num2words 24,120.10
twenty-four thousand, one hundred and twenty point one
$ num2words 24,120.10 -l es
veinticuatro mil ciento veinte punto uno
$num2words 2.14 -l es --to currency
dos euros con catorce centimos
"""
from __future__ import print_function, unicode_literals
import os
import sys
from docopt import docopt
import num2words

__version__ = "0.5.7"
__license__ = "LGPL"


def get_languages():
return sorted(list(num2words.CONVERTER_CLASSES.keys()))


def get_converters():
return sorted(list(num2words.CONVERTES_TYPES))


def main():
version = "{}=={}".format(os.path.basename(__file__), __version__)
args = docopt(__doc__, argv=None, help=True, version=version, options_first=False)
if args["--list-languages"]:
for lang in get_languages():
sys.stdout.write(lang)
sys.stdout.write(os.linesep)
sys.exit(0)
if args["--list-converters"]:
for lang in get_converters():
sys.stdout.write(lang)
sys.stdout.write(os.linesep)
sys.exit(0)
try:
words = num2words.num2words(args['<number>'], lang=args['--lang'], to=args['--to'])
sys.stdout.write(words+os.linesep)
sys.exit(0)
except Exception as err:
sys.stderr.write(str(args['<number>']))
sys.stderr.write(str(err) + os.linesep)
sys.stderr.write(__doc__)
sys.exit(1)


if __name__ == '__main__':
main()
48 changes: 12 additions & 36 deletions num2words/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class Num2Word_Base(object):
CURRENCY_ADJECTIVES = {}

def __init__(self):
self.cards = OrderedDict()
self.is_title = False
self.precision = 2
self.exclude_title = []
Expand All @@ -40,21 +39,22 @@ def __init__(self):
self.errmsg_negord = "Cannot treat negative num %s as ordinal."
self.errmsg_toobig = "abs(%s) must be less than %s."

self.base_setup()
self.setup()
self.set_numwords()

self.MAXVAL = 1000 * list(self.cards.keys())[0]
# uses cards
if any(hasattr(self, field) for field in
['high_numwords', 'mid_numwords', 'low_numwords']):
self.cards = OrderedDict()
self.set_numwords()
self.MAXVAL = 1000 * list(self.cards.keys())[0]

def set_numwords(self):
self.set_high_numwords(self.high_numwords)
self.set_mid_numwords(self.mid_numwords)
self.set_low_numwords(self.low_numwords)

def gen_high_numwords(self, units, tens, lows):
out = [u + t for t in tens for u in units]
out.reverse()
return out + lows
def set_high_numwords(self, *args):
raise NotImplementedError

def set_mid_numwords(self, mid):
for key, val in mid:
Expand Down Expand Up @@ -102,8 +102,6 @@ def to_cardinal(self, value):
except (ValueError, TypeError, AssertionError):
return self.to_cardinal_float(value)

self.verify_num(value)

out = ""
if value < 0:
value = abs(value)
Expand Down Expand Up @@ -197,9 +195,6 @@ def verify_ordinal(self, value):
if not abs(value) == value:
raise TypeError(self.errmsg_negord % value)

def verify_num(self, value):
return 1

def set_wordnums(self):
pass

Expand Down Expand Up @@ -264,6 +259,9 @@ def pluralize(self, n, forms):
def _cents_verbose(self, number, currency):
return self.to_cardinal(number)

def _cents_terse(self, number, currency):
return "%02d" % number

def to_currency(self, val, currency='EUR', cents=True, seperator=',',
adjective=False):
"""
Expand Down Expand Up @@ -292,7 +290,7 @@ def to_currency(self, val, currency='EUR', cents=True, seperator=',',

minus_str = "%s " % self.negword if is_negative else ""
cents_str = self._cents_verbose(right, currency) \
if cents else "%02d" % right
if cents else self._cents_terse(right, currency)

return u'%s%s %s%s %s %s' % (
minus_str,
Expand All @@ -303,27 +301,5 @@ def to_currency(self, val, currency='EUR', cents=True, seperator=',',
self.pluralize(right, cr2)
)

def base_setup(self):
pass

def setup(self):
pass

def test(self, value):
try:
_card = self.to_cardinal(value)
except Exception:
_card = "invalid"

try:
_ord = self.to_ordinal(value)
except Exception:
_ord = "invalid"

try:
_ordnum = self.to_ordinal_num(value)
except Exception:
_ordnum = "invalid"

print("For %s, card is %s;\n\tord is %s; and\n\tordnum is %s."
% (value, _card, _ord, _ordnum))

0 comments on commit be77739

Please sign in to comment.