From 6826941c55800848395df94944f6695f25782fc9 Mon Sep 17 00:00:00 2001 From: aulojor Date: Tue, 11 Nov 2025 13:27:29 -0100 Subject: [PATCH] =?UTF-8?q?feat:=20cria=C3=A7=C3=A3o=20de=20entrada=20e=20?= =?UTF-8?q?balizas=20para=20linhas,=20adicionar=20tempo=20=C3=A0=20visuali?= =?UTF-8?q?za=C3=A7=C3=A3o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- earthquakes.py | 30 +++++++++++++++++++++++++++--- utils/crud.py | 42 ++++++++++++++++++++++++++++++++++++------ 2 files changed, 63 insertions(+), 9 deletions(-) diff --git a/earthquakes.py b/earthquakes.py index 8eeada4..1a00c7d 100644 --- a/earthquakes.py +++ b/earthquakes.py @@ -18,6 +18,7 @@ MENU ="""[1] Criar a base de dados [6] Guardar como JSON [7] Guardar como CSV [8] Estatísticas +[9] Criar uma entrada [Q] Sair """ @@ -128,12 +129,11 @@ def main(): row_choice = _get_usr_input("Escolhe a linha a apagar:", int) # TODO: balizar a escolha para apenas as linhas do evento em questao - db = crud.delete_table_row(db, eid_choice, row_choice) + db, msg = crud.delete_table_row(db, eid_choice, row_choice) new_table = crud.get_table(db, eid_choice) crud.show_table(new_table) - print(f"Linha {row_choice} apagada com sucesso!") + print(msg) input() - else: retInfo = "Base de dados não encontrada!" @@ -177,7 +177,31 @@ def main(): stats.stat_menu(db) else: retInfo = "Base de dados não encontrada!" + + case "9": + if db is not None: + crud.read_ids(db) + eid_choice = _get_usr_input("Escolhe o ID: ", int) + if not _event_exists(db, eid_choice): + retInfo = "ID do event não encontrado!" + + else: + os.system("cls") + table = crud.get_table(db, eid_choice) + _prettify_event(table) + crud.show_table(table) + + insertion_point = _get_usr_input("Posição da nova linha: ", int) + # TODO: balizar a escolha para apenas as linhas do evento em questao + + db, msg = crud.create_table_row(db, eid_choice, insertion_point) + new_table = crud.get_table(db, eid_choice) + crud.show_table(new_table) + print(msg) + input() + else: + retInfo = "Base de dados não encontrada!" case "q": isRunning = False continue diff --git a/utils/crud.py b/utils/crud.py index 9ba67c5..338c053 100644 --- a/utils/crud.py +++ b/utils/crud.py @@ -9,7 +9,7 @@ pd.set_option('display.width', 150) # -- globals HEADER_COLS = ["Data", "Distancia", "Tipo Ev", "Lat", "Long", "Prof", "Magnitudes"] -TABLE_READ_RET = ["Estacao","Componente","", "Amplitude"] +TABLE_READ_RET = ["Estacao", "Hora", "Min", "Seg", "Componente", "Amplitude"] # -- helper funcs @@ -95,8 +95,16 @@ def delete_event(df, event_id): def delete_table_row(df, event_id, row_number): # Apaga uma linha específica da tabela do evento + # Cria uma nova linha vazia no dataframe na posição insertion_point + matching_indices = df.index[df['ID'] == event_id].tolist() + + first_event_row = matching_indices[0] + last_event_row = matching_indices[-1] + + if row_number < first_event_row or row_number > last_event_row: + return df, f"Erro: A posição a apagar, {row_number} está fora do intervalo permitido para o evento {event_id}." new_df = df.drop([row_number]).reset_index(drop=True) - return new_df + return new_df, f"Linha {row_choice} apagada com sucesso!" def create_blank_event(df, event_id): @@ -113,7 +121,32 @@ def create_blank_event(df, event_id): return new_df -def create_table_row(df, event_id, row_number_1): +def create_table_row(df, event_id, insertion_point): + # Cria uma nova linha vazia no dataframe na posição insertion_point + matching_indices = df.index[df['ID'] == event_id].tolist() + + first_event_row = matching_indices[0] + last_event_row = matching_indices[-1] + + if insertion_point < first_event_row or insertion_point > last_event_row + 1: + return df, f"Erro: A posição de inserção {insertion_point} está fora do intervalo permitido para o evento {event_id}" + + new_row_df = pd.DataFrame(columns=df.columns, index=[0]) + new_row_df['ID'] = event_id + new_row_df = new_row_df.fillna(0) + new_row_df = new_row_df.astype(df.dtypes) + + df_before = df.iloc[:insertion_point] + df_after = df.iloc[insertion_point:] + + new_df = pd.concat([df_before, new_row_df, df_after], ignore_index=True) + + return new_df, f"Linha inserida com sucesso na posição {insertion_point}" + +def create_entire_database() -> pd.DataFrame: + pass + +def create_table_row_old(df, event_id, row_number_1): event_rows = df[df["ID"] == event_id] if event_rows.empty: return df, f"Erro: Evento com ID {event_id} não encontrado." @@ -136,6 +169,3 @@ def create_table_row(df, event_id, row_number_1): return new_df, f"Linha inserida com sucesso na posição {row_number_1} do evento {event_id}." -def create_entire_database() -> pd.DataFrame: - pass -