This repository has been archived by the owner on Apr 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
data_migrator.py
127 lines (98 loc) · 3.97 KB
/
data_migrator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
"""
MIT License
Copyright (c) 2022 UnB
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 os
import sqlite3
import pandas
def format_number(value: str) -> int:
return int(value.replace(",", ""))
def format_percentage(value: str) -> float:
return float(value[:-1])
def initialize_database() -> None:
"""
Esta função executa a criação e migração dos dados para o banco de
dados. Ela deve ser executada apenas uma vez.
"""
columns = [
"year",
"population",
"yearly_change_percentage",
"yearly_change",
"net_migrants",
"median_age",
"fertility_rate",
"population_density",
"urban_population_percentage",
"urban_population",
"relative_population_percentage",
"relative_population",
"rank",
"region",
]
encoding = "unicode_escape"
filename = "data.db"
# Caso já exista um arquivo `data.db`, será excluido.
if os.path.isfile(filename):
os.remove(filename)
conn = sqlite3.connect(filename)
cursor = conn.cursor()
# Fazer formatação de strings dentro de uma query SQL é uma má
# prática pois é inseguro (permite SQL Injection). Como neste caso
# é um projeto acadêmico simples e vai ser executado em um ambiente
# controlado, não há problemas. Para mais informações, veja:
# <https://en.wikipedia.org/wiki/SQL_injection>
cursor.execute(f"CREATE TABLE IF NOT EXISTS data ({', '.join(columns)})")
for file in os.listdir("data/"):
filename, ext = os.path.splitext(file)
if ext != ".csv":
continue
path = f"data/{file}"
csv_df = pandas.read_csv(path, encoding=encoding) # type: ignore
rows: list[list[int | float]] = []
for row in csv_df.values.tolist(): # type: ignore
# Alguns valores precisam ser tratados antes de serem inseridos
# no banco de dados.
row[1] = format_number(row[1])
row[2] = format_percentage(row[2])
row[3] = format_number(row[3])
row[4] = format_number(row[4])
row[8] = format_percentage(row[8])
row[9] = format_number(row[9])
row[10] = format_percentage(row[10])
row[11] = format_number(row[11])
row.append(filename)
rows.append(row)
# Aqui também há SQL Injection, mas pelo mesmo motivo que anterior,
# não haverá problemas aqui.
sql = f"INSERT INTO data VALUES ({', '.join(['?'] * len(columns))})"
cursor.executemany(sql, rows)
conn.commit()
cursor.close()
conn.close()
if __name__ == "__main__":
initialize_database()
else:
# Caso o módulo seja importado, o banco de dados já deve ter sido
# inicializado. Então, basta carregar os dados para um `DataFrame`.
conn = sqlite3.connect("data.db")
cursor = conn.cursor()
sql = "SELECT * FROM data"
df = pandas.read_sql(sql, conn) # type: ignore
cursor.close()
conn.close()