简体   繁体   中英

How can I write the code in such a way that multiple functions get created but I don't have to write the same code again and again?

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

You can use 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

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)

I would go ahead and say that you don't even need the 3 functions at all.

You are performing 2 basic operations:

  • creating a keyword_string for a specific document name;
  • getting the final result, for both the user_skill_string and the above keyword_string .

Well, It seems you already have the second function get_result coded somewhere. You just need one for get_keyword_string .

def get_keyword_string(document_name): 
    keyword = firestore.client().collection('keyword').document(document_name).get().to_dict()['key']
    return ' '.join(str(e) for e in engineering_keyword)

That is it!

Now, in the call site, where you wanted to invoke get_civilservice_result , you just need to do this instead:

get_result(user_skill_string, get_keyword_string('civilservice'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM