开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
1 Star 0 Fork 265

shankhand/python-learn

forked from mktime/python-learn
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (1)
master
master
分支 (1)
master
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (1)
master
python-learn
/
bloom_1.py
python-learn
/
bloom_1.py
bloom_1.py 7.53 KB
一键复制 编辑 原始数据 按行查看 历史
mr.github 提交于 2015年05月21日 13:41 +08:00 . bloom filter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
#coding:utf-8
# Bloom filters in Python
# Adam Langley <agl@imperialviolet.org>
# 给CountedBloom加了一个max_count 张沈鹏 <zsp007@gmail.com>
# Bloom-Filter算法简介
# http://www.googlechinablog.com/2007/07/bloom-filter.html
# http://zh.wikipedia.org/wiki/%E5%B8%83%E9%9A%86%E8%BF%87%E6%BB%A4%E5%99%A8
# 这个计算器可以帮你求最佳的参数
# http://www.cc.gatech.edu/~manolios/bloom-filters/calculator.html
# CountedBloom 的 buckets 参数对应于计算器的m,也就是"m denotes the number of bits in the Bloom filter"
import array
import struct
mixarray = array.array ('B', '\x00' * 256)
# The mixarray is based on RC4 and is used as diffusion in the hashing function
def mixarray_init (mixarray):
for i in range (256):
mixarray[i] = i
k = 7
for j in range (4):
for i in range (256):
s = mixarray[i]
k = (k + s) % 256
mixarray[i] = mixarray[k]
mixarray[k] = s
mixarray_init (mixarray)
class Bloom (object):
'''Bloom filters provide a fast and compact way of checking set membership. They do this by introducing a risk of a
false positive (but there are no false negatives).
For more information see http://www.cs.wisc.edu/~cao/papers/summary-cache/node8.html'''
def __init__ (self, bytes, hashes, data = None):
'''@bytes is the size of the bloom filter in 8-bit bytes and @hashes is the number of hash functions to use. Consult the
web page linked above for values to use. If in doubt, bytes = num_elements and hashes = 4'''
self.hashes = hashes
self.bytes = bytes
if data == None:
self.a = self._make_array (bytes)
else:
assert len (data) == bytes
self.a = data
def init_from_counted (self, cnt):
'''Set the contents of this filter from the contents of the counted filter @cnt. You have to match sizes'''
if self.bytes * 8 != (len (cnt.a) * 2):
raise ValueError ('Filters are not the same size')
for i in xrange (len (cnt.a)):
b = cnt.a[i]
b1 = (b & 0xf0) >> 4
b2 = (b & 0x0f)
if b1:
self.a[(i * 2) // 8] |= self.bitmask[(i * 2) % 8]
if b2:
self.a[(i * 2 + 1) // 8] |= self.bitmask[(i * 2 + 1) % 8]
def _make_array (self, size):
a = array.array ('B')
# stupidly, there's no good way that I can see of resizing an array without allocing a huge string to do so
# thus I use this, slightly odd, method:
blocklen = 256
arrayblock = array.array ('B', '\x00' * blocklen)
todo = size
while (todo >= blocklen):
a.extend (arrayblock)
todo -= blocklen
if todo:
a.extend (array.array ('B', '\x00' * todo))
# now a is of the right length
return a
def _hashfunc (self, n, val):
'''Apply the nth hash function'''
global mixarray
b = [ord(x) for x in struct.pack ('I', val)]
c = array.array ('B', [0, 0, 0, 0])
for i in range (4):
c[i] = mixarray[(b[i] + n) % 256]
return struct.unpack ('I', c.tostring())[0]
bitmask = [0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01]
def insert (self, val):
for i in range (self.hashes):
n = self._hashfunc (i, val) % (self.bytes * 8)
self.a[n // 8] |= self.bitmask[n % 8]
def __contains__ (self, val):
for i in range (self.hashes):
n = self._hashfunc (i, val) % (self.bytes * 8)
if not self.a[n // 8] & self.bitmask[n % 8]:
return 0
return 1
MAX_COUNT = 15
class CountedBloom (Bloom):
'''Just like a Bloom filter, but provides counting (e.g. you can delete as well). This uses 4 bits per bucket, so is
generally four times larger than the same non-counted bloom filter.'''
def __init__ (self, buckets, hashes):
'''Please note that @buckets must be even. Also note that with a Bloom object you give the number of *bytes* and each byte is 8 buckets. Here you're giving the number of buckets.'''
assert buckets % 2 == 0
self.hashes = hashes
self.buckets = buckets
self.a = self._make_array (buckets // 2)
def insert (self, val):
masks = [(0x0f, 0xf0), (0xf0, 0x0f)]
shifts = [4, 0 ]
for i in range (self.hashes):
n = self._hashfunc (i, val) % self.buckets
byte = n // 2
bucket = n % 2
(notmask, mask) = masks[bucket]
shift = shifts[bucket]
bval = ((self.a[byte] & mask) >> shift)
if bval < MAX_COUNT: # we shouldn't increment it if it's at the maximum
bval += 1
self.a[byte] = (self.a[byte] & notmask) | (bval << shift)
def __contains__ (self, val):
masks = [(0x0f, 0xf0), (0xf0, 0x0f)]
shifts = [4, 0]
for i in range (self.hashes):
n = self._hashfunc (i, val) % self.buckets
byte = n // 2
bucket = n % 2
(notmask, mask) = masks[bucket]
shift = shifts[bucket]
bval = ((self.a[byte] & mask) >> shift)
if bval == 0:
return 0
return 1
def max_count(self, val):
masks = [(0x0f, 0xf0), (0xf0, 0x0f)]
shifts = [4, 0]
count_val = MAX_COUNT
for i in range (self.hashes):
n = self._hashfunc (i, val) % self.buckets
byte = n // 2
bucket = n % 2
(notmask, mask) = masks[bucket]
shift = shifts[bucket]
bval = ((self.a[byte] & mask) >> shift)
if bval < MAX_COUNT:
if bval == 0:
return 0
else:
count_val = bval
return count_val
def __delitem__ (self, val):
masks = [(0x0f, 0xf0), (0xf0, 0x0f)]
shifts = [4, 0]
for i in range (self.hashes):
n = self._hashfunc (i, val) % self.buckets
byte = n // 2
bucket = n % 2
(notmask, mask) = masks[bucket]
shift = shifts[bucket]
bval = ((self.a[byte] & mask) >> shift)
if bval < MAX_COUNT: # we shouldn't decrement it if it's at the maximum
bval -= 1
self.a[byte] = (self.a[byte] & notmask) | (bval << shift)
__all__ = ['Bloom']
if __name__ == '__main__':
print 'Testing bloom filter: there should be no assertion failures'
a = Bloom (3, 4)
a.insert (45)
print a.a
a.insert (17)
print a.a
a.insert (12)
print a.a
assert 45 in a
assert 45 in a
assert not 33 in a
assert 45 in a
assert 17 in a
assert 12 in a
c = 0
for x in range (255):
if x in a:
c += 1
print c
print float(c)/255
a = CountedBloom (24, 4)
a.insert (45)
print a.a
a.insert (17)
print a.a
a.insert (12)
a.insert (12)
print "a.max_count(12)", a.max_count(12)
a.insert ("张沈鹏")
a.insert ("张沈鹏")
a.insert ("张沈鹏")
print "a.max_count(zsp)", a.max_count(12)
print a.a
assert 45 in a
assert 45 in a
assert not 33 in a
assert 45 in a
assert 17 in a
assert 12 in a
c = 0
for x in range (255):
if x in a:
c += 1
print c
print float(c)/255
del a[45]
assert not 45 in a
a2 = Bloom (3, 4)
a2.init_from_counted (a)
print a2.a
assert 17 in a2
assert 12 in a2
assert not 45 in a
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

个人AI助理,音频实时监听,GPT对话,opencv获取摄像头视频截图;Python基础编程示例:Excel读写追加处理,XML解析、JSON解析、FLV与MP4转换,PyQT界面应用程序开发示例等,https证书到期检测,糗百爬虫,pdf和图片互相转换,socket使用,百度OCR调用例子,IP及端口快速扫描。
暂无标签
MIT
使用 MIT 开源许可协议
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/wsldwps/python-learn.git
git@gitee.com:wsldwps/python-learn.git
wsldwps
python-learn
python-learn
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /