-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjson_form_handler.py
68 lines (59 loc) · 2.14 KB
/
json_form_handler.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
import json
class JSONFormHandler:
def __init__(self):
self.databaseDictionary = "org_database.json"
self.sampleInput = "sample_input.json"
def getKeyFromDictionary(self, orgDict):
return orgDict["name"] + " " + orgDict["location"]
def getKeyFromJSON(self, path):
dictionary = self.getDictFromJSON(path)
return self.getKeyFromDictionary(dictionary)
def addOrg(self, path):
jDict = self.getDictFromJSON(path)
database = self.getDictFromJSON(self.databaseDictionary)
try:
temp = database[self.getKeyFromDictionary(jDict)]
return False
except:
database[self.getKeyFromDictionary(jDict)] = jDict
with open(self.databaseDictionary, 'w') as fp:
json.dump(database, fp)
return True
def addOrgByDict(self, dict):
jDict = dict
database = self.getDictFromJSON(self.databaseDictionary)
try:
temp = database[self.getKeyFromDictionary(jDict)]
return False
except:
database[self.getKeyFromDictionary(jDict)] = jDict
with open(self.databaseDictionary, 'w') as fp:
json.dump(database, fp)
return True
def getOrg(self, key):
database = self.getDictFromJSON(self.databaseDictionary)
try:
return database[key]
except:
return "Org not found!"
def removeOrg(self, key):
database = self.getDictFromJSON(self.databaseDictionary)
try:
org = database[key]
del database[key]
with open(self.databaseDictionary, 'w') as fp:
json.dump(database, fp)
return org
except:
return None
def getDictFromJSON(self, path):
return json.load(open(path))
def findByType(self, ty):
dictionary = self.getDictFromJSON(self.databaseDictionary)
values = dictionary.values()
validOrgs = []
for value in values:
types = value["types"]
if ty in types:
validOrgs.append(value["name"]);
return validOrgs