-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash.py
More file actions
41 lines (34 loc) · 1.14 KB
/
Copy pathhash.py
File metadata and controls
41 lines (34 loc) · 1.14 KB
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
import hashlib
import os
import pickle
class HashDatabase:
def __init__(self, filename):
self.filename = filename
self.hash_set = set()
# 初始化时加载现有哈希值
if os.path.exists(self.filename):
with open(self.filename, 'rb') as f:
self.hash_set = pickle.load(f)
else:
# 创建空文件
open(self.filename, 'wb').close()
def check(self,input_str):
if input_str in self.hash_set:
return True
else:
return False
def add(self, input_str):
if input_str in self.hash_set:
return False # 已存在,返回False
# 更新内存中的哈希集合
self.hash_set.add(input_str)
return True # 新增成功,返回True
def save(self):
with open(self.filename, 'wb') as f:
pickle.dump(self.hash_set,f)
# 测试用例
if __name__ == "__main__":
db = HashDatabase("hashes.db")
print(db.add("Hello World")) # True(首次添加)
print(db.add("Hello World")) # False(重复)
print(db.add("Another string")) # True