From 244f787176c4e40af8fa251dd523debe8492127c Mon Sep 17 00:00:00 2001 From: Stephen Diehl Date: Wed, 16 May 2012 16:55:26 -0400 Subject: [PATCH] Cleaned up OOP on transforms. --- zipline/components/feed.py | 12 ++++++------ zipline/components/passthrough.py | 31 +++++++------------------------ zipline/core/host.py | 4 +--- zipline/finance/sources.py | 12 ++++++++++-- zipline/transforms/base.py | 17 ++++------------- zipline/utils/factory.py | 5 +++++ 6 files changed, 33 insertions(+), 48 deletions(-) diff --git a/zipline/components/feed.py b/zipline/components/feed.py index 42c08c73..75847e0d 100644 --- a/zipline/components/feed.py +++ b/zipline/components/feed.py @@ -11,10 +11,10 @@ LOGGER = logging.getLogger('ZiplineLogger') class Feed(Component): """ - Connects to N PULL sockets, publishing all messages received to a PUB - socket. Published messages are guaranteed to be in chronological order - based on message property dt. Expects to be instantiated in one execution - context (thread, process, etc) and run in another. + Connects to N PULL sockets, publishing all messages received to a + PUB socket. Published messages are guaranteed to be in chronological + order based on message property dt. Expects to be instantiated in + one execution context (thread, process, etc) and run in another. """ def init(self): @@ -198,7 +198,7 @@ class Feed(Component): def __len__(self): """ - Buffer's length is same as internal map holding separate - sorted arrays of events keyed by source id. + Buffer's length is same as internal map holding separate sorted + arrays of events keyed by source id. """ return len(self.data_buffer) diff --git a/zipline/components/passthrough.py b/zipline/components/passthrough.py index e7fa5d52..2b176774 100644 --- a/zipline/components/passthrough.py +++ b/zipline/components/passthrough.py @@ -1,35 +1,18 @@ -import zipline.protocol as zp -from zipline.transforms import BaseTransform - -from zipline.protocol import CONTROL_PROTOCOL, COMPONENT_TYPE, \ - COMPONENT_STATE, CONTROL_FRAME, CONTROL_UNFRAME +from zipline.transforms import BaseTransform +from zipline.protocol import FEED_FRAME, TRANSFORM_TYPE class PassthroughTransform(BaseTransform): """ - A bypass transform which is also an identity transform:: - - +-------+ - +---| f |---> - +-------+ - +------id-------> - + A bypass transform passes data through unchanged. """ - def __init__(self, **kwargs): - BaseTransform.__init__(self, "PASSTHROUGH") - self.init(**kwargs) - - def init(self, **kwargs): - pass - - @property - def get_type(self): - return COMPONENT_TYPE.CONDUIT + def init(self): + self.state = { 'name': 'PASSTHROUGH' } #TODO, could save some cycles by skipping the _UNFRAME call # and just setting value to original msg string. def transform(self, event): return { - 'name' : zp.TRANSFORM_TYPE.PASSTHROUGH, - 'value' : zp.FEED_FRAME(event) + 'name' : TRANSFORM_TYPE.PASSTHROUGH, + 'value' : FEED_FRAME(event) } diff --git a/zipline/core/host.py b/zipline/core/host.py index 4a6f790a..052e2d5c 100644 --- a/zipline/core/host.py +++ b/zipline/core/host.py @@ -24,7 +24,7 @@ class ComponentHost(Component): self.addresses = addresses self.running = False - # Component Registry, keyed by get_id + # Component Registry, keyed by unique string # ---------------------- self.components = {} # ---------------------- @@ -109,8 +109,6 @@ class ComponentHost(Component): DEPRECATED, left in for compatability for now. """ - cur_time = datetime.datetime.utcnow() - if len(self.components) == 0: LOGGER.info("Component register is empty.") return False diff --git a/zipline/finance/sources.py b/zipline/finance/sources.py index 52a13c75..73fa56e0 100644 --- a/zipline/finance/sources.py +++ b/zipline/finance/sources.py @@ -1,9 +1,17 @@ """ Provides data handlers that can push messages to a zipline.core.DataFeed + +:: + DataSource + | + TradeDataSource + / \ + RandomEquityTrades SpecificEquityTrades + """ -import datetime -import random import pytz +import random +import datetime from mock import Mock from zipline.components import DataSource diff --git a/zipline/transforms/base.py b/zipline/transforms/base.py index 90162437..8ab922dc 100644 --- a/zipline/transforms/base.py +++ b/zipline/transforms/base.py @@ -21,15 +21,6 @@ class BaseTransform(Component): method to create a new derived value from the combined feed. """ - def __init__(self, name, **kwargs): - Component.__init__(self) - - self.state = { - 'name': name - } - - self.init(**kwargs) - def init(self): pass @@ -124,10 +115,10 @@ class BaseTransform(Component): {name:"name of new transform", value: "value of new field"} - Transforms run in parallel and results are merged into a single map, so - transform names must be unique. Best practice is to use the self.state - object initialized from the transform configuration, and only set the - transformed value:: + Transforms run in parallel and results are merged into a + single map, so transform names must be unique. Best practice + is to use the self.state object initialized from the transform + configuration, and only set the transformed value:: self.state['value'] = transformed_value """ diff --git a/zipline/utils/factory.py b/zipline/utils/factory.py index 88c7676c..46828a41 100644 --- a/zipline/utils/factory.py +++ b/zipline/utils/factory.py @@ -11,9 +11,14 @@ from operator import attrgetter from datetime import datetime, timedelta import zipline.finance.risk as risk import zipline.protocol as zp + from zipline.finance.sources import SpecificEquityTrades, RandomEquityTrades from zipline.finance.trading import TradingEnvironment +# TODO +def data_locator(): + pass + def load_market_data(): fp_bm = open("./tests/benchmark.msgpack", "rb") bm_list = msgpack.loads(fp_bm.read())