Created
April 7, 2015 20:39
-
-
Save MarcelCutts/2a26916a6e3bb77ab296 to your computer and use it in GitHub Desktop.
Difference in readability of dict comprehension vs more verbose by simpler loops
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_time_to_work(property, house_member): | |
""" | |
Calculates time taken to get to work for a particular | |
house member if they were to live at a particular property | |
:param property: Property to travel to and from | |
:param house_member: House member details | |
:return: A series of commute times | |
""" | |
commute_times = {} | |
for travel_mode in house_member['allowed_travel_methods']: | |
travel_parameters = { | |
'origin': property['displayable_address'], | |
'destination': house_member['commute_destination'], | |
'mode': travel_mode | |
} | |
commute_times[travel_mode] = query_google_distance_matrix(travel_parameters) | |
# Shorthand method of stating the above, but not as readable | |
commute_times_2 = {travel_mode: query_google_distance_matrix({ | |
'origin': property['displayable_address'], | |
'destination': house_member['commute_destination'], | |
'mode': travel_mode | |
}) | |
for travel_mode | |
in house_member['allowed_travel_methods']} | |
return commute_times |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment