|
from pathlib import Path |
|
import json |
|
from datetime import datetime |
|
from tqdm import tqdm |
|
|
|
|
|
usuarios_path = 'usuarios' |
|
usuarios_macros_difference_path = 'usuarios_macros_difference' |
|
|
|
|
|
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 |
|
|
|
|
|
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 |
|
|
|
|
|
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 |
|
|
|
|
|
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 |
|
|
|
|
|
def add_real_macros_difference(data, keys_dates): |
|
|
|
number_of_keys_dates = len(keys_dates) |
|
|
|
|
|
for i in range(1,number_of_keys_dates): |
|
|
|
previous_macros = data[keys_dates[i-1]]['macros'] |
|
previous_macros = get_macros_from_string(previous_macros) |
|
if previous_macros is None: |
|
return data |
|
|
|
|
|
current_macros = data[keys_dates[i]]['macros'] |
|
current_macros = get_macros_from_string(current_macros) |
|
if current_macros is None: |
|
return data |
|
|
|
|
|
diferencia_macros = [current_macros[i] - previous_macros[i] for i in range(8)] |
|
diferencia_macros = ' '.join(str(diferencia) for diferencia in diferencia_macros) |
|
|
|
|
|
data[keys_dates[i]]['diferencia_macros'] = diferencia_macros |
|
return data |
|
|
|
if __name__ == '__main__': |
|
|
|
files = Path(usuarios_path).glob('*.json') |
|
|
|
|
|
files = sorted(files, key=lambda x: x.name) |
|
|
|
|
|
for file in tqdm(files): |
|
|
|
with open(file, 'r') as f: |
|
data = json.load(f) |
|
|
|
|
|
data_sorted = {} |
|
|
|
|
|
keys_dates = get_sorted_date_keys(data) |
|
keys_non_dates = get_non_date_keys(data) |
|
|
|
|
|
for key_date in keys_dates: |
|
data[key_date] = add_empty_macros_difference(data[key_date]) |
|
|
|
|
|
data = add_real_macros_difference(data, keys_dates) |
|
|
|
|
|
sorted_keys = keys_non_dates + keys_dates |
|
|
|
|
|
for key in sorted_keys: |
|
data_sorted[key] = data[key] |
|
|
|
|
|
file_path = Path(usuarios_macros_difference_path) / file.name |
|
with open(file_path, 'w') as f: |
|
json.dump(data_sorted, f, indent=4) |
|
|