55 http://cr.yp.to/cdb.html
66
77'''
8- from __future__ import unicode_literals
9-
108from struct import Struct
119from itertools import chain
1210
13- import six
14- from six .moves import range
15-
1611from .djb_hash import djb_hash
1712
1813# Structs for 32-bit databases
2419write_2_le8 = Struct ('<QQ' ).pack
2520
2621# Encoders for keys
27- DEFAULT_ENCODERS = {six .text_type : lambda x : x .encode ('utf-8' )}
28- for t in six .integer_types :
29- DEFAULT_ENCODERS [t ] = lambda x : six .text_type (x ).encode ('utf-8' )
22+ DEFAULT_ENCODERS = {
23+ str : lambda x : x .encode ('utf-8' ),
24+ int : lambda x : str (x ).encode ('utf-8' ),
25+ }
3026
3127
3228class _CDBBase (object ):
@@ -41,7 +37,7 @@ def __init__(self, hashfn=djb_hash, strict=False, encoders=None):
4137 self .encoders .update (encoders )
4238
4339 def hash_key (self , key ):
44- if not isinstance (key , six . binary_type ):
40+ if not isinstance (key , bytes ):
4541 try :
4642 encoded_key = self .encoders [type (key )](key )
4743 except KeyError as e :
@@ -58,8 +54,8 @@ def hash_key_strict(self, key):
5854 try :
5955 h = self .hashfn (key )
6056 except TypeError as e :
61- msg = 'key must be of type {} '
62- e .args = (msg . format ( six . binary_type . __name__ ) ,)
57+ msg = 'key must be of type bytes '
58+ e .args = (msg ,)
6359 raise
6460
6561 # Truncate to 32 bits and remove sign.
@@ -232,9 +228,8 @@ def __exit__(self, exc_type, exc_value, traceback):
232228 def put (self , key , value = b'' ):
233229 '''Write a string key/value pair to the output file.'''
234230 # Ensure that the value is binary
235- if not isinstance (value , six .binary_type ):
236- msg = 'value must be of type {}'
237- raise TypeError (msg .format (six .binary_type .__name__ ))
231+ if not isinstance (value , bytes ):
232+ raise TypeError ('value must be of type bytes' )
238233
239234 # Computing the hash for the key also ensures that it's binary
240235 key , h = self .hash_key (key )
@@ -265,12 +260,12 @@ def putints(self, key, values):
265260 def putstring (self , key , value , encoding = 'utf-8' ):
266261 '''Write a unicode string associated with the given key to the output
267262 file after encoding it as UTF-8 or the given encoding.'''
268- self .put (key , six . text_type . encode (value , encoding ))
263+ self .put (key , value . encode (encoding ))
269264
270265 def putstrings (self , key , values , encoding = 'utf-8' ):
271266 '''Write zero or more unicode strings to the output file. Equivalent to
272267 calling putstring() in a loop.'''
273- self .puts (key , (six . text_type . encode (v , encoding ) for v in values ))
268+ self .puts (key , (v . encode (encoding ) for v in values ))
274269
275270 def finalize (self ):
276271 '''Write the final hash tables to the output file, and write out its
0 commit comments