From: Tomas Gavenciak Date: Sat, 18 Sep 2010 21:38:03 +0000 (+0200) Subject: Writing status uses dump X-Git-Tag: python-dummy-working~2 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=8faf6b71514231fc3af6f60a9192028e1e64373f;p=moe.git Writing status uses dump Also use 'with' around IO --- diff --git a/t/moe/status.py b/t/moe/status.py index 320d7c9..5a5adf2 100644 --- a/t/moe/status.py +++ b/t/moe/status.py @@ -38,7 +38,10 @@ class MoeStatus: return m[k] def dump(self, prefix=""): - "Return a list of lines, `prefix` is indent prefix." + """ + Dump status in metafile format. + Return a list of lines, `prefix` is indent prefix. + """ l = [] for k,v in self.stat.items(): if type(v) == types.ListType: vals = v @@ -52,25 +55,19 @@ class MoeStatus: l.append(prefix + k + ":" + str(w) + "\n") return l - def write(self, file=None, name=None): - if file is None: - if name is not None: - file = open(name, "w") - else: - file = sys.stdout - self.write_nested(file, 0) - - def write_nested(self, file, indent): - for k,v in self.stat.items(): - if type(v) == types.ListType: vals = v - else: vals = [v] - for w in vals: - if isinstance(w, MoeStatus): - file.write("\t" * indent + k + "(\n") - w.write_nested(file, indent+1); - file.write("\t" * indent + ")\n") - else: - file.write("\t" * indent + k + ":" + str(w) + "\n") + def write(self, f=None, name=None): + """ + Write status (as a metafile) to `f` or file `name` or `stdout` + """ + if not f and name is not None: + with open(name, "w") as f: + for l in self.dump(): + f.write(l+"\n") + else: + if not f: + f = sys.stdout + for l in self.dump(): + f.write(l+"\n") def read(self, file=None, name=None): if file is None: