コード例 #1
0
def test_double_filter():
    
    (first_iterable, second_iterable) = \
                        double_filter(lambda value: value % 2 == 0, xrange(20))
    assert tuple(first_iterable) == tuple(xrange(0, 20, 2))
    assert tuple(second_iterable) == tuple(xrange(1, 20, 2))
    
    (first_iterable, second_iterable) = \
                        double_filter(lambda value: value % 3 == 0, range(20))
    assert tuple(first_iterable) == tuple(range(0, 20, 3))
    assert tuple(second_iterable) == tuple(i for i in range(20) if i % 3 != 0)
    
    (first_lazy_tuple, second_lazy_tuple) = \
        double_filter(lambda value: value % 3 == 0, range(20), lazy_tuple=True)
    
    assert isinstance(first_lazy_tuple, nifty_collections.LazyTuple)
    assert isinstance(second_lazy_tuple, nifty_collections.LazyTuple)
    assert first_lazy_tuple.collected_data == \
                                         second_lazy_tuple.collected_data == []
    
    assert first_lazy_tuple == nifty_collections.LazyTuple(range(0, 20, 3))
    assert second_lazy_tuple == nifty_collections.LazyTuple(
        i for i in range(20) if i % 3 != 0
    )
    
コード例 #2
0
def test_double_filter():
    
    (first_iterable, second_iterable) = \
                        double_filter(lambda value: value % 2 == 0, range(20))
    assert tuple(first_iterable) == tuple(range(0, 20, 2))
    assert tuple(second_iterable) == tuple(range(1, 20, 2))
    
    (first_iterable, second_iterable) = \
                        double_filter(lambda value: value % 3 == 0, range(20))
    assert tuple(first_iterable) == tuple(range(0, 20, 3))
    assert tuple(second_iterable) == tuple(i for i in range(20) if i % 3 != 0)
    
    (first_lazy_tuple, second_lazy_tuple) = \
        double_filter(lambda value: value % 3 == 0, range(20), lazy_tuple=True)
    
    assert isinstance(first_lazy_tuple, nifty_collections.LazyTuple)
    assert isinstance(second_lazy_tuple, nifty_collections.LazyTuple)
    assert first_lazy_tuple.collected_data == \
                                         second_lazy_tuple.collected_data == []
    
    assert first_lazy_tuple == nifty_collections.LazyTuple(range(0, 20, 3))
    assert second_lazy_tuple == nifty_collections.LazyTuple(
        i for i in range(20) if i % 3 != 0
    )
    
コード例 #3
0
ファイル: dict_tools.py プロジェクト: cool-RR/python_toolbox
def filter_items(d, condition, double=False, force_dict_type=None):
    '''
    Get new dict with items from `d` that satisfy the `condition` functions.
    
    `condition` is a function that takes a key and a value.
    
    The newly created dict will be of the same class as `d`, e.g. if you passed
    an ordered dict as `d`, the result will be an ordered dict, using the
    correct order.
    
    Specify `double=True` to get a tuple of two dicts instead of one. The
    second dict will have all the rejected items.
    '''
    # todo future: possibly shallow-copy `d` to allow for dict classes that
    # have more state, (like default factory.)
    if force_dict_type is not None:
        dict_type = force_dict_type
    else:
        dict_type = type(d) if (type(d).__name__ != 'dictproxy') else dict
        
    if double:
        return map(
            dict_type,
            cute_iter_tools.double_filter(
                lambda (key, value): condition(key, value),
                d.iteritems()
            )
        )
    else:
        return dict_type(
                (key, value) for (key, value) in d.iteritems() if condition(key, value)
        )
コード例 #4
0
def test_double_filter():
    
    (first_iterable, second_iterable) = \
                        double_filter(lambda value: value % 2 == 0, xrange(20))
    assert tuple(first_iterable) == tuple(xrange(0, 20, 2))
    assert tuple(second_iterable) == tuple(xrange(1, 20, 2))
    
    (first_iterable, second_iterable) = \
                        double_filter(lambda value: value % 3 == 0, xrange(20))
    assert tuple(first_iterable) == tuple(xrange(0, 20, 3))
    assert tuple(second_iterable) == tuple(i for i in xrange(20) if i % 3 != 0)
    
コード例 #5
0
def filter_items(d, condition, double=False, force_dict_type=None):
    '''
    Get new dict with items from `d` that satisfy the `condition` functions.
    
    `condition` is a function that takes a key and a value.
    
    The newly created dict will be of the same class as `d`, e.g. if you passed
    an ordered dict as `d`, the result will be an ordered dict, using the
    correct order.
    
    Specify `double=True` to get a tuple of two dicts instead of one. The
    second dict will have all the rejected items.
    '''
    # todo future: possibly shallow-copy `d` to allow for dict classes that
    # have more state, (like default factory.)
    if force_dict_type is not None:
        dict_type = force_dict_type
    else:
        dict_type = type(d) if (type(d).__name__ != 'dictproxy') else dict

    if double:
        return tuple(
            map(
                dict_type,
                cute_iter_tools.double_filter(
                    lambda key_value: condition(key_value[0], key_value[1]),
                    d.items())))
    else:
        return dict_type((key, value) for (key, value) in d.items()
                         if condition(key, value))