Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

int-679 Skip empty lines from the parsing #27

Merged
merged 2 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 27 additions & 21 deletions magicparse/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,30 +72,31 @@ def stream_parse(
row_number += 1

for row in reader:
errors = []
row_number += 1
antoine-b-smartway marked this conversation as resolved.
Show resolved Hide resolved
item = {}
for field in self.fields:
try:
value = field.read_value(row)
except Exception as exc:
errors.append({"row-number": row_number, **field.error(exc)})
continue
if any(row):
antoine-b-smartway marked this conversation as resolved.
Show resolved Hide resolved
errors = []
item = {}
for field in self.fields:
try:
value = field.read_value(row)
except Exception as exc:
errors.append({"row-number": row_number, **field.error(exc)})
continue

item[field.key] = value
item[field.key] = value

for computed_field in self.computed_fields:
try:
value = computed_field.read_value(item)
except Exception as exc:
errors.append(
{"row-number": row_number, **computed_field.error(exc)}
)
continue
for computed_field in self.computed_fields:
try:
value = computed_field.read_value(item)
except Exception as exc:
errors.append(
{"row-number": row_number, **computed_field.error(exc)}
)
continue

item[computed_field.key] = value
item[computed_field.key] = value

yield item, errors
yield item, errors


class CsvSchema(Schema):
Expand Down Expand Up @@ -124,8 +125,13 @@ def key() -> str:

class ColumnarSchema(Schema):
def get_reader(self, stream: BytesIO) -> Iterable[str]:
stream_reader = codecs.getreader(self.encoding)
return stream_reader(stream)
stream_reader_factory = codecs.getreader(self.encoding)
stream_reader = stream_reader_factory(stream)
while True:
antoine-b-smartway marked this conversation as resolved.
Show resolved Hide resolved
line = stream_reader.readline(None, False)
if not line:
break
yield line

@staticmethod
def key() -> str:
Expand Down
37 changes: 37 additions & 0 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,21 @@ def test_errors_do_not_halt_parsing(self):
}
]

def test_parse_should_skip_empty_lines(self):
schema = Schema.build(
{
"file_type": "csv",
"fields": [{"key": "name", "type": "str", "column-number": 1}],
}
)
rows, errors = schema.parse(
b"""1

"""
)
assert rows == [{"name": "1"}]
assert not errors


class TestColumnarParse(TestCase):
def test_with_no_data(self):
Expand Down Expand Up @@ -216,6 +231,28 @@ def test_errors_do_not_halt_parsing(self):
}
]

def test_parse_should_skip_empty_lines(self):
schema = Schema.build(
{
"file_type": "columnar",
"fields": [
{
"key": "name",
"type": "str",
"column-start": 0,
"column-length": 8,
}
],
}
)
rows, errors = schema.parse(
b"""8013109C

"""
)
assert rows == [{"name": "8013109C"}]
assert not errors


class TestQuotingSetting(TestCase):
def test_no_quote(self):
Expand Down
Loading