From 0c57487ac13e4716382efaed7276ab47c14cf74f Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Mon, 8 Aug 2011 15:35:10 +0100 Subject: Fix things so that JSON output does not keep all objects in memory at once. --- summain | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'summain') diff --git a/summain b/summain index cf35a41..579c6d9 100755 --- a/summain +++ b/summain @@ -70,10 +70,22 @@ class CSV(OutputFormat): self.writer.writerow(values) +class GeneratorList(list): + + def __init__(self, gen): + self.gen = gen + + def __len__(self): + return 1 + + def __iter__(self): + return iter(self.gen) + + class Json(OutputFormat): def write(self): - json.dump(list(self.dictify(name, o) for name, o in self.objects), + json.dump(GeneratorList(self.dictify(name, o) for name, o in self.objects), self.output, sort_keys=True, indent=1) self.output.write('\n') -- cgit v1.2.1