Ajout d'un paramètre de tour pour importer des données

This commit is contained in:
2024-07-19 22:10:20 +02:00
parent 645951823a
commit 2908f94b4a
4 changed files with 122 additions and 57 deletions

View File

@ -9,7 +9,7 @@ from nupes.models import BureauVote
from nupes.models.legislatives2024 import *
def importer_resultats_bv(engine: Engine, verbose: bool = False) -> None:
def importer_resultats_bv(engine: Engine, data_round: int = 0, verbose: bool = False) -> None:
tours = [(1, "https://www.data.gouv.fr/fr/datasets/r/6813fb28-7ec0-42ff-a528-2bc3d82d7dcd"),
(2, "https://www.data.gouv.fr/fr/datasets/r/ca974f04-cfd9-4da8-8554-4a868a09c6c2")]
@ -20,6 +20,10 @@ def importer_resultats_bv(engine: Engine, verbose: bool = False) -> None:
for bv in session.execute(select(BureauVote)).scalars().all()}
for tour, file_url in tours:
if data_round > 0 and tour != data_round:
# On saute ce tour
continue
file = get_file(file_url, f"resultats-legislatives-2024-t{tour}-par-bureau-de-vote.csv")
with file.open('r') as f:
@ -150,34 +154,42 @@ def importer_resultats_bv(engine: Engine, verbose: bool = False) -> None:
session.commit()
def importer_resultats_commune(engine: Engine, verbose: bool = False) -> None:
def importer_resultats_commune(engine: Engine, data_round: int = 0, verbose: bool = False) -> None:
tours = [(1, "https://www.data.gouv.fr/fr/datasets/r/bd32fcd3-53df-47ac-bf1d-8d8003fe23a1"),
(2, "https://www.data.gouv.fr/fr/datasets/r/5a8088fd-8168-402a-9f40-c48daab88cd1")]
with Session(engine) as session:
for resultats_commune in session.execute(select(ResultatsCommuneLegislatives2024)).scalars().all():
resultats_commune.inscrits_t1 = 0
resultats_commune.votants_t1 = 0
resultats_commune.abstentions_t1 = 0
resultats_commune.exprimes_t1 = 0
resultats_commune.blancs_t1 = 0
resultats_commune.nuls_t1 = 0
resultats_commune.inscrits_t2 = 0
resultats_commune.votants_t2 = 0
resultats_commune.abstentions_t2 = 0
resultats_commune.exprimes_t2 = 0
resultats_commune.blancs_t2 = 0
resultats_commune.nuls_t2 = 0
if data_round == 0 or data_round == 1:
resultats_commune.inscrits_t1 = 0
resultats_commune.votants_t1 = 0
resultats_commune.abstentions_t1 = 0
resultats_commune.exprimes_t1 = 0
resultats_commune.blancs_t1 = 0
resultats_commune.nuls_t1 = 0
if data_round == 0 or data_round == 2:
resultats_commune.inscrits_t2 = 0
resultats_commune.votants_t2 = 0
resultats_commune.abstentions_t2 = 0
resultats_commune.exprimes_t2 = 0
resultats_commune.blancs_t2 = 0
resultats_commune.nuls_t2 = 0
for voix_nuance in resultats_commune.voix:
voix_nuance.voix_t1 = 0
voix_nuance.voix_t2 = 0
if data_round == 0 or data_round == 1:
voix_nuance.voix_t1 = 0
if data_round == 0 or data_round == 2:
voix_nuance.voix_t2 = 0
session.commit()
com_codes = {commune.code_insee for commune in session.execute(select(Commune)).scalars().all()}
for tour, file_url in tours:
if data_round > 0 and tour != data_round:
# On saute ce tour
continue
file = get_file(file_url,
f"resultats-legislatives-2024-t{tour}-par-commune.csv")
@ -285,11 +297,15 @@ def importer_resultats_commune(engine: Engine, verbose: bool = False) -> None:
session.commit()
def importer_resultats_circo(engine: Engine, verbose: bool = False) -> None:
def importer_resultats_circo(engine: Engine, data_round: int = 0, verbose: bool = False) -> None:
tours = [(1, "https://www.data.gouv.fr/fr/datasets/r/5163f2e3-1362-4c35-89a0-1934bb74f2d9"),
(2, "https://www.data.gouv.fr/fr/datasets/r/41ed46cd-77c2-4ecc-b8eb-374aa953ca39")]
for tour, file_url in tours:
if data_round > 0 and tour != data_round:
# On saute ce tour
continue
file = get_file(file_url, f"resultats-legislatives-2024-t{tour}-par-circonscription.csv")
with file.open('r') as f:
@ -388,11 +404,15 @@ def importer_resultats_circo(engine: Engine, verbose: bool = False) -> None:
session.commit()
def importer_resultats_departement(engine: Engine, verbose: bool = False) -> None:
def importer_resultats_departement(engine: Engine, data_round: int = 0, verbose: bool = False) -> None:
tours = [(1, "https://www.data.gouv.fr/fr/datasets/r/78c708c5-5bc5-438d-8379-f432beae3f2b"),
(2, "https://www.data.gouv.fr/fr/datasets/r/8d4a6927-c96f-4cf5-b757-ea745eca26bd")]
for tour, file_url in tours:
if data_round > 0 and tour != data_round:
# On saute ce tour
continue
file = get_file(file_url, f"resultats-legislatives-2024-t{tour}-par-departement.csv")
with file.open('r') as f:
@ -476,11 +496,15 @@ def importer_resultats_departement(engine: Engine, verbose: bool = False) -> Non
session.commit()
def importer_resultats_region(engine: Engine, verbose: bool = False) -> None:
def importer_resultats_region(engine: Engine, data_round: int = 0, verbose: bool = False) -> None:
tours = [(1, "https://www.data.gouv.fr/fr/datasets/r/f69ffab7-fe37-494e-ad6d-a7cfc80ddc1f"),
(2, "https://www.data.gouv.fr/fr/datasets/r/ac4e272a-3ce2-4f20-941e-fb0ded444a5d")]
for tour, file_url in tours:
if data_round > 0 and tour != data_round:
# On saute ce tour
continue
file = get_file(file_url, f"resultats-legislatives-2024-t{tour}-par-region.csv")
with file.open('r') as f:
@ -548,11 +572,15 @@ def importer_resultats_region(engine: Engine, verbose: bool = False) -> None:
session.commit()
def importer_resultats_france(engine: Engine, verbose: bool = False) -> None:
def importer_resultats_france(engine: Engine, data_round: int = 0, verbose: bool = False) -> None:
# Erreur avec les données France Entière du 1er tour, qui sera j'espère corrigée un jour
tours = [# (1, "https://www.data.gouv.fr/fr/datasets/r/386fd5ac-e7f1-4e0f-8929-12d2c5391081"),
(2, "https://www.data.gouv.fr/fr/datasets/r/f64d2781-4ffa-414b-8f6d-e9d849f2f5e7")]
for tour, file_url in tours:
if data_round > 0 and tour != data_round:
# On saute ce tour
continue
file = get_file(file_url, f"resultats-legislatives-2024-t{tour}-france-entiere.csv")
with file.open('r') as f:
@ -607,10 +635,10 @@ def importer_resultats_france(engine: Engine, verbose: bool = False) -> None:
session.commit()
def run(engine: Engine, verbose: bool = False) -> None:
importer_resultats_france(engine, verbose)
importer_resultats_region(engine, verbose)
importer_resultats_departement(engine, verbose)
importer_resultats_circo(engine, verbose)
importer_resultats_commune(engine, verbose)
importer_resultats_bv(engine, verbose)
def run(engine: Engine, data_round: int = 1, verbose: bool = False) -> None:
importer_resultats_france(engine, data_round, verbose)
importer_resultats_region(engine, data_round, verbose)
importer_resultats_departement(engine, data_round, verbose)
importer_resultats_circo(engine, data_round, verbose)
importer_resultats_commune(engine, data_round, verbose)
importer_resultats_bv(engine, data_round, verbose)