Skip to content
Snippets Groups Projects
Commit 7cd7b017 authored by aldo.gonzalez-lorenzo's avatar aldo.gonzalez-lorenzo
Browse files

Ajouter la table avec les sportifs

parent 8e0fd1da
No related branches found
No related tags found
No related merge requests found
"""
Script pour générer le fichier SQL avec les données des JO de Paris 2024
"""
import json import json
import random
labels = { datasets = [
{
'filename': 'paris-2024-results-medals-oly-eng.json',
'url': 'https://data.education.gouv.fr/explore/dataset/paris-2024-results-medals-oly-eng/',
'table': 'medals',
'labels': {
'name': {'comment': "Nom de l'athlète", 'type': 'varchar(100)'}, 'name': {'comment': "Nom de l'athlète", 'type': 'varchar(100)'},
'gender': {'comment': "Genre de l'athlète", 'type': 'varchar(1)'}, 'gender': {'comment': "Genre de l'athlète", 'type': 'varchar(1)'},
'discipline': {'comment': "Discipline", 'type': 'varchar(100)'}, 'discipline': {'comment': "Discipline", 'type': 'varchar(100)'},
'code_discipline': {'comment': "Code de la discipline", 'type': 'varchar(4)'}, 'code_discipline': {'comment': "Code de la discipline", 'type': 'varchar(4)'},
'event': {'comment': "Sport", 'type': 'varchar(100)'}, 'event': {'comment': "Compétition", 'type': 'varchar(100)'},
'country': {'comment': "Pays", 'type': 'varchar(100)'}, 'country': {'comment': "Pays", 'type': 'varchar(100)'},
'country_code': {'comment': "Code du pays", 'type': 'varchar(4)'}, 'country_code': {'comment': "Code du pays", 'type': 'varchar(4)'},
'medal_type': {'comment': "Type de la médaille", 'type': 'varchar(20)'}, 'medal_type': {'comment': "Type de la médaille", 'type': 'varchar(20)'},
'medal_code': {'comment': "Code de la médaille", 'type': 'int(1)'}, 'medal_code': {'comment': "Code de la médaille", 'type': 'int(1)'},
'medal_date': {'comment': "Date de la médaille", 'type': 'varchar(10)'}, 'medal_date': {'comment': "Date de la médaille", 'type': 'varchar(10)'},
# 'url_event': {'comment': "URL des résultats", 'type': 'varchar(100)'}, 'url_event': {'comment': "URL des résultats", 'type': 'varchar(100)'},
} }
},
{
'filename': 'paris-2024-liste-athletes-engages-olypara.json',
'url': 'https://data.education.gouv.fr/explore/dataset/paris-2024-liste-athletes-engages-olypara/',
'table': 'athletes',
'labels': {
'preferred_given_name': {'comment': "Prénom", 'type': 'varchar(100)'},
'preferred_family_name': {'comment': "Nom", 'type': 'varchar(100)'},
'nationality': {'comment': "Nationalité", 'type': 'varchar(4)'},
'national_olympic_committee': {'comment': "Comité Olympique National", 'type': 'varchar(4)'},
'date_of_birth': {'comment': "Date de naissance", 'type': 'varchar(10)'},
'gender': {'comment': "Genre", 'type': 'varchar(1)'},
'discipline': {'comment': "Discipline", 'type': 'varchar(4)'},
'event': {'comment': "Compétition", 'type': 'varchar(100)'},
}
},
]
beginning = """SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; beginning = """SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
...@@ -31,50 +56,51 @@ ending = """ ...@@ -31,50 +56,51 @@ ending = """
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;""" /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;"""
insert_into = 'INSERT INTO `joparis2024` (`id`, ' + ', '.join(f'`{k}`' for k in labels) + ') VALUES\n'
def make_table(dataset):
lines = []
lines.append(f"\nDROP TABLE IF EXISTS `{dataset['table']}`;\n")
lines.append(f"CREATE TABLE IF NOT EXISTS `{dataset['table']}` (\n")
lines.append(" `id` int(11) NOT NULL AUTO_INCREMENT,\n")
for k, v in dataset['labels'].items():
lines.append(f' `{k}` {v["type"]} NOT NULL COMMENT "{v["comment"]}",\n')
lines.append(" PRIMARY KEY (`id`)")
lines.append("\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;\n\n")
return lines
def make_row(medal, i) -> str: def make_row(row, labels, i) -> str:
"""Faire une ligne de code SQL""" """Faire une ligne de code SQL"""
values = [i] values = [i]
for key in labels: for key in labels:
if 'varchar' in labels[key]['type']: if 'varchar' in labels[key]['type']:
if key in medal and medal[key] is not None: if key in row and row[key] is not None:
string = medal[key].replace('"', '') string = row[key].replace('"', '')
value = f'"{string}"' value = f'"{string}"'
else: else:
value = '""' value = '""'
else: else:
value = medal[key] if key in medal else 0 value = row[key] if key in row else 0
values.append(value) values.append(value)
return '(' + ', '.join(f'{v}' for v in values) + ')' return '(' + ', '.join(f'{v}' for v in values) + ')'
# return f'({i}, "{fields["acad_mies"]}", "{fields["acc_ab"]}", "{fields["acc_aca_orig"]}", "{fields["ville_etab"]}", "{fields["lib_for_voe_ins"]}"),\n'
if __name__ == "__main__": if __name__ == "__main__":
with open('data/paris-2024-results-medals-oly-eng.json', 'r') as f:
data = json.load(f)
lines = [beginning] lines = [beginning]
lines.append("DROP TABLE IF EXISTS `joparis2024`;\n") for dataset in datasets:
lines.append("CREATE TABLE IF NOT EXISTS `joparis2024` (\n") insert_into = f"INSERT INTO `{dataset['table']}` (`id`, " + ', '.join(f'`{k}`' for k in dataset['labels']) + ") VALUES\n"
lines.append(" `id` int(11) NOT NULL AUTO_INCREMENT,\n") lines += make_table(dataset)
for k, v in labels.items(): with open(dataset['filename'], 'r') as f:
lines.append(f' `{k}` {v["type"]} NOT NULL COMMENT "{v["comment"]}",\n') data = json.load(f)
lines.append(" PRIMARY KEY (`id`)")
lines.append("\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;\n\n")
lines.append(insert_into) lines.append(insert_into)
for i, datum in enumerate(data): for i, datum in enumerate(data):
if i % 100 == 99: if i % 100 == 99:
lines.append(make_row(datum, i+1) + ";\n") lines.append(make_row(datum, dataset['labels'], i+1) + ";\n")
lines.append(insert_into) lines.append(insert_into)
elif i == len(data) - 1: elif i == len(data) - 1:
lines.append(make_row(datum, i+1) + ";\n") lines.append(make_row(datum, dataset['labels'], i+1) + ";\n")
else: else:
lines.append(make_row(datum, i+1) + ",\n") lines.append(make_row(datum, dataset['labels'], i+1) + ",\n")
lines.append(ending) lines.append(ending)
with open('sae303.sql', 'w') as writer: with open('sae303.sql', 'w') as writer:
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment