def pjw_hash(key): bits_in_unsigned_int = 4 * 8 three_quarters = (bits_in_unsigned_int * 3) / 4 one_eighth = bits_in_unsigned_int / 8 high_bits = 0xFFFFFFFF << int(bits_in_unsigned_int - one_eighth) hash_value = 0 test = 0 for i in range(len(key)): hash_value = (hash_value << int(one_eighth)) + ord(key[i]) test = hash_value & high_bits if test != 0: hash_value = ((hash_value ^ (test >> int(three_quarters))) & (~high_bits)) return hash_value & 0x7FFFFFFF