class GzipJsonFile(JsonFile): ''' A gzip compressed JsonFile. Usage is the same as JsonFile ''' def __enter__(self): self._file = GzipFile(*self._args, **self._kwargs) self._file.__enter__() return self def __iter__(self): for line in self._file: yield json.loads(line.decode('utf-8', 'ignore')) def write(self, item): item_as_json = json.dumps(item, ensure_ascii=False) encoded = '{0}\n'.format(item_as_json).encode('utf-8', 'ignore') self._file.write(encoded)
def __enter__(self): if hasattr(GzipFile, '__enter__'): return GzipFile.__enter__(self) else: return self