2017-10-03 36 views
0

Je me demande comment utiliser une boucle imbriquée pour améliorer la lisibilité de cette fonction. Peut-être que je pourrais utiliser un nested for loop pour tags_files =?Comment utiliser une boucle imbriquée pour améliorer cette fonction?

def search_repotag_for_file(search_filepath, repo): 
    '''Goes through all tags, all files to find a github file entity 
     which matches the search_filepath we are looking for''' 
    all_tags = (tag for tag in repo.tags) 
    tags_files = ((tag, file_ent) for tag in all_tags for file_ent in tag.commit.tree.traverse()) 
    matches = (tup for tup in tags_files if tup[1].path == search_filepath) 
    return matches 

Répondre

5

Permettez-moi Ressaisissez votre fonction pour vous:

def search_repotag_for_file(search_filepath, repo): 
    for tag in repo.tags: 
     for file_ent in tag.commit.tree.traverse(): 
      if file_ent.path == search_filepath: 
       yield (tag, file_ent) 

Le mot-clé que vous recherchez est yield

+0

Wow! C'est parfait. Je suis venu près de cette réponse, mais pas exactement. Merci! :RÉ – PandasRocks