2 minutes
Python: Blind poule generator
What is it?
A blind poule is a list of randomly generated soccer score outcomes. The script I made favours the lower numbers first (because this is more likely to happen) but it can generate scores where one team scores 6 or 7 goals.
Requirements
- weasyprint
- numpy
- jinja2
Weasyprint can be installed using your distro’s package manager. The remaining Python modules can be installed using pip:
pip install numpy jinja2
The script
import random
import numpy as np
import datetime
from jinja2 import Template
# Set
generated_scores = set()
# Generate random scores
def generate_score():
global generated_scores
while True:
# Generate random scores favoring towards lower values
team1_score = max(0, int(np.random.normal(2, 1.5)))
team2_score = max(0, int(np.random.normal(2, 1.5)))
score = f"{team1_score}-{team2_score}"
if score not in generated_scores:
generated_scores.add(score)
return score
# Reset the set
def reset_generated_scores():
global generated_scores
generated_scores = set()
# Render the Jinja2 template
def render_template(template_path, num_scores):
with open(template_path, 'r') as f:
template_content = f.read()
template = Template(template_content)
rendered_content = template.render(
generate_score=generate_score,
num_scores=num_scores,
)
return rendered_content
# Generate and save the template
def generate_markdown_file(template_path, output_path, num_scores):
reset_generated_scores() # Reset!
rendered_content = render_template(template_path, num_scores)
with open(output_path, 'w') as f:
f.write(rendered_content)
# Call it
generate_markdown_file("pool-template.html", "pool.html", 50)
Jinja2 template for scores (save as pool-template.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Blinde voetbalpoule</title>
<style>
@page {
size: A4;
margin: 0;
}
table {
width: 100%;
font-size: 9px; /* Smaller font size */
}
th, td {
padding: 2.2px; /* Smaller padding */
}
</style>
</head>
<body>
<h3 style="text-align: center;">Blinde voetbalpoule</h3>
<h5 style="text-align:center;">____________ tegen ____________ </h5>
<h6>Inleg: €1 per nummer, prijs bij goede uitslag: €25</h6>
<table>
<thead>
<tr>
<th style="width: 5%;">Nr.</th>
<th style="width: 70%;">Naam deelnemer:</th>
<th style="width: 20%;"></th>
<th style="text-align:center; width: 30%;">Score</th>
</tr>
</thead>
<tbody>
{% for x in range(1, num_scores + 1) %}
<tr>
<td style="border: 1px solid black;">{{ x }}</td>
<td style="border: 1px solid black;"></td>
<td style="width: 20%;"></td>
<td style="text-align:center;border: 1px solid black;">{{ generate_score() }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>
Automating it and mailing the PDF’s
To make it easy I made a simple Bash script which runs the script and mails it to me as a attachment.
#!/bin/bash
TO_EMAIL=nobody@example.com
python3 main.py
weasyprint pool.html voetbalpoule.pdf
echo "Voetbalpoule" | mutt -s "Voetbalpoule $(date +'%d-%m')" -a "voetbalpoule.pdf" -- $TO_EMAIL