-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdata_loading_helpers.py
More file actions
36 lines (29 loc) · 1.05 KB
/
data_loading_helpers.py
File metadata and controls
36 lines (29 loc) · 1.05 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
from __future__ import annotations
import os
from pathlib import Path
from typing import Optional, Union
import requests
def get_url_text(
url: Union[str, tuple[str, str]],
data_path: Optional[Path],
encoding: Optional[str] = None,
) -> str:
"""
If data_path is not None, returns the file from that path
(assuming it's stored there with the same filename as in the URL).
Otherwise, downloads it from the web.
:param url: If url is a str, assumes that the path in the local case is
just data_path + the "basename" of the url.
If url is a tuple of the form (web_prefix, common_path), then the
path in the local case is assuemd to be `common_path`
"""
if isinstance(url, tuple):
web_prefix, common_path = url
else:
web_prefix, common_path = os.path.split(url)
if data_path is not None:
path = Path(data_path, common_path)
return path.read_text(encoding=encoding)
else:
web_url = os.path.join(web_prefix, common_path)
return requests.get(web_url).text