-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
68 lines (55 loc) · 1.6 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
import os
import json
import requests
import pandas as pd
_dir_pkg_root = os.path.dirname(__file__)
def load_secrets():
fn_secrets = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"fantrax.secrets",
)
with open(fn_secrets, "r") as fsecrets:
secrets = json.load(fsecrets)
return secrets
def dump_to_json(fn_json, data):
with open(fn_json, "w") as f_json:
json.dump(data, f_json, indent=4)
def rest_request(
url,
body,
note="",
resp_format="json",
):
if resp_format == "json":
headers = {"Content-Type": "application/json"}
elif resp_format == "csv":
headers = {"Content-Type": "application/csv"}
response = requests.post(
url,
data=json.dumps(body),
headers=headers,
)
print(f"{note}status code:", response.status_code)
if resp_format == "json":
return response.json()
elif resp_format == "csv":
return response.text
else:
return response
def load_playerIDMap():
fn_IDmap = os.path.join(_dir_pkg_root, "data", "PLAYERIDMAP.csv")
print("loading player ID map... ", end="", flush=True)
df_datamap = pd.read_csv(
fn_IDmap,
dtype={
"FANTRAXID": str,
"MLBID": str,
"BPID": str,
}
)
df_datamap.FANTRAXID = [str(x).strip("*") for x in df_datamap.FANTRAXID]
# df_datamap["FANTRAXID"] = df_datamap.FANTRAXID.astype(str)
# df_datamap["MLBID"] = df_datamap.MLBID.astype(str)
# df_datamap["BPID"] = df_datamap.BPID.astype(str)
print("done.")
return df_datamap