-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile_dict.py
85 lines (73 loc) · 2.3 KB
/
file_dict.py
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
# Persistent file dictionary
# Purpose: local KV-store
import config
import json
import os.path
from random import randrange
import datetime
from pprint import pprint
class FileDictionary:
def __init__(self, id):
if not os.path.exists(config.file_dict_dir):
os.makedirs(config.file_dict_dir)
self.filename = str(id) + '.json'
self.filepath = os.path.join(config.file_dict_dir, self.filename)
self.data = self.load() if os.path.isfile(self.filepath) else {}
def __contains__(self, key):
return key in self.data
def __getitem__(self, key):
return self.get(key)
def put(self, item):
"""
The structure of item:
item = {
'messageId': messageId,
'key': key,
'value': value,
'serverId': serverId,
'timeStamp': timestamp
}
:param item: a dictionary with "key", "val", "serverId", "time"
:return: None
"""
self.data[item["key"]] = item
def get(self, key):
"""
Get the key-item pair
:param key: key value
:return: a dictionary with 'key', 'val', 'time'
"""
return self.data[key]
def dump(self):
with open(self.filepath, 'w') as f:
json_str = json.dumps(self.data)
json.dump(json_str, f)
def load(self):
with open(self.filepath, 'r') as f:
json_str = json.load(f)
data = json.loads(json_str)
return data
if __name__ == "__main__":
num_records = 10
# generate a random list of timestamps
def random_date(start, l):
current = start
while l >= 0:
curr = current + datetime.timedelta(minutes=randrange(60))
yield curr
l -= 1
startDate = datetime.datetime(2018, 2, 10, 13, 00)
timestamp_list = random_date(startDate, num_records)
# Test
fileDict = FileDictionary(1)
for key, timestamp in zip(range(num_records), timestamp_list):
item = {}
item['key'] = key
item['value'] = key+1
item['timeStamp'] = timestamp.strftime("%m/%d/%y %H:%M")
item['serverId'] = 1
fileDict.put(item)
print(fileDict.get(0))
fileDict.dump()
data = fileDict.load()
pprint(data)