PandasRocks
PandasRocks

Reputation: 1697

How can I use a nested for loop to improve this function?

I am wondering how I should use a nested for loop to improve the readability of this function. Perhaps I could use a nested for loop for tags_files =?

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

Upvotes: 0

Views: 53

Answers (1)

Guillaume
Guillaume

Reputation: 6009

Let me rewrite your function for you:

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)

The keyword you are looking for is yield

Upvotes: 5

Related Questions