Skip to content

Commit

Permalink
Implemented to_file resolving #32 and use six in more places
Browse files Browse the repository at this point in the history
to_file was implemented and tested, which leaves behind a file in the
tmp directory within functional/test/data.

In implementing this and using six.u, I found that I can use six.viewitems instead of
defining my own custom function.
  • Loading branch information
EntilZha committed Oct 30, 2015
1 parent 7858354 commit 69a930f
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,5 @@ docs/_build/

# PyBuilder
target/

functional/test/data/tmp
11 changes: 11 additions & 0 deletions functional/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from operator import mul
import collections
from functools import reduce
import builtins
import six

from .lineage import Lineage
from .util import is_iterable, is_primitive, identity
Expand Down Expand Up @@ -1301,6 +1303,15 @@ def dict(self, default=None):
"""
return self.to_dict(default=default)

def to_file(self, path, mode='w', buffering=-1, encoding=None, errors=None, newline=None):
"""
Saves the sequence to a file by executing str(self) which becomes str(self.to_list())
"""
# pylint: disable=too-many-arguments
with builtins.open(path, mode=mode, buffering=buffering, encoding=encoding, errors=errors,
newline=newline) as output:
output.write(six.u(str(self)))


def _wrap(value):
"""
Expand Down
5 changes: 3 additions & 2 deletions functional/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
import re
import csv as csvapi
import json as jsonapi
import six

from .pipeline import Sequence
from .util import is_primitive, LazyFile, dict_item_iter
from .util import is_primitive, LazyFile


def seq(*args):
Expand Down Expand Up @@ -153,7 +154,7 @@ def json(json_file):
if isinstance(json_input, list):
return seq(json_input)
else:
return seq(dict_item_iter(json_input))
return seq(six.viewitems(json_input))


seq.open = open
Expand Down
Empty file.
12 changes: 10 additions & 2 deletions functional/test/test_streams.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest
import six
from .. import seq
from .. import util


class TestStreams(unittest.TestCase):
Expand Down Expand Up @@ -42,7 +42,7 @@ def test_json(self):
list_test_path = 'functional/test/data/test_list.json'
dict_test_path = 'functional/test/data/test_dict.json'
list_expect = [1, 2, 3, 4, 5]
dict_expect = list(util.dict_item_iter({u'a': 1, u'b': 2, u'c': 3}))
dict_expect = list(six.viewitems({u'a': 1, u'b': 2, u'c': 3}))

result = seq.json(list_test_path).to_list()
self.assertEqual(list_expect, result)
Expand All @@ -58,3 +58,11 @@ def test_json(self):

with self.assertRaises(ValueError):
seq.json(1)

def test_to_file(self):
tmp_path = 'functional/test/data/tmp/output.txt'
sequence = seq(1, 2, 3, 4)
sequence.to_file(tmp_path)
with open('functional/test/data/tmp/output.txt', 'r') as output:
self.assertEqual('[1, 2, 3, 4]', output.readlines()[0])

11 changes: 6 additions & 5 deletions functional/transformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
import collections
from enum import Enum
import types
import six

from .util import dict_item_iter, filterfalse
from .util import filterfalse


Transformation = collections.namedtuple(
Expand Down Expand Up @@ -253,7 +254,7 @@ def group_by_key_impl(sequence):
result.get(element[0]).append(element[1])
else:
result[element[0]] = [element[1]]
return dict_item_iter(result)
return six.viewitems(result)


def group_by_key_t():
Expand Down Expand Up @@ -281,7 +282,7 @@ def group_by_impl(func, sequence):
result.get(func(element)).append(element)
else:
result[func(element)] = [element]
return dict_item_iter(result)
return six.viewitems(result)


def group_by_t(func):
Expand Down Expand Up @@ -326,7 +327,7 @@ def inner_join_impl(other, sequence):
for k in keys:
if k in seq_kv and k in other_kv:
result[k] = (seq_kv[k], other_kv[k])
return dict_item_iter(result)
return six.viewitems(result)


def join_impl(other, join_type, sequence):
Expand All @@ -348,7 +349,7 @@ def join_impl(other, join_type, sequence):
result = {}
for k in keys:
result[k] = (seq_kv.get(k), other_kv.get(k))
return dict_item_iter(result)
return six.viewitems(result)


def join_t(other, join_type):
Expand Down
6 changes: 0 additions & 6 deletions functional/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,9 @@

if six.PY2:
from itertools import ifilterfalse as filterfalse

def dict_item_iter(dictionary):
return dictionary.viewitems()
else:
from itertools import filterfalse

def dict_item_iter(dictionary):
return dictionary.items()


def is_primitive(val):
"""
Expand Down

0 comments on commit 69a930f

Please sign in to comment.