-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettingFileManager.py
69 lines (54 loc) · 2.04 KB
/
SettingFileManager.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
import json
exampleFactors = {"WheelSpeedFactor": 2.0,
"SuspensionFactor": 1.0, "TemperatureFactor": 1.0}
# with open("data/settings.json", "r+") as file:
# json.dump(exampleFactors, file)
class FileManager():
def __init__(self, fileTrack):
self.fileTrack = fileTrack
def save_setting_file(self, settings):
try:
with open(self.fileTrack, "r+", encoding="UTF-8") as file:
json.dump(settings, file)
except TypeError:
print("only string")
except json.decoder.JSONDecodeError:
print("wrong file format")
def change_setting_factor(self, key, factor):
with open(self.fileTrack, "r+", encoding="UTF-8") as file:
try:
settings = json.load(file)
settings[key] = factor
file.seek(0)
print(settings)
json.dump(settings, file)
file.truncate()
except (ValueError, TypeError):
print("wrong type")
def load_setting_factor(self, key):
with open(self.fileTrack, "r+", encoding="UTF-8") as file:
settings = json.load(file)
return settings[key]
def load_setting_file(self):
try:
with open(self.fileTrack, encoding="UTF-8") as file:
return json.load(file)
except TypeError:
print("only string")
return 0
def get_settings_keys(self):
settings = self.load_setting_file()
return settings.keys()
def get_settings_factor(self, key):
settings = self.load_setting_file()
return settings.get(key)
def get_all_factors(self):
settings = self.load_setting_file()
factorList = []
for settingNumber in settings:
factorList.append(round(float(settings[settingNumber]), 3))
return factorList
fm = FileManager(
"C:\\programing\\workspace\\workspacePython\\Data\\settings.json")
fm.change_setting_factor("WheelSpeedFactor", 5.0)
print(fm.get_all_factors())