本文整理汇总了Python中pybloom.BloomFilter.add方法的典型用法代码示例。如果您正苦于以下问题:Python BloomFilter.add方法的具体用法?Python BloomFilter.add怎么用?Python BloomFilter.add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pybloom.BloomFilter
的用法示例。
在下文中一共展示了BloomFilter.add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from pybloom import BloomFilter [as 别名]
# 或者: from pybloom.BloomFilter import add [as 别名]
class UrlBloom:
'''BloomFilter: check elements repetition'''
def __init__(self, _capacity=1000000, _error_rate=0.00001):
self.is_full = False
# determine if open backup bloom data by time
if CONFIG.get('BACKUP', 0) == 1:
self.bomb = TimeBomb(CONFIG['TMP_DIR'] + CONFIG['BLOOM_FILE'])
self.filter = self.bomb.load()
if self.filter is None:
self.filter = BloomFilter(capacity=_capacity, error_rate=_error_rate)
self.bomb.dump(self.filter)
else:
self.filter = BloomFilter(capacity=_capacity, error_rate=_error_rate)
def add(self, links):
if self.is_full:
return
try:
for ele in links:
self.filter.add(ele)
except IndexError:
# rasie IndexError when bloom is at capacity
self.is_full = True
def clean(self, links):
res = []
for ele in links:
if ele not in self.filter:
res.append(ele)
return res
示例2: UrlSpider
# 需要导入模块: from pybloom import BloomFilter [as 别名]
# 或者: from pybloom.BloomFilter import add [as 别名]
class UrlSpider(CrawlSpider):
name = "urlspider"
allowed_domains = ["tianya.cn"]
start_urls = ("http://www.hao123.com", )
rules = (
Rule(SgmlLinkExtractor(allow=()), callback="parse_resp", follow= True),
)
def __init__(self, *args, **kwargs):
# run using: scrapy crawl xss_spider -a url='http://example.com'
super(UrlSpider, self).__init__(*args, **kwargs)
self.start_urls = [kwargs.get('url')]
hostname = urlparse(self.start_urls[0]).hostname
self.allowed_domains = [hostname] # adding [] around the value seems to allow it to crawl subdomain of value
self.fingerprints = BloomFilter(3000000, 0.0001)
def parse_start_url(self, response):
print "start:"+response.url
return
def parse_resp(self, response):
fp = response.url
new_fp = obtain_key(fp)
if new_fp in self.fingerprints:
return
self.fingerprints.add(new_fp)
item = SiteCrawlItem()
item["url"] = response.url
yield item
示例3: main
# 需要导入模块: from pybloom import BloomFilter [as 别名]
# 或者: from pybloom.BloomFilter import add [as 别名]
def main():
if os.path.isfile(nsrl_path):
print "BUILDING: Reading in NSRL Database"
with open(nsrl_path) as f_line:
# Strip off header
_ = f_line.readline()
print "BUILDING: Calculating number of hashes in NSRL..."
num_lines = sum(bl.count("\n") for bl in blocks(f_line))
print "BUILDING: There are %s hashes in the NSRL Database" % num_lines
with open(nsrl_path) as f_nsrl:
# Strip off header
_ = f_nsrl.readline()
print "BUILDING: Creating bloomfilter"
bf = BloomFilter(num_lines, error_rate)
print "BUILDING: Inserting hashes into bloomfilter"
for line in f_nsrl:
md5_hash = line.split(",")[1].strip('"')
if md5_hash:
try:
bf.add(md5_hash)
except Exception as e:
print "ERROR: %s" % e
print "BUILDING: NSRL bloomfilter contains {} items.".format(len(bf))
with open('nsrl.bloom', 'wb') as nb:
bf.tofile(nb)
print "BUILDING: Complete"
else:
print("ERROR: No such file or directory: %s", nsrl_path)
return
示例4: user_init
# 需要导入模块: from pybloom import BloomFilter [as 别名]
# 或者: from pybloom.BloomFilter import add [as 别名]
def user_init():
import re
users = BloomFilter(10000000, 0.001)
f= open(u"D:/工作/数据美化/data/简书用户id1.txt")
for line in f:
users.add(line.strip())
return users
示例5: main
# 需要导入模块: from pybloom import BloomFilter [as 别名]
# 或者: from pybloom.BloomFilter import add [as 别名]
def main(argv):
if argv:
error_rate = float(argv[0])
print "[BUILDING] Using error-rate: {}".format(error_rate)
if os.path.isfile(nsrl_path):
print "[BUILDING] Reading in NSRL Database"
with open(nsrl_path) as f_line:
# Strip off header
_ = f_line.readline()
print "[BUILDING] Calculating number of hashes in NSRL..."
num_lines = sum(bl.count("\n") for bl in blocks(f_line))
print "[BUILDING] There are %s hashes in the NSRL Database" % num_lines
with open(nsrl_path) as f_nsrl:
# Strip off header
_ = f_nsrl.readline()
print "[BUILDING] Creating bloomfilter"
bf = BloomFilter(num_lines, error_rate)
print "[BUILDING] Inserting hashes into bloomfilter"
for line in f_nsrl:
sha1_hash = line.split(",")[0].strip('"')
if sha1_hash:
try:
sha1 = binascii.unhexlify(sha1_hash)
bf.add(sha1)
except Exception as e:
print "[ERROR] %s" % e
print "[BUILDING] NSRL bloomfilter contains {} items.".format(len(bf))
with open('nsrl.bloom', 'wb') as nb:
bf.tofile(nb)
print "[BUILDING] Complete"
else:
print("[ERROR] No such file or directory: %s", nsrl_path)
return
示例6: BLOOMDupeFilter
# 需要导入模块: from pybloom import BloomFilter [as 别名]
# 或者: from pybloom.BloomFilter import add [as 别名]
class BLOOMDupeFilter(BaseDupeFilter):
"""Request Fingerprint duplicates filter"""
def __init__(self, path=None):
self.file = None
# capacity
# this BloomFilter must be able to store at least *capacity* elements
# while maintaining no more than *error_rate* chance of false
# positives
# error_rate
# the error_rate of the filter returning false positives. This
# determines the filters capacity. Inserting more than capacity
# elements greatly increases the chance of false positives.
self.fingerprints = BloomFilter(capacity=2000000, error_rate=0.00001)
# get all the urls from database
db = DynamoDBPipeline()
urls = db.get_url_list()
[self.fingerprints.add(url) for url in urls]
@classmethod
def from_settings(cls, settings):
return cls(job_dir(settings))
def request_seen(self, request):
fp = request.url
if fp in self.fingerprints:
return True
self.fingerprints.add(fp)
def close(self, reason):
self.fingerprints = None
示例7: add
# 需要导入模块: from pybloom import BloomFilter [as 别名]
# 或者: from pybloom.BloomFilter import add [as 别名]
def add(self, key):
"""Adds a key to this bloom filter.
If the key already exists in this filter it will return True.
Otherwise False.
>>> b = ScalableBloomFilter(initial_capacity=100, error_rate=0.001, \
mode=ScalableBloomFilter.SMALL_SET_GROWTH)
>>> b.add("hello")
False
>>> b.add("hello")
True
"""
if key in self:
return True
if not self.filters:
filter = BloomFilter(capacity=self.initial_capacity, error_rate=self.error_rate * (1.0 - self.ratio))
self.filters.append(filter)
else:
filter = self.filters[-1]
if filter.count >= filter.capacity:
filter = BloomFilter(capacity=filter.capacity * self.scale, error_rate=filter.error_rate * self.ratio)
self.filters.append(filter)
filter.add(key, skip_check=True)
return False
示例8: determine_lookup_speed_threshold
# 需要导入模块: from pybloom import BloomFilter [as 别名]
# 或者: from pybloom.BloomFilter import add [as 别名]
def determine_lookup_speed_threshold(self):
from time import time
#do each one 5 times
bf = BloomFilter(capacity=self.bloom_size, error_rate=self.bloom_error)
count = 1
repetitions = 5
self_bf_holder = self.bf
self.bf = bf
while True:
bf.add('andrew_' + str(count))
bin_faster_count = 0
for j in xrange(repetitions):
#Linear scan
t1 = time()
self.linear_scan_count('andrew')
t2 = time()
linear_time = t2-t1
t1 = time()
self.binsearch_count('andrew')
t2 = time()
bin_time = t2-t1
bin_faster_count += int(bin_time < linear_time)
if 1.*bin_faster_count / repetitions >= 0.75:
del bf
self.bf = self_bf_holder
return count
count += 1
示例9: generateBloomFilter
# 需要导入模块: from pybloom import BloomFilter [as 别名]
# 或者: from pybloom.BloomFilter import add [as 别名]
def generateBloomFilter(file):
"Generates the bloom filter for entries in file."
# this probably isnt enough, need to look the data formatting over more
# thoroughly
d = BloomFilter(1000, 0.001)
for line in file:
d.add(line.split(1)[0])
示例10: vacuum_all
# 需要导入模块: from pybloom import BloomFilter [as 别名]
# 或者: from pybloom.BloomFilter import add [as 别名]
def vacuum_all(self, limit=None, time_limit=None, unupdated=False):
logger.debug('Begin vacuum_all(limit=%s, time_limit=%s, unupdated=%s)', limit, time_limit, unupdated)
##TODO delete SCIFields with SCFilterId not found in SCFilter
self.plugins = self.load_plugins()
self.ts = self.term_stat('SupplierCatalogItemFields Vacuum', len(self.plugins))
now = start_time = datetime.now()
try:
transaction.begin()
for plug in self.plugins.itervalues():
supplier_catalog_filter_id = plug.supplier_catalog_filter_id()
### Generate a bloom filter set of SCIF id's in VersionModel
model_name = plug.version_model() + 'Model'
VersionModel = getattr(model, model_name)
query = DBSession.query(VersionModel.supplier_catalog_item_field_id)
s = BloomFilter(capacity=query.count() + 1)
self.ts['sub_total'] = query.count()
for (supplier_catalog_item_field_id, ) in query.yield_per(100):
s.add(supplier_catalog_item_field_id)
self.ts['sub_done'] += 1
del query
### Iterate through SCIFields, deleting any that don't appear in the bloom filter.
query = DBSession.query(SupplierCatalogItemFieldModel)
query = query.filter(SupplierCatalogItemFieldModel.supplier_catalog_filter_id == supplier_catalog_filter_id)
if unupdated is not True:
query = query.filter(SupplierCatalogItemFieldModel.updated != None)
if limit is not None:
query = query.order_by(SupplierCatalogItemFieldModel.vacuumed.nullsfirst())
query = query.limit(limit)
logger.debug("LIMIT %i, supplier_catalog_filter_id %s", limit, supplier_catalog_filter_id)
self.ts['sub_done'] = 0
self.ts['sub_total'] = query.count()
for supplier_catalog_item_field in query.yield_per(100):
if supplier_catalog_item_field.id not in s:
logger.debug("Deleting SupplierCatalogItemField %s", supplier_catalog_item_field.id)
DBSession.delete(supplier_catalog_item_field)
else:
supplier_catalog_item_field.vacuumed = now
if self.ts['sub_done'] % 1000 == 0:
DBSession.flush()
self.ts['sub_done'] += 1
del query
DBSession.flush()
if time_limit is not None:
if datetime.now() > start_time + time_limit:
logger.info("Reached Time Limit at %i of %i", self.ts['done'], self.ts['total'])
transaction.commit()
break;
self.ts['done'] += 1
transaction.commit()
except Exception:
logger.exception("Caught Exception: ")
transaction.abort()
finally:
self.ts.finish()
logger.debug('End vacuum()')
示例11: get_bloom
# 需要导入模块: from pybloom import BloomFilter [as 别名]
# 或者: from pybloom.BloomFilter import add [as 别名]
def get_bloom(self):
bloom_cache = BloomFilter(capacity=10000000, error_rate=0.00001)
sql = "select url from user_tbl"
self.cursor.execute(sql)
datalist = self.cursor.fetchall()
for data in datalist:
bloom_cache.add(data[0])
return bloom_cache
示例12: BlogSpider
# 需要导入模块: from pybloom import BloomFilter [as 别名]
# 或者: from pybloom.BloomFilter import add [as 别名]
class BlogSpider(Spider):
def __init__(self):
self.pageNumber =0
self.logfile = open("/home/hduser/Logs/csdnUserlog.log","w")
self.f = BloomFilter(capacity=10000000, error_rate=0.0001)
name = "csdnUserScrapy"
#减慢爬取速度 为2s
download_delay = 0.5
allowed_domains = ["my.csdn.net"]
start_urls = [
"http://my.csdn.net/jiazhijun","http://my.csdn.net/sodino","http://my.csdn.net/bill_man","http://my.csdn.net/lhc2207221755","http://my.csdn.net/xgbing","http://my.csdn.net/LoongEmbedded","http://my.csdn.net/jdh99","http://my.csdn.net/zqiang_55","http://my.csdn.net/zhao_zepeng","http://my.csdn.net/linyt","http://my.csdn.net/kmyhy","http://my.csdn.net/lincyang","http://my.csdn.net/jdsjlzx","http://my.csdn.net/u011012932","http://my.csdn.net/yayun0516","http://my.csdn.net/qq_23547831","http://my.csdn.net/CHENYUFENG1991","http://my.csdn.net/qq_26787115","http://my.csdn.net/kongki","http://my.csdn.net/you23hai45","http://my.csdn.net/cometwo","http://my.csdn.net/yuanziok","http://my.csdn.net/woxueliuyun","http://my.csdn.net/gatieme","http://my.csdn.net/u010850027","http://my.csdn.net/yinwenjie","http://my.csdn.net/teamlet","http://my.csdn.net/wangyangzhizhou","http://my.csdn.net/xiaoxian8023","http://my.csdn.net/ooppookid","http://my.csdn.net/wsl211511","http://my.csdn.net/liyuanbhu","http://my.csdn.net/sxhelijian","http://my.csdn.net/raylee2007","http://my.csdn.net/luozhuang","http://my.csdn.net/shaqoneal","http://my.csdn.net/dc_726","http://my.csdn.net/tobacco5648","http://my.csdn.net/wowkk","http://my.csdn.net/csfreebird","http://my.csdn.net/xukai871105","http://my.csdn.net/tuzongxun","http://my.csdn.net/mchdba","http://my.csdn.net/lichangzai","http://my.csdn.net/leftfist","http://my.csdn.net/wonder4","http://my.csdn.net/fogyisland2000","http://my.csdn.net/smstong","http://my.csdn.net/david_520042","http://my.csdn.net/ghostbear","http://my.csdn.net/xuyaqun","http://my.csdn.net/force_eagle","http://my.csdn.net/Jmilk","http://my.csdn.net/xiangpingli","http://my.csdn.net/quqi99","http://my.csdn.net/michaelzhou224","http://my.csdn.net/zzq900503","http://my.csdn.net/pipisorry","http://my.csdn.net/zhangmike","http://my.csdn.net/foruok","http://my.csdn.net/fengbingchun","http://my.csdn.net/qingrun","http://my.csdn.net/harrymeng","http://my.csdn.net/pukuimin1226","http://my.csdn.net/lihuoming","http://my.csdn.net/zhazha1980518","http://my.csdn.net/redarmy_chen","http://my.csdn.net/yuanmeng001","http://my.csdn.net/yeka","http://my.csdn.net/xieqq","http://my.csdn.net/zhangxiaoxiang","http://my.csdn.net/oiio","http://my.csdn.net/jobchanceleo","http://my.csdn.net/broadview2006"
]
def parse(self, response):
sel = Selector(response)
item = CsdnusersspyderItem()
print "response URL %s\n" % str(response.url)
self.f.add(str(response.url))
#print "*********\nBloom added self.url: %s \n**********\n" % str(response.url)
item["userName"] = str(response.url).split('/')[-1]
relativeMarks =response.xpath("//div[@class='header clearfix']/a[@href]").extract()
item["follow"] = []
item["befollowed"] = []
i = 0
for u in relativeMarks:
unameMark = re.findall(r'username="\b.*"',u)
(s,e) = re.search(r'".*"',unameMark[0]).span()
uname = unameMark[0][s+1:e-1]
if i <= 7:
item["follow"].append(uname.encode('utf-8'))
else:
item["befollowed"].append(uname.encode('utf-8'))
newUrl = "http://my.csdn.net/"+uname
if newUrl in self.f:
self.logfile.write("Duplicated URL: %s\n" % newUrl)
pass
else:
#self.logfile.write("wei chong fu %s\n" % newUrl)
yield Request(newUrl,callback=self.parse)
i += 1
item["pageUrl"] = str(response.url)
focusNumMark = response.xpath("//dd[@class='focus_num']").extract()[0]
(s ,e) = re.search(r'\d+',focusNumMark).span()
focusNum = focusNumMark[s:e].encode('utf-8')
item["followNum"] = focusNum
fansNumMark = response.xpath("//dd[@class='fans_num']").extract()[0]
(s ,e) = re.search(r'\d+',fansNumMark).span()
fansNum = fansNumMark[s:e].encode('utf-8')
item["befollowedNum"] = fansNum
item["pageID"] = self.pageNumber
item["pageMD5"] =GetMD5.getMD5(item["pageUrl"])
yield item
self.pageNumber = self.pageNumber +1
if self.pageNumber % 1000 == 0:
time.sleep(15)
示例13: BloomZip
# 需要导入模块: from pybloom import BloomFilter [as 别名]
# 或者: from pybloom.BloomFilter import add [as 别名]
class BloomZip(object):
def __init__(self, name):
super(BloomZip, self).__init__()
self.__data = StringIO()
self._name = name
self._bf = None
if os.path.isfile(self._name):
with open(self._name, 'rb') as f:
length = struct.unpack(">L", f.read(4))[0]
self._bf = BloomFilter.fromfile(f, length)
def contains(self, word):
return word in self._bf
def write(self, data):
self.__data.write(data)
def close(self):
if self._bf is None and self.__data is None:
return
words = self.__data.getvalue().split()
self._bf = BloomFilter(capacity=len(words) + 1)
for word in words:
self._bf.add(word, skip_check=True)
def get_bl_size():
t = tempfile.NamedTemporaryFile().name
with open(t, 'w') as fn:
self._bf.tofile(fn)
s = os.path.getsize(t)
os.remove(t)
return s
if os.path.isfile(self._name):
return
a = open(self._name, 'w')
a.write(struct.pack(">L", get_bl_size()))
self._bf.tofile(a)
with GzipFile(self._name, 'w', fileobj=a) as f:
f.write(self.__data.getvalue())
a.close()
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.close()
if exc_type is not None:
print(exc_tb)
raise exc_val
示例14: to_bloom
# 需要导入模块: from pybloom import BloomFilter [as 别名]
# 或者: from pybloom.BloomFilter import add [as 别名]
def to_bloom(filename):
with open(filename, 'r') as f:
b = BloomFilter(capacity=1000000, error_rate=0.001)
for line in f:
if line != "":
b.add(line)
new_filename = filename + ".bloom"
out_f = open(new_filename, 'wb')
b.tofile(out_f)
示例15: test_union
# 需要导入模块: from pybloom import BloomFilter [as 别名]
# 或者: from pybloom.BloomFilter import add [as 别名]
def test_union(self):
bloom_one = BloomFilter(100, 0.001)
bloom_two = BloomFilter(100, 0.001)
chars = [chr(i) for i in range(97, 123)]
for char in chars[len(chars)/2:]:
bloom_one.add(char)
for char in chars[:len(chars)/2]:
bloom_two.add(char)
new_bloom = bloom_one.union(bloom_two)
for char in chars:
self.assert_(char in new_bloom)