Example #1
0
    def test_apply (self):
        # args
        assert _apply(isint)([42,])
        assert _apply(_all(isstring))(['jack', 'sawyer'])
        assert _zip(isint, _apply(_all(isstring)))(42, ['jack', 'sawyer'])

        # args + kwargs
        assert _apply(_npos(exactly=2)([42, "bad robot!"],
                                       {'jack': 4, 'kate': 15}))
        assert _apply(_nkw(exactly=2)([42, "bad robot!"],
                                      {'jack': 4, 'kate': 15}))
        assert _apply(_nargs(exactly=4)([42, "bad robot!"],
                                        {'jack': 4, 'kate': 15}))

        # recursive (yeah, it makes my brain hurt, too...) It's
        # actually pretty simple, though...  It's equivalent to
        # _and(isstring)(chain(people)) (in other words, it's a
        # completely contrived example which is much more cleanly
        # solved in another way! :-))

        # apply `isstring` to each element in an iterable. I.e.,
        # ``_apply(_all(isstring))(['jack', 'hurley'])`` is equivalent
        # to ``_all(isstring)('jack', 'hurley')
        all_strings = _apply(_all(isstring))

        # make two groups of people, then combine them
        group_1 = ['jack', 'sawyer']
        group_2 = ['kate', 'hurley']
        people = (group_1, group_2)

        assert _apply(_zip(all_strings, all_strings))(people)
Example #2
0
    def test_zip (self):
        # no predicates
        assert _zip()()
        assert _zip()(jack=4, kate=16)
        assert _zip()(4, 8, 15, 16, 23, 42)
        assert _zip()(4, 8, 15, hurley=16, locke=23)

        # len(predicates) == len(args)
        assert _zip(isstring, isempty, isnsiterable)(
            "bad robot!", (), (4, 8))
        assert _zip(isstring, isempty, isnsiterable)(
            "bad robot!", (), (4, 8), kate=15, hurley=16)

        assert not _zip(isstring, isempty, isnsiterable)(
            "bad robot!", (4, 8), ())
        assert not _zip(isstring, isempty, isnsiterable)(
            "bad robot!", (4, 8), (), kate=15, hurley=16)

        # len(predicates) != len(args)
        assert _zip(isstring)("bad robot!", (), (4, 8))
        assert _zip(isstring)("bad robot!", (), (4, 8),
                              kate=15, hurley=16)

        assert _zip(isstring, isempty)("bad robot!", (), (4, 8))
        assert _zip(isstring, isempty)("bad robot!", (), (4, 8),
                                       kate=15, hurley=16)

        assert not _zip(isstring)((), (4, 8))
        assert not _zip(isstring)((), (4, 8), kate=15, hurley=16)
        assert not _zip(isstring, isempty)("bad robot!", (4, 8), ())
        assert not _zip(isstring, isempty)("bad robot!", (4, 8), (),
                                           kate=15, hurley=16)