Commits

Gora Khargosh committed c4043da

More clean up.

Signed-off-by: Gora Khargosh <gora.khargosh@gmail.com>

  • Participants
  • Parent commits f4d1ab2

Comments (0)

Files changed (5)

File mom/codec/_alt_base.py

 from array import array
 
 import re
-from struct import pack
 from mom._compat import ZERO_BYTE, UINT128_MAX
 from mom.builtins import is_bytes, b
 from mom.codec import uint_to_bytes
-from mom.codec.base58 import ASCII58_CHARSET, ASCII58_ORDS
-from mom.codec.base62 import ASCII62_CHARSET, ASCII62_ORDS
-from mom.codec.base85 import RFC1924_ORDS, POW_85, RFC1924_BYTES
+from mom.codec.base58 import ASCII58_BYTES, ASCII58_ORDS
+from mom.codec.base62 import ASCII62_BYTES, ASCII62_ORDS
+from mom.codec.base85 import RFC1924_ORDS, RFC1924_BYTES
 from mom.codec.integer import bytes_to_uint
 from mom.functional import leading
 
 
 
 def b58encode_naive(raw_bytes,
-                    _charset=ASCII58_CHARSET,
+                    base_bytes=ASCII58_BYTES,
                     _padding=True,
                     _zero_byte=ZERO_BYTE):
     """
 
     :param raw_bytes:
         Raw bytes to encode.
-    :param _charset:
-        (Internal) The character set to use. Defaults to ``ASCII58_CHARSET``
+    :param base_bytes:
+        The character set to use. Defaults to ``ASCII58_BYTES``
         that uses natural ASCII order.
     :param _padding:
         (Internal) ``True`` (default) to include prefixed zero-byte sequence
     number = bytes_to_uint(raw_bytes)
     encoded = b('')
     while number > 0:
-        encoded = _charset[number % 58] + encoded
+        encoded = base_bytes[number % 58] + encoded
         number //= 58
         # The following makes more divmod calls but is 2x faster.
 #        number, remainder = divmod(number, 58)
 #        encoded = _charset[remainder] + encoded
     if _padding:
         zero_leading = leading(lambda w: w == _zero_byte[0], raw_bytes)
-        encoded = (_charset[0] * zero_leading) + encoded
+        encoded = (base_bytes[0] * zero_leading) + encoded
     return encoded
 
 
 def b62encode_naive(raw_bytes,
-                    _charset=ASCII62_CHARSET,
+                    base_bytes=ASCII62_BYTES,
                     _padding=True,
                     _zero_byte=ZERO_BYTE):
     """
 
     :param raw_bytes:
         Raw bytes to encode.
-    :param _charset:
-        (Internal) The character set to use. Defaults to ``ASCII62_CHARSET``
+    :param base_bytes:
+        The character set to use. Defaults to ``ASCII62_CHARSET``
         that uses natural ASCII order.
     :param _padding:
         (Internal) ``True`` (default) to include prefixed zero-byte sequence
     number = bytes_to_uint(raw_bytes)
     encoded = b('')
     while number > 0:
-        encoded = _charset[number % 62] + encoded
+        encoded = base_bytes[number % 62] + encoded
         number //= 62
         # The following makes more divmod calls but is 2x faster.
 #        number, remainder = divmod(number, 62)
 #        encoded = _charset[remainder] + encoded
     if _padding:
         zero_leading = leading(lambda w: w == _zero_byte[0], raw_bytes)
-        encoded = (_charset[0] * zero_leading) + encoded
+        encoded = (base_bytes[0] * zero_leading) + encoded
     return encoded
 
 
 def b62decode_naive(encoded,
-                    _charset=ASCII62_CHARSET,
+                    _charset=ASCII62_BYTES,
                     _lookup=ASCII62_ORDS):
     """
     Base-62 decodes a sequence of bytes into raw bytes. Whitespace is ignored.
 
 
 def b58decode_naive(encoded,
-                    _charset=ASCII58_CHARSET,
+                    _charset=ASCII58_BYTES,
                     _lookup=ASCII58_ORDS):
     """
     Simple implementation for benchmarking.
     :param encoded:
         Base-58 encoded bytes.
     :param _charset:
-        (Internal) The character set to use. Defaults to ``ASCII58_CHARSET``
+        (Internal) The character set to use. Defaults to ``ASCII58_BYTES``
         that uses natural ASCII order.
     :param _lookup:
         (Internal) Ordinal-to-character lookup table for the specified

File mom/codec/base58.py

 
 from __future__ import absolute_import, division
 
+from collections import deque
 from mom._compat import have_python3, ZERO_BYTE, range
 from mom.builtins import byte, is_bytes, b, bytes_leading
 from mom.codec._base import base_to_uint, uint_to_base256
 
 
 # Follows ASCII order.
-ASCII58_CHARSET = ("123456789"
+ASCII58_BYTES = ("123456789"
                   "ABCDEFGHJKLMNPQRSTUVWXYZ"
                   "abcdefghijkmnopqrstuvwxyz").encode("ascii")
 # Therefore, b'1' represents b'\0'.
-ASCII58_ORDS = dict((x, i) for i, x in enumerate(ASCII58_CHARSET))
+ASCII58_ORDS = dict((x, i) for i, x in enumerate(ASCII58_BYTES))
 
 
 # Really, I don't understand why people use the non-ASCII order,
 # is what you will need:
 #
 # Does not follow ASCII order.
-ALT58_CHARSET = ("123456789"
+ALT58_BYTES = ("123456789"
                  "abcdefghijkmnopqrstuvwxyz"
                  "ABCDEFGHJKLMNPQRSTUVWXYZ").encode("ascii")
 # Therefore, b'1' represents b'\0'.
-ALT58_ORDS = dict((x, i) for i, x in enumerate(ALT58_CHARSET))
+ALT58_ORDS = dict((x, i) for i, x in enumerate(ALT58_BYTES))
 
 if have_python3:
-    ASCII58_CHARSET = tuple(byte(x) for x in ASCII58_CHARSET)
-    ALT58_CHARSET = tuple(byte(x) for x in ALT58_CHARSET)
+    ASCII58_BYTES = tuple(byte(x) for x in ASCII58_BYTES)
+    ALT58_BYTES = tuple(byte(x) for x in ALT58_BYTES)
 
 # If you're going to make people type stuff longer than this length
 # I don't know what to tell you. Beyond this length powers
 
 
 def b58encode(raw_bytes,
-              _charset=ASCII58_CHARSET, _padding=True, _zero_byte=ZERO_BYTE):
+              base_bytes=ASCII58_BYTES, _padding=True):
     """
     Base58 encodes a sequence of raw bytes. Zero-byte sequences are
     preserved by default.
 
     :param raw_bytes:
         Raw bytes to encode.
-    :param _charset:
-        (Internal) The character set to use. Defaults to ``ASCII58_CHARSET``
+    :param base_bytes:
+        The character set to use. Defaults to ``ASCII58_BYTES``
         that uses natural ASCII order.
     :param _padding:
         (Internal) ``True`` (default) to include prefixed zero-byte sequence
     encoded = b('')
     while number > 0:
         number, remainder = divmod(number, 58)
-        encoded = _charset[remainder] + encoded
+        encoded = base_bytes[remainder] + encoded
     if _padding:
         zero_leading = bytes_leading(raw_bytes)
-        encoded = (_charset[0] * zero_leading) + encoded
+        encoded = (base_bytes[0] * zero_leading) + encoded
     return encoded
 
 
 def b58decode(encoded,
-              _charset=ASCII58_CHARSET,
+              _charset=ASCII58_BYTES,
               _lookup=ASCII58_ORDS,
               _powers=POW_58):
     """
     :param encoded:
         Base-58 encoded bytes.
     :param _charset:
-        (Internal) The character set to use. Defaults to ``ASCII58_CHARSET``
+        (Internal) The character set to use. Defaults to ``ASCII58_BYTES``
         that uses natural ASCII order.
     :param _lookup:
         (Internal) Ordinal-to-character lookup table for the specified

File mom/codec/base62.py

 
 
 # Follows ASCII order.
-ASCII62_CHARSET = ("0123456789"
+ASCII62_BYTES = ("0123456789"
                   "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                   "abcdefghijlkmnopqrstuvwxyz").encode("ascii")
 # Therefore, b'0' represents b'\0'.
-ASCII62_ORDS = dict((x, i) for i, x in enumerate(ASCII62_CHARSET))
+ASCII62_ORDS = dict((x, i) for i, x in enumerate(ASCII62_BYTES))
 
 
 # Really, I don't understand why people use the non-ASCII order,
 # is what you will need:
 #
 # Does not follow ASCII order.
-ALT62_CHARSET = ("0123456789"
+ALT62_BYTES = ("0123456789"
                  "abcdefghijlkmnopqrstuvwxyz"
                  "ABCDEFGHIJKLMNOPQRSTUVWXYZ").encode("ascii")
 # Therefore, b'0' represents b'\0'.
-ALT62_ORDS = dict((x, i) for i, x in enumerate(ALT62_CHARSET))
+ALT62_ORDS = dict((x, i) for i, x in enumerate(ALT62_BYTES))
 
 
 if have_python3:
-    ASCII62_CHARSET = tuple(byte(x) for x in ASCII62_CHARSET)
-    ALT62_CHARSET = tuple(byte(x) for x in ALT62_CHARSET)
+    ASCII62_BYTES = tuple(byte(x) for x in ASCII62_BYTES)
+    ALT62_BYTES = tuple(byte(x) for x in ALT62_BYTES)
 
 # If you're going to make people type stuff longer than this length
 # I don't know what to tell you. Beyond this length powers
 
 
 def b62encode(raw_bytes,
-              _charset=ASCII62_CHARSET, _padding=True, _zero_byte=ZERO_BYTE):
+              _charset=ASCII62_BYTES, _padding=True, _zero_byte=ZERO_BYTE):
     """
     Base62 encodes a sequence of raw bytes. Zero-byte sequences are
     preserved by default.
     :param raw_bytes:
         Raw bytes to encode.
     :param _charset:
-        (Internal) The character set to use. Defaults to ``ASCII62_CHARSET``
+        (Internal) The character set to use. Defaults to ``ASCII62_BYTES``
         that uses natural ASCII order.
     :param _padding:
         (Internal) ``True`` (default) to include prefixed zero-byte sequence
 
 
 def b62decode(encoded,
-              _charset=ASCII62_CHARSET,
+              _charset=ASCII62_BYTES,
               _lookup=ASCII62_ORDS,
               _powers=POW_62):
     """
     :param encoded:
         Base-62 encoded bytes.
     :param _charset:
-        (Internal) The character set to use. Defaults to ``ASCII62_CHARSET``
+        (Internal) The character set to use. Defaults to ``ASCII62_BYTES``
         that uses natural ASCII order.
     :param _lookup:
         (Internal) Ordinal-to-character lookup table for the specified

File tests/test_mom_codec_base58.py

 from mom.builtins import b
 from mom.codec import hex_decode, base58_decode, base58_encode
 from mom.codec._alt_base import b58decode_naive
-from mom.codec.base58 import b58encode, b58decode, ALT58_CHARSET, ASCII58_CHARSET
+from mom.codec.base58 import b58encode, b58decode, ALT58_BYTES, ASCII58_BYTES
 from mom.security.random import generate_random_bytes
 
 random_bytes = generate_random_bytes(384)
 
 class Test_base58_codec(unittest2.TestCase):
     def test_ensure_charset_length(self):
-        self.assertEqual(len(ASCII58_CHARSET), 58)
-        self.assertEqual(len(ALT58_CHARSET), 58)
+        self.assertEqual(len(ASCII58_BYTES), 58)
+        self.assertEqual(len(ALT58_BYTES), 58)
 
     def test_codec_identity(self):
         self.assertEqual(b58decode(b58encode(random_bytes)), random_bytes)

File tests/test_mom_codec_base62.py

 from mom.builtins import b
 from mom.codec import hex_decode, base62_decode, base62_encode
 from mom.codec._alt_base import b62decode_naive
-from mom.codec.base62 import b62encode, b62decode, ASCII62_CHARSET, ALT62_CHARSET
+from mom.codec.base62 import b62encode, b62decode, ASCII62_BYTES, ALT62_BYTES
 from mom.security.random import generate_random_bytes
 
 random_bytes_len_512 = generate_random_bytes(512)
 
 class Test_base62_codec(unittest2.TestCase):
     def test_ensure_charset_length(self):
-        self.assertEqual(len(ASCII62_CHARSET), 62)
-        self.assertEqual(len(ALT62_CHARSET), 62)
+        self.assertEqual(len(ASCII62_BYTES), 62)
+        self.assertEqual(len(ALT62_BYTES), 62)
 
     def test_codec_identity(self):
         self.assertEqual(