File tree Expand file tree Collapse file tree 3 files changed +23
-1
lines changed Expand file tree Collapse file tree 3 files changed +23
-1
lines changed Original file line number Diff line number Diff line change 21
21
OBJECT = Dict [KEYNAME , Any ]
22
22
23
23
24
+ class MissingColumnError (ValueError ):
25
+ """Exception raised when a column is missing from a row."""
26
+
27
+ def __init__ (self , row : ROW , value : Any ):
28
+ """Initialize exception."""
29
+ super ().__init__ (f"Value { value } has no column row { row } " )
30
+
31
+
24
32
@unique
25
33
class Serializer (Enum ):
26
34
"""Vocabulary of methods to use for serialization objects."""
@@ -441,6 +449,8 @@ def _getval(x: str) -> Optional[Any]:
441
449
for row in r :
442
450
nu_obj = {}
443
451
for k , v in row .items ():
452
+ if k is None :
453
+ raise MissingColumnError (row , v )
444
454
key_config = config .key_configs .get (k , None )
445
455
v = v .replace ("\\ n" , "\n " ).replace ("\\ t" , "\t " )
446
456
# lists are demarcated by list markers
Original file line number Diff line number Diff line change
1
+ source_language translation_language subject_id predicate_id source_value translation_value translation_status
2
+ en tr HP :0004432 IAO :0000115 ab "a b" CANDIDATE
Original file line number Diff line number Diff line change 4
4
import json
5
5
import logging
6
6
import unittest
7
+ from pathlib import Path
7
8
from typing import Any , List
8
9
9
10
import yaml
17
18
unflatten ,
18
19
unflatten_from_csv ,
19
20
)
20
- from tests import INPUT
21
+ from json_flattener .flattener import MissingColumnError
22
+ from tests import INPUT , INPUT_DIR
21
23
22
24
23
25
def _json (obj ) -> str :
@@ -392,6 +394,14 @@ def test_roundtrip_from_file(self):
392
394
logging .info (roundtrip_json )
393
395
self ._roundtrip_to_tsv (objs , config = config )
394
396
397
+ def test_badly_formatted (self ):
398
+ """
399
+ Tests graceful failure on badly formatted TSV input.
400
+ """
401
+ with open (str (Path (INPUT_DIR ) / "badly-formatted.tsv" )) as stream :
402
+ with self .assertRaises (MissingColumnError ) as _e :
403
+ _objs = unflatten_from_csv (stream )
404
+
395
405
396
406
if __name__ == "__main__" :
397
407
unittest .main ()
You can’t perform that action at this time.
0 commit comments