Skip to content

Commit

Permalink
Merge pull request #68 from ImperialCollegeLondon/flush_immediately
Browse files Browse the repository at this point in the history
Add flush_immediately to writer.
  • Loading branch information
jamesturner246 authored Aug 3, 2023
2 parents 1f97c6e + 84309dd commit 3db57a7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ dictionary. Likewise you can set the `yaml_options` dictionary with whatever opt
want to pass to `yaml.safe_load` and `yaml.safe_dump` functions, reading/writing the
YAML-formatted header, respectively.

You can also instruct a writer to use line buffering, instead of the usual chunk buffering.

Finally, you can control the character(s) used to indicate comments by setting the
`comment` keyword when writing a file. By default, there is no character (""). During reading, the comment character is found atomatically.

Expand Down
7 changes: 6 additions & 1 deletion csvy/writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def __init__(
comment: str = "",
csv_options: Optional[Dict[str, Any]] = None,
yaml_options: Optional[Dict[str, Any]] = None,
line_buffering: bool = False,
) -> None:
"""Create a new Writer.
Expand All @@ -69,15 +70,19 @@ def __init__(
csv_options: Arguments to pass to csv.writer()
yaml_options: Arguments to pass to the 'yaml.safe_dump' function to control
writing the header.
line_buffering: Line buffering instead of chunk buffering (default False).
"""

if not csv_options:
csv_options = {}
if not yaml_options:
yaml_options = {}

# Line buffering: 1 and default chunk buffering: -1
buffering = 1 if line_buffering else -1

# Newline must be "" as per csv.writer's documentation
self._file = Path(filename).open("w", newline="")
self._file = Path(filename).open("w", newline="", buffering=buffering)
write_header(self._file, header, comment, **yaml_options)

self._writer = csv.writer(self._file, **csv_options)
Expand Down

0 comments on commit 3db57a7

Please sign in to comment.