Commits

Gora Khargosh committed deb8d5b

Removes b() calls sprayed around. Replace with "constants".

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

Comments (0)

Files changed (4)

 # and just use byte literals.
 if str is unicode_type:
     def byte_literal(s):
+        """
+        This innocent-looking byte literal faker can be detrimental to
+        performance so define these as constants in your code instead.
+        Don't call it repeatedly inside tight loops.
+        """
         return s.encode('latin1')
 else:
     def byte_literal(s):
+        """
+        This innocent-looking byte literal faker can be detrimental to
+        performance so define these as constants in your code instead.
+        Don't call it repeatedly inside tight loops.
+        """
         return s
 
-# This is used in a large number of places. Do not remove.
+# These are used in a large number of places. Do not remove.
+# This may not make sense, but remember that our code may be used within
+# tight loops, and we do not want user code to slow down because of
+# thousands of calls to byte_literal or b. Do it once here.
 ZERO_BYTE = byte_literal('\x00')
 EMPTY_BYTE = byte_literal('')
+EQUAL_BYTE = byte_literal('=')
+PLUS_BYTE = byte_literal('+')
+HYPHEN_BYTE = byte_literal('-')
+FORWARD_SLASH_BYTE = byte_literal('/')
+UNDERSCORE_BYTE = byte_literal('_')
+DIGIT_ZERO_BYTE = byte_literal('0')
+
 
 have_little_endian = bool(byte_ord(array("i",[1]).tostring()[0]))
 
-
 try:
     # Check whether we have reduce as a built-in.
     __reduce_test__ = reduce((lambda num1, num2: num1 + num2), [1, 2, 3, 4])
     :returns:
         The number of leading needle bytes.
     """
+    if not is_bytes(raw_bytes):
+        raise TypeError("argument must be raw bytes: got %r" %
+                        type(raw_bytes).__name__)
     leading = 0
     # Indexing keeps compatibility between Python 2.x and Python 3.x
     _byte = needle[0]
     :returns:
         The number of trailing needle bytes.
     """
+    if not is_bytes(raw_bytes):
+        raise TypeError("argument must be raw bytes: got %r" %
+                        type(raw_bytes).__name__)
     trailing = 0
     # Indexing keeps compatibility between Python 2.x and Python 3.x
     _byte = needle[0]

mom/codec/__init__.py

 from __future__ import absolute_import
 
 import binascii
-from mom._compat import have_python3, ZERO_BYTE, EMPTY_BYTE
-from mom.builtins import is_bytes, b
+from mom._compat import have_python3, ZERO_BYTE, EMPTY_BYTE, UNDERSCORE_BYTE, FORWARD_SLASH_BYTE, HYPHEN_BYTE, PLUS_BYTE, EQUAL_BYTE, DIGIT_ZERO_BYTE
+from mom.builtins import is_bytes, b, bytes_leading
 from mom.functional import leading, chunks
 from mom.codec.integer import bytes_to_uint, uint_to_bytes
 from mom.codec.base62 import b62encode, b62decode
     # this module itself is a wrapper. binascii is implemented in C, so
     # we avoid module overhead however small.
     encoded = binascii.b2a_base64(raw_bytes)[:-1]
-    return encoded.rstrip(b('=')).replace(b('+'), b('-')).replace(b('/'),b('_'))
+    return encoded.rstrip(EQUAL_BYTE).\
+        replace(PLUS_BYTE, HYPHEN_BYTE).\
+        replace(FORWARD_SLASH_BYTE, UNDERSCORE_BYTE)
 
 
 def base64_urlsafe_decode(encoded):
                         type(encoded).__name__)
     remainder = len(encoded) % 4
     if remainder:
-        encoded += b('=') * (4 - remainder)
+        encoded += EQUAL_BYTE * (4 - remainder)
     # This is 3-4x faster than urlsafe_b64decode() -Guido.
     # We're not using the base64.py wrapper around binascii because
     # this module itself is a wrapper. binascii is implemented in C, so
     # we avoid module overhead however small.
-    encoded = encoded.replace(b('-'), b('+')).replace(b('_'),b('/'))
+    encoded = encoded.replace(HYPHEN_BYTE, PLUS_BYTE).\
+        replace(UNDERSCORE_BYTE, FORWARD_SLASH_BYTE)
     return binascii.a2b_base64(encoded)
 
 
     :returns:
         Decimal-encoded representation.
     """
-    if not is_bytes(raw_bytes):
-        raise TypeError("argument must be raw bytes: got %r" %
-                        type(raw_bytes).__name__)
-    padding = b("0") * leading((lambda w: w == ZERO_BYTE[0]), raw_bytes)
+    padding = DIGIT_ZERO_BYTE * bytes_leading(raw_bytes)
     int_val = bytes_to_uint(raw_bytes)
-    return padding + str(int_val).encode("ascii") if int_val else padding
+    if int_val:
+        encoded = padding + str(int_val).encode("ascii")
+    else:
+        encoded = padding
+    return encoded
 
 
 def decimal_decode(encoded):
     :returns:
         Raw bytes.
     """
-    if not is_bytes(encoded):
-        raise TypeError("argument must be bytes: got %r" %
-                        type(encoded).__name__)
-    padding = ZERO_BYTE * leading((lambda x: x == b("0")[0]), encoded)
+    padding = ZERO_BYTE * bytes_leading(encoded, DIGIT_ZERO_BYTE)
     int_val = int(encoded)
-    return padding + uint_to_bytes(int_val) if int_val else padding
+    if int_val:
+        decoded = padding + uint_to_bytes(int_val)
+    else:
+        decoded = padding
+    return decoded
 
 
 _HEX_TO_BIN_LOOKUP = {
         raise TypeError("argument must be raw bytes: got %r" %
                         type(raw_bytes).__name__)
     return EMPTY_BYTE.join(_HEX_TO_BIN_LOOKUP[hex_char]
-                   for hex_char in hex_encode(raw_bytes))
-
-    # Prefixed zero-bytes destructive. '\x00\x00' treated as '\x00'
-    #return bin(bytes_to_integer(byte_string))[2:]
+                   for hex_char in binascii.b2a_hex(raw_bytes))
 
 
 def bin_decode(encoded):
     if not is_bytes(encoded):
         raise TypeError("argument must be bytes: got %r" %
                         type(encoded).__name__)
-    return hex_decode(EMPTY_BYTE.join(_BIN_TO_HEX_LOOKUP[nibble]
-                              for nibble in chunks(encoded, 4)))
-
-    # Prefixed zero-bytes destructive. '\x00\x00\x00' treated as '\x00'
-    #return integer_to_bytes(int(encoded, 2))
-
+    return binascii.a2b_hex(EMPTY_BYTE.join(_BIN_TO_HEX_LOOKUP[nibble]
+                                            for nibble in chunks(encoded, 4)))

mom/codec/_alt_integer.py

 from mom._compat import range, ZERO_BYTE, \
     get_word_alignment, EMPTY_BYTE, \
     map, reduce
-from mom.builtins import integer_byte_length, b, byte, is_bytes, byte_ord
+from mom.builtins import integer_byte_length, byte, is_bytes, byte_ord
 
 
 def uint_to_bytes_naive_array_based(uint, chunk_size=0):
             )
         remainder = length % chunk_size
         if remainder:
-            raw_bytes = (chunk_size - remainder) * b('\x00') + raw_bytes
+            raw_bytes = (chunk_size - remainder) * ZERO_BYTE + raw_bytes
     return raw_bytes
 
 
         n >>= 32
     # strip off leading zeros
     for i in range(len(s)):
-        if s[i] != b('\000')[0]:
+        if s[i] != ZERO_BYTE[0]:
             break
     else:
         # only happens when n == 0
-        s = b('\000')
+        s = ZERO_BYTE
         i = 0
     s = s[i:]
     # add back some pad bytes. this could be done more efficiently w.r.t. the
     # de-padding being done above, but sigh...
     if blocksize > 0 and len(s) % blocksize:
-        s = (blocksize - len(s) % blocksize) * b('\000') + s
+        s = (blocksize - len(s) % blocksize) * ZERO_BYTE + s
     return s
 
 
-def uint_to_bytes_array_based(number, chunk_size=0,
-                               _zero_byte=ZERO_BYTE,
-                               _get_machine_alignment=get_word_alignment):
+def uint_to_bytes_array_based(number, chunk_size=0):
     """
     Convert a integer to bytes (base-256 representation)::
 
 
     # Align packing to machine word size.
     num = number
-    word_bits, word_bytes, max_uint, pack_type = _get_machine_alignment(num)
+    word_bits, word_bytes, max_uint, pack_type = get_word_alignment(num)
     pack_format = ">" + pack_type
 
     temp_buffer = array("B", [0] * word_bytes)
             )
         remainder = length % chunk_size
         if remainder:
-            raw_bytes = (chunk_size - remainder) * _zero_byte + raw_bytes
+            raw_bytes = (chunk_size - remainder) * ZERO_BYTE + raw_bytes
     return raw_bytes