-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.py
More file actions
38 lines (28 loc) · 1.24 KB
/
Copy pathhandler.py
File metadata and controls
38 lines (28 loc) · 1.24 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
import json
from typing import Any, Dict, List, Union
def load_json(file_path: str) -> Dict[str, Any]:
"""Load a JSON file and return its content as a dictionary."""
with open(file_path, 'r') as file:
return json.load(file)
def save_json(data: Dict[str, Any], file_path: str) -> None:
"""Save a dictionary as a JSON file."""
with open(file_path, 'w') as file:
json.dump(data, file, indent=4)
def flatten_dict(data: Dict[str, Any], parent_key: str = '', sep: str = '.') -> Dict[str, Any]:
"""Flatten a nested dictionary."""
items = []
for k, v in data.items():
new_key = f'{parent_key}{sep}{k}' if parent_key else k
if isinstance(v, dict):
items.extend(flatten_dict(v, new_key, sep=sep).items())
else:
items.append((new_key, v))
return dict(items)
def merge_dicts(dict1: Dict[str, Any], dict2: Dict[str, Any]) -> Dict[str, Any]:
"""Merge two dictionaries, with dict2 values overriding dict1."""
result = dict1.copy()
result.update(dict2)
return result
def filter_list(data: List[Union[str, int]], filter_func: Any) -> List[Union[str, int]]:
"""Filter a list based on a provided function."""
return [item for item in data if filter_func(item)]