File size: 4,482 Bytes
478fd8c |
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 128 129 130 131 132 |
from pathlib import Path
import json
from datetime import datetime
from tqdm import tqdm
# Paths
usuarios_path = 'usuarios'
usuarios_macros_difference_path = 'usuarios_macros_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
# Get macros from string data
def get_macros_from_string(data):
macros = data.split(' ')
if len(macros) != 8:
return None
for i, macro in enumerate(macros):
if '.' in macro:
macro = macro.split('.')[0]
macros[i] = macro
if macro == '':
return None
macros = [int(macro) for macro in macros]
is_all_numbers = all((type(macro)==int or type(macro)==float) for macro in macros)
if not is_all_numbers:
return None
return macros
# Add empty weight difference to all the dates
def add_empty_macros_difference(data):
new_data = {}
new_data['macros'] = data
new_data['diferencia_macros'] = '0 0 0 0 0 0 0 0'
return new_data
# Add real macros difference to all the dates
def add_real_macros_difference(data, keys_dates):
# Get the number of keys dates
number_of_keys_dates = len(keys_dates)
# Iterate over the keys dates
for i in range(1,number_of_keys_dates):
# Get the previous macros
previous_macros = data[keys_dates[i-1]]['macros']
previous_macros = get_macros_from_string(previous_macros)
if previous_macros is None:
return data
# Get the current macros
current_macros = data[keys_dates[i]]['macros']
current_macros = get_macros_from_string(current_macros)
if current_macros is None:
return data
# Calculate the difference
diferencia_macros = [current_macros[i] - previous_macros[i] for i in range(8)]
diferencia_macros = ' '.join(str(diferencia) for diferencia in diferencia_macros)
# Add the difference to the data
data[keys_dates[i]]['diferencia_macros'] = diferencia_macros
return data
if __name__ == '__main__':
# Get all the files in the formularios_path
files = Path(usuarios_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_macros_difference(data[key_date])
# Add real weight difference to all the dates
data = add_real_macros_difference(data, keys_dates)
# Concatenate the non date keys and the date 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(usuarios_macros_difference_path) / file.name
with open(file_path, 'w') as f:
json.dump(data_sorted, f, indent=4)
|