I am new and try to learn class method in python. I have two dictionaries as follows:
d = {
"day_rate":None,
"hourly_rate": 50,
"Comp_ID":[482,483],
"availability":"",
"page": 1
}
ans = {
"hourly_rate": "50",
"day_rate":None,
"available": "Available Immediately",
"competencies": "485,483,488"
}
Now I want to insert this two dictionaries in python class and want to use their keys in class function. I tried in simple way like I just insert dict and try to use directly. My approach as follows:
class employer_search(object):
def __init__(self,ans,d):
self.ans = ans
self.d = d
print(ans)
print(d)
def Competency_scoring(self):
if self.ans['competencies'] is not None:
Job_comp = []
for i in (self.ans['competencies'].split(",")):
Job_comp.append(int(i))
h = []
for j in self.d['Comp_ID']:
for i in Job_comp:
if j == i:
a = i
h.append(a)
matched_comp = []
final_result = []
q = [[x,h.count(x)] for x in set(h)]
print(len(q))
if len(q) == 1:
comp_score = 0.25
elif len(q) == 2:
comp_score = 0.50
elif len(q) == 3:
comp_score = 0.75
elif len(q) >= 4:
comp_Score = 1.0
return(comp_score)
else:
return(0)
then I did
es = employer_search(d,ans)
es.Competency_scoring()
it gives me keyerror of dict key. Error is follows:
KeyError: 'competencies'
I tried to correct key name but it gives me error continuesly. Can anyone please help me how to use dictionary keys in Python class?
Thanks in advance.