Created
March 23, 2021 08:55
-
-
Save Psykar/21f4f0f24f9f7f5fc65beef0c6982694 to your computer and use it in GitHub Desktop.
Pick two recipes at random from a weighted csv
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
from random import choices | |
import csv | |
import string | |
def pick(): | |
with open("recipes.csv") as f: | |
r = csv.reader(f) | |
data = list(r) | |
recipes, weights = zip(*data) | |
weights = [int(x) for x in weights] | |
picks = choices(recipes, weights=weights, k=2) | |
return picks | |
def app(environ, start_response): | |
"""Simplest possible application object""" | |
picks = '<br/>'.join(pick()) | |
with open("template.html") as f: | |
template = f.read() | |
t = string.Template(template) | |
b = t.substitute(data=picks).encode() | |
status = '200 OK' | |
response_headers = [ | |
('Content-type', 'text/html'), | |
('Content-Length', str(len(b))) | |
] | |
start_response(status, response_headers) | |
return iter([b]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment