1. Gustavo Picon
  2. numconv

Commits

Gustavo Picon  committed bbef845

1.2.0 release, small optimization to int2str and updated exception formatting

  • Participants
  • Parent commits ce63456
  • Branches default
  • Tags 1.2

Comments (0)

Files changed (4)

File docs/conf.py

View file
 
 # General substitutions.
 project = 'numconv'
-copyright = '2008, Gustavo Picon'
+copyright = '2008-2009, Gustavo Picon'
 
 # The default replacements for |version| and |release|, also used in various
 # other places throughout the built documents.
 #
 # The short X.Y version.
-version = '1.1'
+version = '1.2'
 # The full version, including alpha/beta/rc tags.
-release = '1.1'
+release = '1.2.0'
 
 # There are two options for replacing |today|: either, you set today to some
 # non-false value, then it is used:

File numconv.py

View file
            strings.
 :copyright: 2008 by Gustavo Picon
 :license: Apache License 2.0
-:version: 1.1-svn
+:version: 1.2.0
 :url: http://code.google.com/p/numconv/
 :documentation:
    `numconv-docs
    <http://code.google.com/p/numconv/source/browse/trunk/tests.py>`_
 
 
-:mod:`numconv` converts a string into a number and a number into a string using
-default or user supplied encoding alphabets.
+:mod:`numconv` converts a string into a number and a number into a string
+using default or user supplied encoding alphabets.
 
 constants
 ~~~~~~~~~
 .. data:: BASE85
 
    Alphabet defined in section 4 of :rfc:`1924`. Supposed to be a joke (it is
-   an April's fools RFC after all), but is quite useful because can be used as
-   a base for the most common numeric conversions.
+   an April's fools RFC after all), but is quite useful because it can be used
+   as a base for the most common numeric conversions.
 
 .. data:: BASE16
           BASE32
 
 """
 
-VERSION = (1, 1, 'svn')
+
+__version__ = '1.2.0'
 
 # from april fool's rfc 1924
 BASE85 = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' \
     :raise ValueError: when *alphabet* has duplicated characters
 
     **Examples** (taken from :file:`tests.py`):
-       
+
        3735928559 to hexadecimal::
 
            >> numconv.int2str(3735928559, 16)
         # just to validate the alphabet
         getcmap(alphabet)
     if int(num) != num:
-        raise TypeError, 'number must be an integer'
+        raise TypeError('number must be an integer')
     if num < 0:
-        raise ValueError, 'number must be positive'
+        raise ValueError('number must be positive')
     if int(radix) != radix:
-        raise TypeError, 'radix must be an integer'
+        raise TypeError('radix must be an integer')
     if not 2 <= radix <= len(alphabet):
-        raise ValueError, 'radix must be >= 2 and <= %d' % (len(alphabet),)
+        raise ValueError('radix must be >= 2 and <= %d' % (len(alphabet), ))
+    if radix in (8, 10, 16) and \
+            alphabet[:radix].lower() == BASE85[:radix].lower():
+        return ({8: '%o', 10: '%d', 16: '%x'}[radix] % num).upper()
     ret = ''
     while True:
         ret = alphabet[num % radix] + ret
     :raise ValueError: when *alphabet* has duplicated characters
 
     **Examples** (taken from :file:`tests.py`):
-       
+
        Hexadecimal 'DEADBEEF' to integer::
 
           >> numconv.str2int('DEADBEEF', 16)
     if alphabet not in CMAPS:
         getcmap(alphabet)
     if int(radix) != radix:
-        raise TypeError, 'radix must be an integer'
+        raise TypeError('radix must be an integer')
     if not 2 <= radix <= len(alphabet):
-        raise ValueError, 'radix must be >= 2 and <= %d' % (len(alphabet),)
+        raise ValueError('radix must be >= 2 and <= %d' % (len(alphabet), ))
     if radix <= 36 and alphabet[:radix].lower() == BASE85[:radix].lower():
         return int(num, radix)
     ret = 0
     lalphabet = alphabet[:radix]
     for char in num:
         if char not in lalphabet:
-            raise ValueError, "invalid literal for radix2int() " \
-                "with radix %d: '%s'" % (radix, num)
+            raise ValueError("invalid literal for radix2int() with radix "
+                             "%d: '%s'" % (radix, num))
         ret = ret * radix + lmap[char]
     return ret
 
     """Builds an internal alphabet lookup table, to be stored in CMAPS"""
     ret = dict(zip(alphabet, range(len(alphabet))))
     if len(ret) != len(alphabet):
-        raise ValueError, "duplicate characters found in '%s'" % (alphabet,)
+        raise ValueError("duplicate characters found in '%s'" % (alphabet, ))
     CMAPS[alphabet] = ret
     return ret
-

File setup.py

View file
 #!/usr/bin/env python
-# -*- coding: utf-8 -*-
 
-
-import os
 from distutils.core import setup
 
-numconv = __import__('numconv')
-version_tuple = numconv.VERSION
-version = "%d.%d" % version_tuple[:2]
-if version_tuple[2] is not None:
-    svnf = os.path.join(os.path.dirname(os.path.abspath(numconv.__file__)),
-                        '.svn/entries')
-    gotdir = False
-    version = '%s_%s' % (version, str(version_tuple[2]))
-    if os.path.isfile(svnf):
-        for ln in open(svnf):
-            ln = ln.rstrip()
-            if gotdir:
-                version = '%s_rev%s' % (version, ln.replace(' ', ''))
-                break
-            if ln == 'dir':
-                gotdir = True
+version = '1.2.0'
+
+classifiers = [
+    "Development Status :: 5 - Production/Stable",
+    "Intended Audience :: Developers",
+    "License :: OSI Approved :: Apache Software License",
+    "Programming Language :: Python",
+    "Operating System :: OS Independent",
+    "Topic :: Software Development :: Libraries",
+]
 
 setup(
-    name = 'numconv',
-    version = version,
-    url = 'http://code.google.com/p/numconv/',
-    author = 'Gustavo Picon',
-    author_email = 'gpicon@gmail.com',
-    license = 'Apache License 2.0',
-    py_modules = ['numconv'],
-    description = 'Python library to convert strings to numbers '
-                  'and numbers to strings.',
+    name='numconv',
+    version=version,
+    url='http://code.google.com/p/numconv/',
+    author='Gustavo Picon',
+    author_email='tabo@gpicon.org',
+    license='Apache License 2.0',
+    py_modules=['numconv'],
+    description='Python library to convert strings to numbers '
+                'and numbers to strings.',
+    classifiers=classifiers,
+    #long_description=__doc__,
 )
-

File tests.py

View file
         self.assertRaises(ValueError, numconv.str2int, 'abcd', -10)
 
 
-class BaseconvGetcmap(unittest.TestCase):
+class BaseconvCmap(unittest.TestCase):
     """tests for getcmap()"""
 
+    def setUp(self):
+        # clear the cmaps cache before every test
+        numconv.CMAPS = {}
+
     def test_getcmap(self):
         """testing getcmap: expected values"""
         self.assertEqual(numconv.getcmap('0123456789'),
         """testing getcmap: error on alphabet with duplicate chars"""
         self.assertRaises(ValueError, numconv.getcmap, 'abcdaf')
 
+    def test_cmap_int2str(self):
+        """tests that a call to int2str loads the cmap correctly"""
+        numconv.int2str(10, 4, 'abcd')
+        self.assertEqual(numconv.CMAPS['abcd'],
+            {'a': 0, 'b': 1, 'c': 2, 'd': 3})
+
+    def test_cmap_str2int(self):
+        """tests that a call to str2int loads the cmap correctly"""
+        numconv.str2int('aaaa', 4, 'abcd')
+        self.assertEqual(numconv.CMAPS['abcd'],
+            {'a': 0, 'b': 1, 'c': 2, 'd': 3})
+
 
 class BaseconvSanity(unittest.TestCase):
     """sanity checks"""