From: Tomas Gavenciak Date: Sat, 18 Sep 2010 16:12:21 +0000 (+0200) Subject: Many changes to pipeline.py X-Git-Tag: python-dummy-working~8 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=a249fb797c925052e3ad692d887eb2004d66c455;p=eval.git Many changes to pipeline.py Rename MoePipeline -> Pipeline Add Eval and skip_to to Pipeline properties Asserts when inserting to pipeline Rewrite dump to return strings instead of writing to file Some docstrings, clenup Update eval.py to new constructor --- diff --git a/t/moe/eval.py b/t/moe/eval.py index f15f9ee..f19df47 100644 --- a/t/moe/eval.py +++ b/t/moe/eval.py @@ -18,8 +18,8 @@ class Eval: def __init__(self): self.log = moe.log.Loggers() self.config = moe.config.ConfigTree() - self.main_pipe = moe.pipeline.MoePipeline("main") - self.test_pipe = moe.pipeline.MoePipeline("test") + self.main_pipe = moe.pipeline.Pipeline(self, "main") + self.test_pipe = moe.pipeline.Pipeline(self, "test") self.status = moe.status.MoeStatus() def __getitem__(self, key): diff --git a/t/moe/pipeline.py b/t/moe/pipeline.py index a064c61..bffca80 100644 --- a/t/moe/pipeline.py +++ b/t/moe/pipeline.py @@ -1,10 +1,7 @@ #!/usr/bin/env python -import sys import bisect -import imp import moe -import moe.log class MoePipeError(moe.MoeError): """Failure of the MoePipeline.""" @@ -14,25 +11,36 @@ class MoeAbortPipeline(Exception): 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 @@ -40,17 +48,14 @@ class MoePipeline: 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)