Skip to content

Commit

Permalink
Adding a data writer module
Browse files Browse the repository at this point in the history
  • Loading branch information
mtlynch committed May 2, 2018
1 parent 7321f08 commit c16bc3f
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
25 changes: 25 additions & 0 deletions ingredient_phrase_tagger/training/labelled_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,28 @@ def _parse_row(row):
'unit': row['unit'].decode('utf-8'),
'comment': row['comment'].decode('utf-8'),
}


class Writer(object):
"""Writes labelled ingredient data to a CSV file."""

def __init__(self, data_file):
self._csv_writer = csv.DictWriter(
data_file, fieldnames=_REQUIRED_COLUMNS, lineterminator='\n')
self._csv_writer.writeheader()

def writerow(self, row):
"""Adds a row of data to the output CSV file.
Args:
row: A dictionary of values for a labelled ingredient. The
dictionary must contain the following keys:
* input
* name
* qty
* range_end
* unit
* comment
"""
self._csv_writer.writerow(row)
47 changes: 47 additions & 0 deletions tests/test_labelled_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,50 @@ def test_raises_error_when_csv_does_not_have_required_columns(self):
77,3 bananas,bananas,3.0,0.0,,
""".strip())
labelled_data.Reader(mock_file).next()


class WriterTest(unittest.TestCase):

def setUp(self):
self.maxDiff = None

def test_writes_valid_rows(self):
mock_file = io.BytesIO()
writer = labelled_data.Writer(mock_file)
mock_rows = [{
'input': u'4 to 6 large cloves garlic',
'qty': 4.0,
'unit': u'clove',
'name': u'garlic',
'range_end': 6.0,
'comment': u'',
}, {
'input': u'3 bananas',
'qty': 3.0,
'unit': u'',
'name': u'bananas',
'comment': u'',
'range_end': 0.0,
}, {
'input': (u'2 1/2 pounds bell peppers (about 6 peppers in '
u'assorted colors), cut into 2-inch chunks'),
'qty':
2.5,
'unit':
u'pound',
'name':
u'bell peppers',
'range_end':
0.0,
'comment': (u'(about 6 peppers in assorted colors), cut into '
u'2-inch chunks'),
}]
for row in mock_rows:
writer.writerow(row)
self.assertMultiLineEqual("""
input,name,qty,range_end,unit,comment
4 to 6 large cloves garlic,garlic,4.0,6.0,clove,
3 bananas,bananas,3.0,0.0,,
"2 1/2 pounds bell peppers (about 6 peppers in assorted colors), cut into 2-inch chunks",bell peppers,2.5,0.0,pound,"(about 6 peppers in assorted colors), cut into 2-inch chunks"
""".strip(),
mock_file.getvalue().strip())

0 comments on commit c16bc3f

Please sign in to comment.