Skip to content

Instantly share code, notes, and snippets.

@turicas
Last active October 31, 2025 16:55
Show Gist options
  • Save turicas/93f3ec8043a5f90e66228567aa28ad4c to your computer and use it in GitHub Desktop.
Save turicas/93f3ec8043a5f90e66228567aa28ad4c to your computer and use it in GitHub Desktop.
Script Python para fazer sorteios (específico para a PythonBrasil 2025)

sorteio.py

Simples script em Python para fazer sorteios em eventos. Lê os participantes de um CSV e salva o resultado em outro CSV.

Esse script será usado pela Pythonic Café para sortear 5 livros "Python Fluente" (2a edição), de Luciano Ramalho, na Python Brasil 2025.

ATENÇÃO: caso queira usar o script para outros sorteios, remova a seção "Filtra apenas pelos que não têm o livro" (depende que exista a coluna ja_possui_o_livro no CSV).

Forma de uso:

# Executará 3 sorteios para os registros contidos em `participantes.csv` e salvará em `resultado-sorteio.csv`
python sorteio.py participantes.csv 3 resultado-sorteio.csv
Copyright 2025 Álvaro Justen
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import argparse
import csv
import random
import re
import string
from unicodedata import normalize
from pathlib import Path
def slug(text):
"""
>>> slug(' Álvaro ')
'alvaro'
>>> slug('E-Mail')
'e_mail'
>>> slug(' ?#$ E-----Mail!!@?')
'e_mail'
"""
text = normalize("NFKD", text).encode("ascii", errors="ignore").decode("ascii").strip().lower()
allowed = string.ascii_letters + string.digits + "_"
escaped = "".join("_" if char not in allowed else char for char in text)
clean = re.sub("_+", "_", escaped)
if clean[0] == "_":
clean = clean[1:]
if clean[-1] == "_":
clean = clean[:-1]
return clean
def plural(n):
return 's' if n != 1 else ''
def _parser():
parser = argparse.ArgumentParser(
prog="sorteio",
description="Lê participantes de CSV e executa N sorteios, salvando o resultado",
)
parser.add_argument("csv_participantes", type=Path)
parser.add_argument("sorteios", type=int, help="Quantidade de sorteios a serem feitos")
parser.add_argument("csv_resultado", type=Path)
return parser
def main(args):
csv_participantes = args.csv_participantes
csv_resultado = args.csv_resultado
sorteios = args.sorteios
# Lê CSV de participantes
participantes = {}
with csv_participantes.open() as fobj:
reader = csv.reader(fobj)
# Cria um "slug" do cabeçalho
header = [slug(value.splitlines()[0]) for value in next(reader)]
max_column_size = max(len(key) for key in header)
if "id" in header:
parser.exit(
status=100,
message="O CSV não pode possuir uma coluna 'id', pois essa será criada para o sorteio.\n",
)
seq_id = 1
for values in reader:
row = {"id": seq_id, **dict(zip(header, values))}
participantes[row["id"]] = row
seq_id += 1
total_lidos = len(participantes)
print(f"Total de {total_lidos} registro{plural(total_lidos)} lido{plural(total_lidos)} do CSV.")
# Filtra apenas pelos que não têm o livro
participantes = {
id_: participante for id_, participante in participantes.items() if participante["ja_possui_o_livro"] == "Não"
}
total_depois = len(participantes)
if total_depois != total_lidos:
dif = total_lidos - total_depois
print(f"Removido{plural(dif)} {dif} participante{plural(dif)} que já {'têm' if dif != 1 else 'tem'} o livro.")
# Faz os sorteios
print("Pressione ENTER para continuar")
input()
print(
f"Iniciando {sorteios} sorteio{plural(sorteios)} "
f"para {total_depois} participante{plural(total_depois)}.\n"
)
with csv_resultado.open(mode="w") as fobj:
writer = None
for sorteio in range(1, sorteios + 1):
id_sorteado = random.choice(list(participantes.keys()))
participante = participantes.pop(id_sorteado)
resultado = {"sorteio": sorteio, **participante}
if writer is None:
writer = csv.DictWriter(fobj, fieldnames=list(resultado.keys()))
writer.writeheader()
writer.writerow(resultado)
print(f"- Sorteio {sorteio}:")
for key, value in participante.items():
if key in ("email", "e_mail", "email_address", "telefone", "telefone_com_ddd", "ja_possui_o_livro"):
# Evita mostrar dados sensíveis
continue
print(f" {key.rjust(max_column_size)}: {value}")
print()
if __name__ == "__main__":
import sys
parser = _parser()
args = parser.parse_args()
sys.exit(main(args))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment