0

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.

2 Answers 2

3

You are passing function argument in wrong order:

Here,

es = employer_search(d,ans)

should be

es = employer_search(ans,d)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. It resolved. Appreciate you answer
1

The issue you are getting is KeyError which is raised when a mapping (dictionary) key is not found in the set of existing keys. In your case the competencies key is not in dict d.

So, you'll need to change your class call from:

es = employer_search(d,ans)

To:

es = employer_search(ans,d)

Side note:

  • You can make __init__ call mandatory using named parameter by writing it as:
    def __init__(self,*,ans,d) and then call it as es = employer_search(ans=ans, d=d)
  • Also, if you are using Python 3 you can simply declare it as class EmployerSearch:. (CamelCase)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.