Skip to content

Commit

Permalink
JSONL writer
Browse files Browse the repository at this point in the history
  • Loading branch information
gbenson committed May 31, 2024
1 parent 9da6b9a commit 7ef09ce
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/dom_tokenizers/internal/jsonl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from datetime import datetime
from functools import cached_property
from typing import Optional

from . import json


class Writer:
def __init__(
self,
filename: Optional[str] = None,
*,
mode: str = "a",
basename: Optional[str] = None,
ext: str = ".jsonl",
with_timestamp: bool = False,
):
if filename is None:
filename = basename
if with_timestamp:
filename = f"{filename}-{datetime.now():%Y%m%d%H%M%S%f}"
filename = f"{filename}{ext}"
self.filename = filename
self._mode = mode

@cached_property
def _fp(self):
return open(self.filename, self._mode)

def write(self, **fields):
json.dump(fields, self._fp)
self._fp.write("\n")

0 comments on commit 7ef09ce

Please sign in to comment.