1. Dinu Gherman
  2. eonums

Commits

Dinu Gherman  committed 6efad0f

Made same code run on Python 2 and Python 3, increased version to 0.9.1.

  • Participants
  • Parent commits 7ef7fdf
  • Branches default
  • Tags 0.9.2

Comments (0)

Files changed (6)

File README.html

View file
  • Ignore whitespace
 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
-<meta name="generator" content="Docutils 0.8: http://docutils.sourceforge.net/" />
+<meta name="generator" content="Docutils 0.9.1: http://docutils.sourceforge.net/" />
 <title>Eonums</title>
 <meta name="author" content="Dinu Gherman &lt;gherman&#64;darwin.in-berlin.de&gt;" />
-<meta name="date" content="2008-09-15" />
+<meta name="date" content="2012-06-20" />
 <meta name="copyright" content="GNU Public Licence v3 (GPLv3)" />
 <style type="text/css">
 
 /*
 :Author: David Goodger (goodger@python.org)
-:Id: $Id: html4css1.css 7056 2011-06-17 10:50:48Z milde $
+:Id: $Id: html4css1.css 7434 2012-05-11 21:06:27Z milde $
 :Copyright: This stylesheet has been placed in the public domain.
 
 Default cascading style sheet for the HTML output of Docutils.
   margin-top: 0 ;
   font: inherit }
 
-pre.literal-block, pre.doctest-block, pre.math {
+pre.literal-block, pre.doctest-block, pre.math, pre.code {
   margin-left: 2em ;
   margin-right: 2em }
 
+pre.code .ln { /* line numbers */
+  color: grey;
+}
+
+.code {
+  background-color: #eeeeee
+}
+
 span.classifier {
   font-family: sans-serif ;
   font-style: oblique }
 <tr class="field"><th class="docinfo-name">Homepage:</th><td class="field-body"><a class="reference external" href="http://www.dinu-gherman.net/">http://www.dinu-gherman.net/</a></td>
 </tr>
 <tr><th class="docinfo-name">Version:</th>
-<td>Version 0.9.0</td></tr>
+<td>Version 0.9.1</td></tr>
 <tr><th class="docinfo-name">Date:</th>
-<td>2008-09-15</td></tr>
+<td>2012-06-20</td></tr>
 <tr><th class="docinfo-name">Copyright:</th>
 <td>GNU Public Licence v3 (GPLv3)</td></tr>
 </tbody>
 module is 10**66 - 1. (This module makes no attempt to extend the
 Esperanto naming rules by introducing names like &quot;undekiliono&quot;,
 &quot;undekiliardo&quot;, &quot;dudekiliono&quot; etc.)</p>
-<p>This module can be fully translated automatically to Python 3.0
-using its migration tool named <tt class="docutils literal">2to3</tt>.</p>
+<p>Version 0.9.1 now works on Python 2 and Python 3.</p>
 </div>
 <div class="section" id="features">
 <h1>Features</h1>
 <li>handle integers from 0 to 10**66 - 1</li>
 <li>provide conversion functions and command-line scripts</li>
 <li>provide a Unittest test suite</li>
-<li>can be automatically migrated to Python 3.0 using <tt class="docutils literal">2to3</tt></li>
+<li>runs the same code under Python 2 and Python 3</li>
 </ul>
 </div>
 <div class="section" id="examples">
 <h1>Examples</h1>
 <p>You can use <cite>eonums</cite> as a Python module e.g. like in the following
-interactive Python session:</p>
+interactive sessions:</p>
+<p>Python 2:</p>
 <pre class="literal-block">
 &gt;&gt;&gt; from eonums import int2eo, eo2int, validate_eo
 &gt;&gt;&gt;
 &gt;&gt;&gt; validate_eo(u&quot;dudek cent tri&quot;)
 False
 </pre>
+<p>Python 3:</p>
+<pre class="literal-block">
+&gt;&gt;&gt; from eonums import int2eo, eo2int, validate_eo
+&gt;&gt;&gt;
+&gt;&gt;&gt; int2eo(22334455)
+'dudek du milionoj tricent tridek kvar mil kvarcent kvindek kvin'
+&gt;&gt;&gt;
+&gt;&gt;&gt; eo2int(&quot;cent dudek tri&quot;)
+123
+&gt;&gt;&gt; validate_eo(&quot;dudek cent tri&quot;)
+False
+</pre>
 <p>In addition there are two (very simple) conversion scripts, <tt class="docutils literal">int2eo</tt>
 and <tt class="docutils literal">eo2int</tt>, which can be used from the system command-line like this:</p>
 <pre class="literal-block">
 123
 </pre>
 </div>
+<div class="section" id="history">
+<h1>History</h1>
+<ul class="simple">
+<li>2008-09-15 (0.9.0): First official realease.</li>
+<li>2012-06-20 (0.9.1): Adapted code to run on Python 2 and Python 3.</li>
+</ul>
+</div>
 <div class="section" id="installation">
 <h1>Installation</h1>
 <p>There are two ways to install <cite>eonums</cite>, depending on whether you have
 the <cite>easy_install</cite> command available on your system or not.</p>
-<div class="section" id="using-easy-install">
-<h2>1. Using <cite>easy_install</cite></h2>
+<div class="section" id="using-easy-install-or-pip">
+<h2>1. Using <cite>easy_install</cite> or <cite>pip</cite></h2>
 <p>With the <cite>easy_install</cite> command on your system and a working internet
 connection you can install <cite>eonums</cite> with only one command in a terminal:</p>
 <pre class="literal-block">
 install it before installing <cite>eonums</cite>, you might want to go to the
 <a class="reference external" href="http://peak.telecommunity.com/DevCenter/EasyInstall">Easy Install homepage</a>
 and follow the <a class="reference external" href="http://peak.telecommunity.com/DevCenter/EasyInstall#installing-easy-install">instructions there</a>.</p>
+<p>You can also install <cite>eonums</cite> using <cite>pip</cite> the usual way, if it is already
+installed:</p>
+<pre class="literal-block">
+$ pip install eonums
+</pre>
 </div>
 <div class="section" id="manual-installation">
 <h2>2. Manual installation</h2>
 the file <tt class="docutils literal"><span class="pre">eonums-0.9.0.tar.gz</span></tt> and decompressing it with the following
 command:</p>
 <pre class="literal-block">
-$ tar xfz eonums-0.9.0.tar.gz
+$ tar xfz eonums-0.9.1.tar.gz
 </pre>
 <p>Then change into the newly created directory <tt class="docutils literal">eonums</tt> and install
 <cite>eonums</cite> by running the following command:</p>

File README.txt

View file
  • Ignore whitespace
 
 :Author:  Dinu Gherman <gherman@darwin.in-berlin.de>
 :Homepage: http://www.dinu-gherman.net/
-:Version: Version 0.9.0
-:Date: 2008-09-15
+:Version: Version 0.9.1
+:Date: 2012-06-20
 :Copyright: GNU Public Licence v3 (GPLv3)
 
 
 Esperanto naming rules by introducing names like "undekiliono", 
 "undekiliardo", "dudekiliono" etc.)
 
-This module can be fully translated automatically to Python 3.0 
-using its migration tool named ``2to3``. 
+Version 0.9.1 now works on Python 2 and Python 3.
 
 
 Features
 - handle integers from 0 to 10**66 - 1
 - provide conversion functions and command-line scripts
 - provide a Unittest test suite
-- can be automatically migrated to Python 3.0 using ``2to3``
+- runs the same code under Python 2 and Python 3
 
 
 Examples
 --------
 
 You can use `eonums` as a Python module e.g. like in the following
-interactive Python session::
+interactive sessions:
+
+Python 2::
 
     >>> from eonums import int2eo, eo2int, validate_eo
     >>>
     >>> validate_eo(u"dudek cent tri")
     False
 
+
+Python 3::
+
+    >>> from eonums import int2eo, eo2int, validate_eo
+    >>>
+    >>> int2eo(22334455)
+    'dudek du milionoj tricent tridek kvar mil kvarcent kvindek kvin'
+    >>>
+    >>> eo2int("cent dudek tri")
+    123
+    >>> validate_eo("dudek cent tri")
+    False
+
 In addition there are two (very simple) conversion scripts, ``int2eo`` 
 and ``eo2int``, which can be used from the system command-line like this::
 
     123
 
 
+History
+-------
+
+- 2008-09-15 (0.9.0): First official realease.
+- 2012-06-20 (0.9.1): Adapted code to run on Python 2 and Python 3.
+
+
 Installation
 ------------
 
 There are two ways to install `eonums`, depending on whether you have
 the `easy_install` command available on your system or not.
 
-1. Using `easy_install`
-++++++++++++++++++++++++
+1. Using `easy_install` or `pip`
+++++++++++++++++++++++++++++++++++
 
 With the `easy_install` command on your system and a working internet 
 connection you can install `eonums` with only one command in a terminal::
 `Easy Install homepage <http://peak.telecommunity.com/DevCenter/EasyInstall>`_ 
 and follow the `instructions there <http://peak.telecommunity.com/DevCenter/EasyInstall#installing-easy-install>`_.
 
+You can also install `eonums` using `pip` the usual way, if it is already 
+installed::
+
+  $ pip install eonums
+
+
 2. Manual installation
 +++++++++++++++++++++++
 
 the file ``eonums-0.9.0.tar.gz`` and decompressing it with the following 
 command::
 
-  $ tar xfz eonums-0.9.0.tar.gz
+  $ tar xfz eonums-0.9.1.tar.gz
 
 Then change into the newly created directory ``eonums`` and install 
 `eonums` by running the following command::

File eo2int

View file
  • Ignore whitespace
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
 
-"Command-line tool for converting Esperanto strings to integers."
+"Command-line tool for converting Esperanto strings to integers >= 0."
+
 
 import sys
 
-
 from eonums import eo2int
 
 
+PY_VERSION_MAJOR = sys.version_info[0]
+
+
 try:
-    arg = sys.argv[1].decode("UTF-8")
+    arg = sys.argv[1]
 except IndexError:
-    print """Usage: eo2int "STRING"
+    print("""Usage: eo2int "STRING"
+
 Example: $ eo2int "cent dudek tri"
-123"""
+123""")
     sys.exit()
 
+
 try:
     num = eo2int(arg)
-    print num
-except ValueError, details:
-    print "ValueError:", details
+    print(num)
+except ValueError as details:
+    if PY_VERSION_MAJOR >= 3:
+        print("ValueError:", details)
+    else:
+        print("ValueError: %s" % details)

File eonums.py

View file
  • Ignore whitespace
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
 
-"""A module for translating between integer numbers and Esperanto strings.
+"""A module for converting integer numbers to/from Esperanto strings.
 
-Examples:
+Examples (Python 2):
 
     >>> from eonums import int2eo, eo2int, validate_eo
     >>>
     123
     >>> validate_eo(u"dudek cent tri")
     False
+
+Examples (Python 3):
+
+    >>> from eonums import int2eo, eo2int, validate_eo
+    >>>
+    >>> int2eo(22334455)
+    'dudek du milionoj tricent tridek kvar mil kvarcent kvindek kvin'
+    >>>
+    >>> eo2int("cent dudek tri")
+    123
+    >>> validate_eo("dudek cent tri")
+    False
 """
 
-__version__ = "0.9.0"
+__version__ = "0.9.1"
 __license__ = "GNU General Public Licence v3 (GPLv3)"
 __author__ = "Dinu Gherman"
-__date__ = "2008-09-15"
+__date__ = "2012-06-20"
 
 
 import re
 # constants
 
 # single digits
-DIGITS = u"nul unu du tri kvar kvin ses sep ok naŭ".split()
+DIGITS = "nul unu du tri kvar kvin ses sep ok naŭ".split()
 
 # powers of ten
-DIGITS10 = DIGITS + [u"dek"]
-POW10_SMALL = u"unu dek cent".split()
-POW10_BIG = {3: u"mil", 6: u"miliono", 9: u"miliardo"}
+DIGITS10 = DIGITS + ["dek"]
+POW10_SMALL = "unu dek cent".split()
+POW10_BIG = {3: "mil", 6: "miliono", 9: "miliardo"}
 for i in range(12, 63, 6):
-    POW10_BIG[i] = DIGITS10[i // 6] + u"iliono"
-    POW10_BIG[i + 3] = DIGITS10[i // 6] + u"iliardo"
-POW10_BIG_INV = dict([(v, k) for (k, v) in POW10_BIG.items()])
+    POW10_BIG[i] = DIGITS10[i // 6] + "iliono"
+    POW10_BIG[i + 3] = DIGITS10[i // 6] + "iliardo"
+POW10_BIG_INV = dict([(v, k) for (k, v) in list(POW10_BIG.items())])
 del DIGITS10
 
 
     bpows2.sort(reverse=True)
     if bpows != bpows2:
         if verbose:
-            print "failing", bpows, bpows2
+            print("failing", bpows, bpows2)
         return False
 
     # check if all occurences of these "big" powers appear
     counts = [bpows.count(p) for p in bpows]
     if counts != [1] * len(bpows):
         if verbose:
-            print "failing", counts, [1] * len(bpows)
+            print("failing", counts, [1] * len(bpows))
         return False
 
     # check if all "small" powers of ten, i.e. 10**k (k=0,1,2),
     hundreds = re.split("\w+lionoj?|\w+iliardoj?|mil", numString)
     hundreds = [h.strip() for h in hundreds if h.strip()]
     if verbose:
-        print "hundreds", hundreds
+        print("hundreds", hundreds)
     dic = {"digs": "|".join(DIGITS)}
     for h in hundreds:
         if verbose:
-            print "testing", repr(h)
+            print("testing", repr(h))
         nc = numComponents = len(h.split())
         if nc == 1:
             pat = "^(%(digs)s)?cent$|^(%(digs)s)?dek$|^(%(digs)s)$" % dic
             if not re.match(pat, h):
                 if verbose:
-                    print "failing (1)", h
+                    print("failing (1)", h)
                 return False
         elif nc == 2:
             pat1 = "^(%(digs)s)?cent +(%(digs)s)?dek$" % dic
             pat3 = "^(%(digs)s)?dek +(%(digs)s)$" % dic
             if not re.match("|".join([pat1, pat2, pat3]), h):
                 if verbose:
-                    print "failing (2)", h
+                    print("failing (2)", h)
                 return False
         elif nc == 3:
             pat = "^(%(digs)s)?cent +(%(digs)s)?dek +(%(digs)s)$" % dic
             if not re.match(pat, h):
                 if verbose:
-                    print "failing (3)", h
+                    print("failing (3)", h)
                 return False
         elif nc == 0 or nc > 3:
             if verbose:
-                print "failing (0)", h
+                print("failing (0)", h)
             return False
 
     return True
         raise ValueError(msg)
 
     if verbose:
-        print "Input:", num
+        print("Input:", num)
 
     if num < 10:
         return DIGITS[num]
         nextMax3Digits = ndigits[i:i + 3]
         l = len(nextMax3Digits)
         if verbose:
-            print "next3", nextMax3Digits, [1, 0, 0][:l]
+            print("next3", nextMax3Digits, [1, 0, 0][:l])
         j = i % 3
         eodig = DIGITS[n]
         if n == 1:
         res = res.strip()
 
         if verbose:
-            print "i:%d, j:%d, n:%d|%s, res:%s" % (i, j, n, eodig, repr(res))
+            print("i:%d, j:%d, n:%d|%s, res:%s" % (i, j, n, eodig, repr(res)))
 
     if verbose:
-        print "Output:", res
-        print
+        print("Output:", res)
+        print()
 
     # catch expressions which are not "well formed"
     if validate and validate_eo(res) == False:
-        raise ValueError(u"Invalid expression: '%s'" % res)
+        raise ValueError("Invalid expression: '%s'" % res)
 
     return res
 
     """
 
     if verbose:
-        print "Input:", numString
+        print("Input:", numString)
 
     # catch expressions which are not "well formed"
     if validate and validate_eo(numString) == False:
-        raise ValueError(u"Invalid expression: '%s'" % numString)
+        raise ValueError("Invalid expression: '%s'" % numString)
 
     # if we have a single digit number like u"kvin",
     # return its integer value immediately
     for s in strList:
         if verbose:
             format = "s: %s, val: %d, val999: %d, total: %d"
-            print format % (s, value, value999, total)
+            print(format % (s, value, value999, total))
         if s in DIGITS:
             value = DIGITS.index(s)
-        elif s == u"dek":
+        elif s == "dek":
             if value == 0:
                 value = 1
             value999 = value999 + value * 10
             value = 0
-        elif s == u"cent":
+        elif s == "cent":
             if value == 0:
                 value = 1
             value999 = value999 + value * 100
             value = 0
-        elif s == u"mil":
+        elif s == "mil":
             value999 = value999 + value
             value999 = max(1, value999)
             total = total + value999 * 1000
             total = total + value999 * (10 ** POW10_BIG_INV[s])
             value999 = 0
             value = 0
-        elif s.endswith(u"oj") and s[:-1] in POW10_BIG_INV:
+        elif s.endswith("oj") and s[:-1] in POW10_BIG_INV:
             value999 = value999 + value
             total = total + value999 * (10 ** POW10_BIG_INV[s[:-1]])
             value999 = 0
 
         if verbose:
             format = "s: %s, val: %d, val999: %d, total: %d"
-            print format % (s, value, value999, total)
-            print
+            print(format % (s, value, value999, total))
+            print()
 
     value999 = value999 + value
     total = total + value999
 
     if verbose:
         format = "s: %s, val: %d, val999: %d, total: %d"
-        print format % (s, value, value999, total)
+        print(format % (s, value, value999, total))
 
     return total

File int2eo

View file
  • Ignore whitespace
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
 
-"Command-line tool for converting integers to Esperanto strings."
+"Command-line tool for converting integers >= 0 to Esperanto strings."
+
 
 import sys
 
+from eonums import int2eo
 
-from eonums import int2eo
+
+PY_VERSION_MAJOR = sys.version_info[0]
 
 
 try:
     arg = sys.argv[1]
 except IndexError:
-    print """Usage: int2eo INTEGER
+    print("""Usage: int2eo INTEGER
+
 Example: $ int2eo 123
-cent dudek tri"""
+cent dudek tri""")
     sys.exit()
 
+
 try:
     num = int(arg)
-except ValueError, details:
-    print "ValueError:", details
+except ValueError as details:
+    if PY_VERSION_MAJOR >= 3:
+        print("ValueError:", details)
+    else:
+        print("ValueError: %s" % details)
     sys.exit()
 
+
 try:
     assert num >= 0
 except AssertionError:
-    print "AssertionError:", "Not a positive integer"
+    print("AssertionError: Not a positive integer")
     sys.exit()
 
 try:
     eo = int2eo(num)
-    print eo
-except ValueError, details:
-    print "ValueError:", details
+    print(eo)
+except ValueError as details:
+    if PY_VERSION_MAJOR >= 3:
+        print("ValueError:", details)
+    else:
+        print("ValueError: %s" % details)

File test_eonums.py

View file
  • Ignore whitespace
 """A module for testing functions in eonums.py.
 """
 
-__version__ = "0.9.0"
+__version__ = "0.9.1"
 __license__ = "GNU General Public Licence v3 (GPLv3)"
 __author__ = "Dinu Gherman"
-__date__ = "2008-09-15"
+__date__ = "20012-06-20"
 
 
 import unittest
 TEST_CASES = {}
 TEST_CASES.update({
     "digits":
-        zip(range(10), DIGITS)
+        list(zip(list(range(10)), DIGITS))
 })
 TEST_CASES.update({
     "doubledigits": [
-        (11, u"dek unu"),
-        (19, u"dek naŭ"),
-        (20, u"dudek"),
-        (22, u"dudek du"),
-        (88, u"okdek ok"),
+        (11, "dek unu"),
+        (19, "dek naŭ"),
+        (20, "dudek"),
+        (22, "dudek du"),
+        (88, "okdek ok"),
     ],
     "hundreds": [
-        (100, u"cent"),
-        (102, u"cent du"),
-        (120, u"cent dudek"),
-        (123, u"cent dudek tri"),
-        (888, u"okcent okdek ok"),
+        (100, "cent"),
+        (102, "cent du"),
+        (120, "cent dudek"),
+        (123, "cent dudek tri"),
+        (888, "okcent okdek ok"),
     ],
     "milions": [
-        (  1000000, u"unu miliono"),
-        (  1001000, u"unu miliono mil"),
-        (  2000000, u"du milionoj"),
-        ( 10000000, u"dek milionoj"),
-        ( 21000000, u"dudek unu milionoj"),
-        (100000000, u"cent milionoj"),
+        (  1000000, "unu miliono"),
+        (  1001000, "unu miliono mil"),
+        (  2000000, "du milionoj"),
+        ( 10000000, "dek milionoj"),
+        ( 21000000, "dudek unu milionoj"),
+        (100000000, "cent milionoj"),
     ],
     "big": [
-        (      10 ** 63, u"unu dekiliardo"),
-        (  2 * 10 ** 63, u"du dekiliardoj"),
-        ( 10 * 10 ** 63, u"dek dekiliardoj"),
-        (100 * 10 ** 63, u"cent dekiliardoj"),
+        (      10 ** 63, "unu dekiliardo"),
+        (  2 * 10 ** 63, "du dekiliardoj"),
+        ( 10 * 10 ** 63, "dek dekiliardoj"),
+        (100 * 10 ** 63, "cent dekiliardoj"),
     ],
     "other": [
-        (    1000, u"mil"),
-        (   10000, u"dek mil"),
-        (  100000, u"cent mil"),
-        (    1234, u"mil ducent tridek kvar"),
-        (   11234, u"dek unu mil ducent tridek kvar"),
-        (    2048, u"du mil kvardek ok"),
+        (    1000, "mil"),
+        (   10000, "dek mil"),
+        (  100000, "cent mil"),
+        (    1234, "mil ducent tridek kvar"),
+        (   11234, "dek unu mil ducent tridek kvar"),
+        (    2048, "du mil kvardek ok"),
         (22334455,
-             u"dudek du milionoj tricent tridek kvar mil kvarcent kvindek kvin"), 
+             "dudek du milionoj tricent tridek kvar mil kvarcent kvindek kvin"), 
     ],
 })
 
     def template_test_failing(self):
         "Test failing case."
 
-        i, exp = 10000000, u"dek milionoj"
+        i, exp = 10000000, "dek milionoj"
         res = int2eo(i, verbose=True)
         self.assertEqual(res, exp)
 
             try:
                 self.assertEqual(i, j)
             except AssertionError:
-                print "Error: %d -> '%s' -> %d" % (i, eo, j)
+                print("Error: %d -> '%s' -> %d" % (i, eo, j))
                 eo = int2eo(i, validate=False, verbose=False)
                 j = eo2int(eo, validate=True, verbose=False)
                 self.assertEqual(i, j)