forked from Unstructured-IO/unstructured
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
46 lines (37 loc) · 1.42 KB
/
utils.py
File metadata and controls
46 lines (37 loc) · 1.42 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
41
42
43
44
45
46
import importlib
import json
from functools import wraps
from typing import Dict, List, Optional, Union
def save_as_jsonl(data: List[Dict], filename: str) -> None:
with open(filename, "w+") as output_file:
output_file.writelines(json.dumps(datum) + "\n" for datum in data)
def read_from_jsonl(filename: str) -> List[Dict]:
with open(filename) as input_file:
return [json.loads(line) for line in input_file]
def requires_dependencies(
dependencies: Union[str, List[str]],
extras: Optional[str] = None,
):
if isinstance(dependencies, str):
dependencies = [dependencies]
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
missing_deps = []
for dep in dependencies:
try:
importlib.import_module(dep)
except ImportError:
missing_deps.append(dep)
if len(missing_deps) > 0:
raise ImportError(
f"Following dependencies are missing: {', '.join(missing_deps)}. "
+ (
f"Please install them using `pip install unstructured[{extras}]`."
if extras
else f"Please install them using `pip install {' '.join(missing_deps)}`."
),
)
return func(*args, **kwargs)
return wrapper
return decorator