-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonSim.py
More file actions
81 lines (62 loc) · 1.98 KB
/
JsonSim.py
File metadata and controls
81 lines (62 loc) · 1.98 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Feb 15 13:38:43 2023
@author: ygaillard
"""
import os
import json
import re
import numpy as np
class NpEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.integer):
return int(obj)
if isinstance(obj, np.floating):
return float(obj)
if isinstance(obj, np.ndarray):
return obj.tolist()
return super(NpEncoder, self).default(obj)
class JsonSim:
siminformation={}
fileExisted=False
overAllFileName='siminformation.json'
def __init__(self,fileName='siminformation.json',simName='',access='r'):
self.overAllFileName=fileName
self.access=access
if 'w' in access:
print('Open writable')
else:
print('Read only mode')
try:
f = open(self.overAllFileName)
# returns JSON object as
# a dictionary
self.siminformation = json.load(f)
# Closing file
f.close()
self.fileExisted=True
except:
print("No file found")
self.siminformation={}
path=os.getcwd()
if simName=='':
simName=re.sub('/.*/', '', path,flags=re.DOTALL)
self.siminformation['name']=simName
def __del__(self):
if 'w' in self.access:
jsonFile=json.dumps(self.siminformation,cls=NpEncoder)
f = open(self.overAllFileName,'w')
f.write(jsonFile)
f.close()
else:
print('Read only!')
def write(self,dictName,value):
self.siminformation[dictName]=value
def delete(self,dictName):
self.siminformation.pop(dictName)
def exist(self,dictName):
if dictName in self.siminformation.keys():
return True
else:
return False