def test_filters_order(): l = ListCollection([Bunch(a='b', b=5), Bunch(a='c', c='c')]) filterd_l = l.filtered(a='b').select(lambda o: o.b > 4) assert len(filterd_l) == 1 with pytest.raises(AttributeError): filterd_l = l.select(lambda o: o.b > 4)
def test_collections_slicing(): L = ListCollection("abcdef") assert L[0] == 'a' assert L[-1] == 'f' assert L[:2] == list('ab') assert L[-2:] == list('ef') assert L[::2] == list('ace') assert L[::-2] == list('fdb')
def test_collection_reprs(): def check(lst): str(lst) repr(lst) check(L) check(L.filtered(id=5)) NL = ListCollection(L, name='lst') check(NL) check(NL.filtered(id=5)) O = SimpleObjectCollection(L, ID_ATTRIBUTE='name') check(O) check(O.filtered(id=5)) NO = SimpleObjectCollection(L, ID_ATTRIBUTE='name', name='objs') check(NO) check(NO.filtered(id=5))
def test_collection_sample(): l = ListCollection("abcdef") assert len(l.sample(2.0)) == 2 with pytest.raises(AssertionError): l.sample(1.5)
def test_collection_filter(): lst = ListCollection("abcdef") assert lst.filtered(lambda c: c == 'a').sample(1) == ['a']
import pytest from easypy.collections import separate from easypy.collections import ListCollection, SimpleObjectCollection, partial_dict, UNIQUE, ObjectNotFound from easypy.bunch import Bunch from collections import Counter class Obj(Bunch): def __repr__(self): return "%(name)s:%(id)s:%(v)s" % self L = ListCollection( Obj(name=n, id=i, v=v) for n, i, v in zip("aabcdddeff", "1234567890", "xxxyyyxxyz") for _ in range(100)) def test_collection_filter(): lst = ListCollection("abcdef") assert lst.filtered(lambda c: c == 'a').sample(1) == ['a'] def test_collection_reprs(): def check(lst): str(lst) repr(lst) check(L) check(L.filtered(id=5))