diff options
author | Lars Wirzenius <liw@liw.fi> | 2011-08-08 15:35:10 +0100 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2011-08-08 15:35:10 +0100 |
commit | 0c57487ac13e4716382efaed7276ab47c14cf74f (patch) | |
tree | 71c03f8c5e4dc8cf1c0f90f7cea15e6f076e4850 | |
parent | 2d85bea835f5d7466a7bb150bfd562c72d81791c (diff) | |
download | summain-0c57487ac13e4716382efaed7276ab47c14cf74f.tar.gz |
Fix things so that JSON output does not keep all objects in memory at once.
-rwxr-xr-x | summain | 14 |
1 files changed, 13 insertions, 1 deletions
@@ -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') |