-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNamedDict.py
136 lines (101 loc) · 3.66 KB
/
NamedDict.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from typing import Dict, Any
class NamedDict(object):
"""Simplify python dictionarys by allowance dot anotation of set and get.
# * Simple Usage *
>> myNamedDict = NamedDict()
>> myNamedDict.name = "Benny"
>> myNamedDict.age = 29
>> myNamedDict.extraInfo = NamedDict()
>> myNamedDict.extraInfo.totalYearsOfExperiance = 5
>> myNamedDict.name == "Benny"
>> True
>> myNamedDict.extraInfo.totalYearsOfExperiance == 5
>> True
>> myNamedDict.hasKey("totalYearsOfExperiance")
>> True
"""
def __init__(self, anyDict: Dict[Any, Any] = {}):
self.__dict__.update(**anyDict)
def __setattr__(self, attr, value):
try:
self.__dict__.__setattr__(attr, value)
except AttributeError:
self.__dict__[attr] = value
def __repr__(self):
return "{}({})".format(self.__class__.__name__, self.__dict__)
def __iter__(self):
for item, value in self.__dict__.items():
yield item, value
def union(self, other: Dict[Any, Any]) -> Dict:
for item, value in other:
self.__setattr__(item, value)
return NamedDict(self.__dict__)
def __eq__(self, other: Dict[Any, Any]) -> bool:
for i in zip(self.__dict__.items(), other.__dict__.items()):
if i[0] != i[1]:
return False
return True
def __len__(self):
return len(self.__dict__)
def hasKey(self, keyName):
if keyName in self.__dict__: return True
else: return False
def load(jsonFilePath: str) -> NamedDict:
newNamedDict = NamedDict()
with open(jsonFilePath, "rb") as jsReader:
import json
jsonDict = json.load(jsReader)
return buildNamedDict(jsonDict, newNamedDict)
def buildNamedDict(jsonDict: Dict[Any, Any], newNamedDict: NamedDict) -> NamedDict:
for item, itemValue in jsonDict.items():
if not isinstance(itemValue, dict):
newNamedDict.__setattr__(item, itemValue)
else:
newNamedDict.__setattr__(item, buildNamedDict(itemValue, newNamedDict))
return newNamedDict
if __name__ == '__main__':
demoDict = {
"name": "benny",
"age": 30,
"birthday": "1989",
"brothers": {
"itay": 21,
"adi": 16,
"vered": {
"age": 27,
"kids": {
"orian": 2,
"linoy": 0.5
}
}
}
}
def testBuildNamedDict():
newNamedDict = NamedDict()
convertedData = buildNamedDict(demoDict, newNamedDict)
assert convertedData.brothers.vered.age == 27
assert convertedData.brothers.vered.kids.orian == 2
def unionTest():
test = NamedDict({"benny": 10, "elgazar": 1, "age": 89})
test2 = NamedDict({"first": 5})
unified = test2.union(test)
assert NamedDict({'first': 5, 'benny': 10, 'elgazar': 1, 'age': 89}) == unified
def simpleCallTest():
test = NamedDict()
test.name = "Benny"
test.age = 15
assert test.age == 15 and test.name == "Benny"
def checkHasKey():
newNamedDict = NamedDict()
convertedData = buildNamedDict(demoDict, newNamedDict)
assert convertedData.brothers.hasKey("vered") == True
def nestedCalls():
test = NamedDict()
test.properties = NamedDict()
test.properties.firstName = "benny"
assert test.properties.firstName == "benny"
checkHasKey()
testBuildNamedDict()
simpleCallTest()
unionTest()
nestedCalls()