Spaces:
Sleeping
Sleeping
File size: 2,160 Bytes
5b6c556 |
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 |
import json
import sys
import os
def merge_json_results(base_file, new_file):
# Merges the 'analyses' from a new results file into a base file.
try:
# Ensure the results directory exists.
results_dir = "circuit_analysis/results"
if not os.path.exists(results_dir):
os.makedirs(results_dir)
base_path = os.path.join(results_dir, base_file)
new_path = os.path.join(results_dir, new_file)
# Load the base file, or create a new one if it doesn't exist.
if os.path.exists(base_path):
with open(base_path, 'r') as f:
base_data = json.load(f)
else:
print(f"Base file '{base_file}' not found. Creating a new one.")
base_data = {"analyses": {}}
# Load the new results file.
with open(new_path, 'r') as f:
new_data = json.load(f)
# Ensure both files have the 'analyses' key.
if 'analyses' not in base_data or 'analyses' not in new_data:
print("Error: Both files must contain an 'analyses' key.")
return
# Update the analyses from the base file.
base_data['analyses'].update(new_data['analyses'])
# Update the timestamp and config from the new file.
base_data['timestamp'] = new_data.get('timestamp', base_data.get('timestamp'))
base_data['config'] = new_data.get('config', base_data.get('config'))
# Write the merged data back to the base file.
with open(base_path, 'w') as f:
json.dump(base_data, f, indent=2)
print(f"Successfully merged '{new_file}' into '{base_file}'.")
except FileNotFoundError as e:
print(f"Error: File not found - {e.filename}")
except json.JSONDecodeError:
print("Error: Invalid JSON format in one of the files.")
except Exception as e:
print(f"An unexpected error occurred: {e}")
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: python merge_circuit_results.py <base_json_file> <new_json_file>")
else:
merge_json_results(sys.argv[1], sys.argv[2]) |