-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
60 lines (49 loc) · 1.61 KB
/
database.py
File metadata and controls
60 lines (49 loc) · 1.61 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
import json
import json_io
def squeeze(text:str) -> str:
if text[-1] == " ":
text = squeeze(text[:-1])
return text
class Database:
fname = '.datadescription'
fname_base = '.database'
metadata = {
'path': '',
'from': '',
'date': '',
'associate': '',
'description': ''
}
@staticmethod
def _search(data: list, key: str, value) -> list:
ret = []
for d in data:
if value in d[key]: ret.append(d)
return ret
def search(self, addr, search_list):
'''
search_list: list of (key, value)
'''
addr = squeeze(addr)
data = json_io.read_json(addr + '/' + self.fname_base)
for key, value in search_list:
data = self._search(data, key, value)
return data
def create_base(self, root_addr):
root_addr = squeeze(root_addr)
files = []
files = json_io.recursive_search(root_addr, self.fname, files)
json_data = []
for file in files:
json_data.append(json_io.read_json(file))
with open(root_addr + '/' + self.fname_base,'w+') as f:
json.dump(json_data, f, indent=4)
def create(self, values, addr):
addr = squeeze(addr)
for ((key, _), value) in zip(self.metadata.items(), values):
self.metadata[key] = value
with open(addr + '/' + self.fname,'w+') as f:
json.dump(self.metadata, f, indent=4)
if __name__ == "__main__":
db = Database()
db.create_base(r'C:\Users\chu-ping.yu\OneDrive - Thermo Fisher Scientific\data')