Skip to content

Commit

Permalink
Implemented to_json which resolves #30
Browse files Browse the repository at this point in the history
  • Loading branch information
EntilZha committed Nov 1, 2015
1 parent a9c4e7e commit 8fc7a09
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
20 changes: 18 additions & 2 deletions functional/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -1335,7 +1335,23 @@ def to_jsonl(self, path, mode='w'):
self.map(json.dumps).make_string('\n') + '\n'
))

def to_csv(self, path, dialect='excel', **fmtparams):
def to_json(self, path, root_array=True, mode=CSV_WRITE_MODE):
"""
Saves the sequence to a json file. If root_array is True, then the sequence will be written
to json with an array at the root. If it is False, then the sequence will be converted from
a sequence of (Key, Value) pairs to a dictionary so that the json root is a dictionary.
:param path: path to write file
:param root_array: write json root as an array or dictionary
:param mode: file open mode
"""
with builtins.open(path, mode=mode) as output:
if root_array:
json.dump(self.to_list(), output)
else:
json.dump(self.to_dict(), output)

def to_csv(self, path, mode=CSV_WRITE_MODE, dialect='excel', **fmtparams):
"""
Saves the sequence to a csv file. Each element should be an iterable which will be expanded
to the elements of each row.
Expand All @@ -1344,7 +1360,7 @@ def to_csv(self, path, dialect='excel', **fmtparams):
:param dialect: passed to csv.writer
:param fmtparams: passed to csv.writer
"""
with builtins.open(path, CSV_WRITE_MODE) as output:
with builtins.open(path, mode) as output:
csv_writer = csv.writer(output, dialect=dialect, **fmtparams)
for row in self:
csv_writer.writerow([six.u(str(element)) for element in row])
Expand Down
13 changes: 13 additions & 0 deletions functional/test/test_streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,19 @@ def test_to_jsonl(self):
result = seq.jsonl(tmp_path).to_list()
self.assertEqual(elements, result)

def test_to_json(self):
tmp_path = 'functional/test/data/tmp/output.txt'
elements = [[u'a', 1], [u'b', 2], [u'c', 3]]
sequence = seq(elements)
sequence.to_json(tmp_path)
result = seq.json(tmp_path).to_list()
self.assertEqual(elements, result)

dict_expect = {u'a': 1, u'b': 2, u'c': 3}
sequence.to_json(tmp_path, root_array=False)
result = seq.json(tmp_path).to_dict()
self.assertEqual(dict_expect, result)

def test_to_csv(self):
tmp_path = 'functional/test/data/tmp/output.txt'
elements = [[1, 2, 3], [4, 5, 6], ['a', 'b', 'c']]
Expand Down

0 comments on commit 8fc7a09

Please sign in to comment.