-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
67 lines (60 loc) · 1.85 KB
/
utils.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python
import json
import logging
import sys
# import warnings; warnings.filterwarnings("ignore")
from pathlib import Path
from typing import List, NoReturn, Optional
import numpy as np
import requests
from requests.adapters import HTTPAdapter
from urllib3.util import Retry
def get_logger() -> logging.Logger:
"""
Get a logger
"""
logger = logging.getLogger(__name__)
if (logger.hasHandlers()):
logger.handlers.clear()
logger.propagate = False
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter(
fmt="%(asctime)s %(levelname)s: %(message)s", datefmt="%Y-%m-%d - %H:%M:%S"
)
console = logging.StreamHandler(sys.stdout)
console.setLevel(logging.INFO)
console.setFormatter(formatter)
logger.addHandler(console)
return logger
def get_http(retries: int=10) -> requests.Session:
"""
Create a HTTP/S session with a Retry adapter
"""
adapter = HTTPAdapter(max_retries=Retry(
total=retries,
))
http = requests.Session()
http.mount("https://", adapter)
http.mount("http://", adapter)
return http
def save_vector(
vector: np.ndarray,
vector_dest: Path,
vector_format: str,
record: Optional[dict]=None,
keys: Optional[List[str]]=None,
) -> NoReturn:
"""
Save a vector in either binary (npy) or plain text (vct) format in
vector_dest. If format is json, the record keys will also be saved
"""
if keys is None:
keys = ('id', 'filename', 'access', 'title', 'creators', 'subject')
if vector_format == "npy":
np.save(vector_dest, vector)
elif vector_format == "json":
vector_data = {key: record[key] for key in keys if key in record}
vector_data["vector"] = vector.tolist()
vector_dest.write_text(json.dumps(vector_data))
else:
np.savetxt(vector_dest, vector)