-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathconcurrentpandas.py
284 lines (241 loc) · 10.6 KB
/
concurrentpandas.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
__author__ = 'Brian M Wilcox'
__version__ = '0.1.3'
"""
Copyright 2014 Brian M Wilcox
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import quandl
import collections
import time
import sys
import pandas_datareader
from pandas_datareader import Options
import multiprocessing
from multiprocessing import Process, Manager
from multiprocessing.pool import ThreadPool
from random import randrange
def data_worker(**kwargs):
"""
Function to be spawned concurrently,
consume data keys from input queue, and push the resulting dataframes to output map
"""
if kwargs is not None:
if "function" in kwargs:
function = kwargs["function"]
else:
Exception("Invalid arguments, no function specified")
if "input" in kwargs:
input_queue = kwargs["input"]
else:
Exception("Invalid Arguments, no input queue")
if "output" in kwargs:
output_map = kwargs["output"]
else:
Exception("Invalid Arguments, no output map")
if "token" in kwargs:
argsdict = {"quandl_token": kwargs["token"]}
else:
if "Quandl" in function.__module__:
Exception("Invalid Arguments, no Quandl token")
if ("source" and "begin" and "end") in kwargs:
argsdict = {"data_source": kwargs["source"], "begin": kwargs["begin"], "end": kwargs["end"]}
else:
if "pandas_datareader" in function.__module__:
Exception("Invalid Arguments, no pandas data source specified")
if ("source" in kwargs) and (("begin" and "end") not in kwargs):
argsdict = {"data_source": kwargs["source"]}
else:
if "pandas_datareader" in function.__module__:
Exception("Invalid Arguments, no pandas data source specified")
else:
Exception("Invalid Arguments")
retries = 5
while not input_queue.empty():
data_key = input_queue.get()
get_data(function, data_key, output_map, retries, argsdict)
def get_data(data_get, data_key, output_map, retries_left, argdict):
"""
Function to use Python Pandas and / or Quandl to download a dataframe
Insert resulting dataframe into output map
"""
if retries_left <= 0:
print(data_key + " Failed to download.")
return
"""
Identify type of function to use, insert result into output map
"""
if "Quandl" in data_get.__module__:
output_map[data_key] = data_get(data_key, authtoken=argdict["quandl_token"])
return
if "pandas_datareader" in data_get.__module__:
# Verify we are not dealing with options
if 'get_call_data' not in dir(data_get):
if ("source" and "begin" and "end") in argdict:
try:
output_map[data_key] = data_get(data_key, argdict["data_source"], argdict["begin"], argdict["end"])
return
except:
print(data_key + " failed to download. Retrying up to " + retries_left.__str__() + " more times...")
else:
try:
output_map[data_key] = data_get(data_key, argdict["data_source"])
return
except:
print(data_key + " failed to download. Retrying up to " + retries_left.__str__() + " more times...")
# Verify we are dealing with options
if 'get_call_data' in dir(data_get):
try:
# Note options data will always be pulled from yahoo
temp = data_get(data_key, 'yahoo')
# For simplicities sake assume user wants all options data
output_map[data_key] = temp.get_all_data()
return
except:
print(data_key + " options failed to download. Retrying up to " + retries_left.__str__() + " more times...")
print("WARNING: If your version of Pandas is not up to date this may fail!")
"""
Retry at random times progressively slower in case of failures when number of retries remaining gets low
"""
if (retries_left == 3):
time.sleep(randrange(0, 5))
if (retries_left == 2):
time.sleep(randrange(5, 15))
if (retries_left == 1):
time.sleep(randrange(30, 90))
get_data(data_get, data_key, output_map, (retries_left-1), argdict)
class ConcurrentPandas:
"""
Concurrent Pandas is a class for concurrent asynchronous data downloads
from a variety of sources using either threads, or processes.
"""
def __init__(self):
self.output_map = Manager().dict()
self.input_queue = Manager().Queue()
self.data_worker = None
self.worker_args = None
self.source_name = None
def consume_keys(self):
"""
Work through the keys to look up sequentially
"""
print("\nLooking up " + self.input_queue.qsize().__str__() + " keys from " + self.source_name + "\n")
self.data_worker(**self.worker_args)
def consume_keys_asynchronous_processes(self):
"""
Work through the keys to look up asynchronously using multiple processes
"""
print("\nLooking up " + self.input_queue.qsize().__str__() + " keys from " + self.source_name + "\n")
jobs = multiprocessing.cpu_count()*4 if (multiprocessing.cpu_count()*4 < self.input_queue.qsize()) \
else self.input_queue.qsize()
pool = multiprocessing.Pool(processes=jobs, maxtasksperchild=10)
for x in range(jobs):
pool.apply(self.data_worker, [], self.worker_args)
pool.close()
pool.join()
def consume_keys_asynchronous_threads(self):
"""
Work through the keys to look up asynchronously using multiple threads
"""
print("\nLooking up " + self.input_queue.qsize().__str__() + " keys from " + self.source_name + "\n")
jobs = multiprocessing.cpu_count()*4 if (multiprocessing.cpu_count()*4 < self.input_queue.qsize()) \
else self.input_queue.qsize()
pool = ThreadPool(jobs)
for x in range(jobs):
pool.apply(self.data_worker, [], self.worker_args)
pool.close()
pool.join()
def return_map(self):
"""
Return hashmap consisting of key string -> data frame
"""
return self.output_map
def return_input_queue(self):
"""
Return input Queue
"""
return self.input_queue
def insert_keys(self, *args):
"""
Unpack each key and add to queue
"""
for key in args:
self.unpack(key)
def unpack(self, to_unpack):
"""
Unpack is a recursive function that will unpack anything that inherits
from abstract base class Container provided it is not also inheriting from Python basestring.
Raise Exception if resulting object is neither a container or a string
Code working in both Python 2 and Python 3
"""
# Python 3 lacks basestring type, work around below
try:
isinstance(to_unpack, basestring)
except NameError:
basestring = str
# Base Case
if isinstance(to_unpack, basestring):
self.input_queue.put(to_unpack)
return
for possible_key in to_unpack:
if isinstance(possible_key, basestring):
self.input_queue.put(possible_key)
elif sys.version_info >= (3, 0):
if isinstance(possible_key, collections.abc.Container) and not isinstance(possible_key, basestring):
self.unpack(possible_key)
else:
raise Exception("A type that is neither a string or a container was passed to unpack. "
"Aborting!")
else:
if isinstance(possible_key, collections.Container) and not isinstance(possible_key, basestring):
self.unpack(possible_key)
else:
raise Exception("A type that is neither a string or a container was passed to unpack. "
"Aborting!")
def set_source_quandl(self, quandl_token):
"""
Set data source to Quandl
"""
self.data_worker = data_worker
self.worker_args = {"function": Quandl.get, "input": self.input_queue, "output": self.output_map,
"token": quandl_token}
self.source_name = "Quandl"
def set_source_yahoo_finance(self):
"""
Set data source to Yahoo Finance
"""
self.data_worker = data_worker
self.worker_args = {"function": pandas_datareader.DataReader, "input": self.input_queue, "output": self.output_map,
"source": 'yahoo'}
self.source_name = "Yahoo Finance"
def set_source_google_finance(self):
"""
Set data source to Google Finance
"""
self.data_worker = data_worker
self.worker_args = {"function": pandas_datareader.DataReader, "input": self.input_queue, "output": self.output_map,
"source": 'google'}
self.source_name = "Google Finance"
def set_source_federal_reserve_economic_data(self):
"""
Set data source to Federal Reserve Economic Data
"""
self.data_worker = data_worker
self.worker_args = {"function": pandas_datareader.DataReader, "input": self.input_queue, "output": self.output_map,
"source": 'fred'}
self.source_name = "Federal Reserve Economic Data"
def set_source_yahoo_options(self):
"""
Set data source to yahoo finance, specifically to download financial options data
"""
self.data_worker = data_worker
self.worker_args = {"function": Options, "input": self.input_queue, "output": self.output_map,
"source": 'yahoo'}
self.source_name = "Yahoo Finance Options"