From 9ffaec64d8a9ebc8dbb3a1f363d1b46a38fd77dd Mon Sep 17 00:00:00 2001 From: Eddie Hebert Date: Mon, 29 Apr 2013 14:31:46 -0400 Subject: [PATCH] MAINT: Factor out check for window length value sanity. Preparing for BatchTransform not using EventWindow, so factoring out the checks for window length into a function that can be shared. --- zipline/transforms/utils.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/zipline/transforms/utils.py b/zipline/transforms/utils.py index b705c15e..0a9375df 100644 --- a/zipline/transforms/utils.py +++ b/zipline/transforms/utils.py @@ -55,6 +55,21 @@ class InvalidWindowLength(Exception): pass +def check_window_length(window_length): + """ + Ensure the window length provided to a transform is valid. + """ + if window_length is None: + raise InvalidWindowLength("window_length must be provided") + if not isinstance(window_length, Integral): + raise InvalidWindowLength( + "window_length must be an integer-like number") + if window_length == 0: + raise InvalidWindowLength("window_length must be non-zero") + if window_length < 0: + raise InvalidWindowLength("window_length must be positive") + + class Passthrough(object): PASSTHROUGH = True """ @@ -178,6 +193,7 @@ class EventWindow(object): def __init__(self, market_aware=True, window_length=None, delta=None): + check_window_length(window_length) self.window_length = window_length self.ticks = deque() @@ -191,16 +207,6 @@ class EventWindow(object): raise UnsupportedEventWindowFlagValue( "delta values are no longer supported." ) - if self.window_length is None: - raise InvalidWindowLength("window_length must be provided") - if not isinstance(self.window_length, Integral): - raise InvalidWindowLength( - "window_length must be an integer-like number") - if self.window_length == 0: - raise InvalidWindowLength("window_length must be non-zero") - if self.window_length < 0: - raise InvalidWindowLength("window_length must be positive") - # Set the behavior for dropping events from the back of the # event window. self.drop_condition = self.out_of_market_window