Skip to content

Instantly share code, notes, and snippets.

@alairock
Last active August 3, 2018 17:06
Show Gist options
  • Save alairock/5a3377048a890374e23f3aa5fba4cba8 to your computer and use it in GitHub Desktop.
Save alairock/5a3377048a890374e23f3aa5fba4cba8 to your computer and use it in GitHub Desktop.
Generators
import time
# Everyone in queue, and their desired order
def get_customers():
return [('Bob', 'Ham Sandwich'), ('Paul', 'Grilled Cheese'), ('Sally', 'Chicken Croissant')]
customers = get_customers()
# make the sandwiches for all the customers, then deliver
def make_sandwiches():
completed_sandwiches = []
for customer in customers:
time.sleep(1) # making the sandwich for our custoemr
completed_sandwiches.append((customer[0], customer[1]))
return completed_sandwiches
sandwiches = make_sandwiches()
# deliver the sandwiches
for sandwich in sandwiches:
print('Delivering sandwich:', sandwich[1], 'for', sandwich[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment