-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.py
More file actions
33 lines (23 loc) · 1.09 KB
/
Copy pathcore.py
File metadata and controls
33 lines (23 loc) · 1.09 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
import json
from typing import Any, Dict, List
def load_json(file_path: str) -> Dict[str, Any]:
"""Load a JSON file and return its contents as a dictionary."""
with open(file_path, 'r', encoding='utf-8') as file:
return json.load(file)
def save_json(file_path: str, data: Dict[str, Any]) -> None:
"""Save a dictionary as a JSON file."""
with open(file_path, 'w', encoding='utf-8') as file:
json.dump(data, file, ensure_ascii=False, indent=4)
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]
def merge_dictionaries(dicts: List[Dict[str, Any]]) -> Dict[str, Any]:
"""Merge multiple dictionaries into one, with later values overwriting earlier ones."""
result = {}
for d in dicts:
result.update(d)
return result
def get_unique_elements(seq: List[Any]) -> List[Any]:
"""Return a list of unique elements, preserving the original order."""
seen = set()
return [x for x in seq if not (x in seen or seen.add(x))]