浏览代码

Better duck typing for hashed objects

dstromberg 13 年之前
父节点
当前提交
9606ba0024
共有 1 个文件被更改,包括 5 次插入4 次删除
  1. 5 4
      bloom_filter_mod.py

+ 5 - 4
bloom_filter_mod.py

@@ -240,13 +240,14 @@ def get_bitno_lin_comb(bloom_filter, key):
 	# This one assumes key is either bytes or str (or other list of integers)
 	# This one assumes key is either bytes or str (or other list of integers)
 
 
 	# I'd love to check for long too, but that doesn't exist in 3.2, and 2.5 doesn't have the numbers.Integral base type
 	# I'd love to check for long too, but that doesn't exist in 3.2, and 2.5 doesn't have the numbers.Integral base type
-	if isinstance(key, int):
+	if hasattr(key, '__divmod__'):
 		int_list = []
 		int_list = []
 		temp = key
 		temp = key
 		while temp:
 		while temp:
-			int_list.append(temp % 256)
-			temp //= 256
-	elif isinstance(key[0], int):
+			quotient, remainder = divmod(temp, 256)
+			int_list.append(remainder)
+			temp = quotient
+	elif hasattr(key[0], '__divmod__'):
 		int_list = key
 		int_list = key
 	elif isinstance(key[0], str):
 	elif isinstance(key[0], str):
 		int_list = [ ord(char) for char in key ]
 		int_list = [ ord(char) for char in key ]