Skip to content

Commit

Permalink
Merge pull request #33 from heikomuller/empty-csv
Browse files Browse the repository at this point in the history
Empty csv
  • Loading branch information
heikomuller authored Apr 28, 2021
2 parents 92a7db6 + fac1386 commit d600052
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,8 @@
* Add empty document class ``histore.document.mem.Schema``.
* Change format of serialized archive JSON files.
* Change internal representation of timestamps.


### 0.4.1 - 2021-04-28

* Handle empty CSV files properly.
9 changes: 7 additions & 2 deletions histore/document/csv/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,13 @@ def __init__(
encoding=self.encoding,
none_is=self.none_is
)
header = next(reader)
reader.close()
try:
header = next(reader)
except StopIteration:
# Empty file. The list of column nmaes is empty.
header = []
finally:
reader.close()
super(CSVFile, self).__init__(
columns=header,
filename=filename,
Expand Down
2 changes: 1 addition & 1 deletion histore/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
# file LICENSE for full license details.

"""Code version information for histore."""
__version__ = '0.4.0'
__version__ = '0.4.1'
9 changes: 9 additions & 0 deletions tests/document/csv/test_csv_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

"""Unit tests for the in-memory document and document reader."""

from pathlib import Path

import os

from histore.document.csv.base import CSVFile
Expand Down Expand Up @@ -35,6 +37,13 @@ def test_custom_header(tmpdir):
with doc.open() as reader:
rows = [row for _, _, row in reader]
assert rows == [['A', 'B'], ['1', '2']]
# -- An empty document has an empty header.
empty_file = os.path.join(tmpdir, 'empty.csv')
Path(empty_file).touch()
doc = CSVFile(filename=empty_file)
assert doc.columns == []
doc = CSVFile(filename=empty_file, header=['X', 'Y'])
assert doc.columns == ['X', 'Y']


def test_document_iterator():
Expand Down

0 comments on commit d600052

Please sign in to comment.