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.legislatives2022 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/a1f73b85-8194-44f4-a2b7-c343edb47d32"),
(2, "https://www.data.gouv.fr/fr/datasets/r/96ffddda-59b4-41b8-a6a3-dfe1adb7fa36")]
@ -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-2022-t{tour}-par-bureau-de-vote.csv")
with file.open('r', encoding="ISO-8859-1") as f:
@ -166,32 +170,40 @@ 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/a9a82bcc-304e-491f-a4f0-c06575113745"),
(2, "https://www.data.gouv.fr/fr/datasets/r/e79b2e51-0841-4266-b626-57cb271c7a35")]
with Session(engine) as session:
for resultats_commune in session.execute(select(ResultatsCommuneLegislatives2022)).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()
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-2022-t{tour}-par-commune.csv")
@ -310,11 +322,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/114b13e8-7ff9-437f-9ec8-7a29258a80e3"),
(2, "https://www.data.gouv.fr/fr/datasets/r/436b9eaa-64e1-46bd-ad1d-9638ebbd6caf")]
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-2022-t{tour}-par-circonscription.csv")
with file.open('r', encoding="ISO-8859-1") as f:
@ -428,11 +444,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/4acba699-e512-4dc5-87c7-ba74db773633"),
(2, "https://www.data.gouv.fr/fr/datasets/r/bc76bd72-cf65-45b2-b651-7716dc3c5520")]
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-2022-t{tour}-par-departement.csv")
with file.open('r', encoding="ISO-8859-1") as f:
@ -531,11 +551,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/f79576c9-9ddc-43f9-8d99-f0856550054e"),
(2, "https://www.data.gouv.fr/fr/datasets/r/fdba421a-d4e9-4f38-9753-982a7c065911")]
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-2022-t{tour}-par-region.csv")
with file.open('r', encoding="ISO-8859-1") as f:
@ -600,10 +624,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:
tours = [(1, "https://www.data.gouv.fr/fr/datasets/r/cbe72f2c-2c56-4251-89cb-213de8bfbea0"),
(2, "https://www.data.gouv.fr/fr/datasets/r/6639bb39-13ec-49dd-ab4d-1f8227b86c01")]
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-2022-t{tour}-france-entiere.csv")
with file.open('r', encoding="ISO-8859-1") as f:
@ -658,10 +687,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 = 0, 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)