Example #1
0
def where(collection, properties):
    """Examines each element in a collection, returning an array of all
    elements that have the given properties.

    Args:
        collection (list|dict): Collection to iterate over.
        properties (dict): property values to filter by

    Returns:
        list: filtered list.

    Example:

        >>> results = where([{'a': 1}, {'b': 2}, {'a': 1, 'b': 3}], {'a': 1})
        >>> assert results == [{'a': 1}, {'a': 1, 'b': 3}]

    .. versionadded:: 1.0.0
    """
    return filter_(collection, pyd.matches(properties))
Example #2
0
def where(collection, properties):
    """Examines each element in a collection, returning an array of all
    elements that have the given properties.

    Args:
        collection (list|dict): Collection to iterate over.
        properties (dict): property values to filter by

    Returns:
        list: filtered list.

    Example:

        >>> results = where([{'a': 1}, {'b': 2}, {'a': 1, 'b': 3}], {'a': 1})
        >>> assert results == [{'a': 1}, {'a': 1, 'b': 3}]

    .. versionadded:: 1.0.0
    """
    return filter_(collection, pyd.matches(properties))
Example #3
0

@parametrize('case,expected', [((lambda a, b: a / b, 4, 2), 2)])
def test_attempt(case, expected):
    assert _.attempt(*case) == expected


@parametrize('case,expected',
             [((lambda a, b: a / b, 4, 0), ZeroDivisionError)])
def test_attempt_exception(case, expected):
    assert isinstance(_.attempt(*case), expected)


@parametrize(
    'pairs,case,expected',
    [(([_.matches({'b': 2}), _.constant('matches B')
        ], [_.matches({'a': 1}), _.constant('matches A')]), {
            'a': 1,
            'b': 2
        }, 'matches B'),
     (([_.matches({'b': 2}), _.constant('matches B')
        ], [_.matches({'a': 1}), _.invert]), {
            'a': 1,
            'b': 3
        }, {
            1: 'a',
            3: 'b'
        }),
     (([_.matches({'a': 1}), _.constant('matches A')
        ], [_.matches({'b': 2}), _.constant('matches B')]), {
            'a': 1,
Example #4
0
def test_matches(case, arg, expected):
    assert _.matches(case)(arg) is expected
Example #5
0
@parametrize('case,expected', [
    ((lambda a, b: a / b, 4, 2), 2)
])
def test_attempt(case, expected):
    assert _.attempt(*case) == expected


@parametrize('case,expected', [
    ((lambda a, b: a / b, 4, 0), ZeroDivisionError)
])
def test_attempt_exception(case, expected):
    assert isinstance(_.attempt(*case), expected)


@parametrize('pairs,case,expected', [
    (([_.matches({'b': 2}), _.constant('matches B')],
      [_.matches({'a': 1}), _.constant('matches A')]),
     {'a': 1, 'b': 2},
     'matches B'),
    (([_.matches({'b': 2}), _.constant('matches B')],
      [_.matches({'a': 1}), _.invert]),
     {'a': 1, 'b': 3},
     {1: 'a', 3: 'b'}),
    (([_.matches({'a': 1}), _.constant('matches A')],
      [_.matches({'b': 2}), _.constant('matches B')]),
     {'a': 1, 'b': 2},
     'matches A')
])
def test_cond(pairs, case, expected):
    func = _.cond(*pairs)
    assert func(case) == expected
Example #6
0
def test_attempt(case, expected):
    assert _.attempt(*case) == expected


@parametrize("case,expected",
             [((lambda a, b: a / b, 4, 0), ZeroDivisionError)])
def test_attempt_exception(case, expected):
    assert isinstance(_.attempt(*case), expected)


@parametrize(
    "pairs,case,expected",
    [
        (
            (
                [_.matches({"b": 2}),
                 _.constant("matches B")],
                [_.matches({"a": 1}),
                 _.constant("matches A")],
            ),
            {
                "a": 1,
                "b": 2
            },
            "matches B",
        ),
        (
            ([_.matches({"b": 2}),
              _.constant("matches B")], [_.matches({"a": 1}), _.invert]),
            {
                "a": 1,