Convert to single codebase for py2 and py3

No more 2to3. Also, added tox config for easy cross-python testing.
This commit is contained in:
Virgil Dupras
2016-11-22 09:13:18 -05:00
parent 0e06eb81dd
commit 20f634028d
15 changed files with 256 additions and 206 deletions

View File

@@ -15,7 +15,11 @@
# MA 02110-1301 USA
from __future__ import unicode_literals
import math
from .orderedmapping import OrderedMapping
from .compat import to_s
class Num2Word_Base(object):
@@ -88,7 +92,7 @@ class Num2Word_Base(object):
def to_cardinal(self, value):
try:
assert long(value) == value
assert int(value) == value
except (ValueError, TypeError, AssertionError):
return self.to_cardinal_float(value)
@@ -113,9 +117,18 @@ class Num2Word_Base(object):
except (ValueError, TypeError, AssertionError):
raise TypeError(self.errmsg_nonnum % value)
value = float(value)
pre = int(value)
post = str(abs(value - pre) * 10**self.precision)
post = '0' * (self.precision - len(post.split('.')[0])) + post
post = abs(value - pre) * 10**self.precision
if abs(round(post) - post) < 0.01:
# We generally floor all values beyond our precision (rather than rounding), but in
# cases where we have something like 1.239999999, which is probably due to python's
# handling of floats, we actually want to consider it as 1.24 instead of 1.23
post = int(round(post))
else:
post = int(math.floor(post))
post = str(post)
post = '0' * (self.precision - len(post)) + post
out = [self.to_cardinal(pre)]
if self.precision:
@@ -123,7 +136,7 @@ class Num2Word_Base(object):
for i in range(self.precision):
curr = int(post[i])
out.append(unicode(self.to_cardinal(curr)))
out.append(to_s(self.to_cardinal(curr)))
return " ".join(out)
@@ -168,10 +181,10 @@ class Num2Word_Base(object):
def verify_ordinal(self, value):
if not value == long(value):
raise TypeError, self.errmsg_floatord %(value)
if not value == int(value):
raise TypeError(self.errmsg_floatord % value)
if not abs(value) == value:
raise TypeError, self.errmsg_negord %(value)
raise TypeError(self.errmsg_negord % value)
def verify_num(self, value):
@@ -181,7 +194,7 @@ class Num2Word_Base(object):
def set_wordnums(self):
pass
def to_ordinal(self, value):
return self.to_cardinal(value)

26
num2words/compat.py Normal file
View File

@@ -0,0 +1,26 @@
# Copyright (c) 2003, Taro Ogawa. All Rights Reserved.
# Copyright (c) 2016, Savoir-faire Linux inc. All Rights Reserved.
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA
import sys
PY3 = sys.version_info[0] == 3
def to_s(val):
if PY3:
return str(val)
else:
return unicode(val)

View File

@@ -15,7 +15,7 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA
from __future__ import unicode_literals
from __future__ import unicode_literals, print_function
from .lang_EU import Num2Word_EU
class Num2Word_DE(Num2Word_EU):
@@ -133,10 +133,10 @@ def main():
n2w.test(3000000)
n2w.test(3000000000001)
n2w.test(3000000324566)
print n2w.to_currency(112121)
print n2w.to_year(2000)
print n2w.to_year(1820)
print n2w.to_year(2001)
print(n2w.to_currency(112121))
print(n2w.to_year(2000))
print(n2w.to_year(1820))
print(n2w.to_year(2001))
if __name__ == "__main__":
main()

View File

@@ -14,7 +14,7 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA
from __future__ import division, unicode_literals
from __future__ import division, unicode_literals, print_function
from num2words import lang_EU
class Num2Word_DK(lang_EU.Num2Word_EU):
@@ -146,8 +146,8 @@ def main():
n2w.test(val)
n2w.test(1325325436067876801768700107601001012212132143210473207540327057320957032975032975093275093275093270957329057320975093272950730)
for val in [1,120, 160, 1000,1120,1800, 1976,2000,2010,2099,2171]:
print val, "er", n2w.to_currency(val)
print val, "er", n2w.to_year(val)
print(val, "er", n2w.to_currency(val))
print(val, "er", n2w.to_year(val))
n2w.test(65132)
if __name__ == "__main__":

View File

@@ -14,7 +14,7 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA
from __future__ import division, unicode_literals
from __future__ import division, unicode_literals, print_function
from . import lang_EU
class Num2Word_EN(lang_EU.Num2Word_EU):
@@ -47,7 +47,9 @@ class Num2Word_EN(lang_EU.Num2Word_EU):
"twelve" : "twelfth" }
def merge(self, (ltext, lnum), (rtext, rnum)):
def merge(self, lpair, rpair):
ltext, lnum = lpair
rtext, rnum = rpair
if lnum == 1 and rnum < 100:
return (rtext, rnum)
elif 100 > lnum > rnum :
@@ -68,9 +70,9 @@ class Num2Word_EN(lang_EU.Num2Word_EU):
lastword = self.ords[lastword]
except KeyError:
if lastword[-1] == "y":
lastword = lastword[:-1] + "ie"
lastword = lastword[:-1] + "ie"
lastword += "th"
lastwords[-1] = self.title(lastword)
lastwords[-1] = self.title(lastword)
outwords[-1] = "-".join(lastwords)
return " ".join(outwords)
@@ -105,9 +107,9 @@ def main():
n2w.test(val)
n2w.test(1325325436067876801768700107601001012212132143210473207540327057320957032975032975093275093275093270957329057320975093272950730)
for val in [1,120,1000,1120,1800, 1976,2000,2010,2099,2171]:
print val, "is", n2w.to_currency(val)
print val, "is", n2w.to_year(val)
print(val, "is", n2w.to_currency(val))
print(val, "is", n2w.to_year(val))
if __name__ == "__main__":
main()

View File

@@ -14,10 +14,10 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA
from __future__ import unicode_literals
from __future__ import unicode_literals, print_function
from .lang_EN import Num2Word_EN
class Num2Word_EN_GB(Num2Word_EN):
def to_currency(self, val, longval=True):
return self.to_splitnum(val, hightxt="pound/s", lowtxt="pence",
@@ -38,9 +38,9 @@ def main():
n2w.test(val)
n2w.test(1325325436067876801768700107601001012212132143210473207540327057320957032975032975093275093275093270957329057320975093272950730)
for val in [1,120,1000,1120,1800, 1976,2000,2010,2099,2171]:
print val, "is", n2w.to_currency(val)
print val, "is", n2w.to_year(val)
print(val, "is", n2w.to_currency(val))
print(val, "is", n2w.to_year(val))
if __name__ == "__main__":
main()

View File

@@ -16,7 +16,7 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA
from __future__ import unicode_literals
from __future__ import unicode_literals, print_function
from .lang_EU import Num2Word_EU
class Num2Word_ES(Num2Word_EU):
@@ -173,9 +173,9 @@ def main():
n2w.test(val)
n2w.test(1325325436067876801768700107601001012212132143210473207540327057320957032975032975093275093275093270957329057320975093272950730)
print n2w.to_currency(1222)
print n2w.to_currency(1222, old=True)
print n2w.to_year(1222)
print(n2w.to_currency(1222))
print(n2w.to_currency(1222, old=True))
print(n2w.to_year(1222))
if __name__ == "__main__":
main()

View File

@@ -15,7 +15,7 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA
from __future__ import unicode_literals
from __future__ import unicode_literals, print_function
from .lang_EU import Num2Word_EU
@@ -106,8 +106,8 @@ def main():
n2w.test(val)
n2w.test(1325325436067876801768700107601001012212132143210473207540327057320957032975032975093275093275093270957329057320975093272950730)
print n2w.to_currency(112121)
print n2w.to_year(1996)
print(n2w.to_currency(112121))
print(n2w.to_year(1996))
if __name__ == "__main__":

View File

@@ -15,7 +15,7 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA
from __future__ import unicode_literals
from __future__ import unicode_literals, print_function
from .lang_EU import Num2Word_EU
class Num2Word_FR_CH(Num2Word_EU):
@@ -101,8 +101,8 @@ def main():
n2w.test(val)
n2w.test(1325325436067876801768700107601001012212132143210473207540327057320957032975032975093275093275093270957329057320975093272950730)
print n2w.to_currency(112121)
print n2w.to_year(1996)
print(n2w.to_currency(112121))
print(n2w.to_year(1996))
if __name__ == "__main__":

View File

@@ -14,6 +14,8 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA
from __future__ import unicode_literals, print_function
class Num2Word_ID():
BASE = {0: [],
@@ -188,7 +190,7 @@ class Num2Word_ID():
return self.to_cardinal(value)
def verify_ordinal(self, value):
if not value == long(value):
raise TypeError, self.errmsg_floatord %(value)
if not value == int(value):
raise TypeError(self.errmsg_floatord % value)
if not abs(value) == value:
raise TypeError, self.errmsg_negord %(value)
raise TypeError(self.errmsg_negord % value)

View File

@@ -14,7 +14,7 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA
from __future__ import division, unicode_literals
from __future__ import division, unicode_literals, print_function
from . import lang_EU
class Num2Word_NO(lang_EU.Num2Word_EU):
@@ -54,7 +54,9 @@ class Num2Word_NO(lang_EU.Num2Word_EU):
"tjue" : "tjuende" }
def merge(self, (ltext, lnum), (rtext, rnum)):
def merge(self, lpair, rpair):
ltext, lnum = lpair
rtext, rnum = rpair
if lnum == 1 and rnum < 100:
return (rtext, rnum)
elif 100 > lnum > rnum :
@@ -75,10 +77,10 @@ class Num2Word_NO(lang_EU.Num2Word_EU):
lastword = self.ords[lastword]
except KeyError:
if lastword[-2:] == "ti":
lastword = lastword + "ende"
lastword = lastword + "ende"
else:
lastword += "de"
lastwords[-1] = self.title(lastword)
lastwords[-1] = self.title(lastword)
outwords[-1] = "".join(lastwords)
return " ".join(outwords)
@@ -113,9 +115,9 @@ def main():
n2w.test(val)
n2w.test(1325325436067876801768700107601001012212132143210473207540327057320957032975032975093275093275093270957329057320975093272950730)
for val in [1,120,1000,1120,1800, 1976,2000,2010,2099,2171]:
print val, "er", n2w.to_currency(val)
print val, "er", n2w.to_year(val)
print(val, "er", n2w.to_currency(val))
print(val, "er", n2w.to_year(val))
if __name__ == "__main__":
main()