132 lines
3.5 KiB
Diff
132 lines
3.5 KiB
Diff
--- 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('<I', fp.read(4))
|
|
return cdbiter(fp, eor)
|
|
|
|
@@ -248,7 +249,7 @@
|
|
q = []
|
|
for it in iters:
|
|
try:
|
|
- q.append((it.next(),it))
|
|
+ q.append((next(it),it))
|
|
except StopIteration:
|
|
pass
|
|
k0 = None
|
|
@@ -262,7 +263,7 @@
|
|
vs.append(v)
|
|
k0 = k
|
|
try:
|
|
- q.append((it.next(),it))
|
|
+ q.append((next(it),it))
|
|
except StopIteration:
|
|
continue
|
|
if vs: yield (k0,vs)
|
|
@@ -280,7 +281,7 @@
|
|
import fileinput
|
|
import os.path
|
|
def usage():
|
|
- print 'usage: %s [-f] outfile [infile ...]' % argv[0]
|
|
+ print('usage: %s [-f] outfile [infile ...]' % argv[0])
|
|
return 100
|
|
try:
|
|
(opts, args) = getopt.getopt(argv[1:], 'dfo:')
|
|
@@ -292,7 +293,7 @@
|
|
if not args: return usage()
|
|
outfile = args.pop(0)
|
|
if not force and os.path.exists(outfile):
|
|
- print >>sys.stderr, 'file exists: %r' % outfile
|
|
+ print('file exists: %r' % outfile, file=sys.stderr)
|
|
return 1
|
|
#
|
|
maker = CDBMaker(outfile, outfile+'.tmp')
|