-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy path_utils.py
64 lines (51 loc) · 1.63 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
import datetime as dt
from pandas import to_datetime
import requests
from pandas_datareader.compat import is_number
class SymbolWarning(UserWarning):
pass
class RemoteDataError(IOError):
pass
def _sanitize_dates(start, end):
"""
Return (timestamp_start, timestamp_end) tuple
if start is None - default is 5 years before the current date
if end is None - default is today
Parameters
----------
start : str, int, date, datetime, Timestamp
Desired start date
end : str, int, date, datetime, Timestamp
Desired end date
"""
if is_number(start):
# regard int as year
start = dt.datetime(start, 1, 1)
start = to_datetime(start)
if is_number(end):
end = dt.datetime(end, 1, 1)
end = to_datetime(end)
if start is None:
# default to 5 years before today
today = dt.date.today()
start = today - dt.timedelta(days=365 * 5)
if end is None:
# default to today
end = dt.date.today()
try:
start = to_datetime(start)
end = to_datetime(end)
except (TypeError, ValueError):
raise ValueError("Invalid date format.")
if start > end:
raise ValueError("start must be an earlier date than end")
return start, end
def _init_session(session):
if session is None:
session = requests.Session()
session.headers.update({'User-Agent': 'Firefox'})
# do not set requests max_retries here to support arbitrary pause
else:
if not isinstance(session, requests.Session):
raise TypeError("session must be a request.Session")
return session