-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain_code.py
More file actions
108 lines (95 loc) · 3.11 KB
/
Copy pathMain_code.py
File metadata and controls
108 lines (95 loc) · 3.11 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# Darwin Wilmut J
# importing necessary modules
import json
import os
from sys import getsizeof
from datetime import datetime
# path defined by user or default:current working directory (If path not set by user)
def set_path(way=os.getcwd()):
global path
path = way
return
# Time to live (user defined)
def set_delay(time=360):
global delay
delay = time
return
# default Time to live
delay = 0
# must run main function inorder to create and initialize json file
def main_function():
try:
f = open(os.path.join(path, "sample.json"))
except FileNotFoundError:
f = open(os.path.join(path, "sample.json"),"w")
if os.stat("sample.json").st_size == 0:
data = {}
with open(os.path.join(path, "sample.json"), 'w') as outfile:
json.dump(data, outfile, indent=4)
# For Time to live
def time_stamp():
now = datetime.now()
timestamp = datetime.timestamp(now)
return timestamp
# Create
def create(key, value, seconds=0):
size = getsizeof(value)
len(key)
if size <= 16000 and key.isalpha() and len(key) <= 32:
f = open(os.path.join(path, "sample.json"),"r")
dict = json.load(f)
if bool(dict):
if key in dict:
return print('Data already Created')
else:
dict[key] = value,seconds
with open(os.path.join(path, "sample.json"),'w') as outfile:
json.dump(dict, outfile, indent=4)
return print('Data Created')
else:
dict[key] = value,seconds
with open(os.path.join(path, "sample.json"), "w") as outfile:
json.dump(dict, outfile, indent=4)
return print('Data Created')
else:
return print(
" File size must be within 16Kb \n Key must be String \n string length must be less than 32 characters")
# Read
def read(key):
f = open(os.path.join(path, "sample.json"))
dict = json.load(f)
if bool(dict):
if key in dict:
value = dict[key]
if value[-1] == 0:
value[-1] = time_stamp()
with open(os.path.join(path, "sample.json"), "w") as outfile:
json.dump(dict, outfile, indent=4)
output = {key:value}
return print(output)
else:
next_time = time_stamp()
elapsed = next_time - value[-1]
if elapsed <= delay:
output = {key: value}
return print(output)
else:
delete(key)
else:
return print('No data found')
else:
return print('Empty File')
# Delete
def delete(key):
f = open(os.path.join(path, "sample.json"))
dict = json.load(f)
if bool(dict):
if key in dict:
del dict[key]
with open(os.path.join(path, "sample.json"), "w") as outfile:
json.dump(dict, outfile, indent=4)
return print('Data Deleted')
else:
return print('Data Unavailable')
else:
return print('Empty File')