I have all these functions doing a similar task. How can I write the code in such a way that all these functions get created but I don't have to write the same code again and again?
def get_civilservice_result(user_skill_string):
civilservice_keyword = firestore.client().collection('keyword').document('civilservice').get().to_dict()['key']
civilservice_keyword_string = ' '.join(str(e) for e in civilservice_keyword)
result = get_result(user_skill_string, civilservice_keyword_string)
return result
def get_education_result(user_skill_string):
education_keyword = firestore.client().collection('keyword').document('education').get().to_dict()['key']
education_keyword_string = ' '.join(str(e) for e in education_keyword)
result = get_result(user_skill_string, education_keyword_string)
return result
def get_engineering_result(user_skill_string):
engineering_keyword = firestore.client().collection('keyword').document('engineering').get().to_dict()['key']
engineering_keyword_string = ' '.join(str(e) for e in engineering_keyword)
result = get_result(user_skill_string, engineering_keyword_string)
return result
CodePudding user response:
You can use a more input variables to change what the function does based on its inputs. Like this:
def get_result_(user_skill_string, document_type: str):
engineering_keyword = firestore.client().collection('keyword').document(document_type).get().to_dict()['key']
engineering_keyword_string = ' '.join(str(e) for e in engineering_keyword)
result = get_result(user_skill_string, engineering_keyword_string)
return result
CodePudding user response:
I would do a loop for the list of keywords you are using:
def get_skill_result(user_skill_string, skill_field):
for skill in skill_field:
skill_keyword = firestore.client().collection('keyword').document(skill).get().to_dict()['key']
skill_keyword_string = ' '.join(str(e) for e in skill_keyword)
result.append(get_result(user_skill_string, skill_keyword_string))
return result
fields = ["civilservice","education","engineering"]
data = get_skill_result(user_skill_string, fields)