#!/usr/bin/env python
-import sys
import bisect
-import imp
import moe
-import moe.log
class MoePipeError(moe.MoeError):
"""Failure of the MoePipeline."""
def __init__(self, skip_to=999):
self.skip_to = skip_to
-class MoePipeline:
+class Pipeline:
"""Moe pipeline."""
- def __init__(self, name):
+ def __init__(self, e, name, skip_to = 70):
+ # e is Eval
+ self.e = e
self.pipe = []
self.index = -1
self.name = name
+ self.skip_to = skip_to
def insert(self, pri, name, fun):
- triple = (pri,name,fun)
+ "Insert callable `fun` to time `pri`, `name` is only informative."
+ assert(isinstance(pri, int))
+ assert(callable(fun))
+ triple = (pri, name, fun)
pos = bisect.bisect(self.pipe, triple)
if pos <= self.index:
- raise MoePipeError, "Pipeline insert cannot alter the past"
+ raise MoePipeError, "Pipeline %r at time %d: Insert cannot alter the past (time %d)" \
+ % (self.name, self.index, pri)
self.pipe.insert(pos, triple)
- def dump(self, file=sys.stdout, prefix=""):
- file.write(">>> Pipeline %s\n" % self.name)
- for pri,name,fun in self.pipe:
- file.write("%s%03d %s\n" % (prefix,pri,name))
+ def dump(self, prefix=""):
+ """
+ Debugging dump of the pipe.
+ Returns a list of lines.
+ """
+ l=["%s >>> Pipeline %s\n" % (prefix, self.name)]
+ for pri, name, fun in self.pipe:
+ l.append("%s% 3d %s, %s\n" % (prefix, pri, name, fun))
def run(self, *args, **kwargs):
self.index = 0
while self.index < len(self.pipe):
(pri,name,fun) = self.pipe[self.index]
if pri >= min_pri:
- moe.log.default.verbose(">> Running %s:%s\n" % (self.name,name))
+ self.e.log.debug("Pipeline %r:%d running: %s\n" % (self.name, pri, name))
try:
fun(*args, **kwargs)
except MoeAbortPipeline, err:
- min_pri = err.skip_to
+ min_pri = self.skip_to
else:
- moe.log.default.verbose(">> Skipping %s:%s\n" % (self.name,name))
+ self.e.log.debug("Pipeline %r:d skipping: %s\n" % (self.name, pri, name))
self.index += 1
self.index = -1
- moe.log.default.verbose(">> Pipeline %s finished\n" % self.name)
+ self.e.log.debug("Pipeline %r finished\n" % (self.name))
- def configure(self, names):
- for name in names.split():
- self.add_hook(name)