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

Add stream parsing in Schema class #25

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
25 changes: 16 additions & 9 deletions magicparse/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ def register(cls, schema: "Schema") -> None:
cls.registry[schema.key()] = schema

def parse(self, data: Union[bytes, BytesIO]) -> Tuple[List[dict], List[dict]]:
items = []
errors = []

for item, row_errors in self.stream_parse(data):
if row_errors:
errors.extend(row_errors)
else:
items.append(item)

return items, errors

def stream_parse(
self, data: Union[bytes, BytesIO]
) -> Iterable[Tuple[dict, list[dict]]]:
if isinstance(data, bytes):
stream = BytesIO(data)
else:
Expand All @@ -57,18 +71,15 @@ def parse(self, data: Union[bytes, BytesIO]) -> Tuple[List[dict], List[dict]]:
next(reader)
row_number += 1

result = []
errors = []
for row in reader:
errors = []
row_number += 1
row_is_valid = True
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)})
row_is_valid = False
continue

item[field.key] = value
Expand All @@ -80,15 +91,11 @@ def parse(self, data: Union[bytes, BytesIO]) -> Tuple[List[dict], List[dict]]:
errors.append(
{"row-number": row_number, **computed_field.error(exc)}
)
row_is_valid = False
continue

item[computed_field.key] = value

if row_is_valid:
result.append(item)

return result, errors
yield item, errors


class CsvSchema(Schema):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ extend-ignore = ["E203", "E722"]
exclude = [".git/", ".pytest_cache/", ".venv"]

[tool.pytest.ini_options]
python_files = ["tests/*"]
python_files = ["tests/*"]
ducdetronquito marked this conversation as resolved.
Show resolved Hide resolved
27 changes: 27 additions & 0 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from decimal import Decimal

from magicparse import Schema
from magicparse.schema import ColumnarSchema, CsvSchema
from magicparse.fields import ColumnarField, CsvField
Expand Down Expand Up @@ -287,3 +288,29 @@ def test_register(self):
}
)
assert isinstance(schema, self.PipedSchema)


class TestSteamParse(TestCase):
def test_stream_parse_errors_do_not_halt_parsing(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

J'ai l'impression que tu tests beaucoup de chose en un :)

Quel est l'intention derrière ce test spécifiquement ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oui, J'ai repris un test existant sur parse qui me semblait testé un cas passant et un cas échouant.
Pour le reste je me suis dit que dupliquer tous les tests n'avait pas de valeur ajoutée sachant que parse appelle stream_parse et me semblait déjà bien couverte.

schema = Schema.build(
{
"file_type": "csv",
"fields": [{"key": "age", "type": "int", "column-number": 1}],
}
)
rows = list(schema.stream_parse(b"1\na\n2"))
assert rows == [
({"age": 1}, []),
(
{},
[
{
"row-number": 2,
"column-number": 1,
"field-key": "age",
"error": "value 'a' is not a valid integer",
}
],
),
({"age": 2}, []),
]
Loading