def test_creates_multiple_pipes_from_iterable_items_own_items(): pairs = Pype(((1, -1), (2, -2), (3, -3))) lefts = 1, 2, 3 rights = -1, -2, -3 assert tuple(map(tuple, pairs.unzip())) == (lefts, rights)
def test_picking_a_property_does_not_consume_pipe(): Person = namedtuple('Person', ['age']) pipe = Pype((Person(n) for n in _123)) next(iter(pipe.pick(Person.age))) assert tuple(pipe) == (Person(2), Person(3))
def test_writes_items_to_json_file_as_list(tmpdir): target = join(tmpdir, 'list.json') Pype(['a', 'fun', 'day']).to_json(target, as_dict=False) with open(target) as file: assert json.load(file) == ['a', 'fun', 'day']
def test_writes_pairs_to_json_file_as_list(tmpdir): target = join(tmpdir, 'list.json') Pype([('a', 1), ('fun', 2), ('day', 3)]).to_json(target, as_dict=False) with open(target) as file: assert json.load(file) == [['a', 1], ['fun', 2], ['day', 3]]
def test_writes_pairs_to_json_file_as_object(tmpdir): target = join(tmpdir, 'object.json') Pype([('a', 1), ('fun', 2), ('day', 3)]).to_json(target) with open(target) as file: assert json.load(file) == {'a': 1, 'fun': 2, 'day': 3}
def test_picks_items_elements_at_the_given_key(): pipe = Pype(str(n) for n in _123) assert tuple(pipe.pick(0)) == ('1', '2', '3')
def test_picks_items_property(): Person = namedtuple('Person', ['age']) pipe = Pype((Person(11), Person(22), Person(33))) assert tuple(pipe.pick(Person.age)) == (11, 22, 33)
def test_writing_to_json_file_consumes_pipe(tmpdir): target = join(tmpdir, 'object.json') pipe = Pype((k, v) for k, v in [('a', 1), ('fun', 2), ('day', 3)]) with raises(StopIteration): next(iter(pipe.to_json(target)))
def _123_pype() -> Pype[int]: return Pype(iter(_123))
def _aba_pype() -> Pype[str]: return Pype(iter('aba'))
def _aAfunFUNdayDAY_pype() -> Pype[Tuple[str, str]]: return Pype((w, w.upper()) for w in _a_fun_day)
def _112233_pype() -> Pype[Tuple[int, int]]: return Pype((n, n) for n in _123)
def _a_fun_day_pype() -> Pype[str]: return Pype(iter(_a_fun_day))
def _654_pype() -> Pype[int]: return Pype(iter(_654))
def test_picking_a_key_does_not_consume_pipe(): pipe = Pype(str(n) for n in _123) next(iter(pipe.pick(0))) assert tuple(pipe) == ('2', '3')
def test_zipping_with_a_pipe_of_different_size_returns_a_pipe_the_size_of_the_longer_one_with_missing_items_padded( ): assert tuple(_123_pype().zip(Pype('funny'), trunc=False, pad=4)) \ == ((1, 'f'), (2, 'u'), (3, 'n'), (4, 'n'), (4, 'y'))
def _empty_pype() -> Pype: return Pype(iter(()))