|
10 | 10 | import tia.util.log as log
|
11 | 11 |
|
12 | 12 |
|
13 |
| -__all__ = ['DataManager', 'BbgDataManager', 'MemoryStorage', 'HDFStorage', 'CachedDataManager', 'Storage'] |
| 13 | +__all__ = ['DataManager', 'BbgDataManager', 'MemoryStorage', 'HDFStorage', 'CachedDataManager', 'Storage', |
| 14 | + 'CacheOnlyDataManager'] |
14 | 15 |
|
15 | 16 | _force_array = lambda x: isinstance(x, basestring) and [x] or x
|
16 | 17 |
|
@@ -266,14 +267,49 @@ def set(self, key, frame, **userdata):
|
266 | 267 | store.close()
|
267 | 268 |
|
268 | 269 |
|
| 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 | + |
269 | 296 | class CachedDataManager(DataManager):
|
270 | 297 | 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 | + """ |
271 | 303 | DataManager.__init__(self)
|
272 | 304 | self.dm = dm
|
273 | 305 | self.storage = storage
|
274 | 306 | self.ts = ts or pd.datetime.now()
|
275 | 307 | self.logger = log.instance_logger('cachemgr', self)
|
276 | 308 |
|
| 309 | + @staticmethod |
| 310 | + def no_fallback(storage, ts=None): |
| 311 | + return CachedDataManager(CacheOnlyDataManager(), storage, ts) |
| 312 | + |
277 | 313 | @property
|
278 | 314 | def sid_result_mode(self):
|
279 | 315 | return self.dm.sid_result_mode
|
@@ -410,3 +446,7 @@ def get_historical(self, sids, flds, start, end, period=None):
|
410 | 446 | if is_fld_str:
|
411 | 447 | result.columns = result.columns.droplevel(1)
|
412 | 448 | return result
|
| 449 | + |
| 450 | + |
| 451 | + |
| 452 | + |
0 commit comments