Skip to content

Commit e192e28

Browse files
committed
added DataManager which fails if cache lookup fails
1 parent c5d1062 commit e192e28

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

tia/bbg/datamgr.py

+41-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
import tia.util.log as log
1111

1212

13-
__all__ = ['DataManager', 'BbgDataManager', 'MemoryStorage', 'HDFStorage', 'CachedDataManager', 'Storage']
13+
__all__ = ['DataManager', 'BbgDataManager', 'MemoryStorage', 'HDFStorage', 'CachedDataManager', 'Storage',
14+
'CacheOnlyDataManager']
1415

1516
_force_array = lambda x: isinstance(x, basestring) and [x] or x
1617

@@ -266,14 +267,49 @@ def set(self, key, frame, **userdata):
266267
store.close()
267268

268269

270+
class CacheMissError(Exception):
271+
"""Raised when cache lookup fails and there is no fallback"""
272+
273+
274+
class CacheOnlyDataManager(DataManager):
275+
def get_attributes(self, sids, flds, **overrides):
276+
sids = _force_array(sids)
277+
flds = _force_array(flds)
278+
sstr = ','.join(sids)
279+
fstr = ','.join(flds)
280+
ostr = ''
281+
if overrides:
282+
ostr = ', overrides=' + ','.join(['{0}={1}'.format(str(k), str(v)) for k, v in overrides.iteritems()])
283+
msg = 'Reference data for sids={0}, flds={1}{2}'.format(sstr, fstr, ostr)
284+
raise CacheMissError(msg)
285+
286+
def get_historical(self, sids, flds, start, end, period=None):
287+
sids = _force_array(sids)
288+
flds = _force_array(flds)
289+
sstr = ','.join(sids)
290+
fstr = ','.join(flds)
291+
msg = 'Historical data for sids={0}, flds={1}, start={2}, end={3}, period={4}'.format(sstr, fstr, start, end,
292+
period)
293+
raise CacheMissError(msg)
294+
295+
269296
class CachedDataManager(DataManager):
270297
def __init__(self, dm, storage, ts=None):
298+
"""
299+
:param dm: DataManager, if not available in cache then use dm to request data
300+
:param storage: Storage for the cached data
301+
:param ts:
302+
"""
271303
DataManager.__init__(self)
272304
self.dm = dm
273305
self.storage = storage
274306
self.ts = ts or pd.datetime.now()
275307
self.logger = log.instance_logger('cachemgr', self)
276308

309+
@staticmethod
310+
def no_fallback(storage, ts=None):
311+
return CachedDataManager(CacheOnlyDataManager(), storage, ts)
312+
277313
@property
278314
def sid_result_mode(self):
279315
return self.dm.sid_result_mode
@@ -410,3 +446,7 @@ def get_historical(self, sids, flds, start, end, period=None):
410446
if is_fld_str:
411447
result.columns = result.columns.droplevel(1)
412448
return result
449+
450+
451+
452+

0 commit comments

Comments
 (0)