-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.py
More file actions
40 lines (30 loc) · 1.38 KB
/
Copy pathcore.py
File metadata and controls
40 lines (30 loc) · 1.38 KB
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
import json
from typing import Any, Dict, List, Union
def load_json(file_path: str) -> Union[Dict[str, Any], List[Any]]:
"""Load JSON data from a file."""
with open(file_path, 'r') as file:
return json.load(file)
def save_json(file_path: str, data: Union[Dict[str, Any], List[Any]]) -> None:
"""Save data to a JSON file."""
with open(file_path, 'w') as file:
json.dump(data, file, indent=4)
def merge_dicts(dict1: Dict[str, Any], dict2: Dict[str, Any]) -> Dict[str, Any]:
"""Merge two dictionaries recursively."""
merged = dict1.copy() # Start with dict1's keys and values
for key, value in dict2.items():
if key in merged and isinstance(merged[key], dict) and isinstance(value, dict):
merged[key] = merge_dicts(merged[key], value) # Recursive merge
else:
merged[key] = value # If not a dict, overwrite value
return merged
def flatten_list(nested_list: List[List[Any]]) -> List[Any]:
"""Flatten a nested list into a single list."""
return [item for sublist in nested_list for item in sublist]
# Example usage
if __name__ == '__main__':
data1 = load_json('data1.json')
data2 = load_json('data2.json')
merged_data = merge_dicts(data1, data2)
save_json('merged_data.json', merged_data)
flattened = flatten_list([[1, 2], [3, 4], [5]])
print(flattened) # Output: [1, 2, 3, 4, 5]