-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
96 lines (75 loc) · 2.65 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import json
import logging
import smtplib
import ssl
import subprocess
import sys
from pathlib import Path
def get_git_revision_hash() -> str:
return subprocess.check_output(["git", "rev-parse", "HEAD"]).decode("ascii").strip()
def get_git_revision_short_hash() -> str:
return (
subprocess.check_output(["git", "rev-parse", "--short", "HEAD"])
.decode("ascii")
.strip()
)
def get_logger(log_type, market, config):
logger = logging.getLogger(log_type)
logger.setLevel(logging.DEBUG)
log_pth = Path(config["log_pth"])
if not log_pth.exists():
log_pth.mkdir(parents=True)
file_handler = logging.FileHandler(log_pth / f"{log_type}.log")
formatter = logging.Formatter(
"%(asctime)s : %(levelname)s : %(name)s : %(message)s"
)
file_handler.setFormatter(formatter)
# add file handler to logger
logger.addHandler(file_handler)
logger.addHandler(logging.StreamHandler(sys.stdout))
return logger
def send_email(subject, text):
return
port = 587 # For starttls
smtp_server = "smtp.gmail.com"
sender_email = "overfitting24@gmail.com"
receiver_email = "overfitting24@gmail.com"
message = f"Subject: {subject} \n \n {text}"
context = ssl.create_default_context()
with smtplib.SMTP(smtp_server, port) as server:
server.ehlo() # Can be omitted
server.starttls(context=context)
server.ehlo() # Can be omitted
server.login(sender_email, "endlichreich")
server.sendmail(sender_email, receiver_email, message)
def save_config(config: dict, save_path: Path = None) -> None:
if save_path is None:
save_path = Path(config["log_pth"])
if not save_path.exists():
save_path.mkdir(parents=True)
with open(save_path / "config.json", "w+") as fout:
json.dump(config, fout, indent=2)
def read_credentials() -> dict:
cred_path = Path("credentials.json")
if not cred_path.exists():
print(f"WARN: credentials file {str(cred_path)} doesn't exist.")
return {}
with open(cred_path, "r") as fin:
creds = json.load(fin)
return creds
def str2bool(s: str) -> bool:
if s.lower() == "true":
return True
elif s.lower() == "false":
return False
else:
raise RuntimeError("Boolean value expected")
def truncate2(f: float, n: int = 8) -> float:
"""Truncates/pads a float f to n decimal places without rounding
https://stackoverflow.com/a/783927
https://stackoverflow.com/a/22155830"""
s = f"{f}"
if "e" in s or "E" in s:
return "{0:.{1}f}".format(f, n)
i, p, d = s.partition(".")
return ".".join([i, (d + "0" * n)[:n]])