Last commit not found
from pathlib import Path | |
import json | |
from datetime import datetime | |
from tqdm import tqdm | |
# Paths | |
formularios_path = 'formularios' | |
formularios_weight_difference_path = 'formularios_weight_difference' | |
# Get sorted date keys | |
def get_sorted_date_keys(data): | |
keys = list(data.keys()) | |
keys_dates = [] | |
for key in keys: | |
try: | |
keys_dates.append(datetime.strptime(key, '%Y-%m-%d')) | |
except ValueError: | |
pass | |
keys_sorted = sorted(keys_dates) | |
keys_sorted = [key.strftime('%Y-%m-%d') for key in keys_sorted] | |
keys_sorted = [key.replace('-01', '-1') for key in keys_sorted] | |
keys_sorted = [key.replace('-02', '-2') for key in keys_sorted] | |
keys_sorted = [key.replace('-03', '-3') for key in keys_sorted] | |
keys_sorted = [key.replace('-04', '-4') for key in keys_sorted] | |
keys_sorted = [key.replace('-05', '-5') for key in keys_sorted] | |
keys_sorted = [key.replace('-06', '-6') for key in keys_sorted] | |
keys_sorted = [key.replace('-07', '-7') for key in keys_sorted] | |
keys_sorted = [key.replace('-08', '-8') for key in keys_sorted] | |
keys_sorted = [key.replace('-09', '-9') for key in keys_sorted] | |
return keys_sorted | |
# Get non date keys | |
def get_non_date_keys(data): | |
keys = list(data.keys()) | |
keys_non_dates = [] | |
for key in keys: | |
try: | |
datetime.strptime(key, '%Y-%m-%d') | |
except ValueError: | |
keys_non_dates.append(key) | |
return keys_non_dates | |
# Add empty weight difference to all the dates | |
def add_empty_weight_difference(data): | |
data['diferencia_peso'] = 'None' | |
return data | |
# Change esfuerzo para cumplir dieta | |
def change_esfuerzo_para_cumplir_dieta(data): | |
# Get the date keys | |
data_date_keys = list(get_sorted_date_keys(data)) | |
# Iterate over the date keys | |
for key in data_date_keys: | |
# If data hasn't 'esfuerzoParaCumplirDieta' key, create it | |
if 'esfuerzoParaCumplirDieta' not in data[key]: | |
data[key]['esfuerzoParaCumplirDieta'] = 'None | no data' | |
# If data has 'esfuerzoParaCumplirDieta' key, change it | |
if 'esfuerzoParaCumplirDieta' in data[key]: | |
if 'No entiendo la calculadora' in data[key]['esfuerzoParaCumplirDieta'] or 'Iba a coger menús tipo' in data[key]['esfuerzoParaCumplirDieta']: | |
data[key]['esfuerzoParaCumplirDieta'] += ' | No data' | |
elif 'Costó demasiado, súbeme macros' in data[key]['esfuerzoParaCumplirDieta']: | |
data[key]['esfuerzoParaCumplirDieta'] += ' | costo subir macros' | |
elif 'Costó demasiado, bájame macros' in data[key]['esfuerzoParaCumplirDieta']: | |
data[key]['esfuerzoParaCumplirDieta'] += ' | costo bajar macros' | |
elif 'Costó, pero me adapto a nuevos ajustes' in data[key]['esfuerzoParaCumplirDieta']: | |
data[key]['esfuerzoParaCumplirDieta'] += ' | costo y me adapto a nuevos ajustes' | |
elif 'No costó nada' in data[key]['esfuerzoParaCumplirDieta']: | |
data[key]['esfuerzoParaCumplirDieta'] += ' | no costo' | |
else: | |
data[key]['esfuerzoParaCumplirDieta'] += ' | no data' | |
return data | |
# Change cumplimiento dieta | |
def change_cumplimiento_dieta(data): | |
# Get the date keys | |
data_date_keys = list(get_sorted_date_keys(data)) | |
# Iterate over the date keys | |
for key in data_date_keys: | |
# If data hasn't 'cumplimientoDieta' key, create it | |
if 'cumplimientoDieta' not in data[key]: | |
data[key]['cumplimientoDieta'] = 'None | no data' | |
# If data has 'cumplimientoDieta' key, change it | |
if 'cumplimientoDieta' in data[key]: | |
if 'al 70%' in data[key]['cumplimientoDieta'] or 'casi perfecta' in data[key]['cumplimientoDieta'] or 'Perfecta' in data[key]['cumplimientoDieta']: | |
data[key]['cumplimientoDieta'] += ' | bien' | |
elif 'regular, me cuesta llegar' in data[key]['cumplimientoDieta'] or 'regular, me salto la dieta' in data[key]['cumplimientoDieta']: | |
data[key]['cumplimientoDieta'] += ' | regular' | |
elif 'Nada, mantén mis macros' in data[key]['cumplimientoDieta']: | |
data[key]['cumplimientoDieta'] += ' | mal' | |
else: | |
data[key]['cumplimientoDieta'] += ' | no data' | |
return data | |
# Change objetivo | |
def change_objetivo(data): | |
# Get the date keys | |
data_date_keys = list(get_sorted_date_keys(data)) | |
# Iterate over the date keys | |
for key in data_date_keys: | |
# If data hasn't 'objetivo' key, create it | |
if 'objetivo' not in data[key]: | |
data[key]['objetivo'] = 'None | no data' | |
# If data has 'objetivo' key, change it | |
if 'objetivo' in data[key]: | |
if 'definición (nada cambia)' in data[key]['objetivo'] or 'empezamos a definir (cambia)' in data[key]['objetivo'] or 'perder peso (nada cambia)' in data[key]['objetivo']: | |
data[key]['objetivo'] += ' | definicion' | |
elif 'volumen (nada cambia)' in data[key]['objetivo'] or 'empezamos a coger volumen (cambia)' in data[key]['objetivo'] or 'empezamos a coger volumen, sobre todo tren inferior (cambia)' in data[key]['objetivo'] or 'empezamos a coger volumen, en todo el cuerpo (cambia)' in data[key]['objetivo']: | |
data[key]['objetivo'] += ' | volumen' | |
else: | |
data[key]['objetivo'] += ' | no data' | |
return data | |
# Change cumplimientoEntrenamiento | |
def change_cumplimiento_entrenamiento(data): | |
# Get the date keys | |
data_date_keys = list(get_sorted_date_keys(data)) | |
# Iterate over the date keys | |
for key in data_date_keys: | |
# If data hasn't 'cumplimientoEntrenamiento' key, create it | |
if 'cumplimientoEntrenamiento' not in data[key]: | |
data[key]['cumplimientoEntrenamiento'] = 'None | no data' | |
# If data has 'cumplimientoEntrenamiento' key, change it | |
if 'cumplimientoEntrenamiento' in data[key]: | |
if 'Lo hice perfecto' in data[key]['cumplimientoEntrenamiento'] or 'He fallado algunos días, pero sí' in data[key]['cumplimientoEntrenamiento'] or 'Lo hice prácticamente perfecto' in data[key]['cumplimientoEntrenamiento']: | |
data[key]['cumplimientoEntrenamiento'] += ' | bien' | |
elif 'Lesión importante' in data[key]['cumplimientoEntrenamiento'] or 'Pequeña lesión' in data[key]['cumplimientoEntrenamiento'] or 'No hice nada, mantenemos la rutina un mes más' in data[key]['cumplimientoEntrenamiento'] or 'Alárgame la rutina una semana más' in data[key]['cumplimientoEntrenamiento']: | |
data[key]['cumplimientoEntrenamiento'] += ' | mal' | |
else: | |
data[key]['cumplimientoEntrenamiento'] += ' | no data' | |
return data | |
# Change compromiso | |
def change_compromiso(data): | |
# Get the date keys | |
data_date_keys = list(get_sorted_date_keys(data)) | |
# Iterate over the date keys | |
for key in data_date_keys: | |
# If data hasn't 'compromiso' key, create it | |
if 'compromiso' not in data[key]: | |
data[key]['compromiso'] = 'None | no data' | |
# If data has 'compromiso' key, change it | |
if 'compromiso' in data[key]: | |
if 'Bueno, pero mejorable' in data[key]['compromiso'] or 'Máximo' in data[key]['compromiso']: | |
data[key]['compromiso'] += ' | bueno' | |
elif 'Mal, pero a partir de ahora voy a por todas' in data[key]['compromiso'] or 'Mal, demasiado exigente' in data[key]['compromiso']: | |
data[key]['compromiso'] += ' | mal' | |
else: | |
data[key]['compromiso'] += ' | no data' | |
return data | |
# Add real weight difference to all the dates | |
def add_real_weight_difference(data, keys_dates): | |
number_of_keys_dates = len(keys_dates) | |
for i in range(1,number_of_keys_dates): | |
data[keys_dates[i]]['diferencia_peso'] = data[keys_dates[i]]['peso'] - data[keys_dates[i-1]]['peso'] | |
return data | |
if __name__ == '__main__': | |
# Get all the files in the formularios_path | |
files = Path(formularios_path).glob('*.json') | |
# Sort the files by name | |
# files = sorted(files, key=lambda x: x.name) | |
# Load all the files | |
for file in tqdm(files): | |
# Get data from the file | |
with open(file, 'r') as f: | |
data = json.load(f) | |
# Create new empty dictionary | |
data_sorted = {} | |
# Get the date and non date keys | |
keys_dates = get_sorted_date_keys(data) | |
keys_non_dates = get_non_date_keys(data) | |
# Add empty weight difference to all the dates | |
for key_date in keys_dates: | |
data[key_date] = add_empty_weight_difference(data[key_date]) | |
# Add real weight difference to all the dates | |
data = add_real_weight_difference(data, keys_dates) | |
# Change esfuerzo para cumplir dieta | |
data = change_esfuerzo_para_cumplir_dieta(data) | |
# Change cumplimiebto dieta | |
data = change_cumplimiento_dieta(data) | |
# Change objetivo | |
data = change_objetivo(data) | |
# Change cumplimientoEntrenamiento | |
data = change_cumplimiento_entrenamiento(data) | |
# Change compromiso | |
data = change_compromiso(data) | |
# Sort the keys | |
sorted_keys = keys_non_dates + keys_dates | |
# Add the sorted keys to the new dictionary | |
for key in sorted_keys: | |
data_sorted[key] = data[key] | |
# Save the data to the file | |
file_path = Path(formularios_weight_difference_path) / file.name | |
with open(file_path, 'w') as f: | |
json.dump(data_sorted, f, indent=4) |