--- a/skk2cdb.py +++ b/skk2cdb.py @@ -16,11 +16,12 @@ import sys, os from struct import pack, unpack from array import array +from functools import reduce # calc hash value with a given key -def cdbhash(s, n=0L): - return reduce(lambda h,c: ((h*33) ^ ord(c)) & 0xffffffffL, s, n+5381L) +def cdbhash(s, n=0): + return reduce(lambda h,c: ((h*33) ^ ord(c)) & 0xffffffff, s, n+5381) if pack('=i',1) == pack('>i',1): # big endian @@ -62,9 +63,9 @@ def __init__(self, cdbname, docache=1): self.name = cdbname - self._fp = file(cdbname, 'rb') + self._fp = open(cdbname, 'rb') hash0 = decode(self._fp.read(2048)) - self._hash0 = [ (hash0[i], hash0[i+1]) for i in xrange(0, 512, 2) ] + self._hash0 = [ (hash0[i], hash0[i+1]) for i in range(0, 512, 2) ] self._hash1 = [ None ] * 256 (self._eod,_) = self._hash0[0] self._docache = docache @@ -93,7 +94,7 @@ self._hash1[h1] = hs i = ((h >> 8) % ncells) * 2 n = ncells*2 - for _ in xrange(ncells): + for _ in range(ncells): p1 = hs[i+1] if p1 == 0: raise KeyError(k) if hs[i] == h: @@ -122,7 +123,7 @@ return False def __contains__(self, k): - return self.has_key(k) + return k in self def firstkey(self): self._keyiter = None @@ -132,7 +133,7 @@ if not self._keyiter: self._keyiter = ( k for (k,v) in cdbiter(self._fp, self._eod) ) try: - return self._keyiter.next() + return next(self._keyiter) except StopIteration: return None @@ -140,7 +141,7 @@ if not self._eachiter: self._eachiter = cdbiter(self._fp, self._eod) try: - return self._eachiter.next() + return next(self._eachiter) except StopIteration: return None @@ -159,9 +160,9 @@ self.fn = cdbname self.fntmp = tmpname self.numentries = 0 - self._fp = file(tmpname, 'wb') + self._fp = open(tmpname, 'wb') self._pos = 2048 # sizeof((h,p))*256 - self._bucket = [ array('I') for _ in xrange(256) ] + self._bucket = [ array('I') for _ in range(256) ] return def __len__(self): @@ -197,7 +198,7 @@ if not b1: continue blen = len(b1) a = array('I', [0]*blen*2) - for j in xrange(0, blen, 2): + for j in range(0, blen, 2): (h,p) = (b1[j],b1[j+1]) i = ((h >> 8) % blen)*2 while a[i+1]: # is cell[i] already occupied? @@ -238,7 +239,7 @@ # cdbdump def cdbdump(cdbname): - fp = file(cdbname, 'rb') + fp = open(cdbname, 'rb') (eor,) = unpack('>sys.stderr, 'file exists: %r' % outfile + print('file exists: %r' % outfile, file=sys.stderr) return 1 # maker = CDBMaker(outfile, outfile+'.tmp')