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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Collect the ratner_stock dataset.
See the README file for more information.
Author: G.J.J. van den Burg
License: This file is part of TCPD, see the top-level LICENSE file.
Copyright: 2019, The Alan Turing Institute
"""
import argparse
import clevercsv
import hashlib
import json
import os
import yfinance
import sys
import time
from functools import wraps
from urllib.error import URLError
MD5_CSV = "db7406dc7d4eb480d73b4fe6c4bb00be"
MD5_JSON = "f7086ff916f35b88463bf8fd1857815e"
SAMPLE = 3
NAME_CSV = "SIG.csv"
NAME_JSON = "ratner_stock.json"
class ValidationError(Exception):
def __init__(self, filename):
self.message = (
"Validating the file '%s' failed. \n"
"Please raise an issue on the GitHub page for this project \n"
"if the error persists." % filename
)
def check_md5sum(filename, checksum):
with open(filename, "rb") as fp:
data = fp.read()
h = hashlib.md5(data).hexdigest()
return h == checksum
def validate(checksum):
"""Decorator that validates the target file."""
def validate_decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
target = kwargs.get("target_path", None)
if os.path.exists(target) and check_md5sum(target, checksum):
return
out = func(*args, **kwargs)
if not os.path.exists(target):
raise FileNotFoundError("Target file expected at: %s" % target)
if not check_md5sum(target, checksum):
raise ValidationError(target)
return out
return wrapper
return validate_decorator
def write_csv(target_path=None):
count = 0
while count < 5:
count += 1
try:
sig = yfinance.download(
"SIG",
start="1988-07-14",
end="1995-08-23",
progress=False,
rounding=False,
)
sig.round(6).to_csv(target_path, float_format="%.6f")
return
except URLError as err:
print(
"Error occurred (%r) when trying to download csv. Retrying in 5 seconds"
% err,
sys.stderr,
)
time.sleep(5)
@validate(MD5_JSON)
def write_json(csv_path, target_path=None):
with open(csv_path, "r", newline="", encoding="ascii") as fp:
reader = clevercsv.reader(
fp, delimiter=",", quotechar="", escapechar=""
)
rows = list(reader)
header = rows.pop(0)
rows = [r for i, r in enumerate(rows) if i % SAMPLE == 0]
# take the first 600 rows
rows = rows[:600]
name = "ratner_stock"
longname = "Ratner Group Stock Price"
time = [r[0] for r in rows]
time_fmt = "%Y-%m-%d"
values = [float(r[4]) for r in rows]
series = [{"label": "Close Price", "type": "float", "raw": values}]
data = {
"name": name,
"longname": longname,
"n_obs": len(time),
"n_dim": len(series),
"time": {
"type": "string",
"format": time_fmt,
"index": list(range(len(time))),
"raw": time,
},
"series": series,
}
with open(target_path, "w") as fp:
json.dump(data, fp, indent="\t")
@validate(MD5_JSON)
def write_patch(source_path, target_path=None):
# This patches rounding differences that started to occur around Feb 2021.
from lzma import decompress
from base64 import b85decode
from diff_match_patch import diff_match_patch
dmp = diff_match_patch()
diff = decompress(b85decode(BLOB)).decode("utf-8")
with open(source_path, "r") as fp:
new_json = fp.read()
patches = dmp.patch_fromText(diff)
patched, _ = dmp.patch_apply(patches, new_json)
with open(target_path, "w") as fp:
fp.write(patched)
def collect(output_dir="."):
csv_path = os.path.join(output_dir, NAME_CSV)
json_path = os.path.join(output_dir, NAME_JSON)
write_csv(target_path=csv_path)
try:
write_json(csv_path, target_path=json_path)
need_patch = False
except ValidationError:
need_patch = True
if need_patch:
write_patch(json_path, target_path=json_path)
def clean(output_dir="."):
csv_path = os.path.join(output_dir, NAME_CSV)
json_path = os.path.join(output_dir, NAME_JSON)
if os.path.exists(csv_path):
os.unlink(csv_path)
if os.path.exists(json_path):
os.unlink(json_path)
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"-o", "--output-dir", help="output directory to use", default="."
)
parser.add_argument(
"action",
choices=["collect", "clean"],
help="Action to perform",
default="collect",
nargs="?",
)
return parser.parse_args()
def main(output_dir="."):
args = parse_args()
if args.action == "collect":
collect(output_dir=args.output_dir)
elif args.action == "clean":
clean(output_dir=args.output_dir)
BLOB = (
b"{Wp48S^xk9=GL@E0stWa8~^|S5YJf5-~z({om~JRV0>CMK>=)+?;9Q77%VlSe-n@RwTxDTAq"
b"Xux?siO{U6G@C3FaW~bN5Z*_f_oBtk6v71E|?<5o1eA9Ph0ws)8e&C5nX<N?S7g`v*e-B4M$"
b"xsUK<=t_0!jQw0{TE0!b-V#yoVUWo}I#>y<%CaK|DwqCb~NKalm6OSmI1f8nAh0~Q%o~@<b>"
b"vQLj7z)h-uL{Tu*hvOp00000nukx=XK5Ee00FrH#03BVY6KKKvBYQl0ssI200dcD"
)
if __name__ == "__main__":
main()
|