mirror of
https://github.com/wassname/catalyst.git
synced 2026-07-04 18:47:17 +08:00
moved MovingAverage state class to its own file
This commit is contained in:
@@ -44,8 +44,10 @@ class MovingAverageEventWindow(EventWindow):
|
||||
"""
|
||||
Calculates a moving average over all specified fields.
|
||||
"""
|
||||
# Subclass initializer. The superclass also requires a timedelta
|
||||
# argument, so instantiation should look like:
|
||||
# mavg = MovingAverageEventWindow(timedelta(minutes=1), ['price'])
|
||||
def init(self, fields):
|
||||
# Subclass initializer. Superclass also expects a timedelta.
|
||||
self.fields = fields
|
||||
self.totals = defaultdict(float)
|
||||
|
||||
|
||||
@@ -139,42 +139,6 @@ class StatefulTransform(object):
|
||||
out_message.dt = message_copy.dt
|
||||
yield out_message
|
||||
|
||||
class MovingAverage(object):
|
||||
"""
|
||||
Class that maintains a dictionary from sids to EventWindows
|
||||
Upon receipt of each message we update the
|
||||
corresponding window and return the calculated average.
|
||||
"""
|
||||
FORWARDER = False
|
||||
|
||||
def __init__(self, delta, fields):
|
||||
self.delta = delta
|
||||
self.fields = fields
|
||||
|
||||
# No way to pass arguments to the defaultdict factory, so we
|
||||
# need to define a method to generate the correct EventWindows.
|
||||
self.sid_windows = defaultdict(self.create_window)
|
||||
|
||||
def create_window(self):
|
||||
"""Factory method for self.sid_windows."""
|
||||
return EventWindow(self.delta, self.fields)
|
||||
|
||||
def update(self, event):
|
||||
"""
|
||||
Update the event window for this event's sid. Return an ndict from
|
||||
tracked fields to averages.
|
||||
"""
|
||||
|
||||
assert isinstance(event, ndict),"Bad event in MovingAverage: %s" % event
|
||||
assert event.has_key('sid'), "No sid in MovingAverage: %s" % event
|
||||
assert event.has_key('dt'), "No dt in MovingAverage: %s" % event
|
||||
|
||||
# This will create a new EventWindow if this is the first
|
||||
# message for this sid.
|
||||
window = self.sid_windows[event.sid]
|
||||
window.update(event)
|
||||
return window.get_averages()
|
||||
|
||||
class EventWindow:
|
||||
"""
|
||||
Abstract base class for transform classes that calculate iterative
|
||||
|
||||
Reference in New Issue
Block a user