CodeSOD: Up the Garden Path

This post was originally published on this site

The Daily WTF

Sam worked on an application which needed to parse files out of a directory. There was a ticket to change the search path for those files to search an additional directory. That didn’t turn out to be terribly hard, but the existing code raised some serious eyebrows.

def get_info_for_identifier(base_name: str, identifier: str) -> dict: … all_ids = os.listdir(base_name) … results = {} for i in all_ids: results.update(parse_identifier(base_name, i)) … return results[identifier]

This is the first method we’re going to look at. You can see that it starts by listing a directory, finding every file in it. Then, we iterate across all of those files, parsing them and adding them to a dictionary. We then return… just one key in that dictionary. The one for the one file we actually cared about when we called this method. So it parses every file, to return the results for one file.

And

To read the full article click on the 'post' link at the top.