From e2ce7c25621d5380f5927d028ae3c0b9bf9bf871 Mon Sep 17 00:00:00 2001 From: William Moreno Reyes Date: Mon, 23 Oct 2017 15:58:00 -0600 Subject: [PATCH] Many PEP8 fixes Signed-off-by: William Moreno Reyes --- num2words/__init__.py | 1 + num2words/base.py | 36 +++++++----------------------------- num2words/compat.py | 2 +- num2words/lang_AR.py | 24 +++++++++++------------- num2words/lang_DE.py | 1 + num2words/lang_DK.py | 2 ++ num2words/lang_EN.py | 2 ++ num2words/lang_HE.py | 2 +- num2words/lang_NL.py | 5 +++-- num2words/lang_PL.py | 2 +- num2words/lang_RU.py | 2 +- num2words/lang_UK.py | 7 ++++--- 12 files changed, 35 insertions(+), 51 deletions(-) diff --git a/num2words/__init__.py b/num2words/__init__.py index 2f1a8f2..9c168d4 100644 --- a/num2words/__init__.py +++ b/num2words/__init__.py @@ -70,6 +70,7 @@ CONVERTER_CLASSES = { 'uk': lang_UK.Num2Word_UK() } + def num2words(number, ordinal=False, lang='en'): # We try the full language first if lang not in CONVERTER_CLASSES: diff --git a/num2words/base.py b/num2words/base.py index e1e4383..d242f34 100644 --- a/num2words/base.py +++ b/num2words/base.py @@ -41,29 +41,24 @@ class Num2Word_Base(object): self.MAXVAL = 1000 * self.cards.order[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_mid_numwords(self, mid): for key, val in mid: self.cards[key] = val - def set_low_numwords(self, numwords): for word, n in zip(numwords, range(len(numwords) - 1, -1, -1)): self.cards[n] = word - def splitnum(self, value): for elem in self.cards: if elem > value: @@ -89,7 +84,6 @@ class Num2Word_Base(object): return out - def to_cardinal(self, value): try: assert int(value) == value @@ -110,21 +104,20 @@ class Num2Word_Base(object): words, num = self.clean(val) return self.title(out + words) - def float2tuple(self, value): pre = int(value) 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 + # 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)) return pre, post - def to_cardinal_float(self, value): try: float(value) == value @@ -146,11 +139,9 @@ class Num2Word_Base(object): return " ".join(out) - def merge(self, curr, next): raise NotImplementedError - def clean(self, val): out = val while len(val) != 1: @@ -172,7 +163,6 @@ class Num2Word_Base(object): val = out return out[0] - def title(self, value): if self.is_title: out = [] @@ -185,30 +175,24 @@ class Num2Word_Base(object): value = " ".join(out) return value - def verify_ordinal(self, value): if not value == int(value): raise TypeError(self.errmsg_floatord % value) if not abs(value) == value: raise TypeError(self.errmsg_negord % value) - def verify_num(self, value): return 1 - def set_wordnums(self): pass - def to_ordinal(self, value): return self.to_cardinal(value) - def to_ordinal_num(self, value): return value - # Trivial version def inflect(self, value, text): text = text.split("/") @@ -216,8 +200,7 @@ class Num2Word_Base(object): return text[0] return "".join(text) - - #//CHECK: generalise? Any others like pounds/shillings/pence? + # //CHECK: generalise? Any others like pounds/shillings/pence? def to_splitnum(self, val, hightxt="", lowtxt="", jointxt="", divisor=100, longval=True, cents=True): out = [] @@ -252,23 +235,18 @@ class Num2Word_Base(object): return " ".join(out) - def to_year(self, value, **kwargs): return self.to_cardinal(value) - def to_currency(self, value, **kwargs): return self.to_cardinal(value) - def base_setup(self): pass - def setup(self): pass - def test(self, value): try: _card = self.to_cardinal(value) @@ -285,5 +263,5 @@ class Num2Word_Base(object): except: _ordnum = "invalid" - print ("For %s, card is %s;\n\tord is %s; and\n\tordnum is %s." % - (value, _card, _ord, _ordnum)) + print("For %s, card is %s;\n\tord is %s; and\n\tordnum is %s." + % (value, _card, _ord, _ordnum)) diff --git a/num2words/compat.py b/num2words/compat.py index 7395f4c..26ea36c 100644 --- a/num2words/compat.py +++ b/num2words/compat.py @@ -18,9 +18,9 @@ import sys PY3 = sys.version_info[0] == 3 + def to_s(val): if PY3: return str(val) else: return unicode(val) - diff --git a/num2words/lang_AR.py b/num2words/lang_AR.py index a43c968..2ff58b4 100644 --- a/num2words/lang_AR.py +++ b/num2words/lang_AR.py @@ -18,6 +18,7 @@ from __future__ import division, unicode_literals, print_function from . import lang_EU + class Num2Word_AR(lang_EU.Num2Word_EU): def set_high_numwords(self, high): max = 3 + 3*len(high) @@ -30,7 +31,7 @@ class Num2Word_AR(lang_EU.Num2Word_EU): self.errmsg_nornum = "Only numbers may be converted to words." self.exclude_title = ["و", "فاصلة", "سالب"] - self.mid_numwords = [(1000000, "مليون"),(1000, "ألف"), (100, "مئة"), + self.mid_numwords = [(1000000, "مليون"), (1000, "ألف"), (100, "مئة"), (90, "تسعين"), (80, "ثمانين"), (70, "سبعين"), (60, "ستين"), (50, "خمسين"), (40, "أربعين"), (30, "ثلاثين")] @@ -39,15 +40,14 @@ class Num2Word_AR(lang_EU.Num2Word_EU): "اثناعشر", "أحد عشر", "عشرة", "تسعة", "ثمانية", "سبعة", "ستة", "خمسة", "أربعة", "ثلاثة", "اثنين", "واحد", "صفر"] - self.ords = { "واحد" : "أول", - "اثنين" : "ثاني", - "ثلاثة" : "ثالث", + self.ords = {"واحد": "أول", + "اثنين": "ثاني", + "ثلاثة": "ثالث", "أربعة": "رابع", - "خمسة" : "خامس", - "ثمانية" : "ثامن", - "تسعة" : "تاسع", - "اثناعشر" : "ثاني عشر" } - + "خمسة": "خامس", + "ثمانية": "ثامن", + "تسعة": "تاسع", + "اثناعشر": "ثاني عشر" } def merge(self, lpair, rpair): ltext, lnum = lpair @@ -68,7 +68,6 @@ class Num2Word_AR(lang_EU.Num2Word_EU): return ("%s %s"%(ltext, rtext), lnum * rnum) return ("%s، %s"%(ltext, rtext), lnum + rnum) - def to_ordinal(self, value): self.verify_ordinal(value) outwords = self.to_cardinal(value).split(" ") @@ -82,12 +81,10 @@ class Num2Word_AR(lang_EU.Num2Word_EU): outwords[-1] = "،".join(lastwords) return " ".join(outwords) - def to_ordinal_num(self, value): self.verify_ordinal(value) return "%s%s"%(value, self.to_ordinal(value)[-2:]) - def to_year(self, val, longval=True): if not (val//100)%10: return self.to_cardinal(val) @@ -105,6 +102,7 @@ to_ord = n2w.to_ordinal to_ordnum = n2w.to_ordinal_num to_year = n2w.to_year + def main(): for val in [ 1, 11, 12, 21, 31, 33, 71, 80, 81, 91, 99, 100, 101, 102, 155, 180, 300, 308, 832, 1000, 1001, 1061, 1100, 1500, 1701, 3000, @@ -112,7 +110,7 @@ def main(): -21212121211221211111, -2.121212, -1.0000100]: n2w.test(val) n2w.test(1325325436067876801768700107601001012212132143210473207540327057320957032975032975093275093275093270957329057320975093272950730) - for val in [1,120,1000,1120,1800, 1976,2000,2010,2099,2171]: + 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)) diff --git a/num2words/lang_DE.py b/num2words/lang_DE.py index 1c2fa0f..c239a48 100644 --- a/num2words/lang_DE.py +++ b/num2words/lang_DE.py @@ -18,6 +18,7 @@ from __future__ import unicode_literals, print_function from .lang_EU import Num2Word_EU + class Num2Word_DE(Num2Word_EU): def set_high_numwords(self, high): max = 3 + 6*len(high) diff --git a/num2words/lang_DK.py b/num2words/lang_DK.py index 64cf793..897a12c 100644 --- a/num2words/lang_DK.py +++ b/num2words/lang_DK.py @@ -17,6 +17,7 @@ from __future__ import division, unicode_literals, print_function from num2words import lang_EU + class Num2Word_DK(lang_EU.Num2Word_EU): def set_high_numwords(self, high): max = 3 + 6*len(high) @@ -138,6 +139,7 @@ to_ord = n2w.to_ordinal to_ordnum = n2w.to_ordinal_num to_year = n2w.to_year + def main(): for val in [ 1, 11, 12, 21, 31, 33, 71, 80, 81, 91, 99, 100, 101, 102, 155, 180, 300, 308, 832, 1000, 1001, 1061, 1100, 1500, 1701, 3000, diff --git a/num2words/lang_EN.py b/num2words/lang_EN.py index 656abfc..8b355d1 100644 --- a/num2words/lang_EN.py +++ b/num2words/lang_EN.py @@ -17,6 +17,7 @@ from __future__ import division, unicode_literals, print_function from . import lang_EU + class Num2Word_EN(lang_EU.Num2Word_EU): def set_high_numwords(self, high): max = 3 + 3*len(high) @@ -99,6 +100,7 @@ to_ord = n2w.to_ordinal to_ordnum = n2w.to_ordinal_num to_year = n2w.to_year + def main(): for val in [ 1, 11, 12, 21, 31, 33, 71, 80, 81, 91, 99, 100, 101, 102, 155, 180, 300, 308, 832, 1000, 1001, 1061, 1100, 1500, 1701, 3000, diff --git a/num2words/lang_HE.py b/num2words/lang_HE.py index 15e67c8..6cd1b64 100644 --- a/num2words/lang_HE.py +++ b/num2words/lang_HE.py @@ -95,7 +95,7 @@ def pluralize(n, forms): def int2word(n): - if n > 9999: #doesn't yet work for numbers this big + if n > 9999: # doesn't yet work for numbers this big raise NotImplementedError() if n == 0: diff --git a/num2words/lang_NL.py b/num2words/lang_NL.py index fcac497..84cdca0 100644 --- a/num2words/lang_NL.py +++ b/num2words/lang_NL.py @@ -18,6 +18,7 @@ from __future__ import unicode_literals, print_function from .lang_EU import Num2Word_EU + class Num2Word_NL(Num2Word_EU): def set_high_numwords(self, high): max = 3 + 6*len(high) @@ -92,10 +93,10 @@ class Num2Word_NL(Num2Word_EU): ntext = "een" if ntext.endswith("e"): - ntext += "ën"#"n" + ntext += "ën" # "n" else: ntext += "en" - ntext, ctext = ctext, ntext #+ "en" + ntext, ctext = ctext, ntext # + "en" elif cnum >= 10**6: ctext += " " val = cnum + nnum diff --git a/num2words/lang_PL.py b/num2words/lang_PL.py index c232f8a..97bfdb7 100644 --- a/num2words/lang_PL.py +++ b/num2words/lang_PL.py @@ -163,7 +163,7 @@ THOUSANDS = { 6: (u'trylion', u'tryliony', u'trylionów'), # 10^18 7: (u'tryliard', u'tryliardy', u'tryliardów'), # 10^21 8: (u'kwadrylion', u'kwadryliony', u'kwadrylionów'), # 10^24 - 9: (u'kwaryliard', u'kwadryliardy', u'kwadryliardów'), #10^27 + 9: (u'kwaryliard', u'kwadryliardy', u'kwadryliardów'), # 10^27 10: (u'kwintylion', u'kwintyliony', u'kwintylionów'), # 10^30 } diff --git a/num2words/lang_RU.py b/num2words/lang_RU.py index baa40cd..a132a39 100644 --- a/num2words/lang_RU.py +++ b/num2words/lang_RU.py @@ -174,7 +174,7 @@ THOUSANDS = { 6: (u'квинтиллион', u'квинтиллиона', u'квинтиллионов'), # 10^18 7: (u'секстиллион', u'секстиллиона', u'секстиллионов'), # 10^21 8: (u'септиллион', u'септиллиона', u'септиллионов'), # 10^24 - 9: (u'октиллион', u'октиллиона', u'октиллионов'), #10^27 + 9: (u'октиллион', u'октиллиона', u'октиллионов'), # 10^27 10: (u'нониллион', u'нониллиона', u'нониллионов'), # 10^30 } diff --git a/num2words/lang_UK.py b/num2words/lang_UK.py index 84fb067..656fb18 100644 --- a/num2words/lang_UK.py +++ b/num2words/lang_UK.py @@ -174,7 +174,7 @@ THOUSANDS = { 6: (u'квiнтильйон', u'квiнтильйони', u'квiнтильйонiв'), # 10^18 7: (u'секстильйон', u'секстильйони', u'секстильйонiв'), # 10^21 8: (u'септильйон', u'септильйони', u'септильйонiв'), # 10^24 - 9: (u'октильйон', u'октильйони', u'октильйонiв'), #10^27 + 9: (u'октильйон', u'октильйони', u'октильйонiв'), # 10^27 10: (u'нонiльйон', u'нонiльйони', u'нонiльйонiв'), # 10^30 } @@ -205,7 +205,8 @@ def get_digits(n): def pluralize(n, forms): - #form = 0 if n==1 else 1 if (n % 10 > 1 and n % 10 < 5 and (n % 100 < 10 or n % 100 > 20)) else 2 + # form = 0 if n==1 else 1 if (n % 10 > 1 and n % 10 < 5 and (n % 100 < 10 or + # n % 100 > 20)) else 2 if (n % 100 < 10 or n % 100 > 20): if n % 10 == 1: form = 0 @@ -241,7 +242,7 @@ def int2word(n, feminine=True): if n2 == 1: words.append(TENS[n1][0]) - #elif n1 > 0 and not (i > 0 and x == 1): + # elif n1 > 0 and not (i > 0 and x == 1): elif n1 > 0: ones = ONES_FEMININE if i == 1 or feminine and i == 0 else ONES words.append(ones[n1][0])