Skip to content

Instantly share code, notes, and snippets.

@Psykar
Created March 23, 2021 08:55
Show Gist options
  • Save Psykar/21f4f0f24f9f7f5fc65beef0c6982694 to your computer and use it in GitHub Desktop.
Save Psykar/21f4f0f24f9f7f5fc65beef0c6982694 to your computer and use it in GitHub Desktop.
Pick two recipes at random from a weighted csv
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