Files
catalyst/zipline/sources/data_source.py
T
Eddie Hebert 8481e2df49 MAINT: Use Python 3 compatible metaclass.
Use six's with_metaclass to have objects that use metaclasses, in
both Python 2 and 3.

Otherwise, in Python 3 the objects were being treated as if they
did not have a metaclass, when the Python 2 syntax is used, leading
to errors because of missing attributes, etc.
2014-01-07 11:58:01 -05:00

68 lines
1.5 KiB
Python

from abc import (
ABCMeta,
abstractproperty
)
from six import with_metaclass
from zipline.protocol import DATASOURCE_TYPE
from zipline.protocol import Event
class DataSource(with_metaclass(ABCMeta)):
@property
def event_type(self):
return DATASOURCE_TYPE.TRADE
@property
def mapping(self):
"""
Mappings of the form:
target_key: (mapping_function, source_key)
"""
return {}
@abstractproperty
def raw_data(self):
"""
An iterator that yields the raw datasource,
in chronological order of data, one event at a time.
"""
NotImplemented
@abstractproperty
def instance_hash(self):
"""
A hash that represents the unique args to the source.
"""
pass
def get_hash(self):
return self.__class__.__name__ + "-" + self.instance_hash
def apply_mapping(self, raw_row):
"""
Override this to hand craft conversion of row.
"""
row = {target: mapping_func(raw_row[source_key])
for target, (mapping_func, source_key)
in self.mapping.items()}
row.update({'source_id': self.get_hash()})
row.update({'type': self.event_type})
return row
@property
def mapped_data(self):
for row in self.raw_data:
yield Event(self.apply_mapping(row))
def __iter__(self):
return self
def next(self):
return self.mapped_data.next()
def __next__(self):
return next(self.mapped_data)