-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkeydb.py
More file actions
79 lines (66 loc) · 1.91 KB
/
Copy pathkeydb.py
File metadata and controls
79 lines (66 loc) · 1.91 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
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
class KeyDB():
FILE = 1
UDP = 2
TCP = 4
SQLITE = 16
def __init__(self, path=None, db_type=None):
self.path = path
if db_type is None:
self.type = self.FILE
self.is_change = False
self.db = self.open()
def __del__(self):
if self.is_change:
self.save()
def save(self):
if self.is_change:
if self.type == self.FILE and self.path is not None:
with open(self.path, 'w') as f:
# content = '\n'.join([str(i).strip() for i in self.db])
f.write('\n'.join([i.strip() for i in self.db if i]))
self.is_change = False
def open(self, mode='r'):
content = []
if self.type == self.FILE and self.path is not None:
with open(self.path, mode) as f:
content = f.read()
content = [s.strip() for s in content.split('\n') if s]
return content
def initDB(self):
print('init KeyDB')
def put(self, val):
val = str(val)
val = val.strip('\n').strip()
if val:
self.db.append(val)
self.is_change = True
# print('put', val)
def remove(self, val):
val = str(val)
val = val.strip('\n').strip()
if val in self.db:
self.db.remove(val)
self.is_change = True
# print('remove', val)
def empty(self):
self.db = []
self.is_change = True
# print('empty')
def query(self, val):
val = str(val)
val = val.strip('\n').strip()
return val in self.db
def show(self):
print(self.db)
if __name__ == '__main__':
db = KeyDB('test.db')
db.initDB()
db.put('123123123')
db.put('asdasd')
db.put('')
db.put(-22)
print(db.query('123123123'))
print(db.query('123'))
db.show()
db.empty()
db.save()