From 2c24a89d6f011422c12116cfa037d71cb2091ea8 Mon Sep 17 00:00:00 2001 From: Gary Benson Date: Sat, 1 Jun 2024 08:27:11 +0100 Subject: [PATCH] Make JSONL writer closeable --- src/dom_tokenizers/internal/jsonl.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/dom_tokenizers/internal/jsonl.py b/src/dom_tokenizers/internal/jsonl.py index e53908f..3f39a60 100644 --- a/src/dom_tokenizers/internal/jsonl.py +++ b/src/dom_tokenizers/internal/jsonl.py @@ -22,11 +22,20 @@ def __init__( filename = f"{filename}{ext}" self.filename = filename self._mode = mode + self._is_open = False @cached_property def _fp(self): - return open(self.filename, self._mode) + try: + return open(self.filename, self._mode) + finally: + self._is_open = True def write(self, **fields): json.dump(fields, self._fp) self._fp.write("\n") + + def close(self): + if not self._is_open: + return + self._fp.close()