def test_split_buffer(): s = Split([ascii_lowercase, ascii_uppercase]) LOW_UP = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" assert "".join(s()) == LOW_UP s = Split([ascii_lowercase, ascii_uppercase], bufsize=1) # bufsize makes no difference, because these are Sources, not Sequences assert "".join(s.run([])) == LOW_UP assert "".join(s.run([0])) == LOW_UP # test Split in Source s = Source(ascii_lowercase, Split([lambda s: s.upper(), lambda s: s.lower()])) assert "".join( s()) == "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" s = Source(ascii_lowercase, Split([lambda s: s.upper(), lambda s: s.lower()], bufsize=1)) assert "".join( s()) == "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz" # bufsize = None or 1000 does not matter s = Source(ascii_lowercase, Split([lambda s: s.upper(), lambda s: s.lower()], bufsize=None)) assert "".join( s()) == "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
def test_split_sequence_with_cache(): lowercase_cached_seq[0].drop_cache() s = Source(ascii_lowercase, Split([lambda s: s.upper(), lowercase_cached_seq])) # print(list(Cache(lowercase_cached_filename)._load_flow())) # print(Call(Cache(lowercase_cached_filename), call="_load_flow")()) # print(list(Call(Cache(lowercase_cached_filename), call="_load_flow")())) assert "".join( s()) == "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" # cache is actually used # Single Cache element s = Source(ascii_uppercase, Split([lambda s: s.upper(), lowercase_cached_seq])) assert "".join( s()) == "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" # Sequence with Cache s = Source( ascii_uppercase, Split([lambda s: s.upper(), (lambda val: val, lowercase_cached_seq)])) assert "".join( s()) == "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" # sequences are called in the order they were initialized in Source s = Source( ascii_uppercase, Split([ lambda s: s.upper(), (lambda val: val, Cache(lowercase_cached_filename)), id_ ])) assert "".join(s()) == \ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" lowercase_cached_seq[0].drop_cache()
def test_cache(): lowercase_cached_seq[0].drop_cache() s = Source(ascii_lowercase, lowercase_cached_seq) assert "".join(s()) == "abcdefghijklmnopqrstuvwxyz" # cache is actually used s = Source(ascii_uppercase, lowercase_cached_seq) assert "".join(s()) == "abcdefghijklmnopqrstuvwxyz" lowercase_cached_seq[0].drop_cache()
def test_print(capsys): # this is one of my very first tests, so it's a bit complicated # mocker.patch('__future__.print') it5 = lambda: Slice(5) printel = Print(end=' ') seq1 = Source(lena.flow.CountFrom(0), printel, it5()) [print(val, sep=" ", end=" ") for val in seq1()] captured = capsys.readouterr() assert captured.out == "0 0 1 1 2 2 3 3 4 4 " seq2 = Source(lena.flow.CountFrom(0), printel, it5(), lena.flow.End()) list(seq2()) captured = capsys.readouterr() assert captured.out == "0 1 2 3 4 "
def test_count_from(): s = Source( CountFrom(), # lambda i: (i, {str(i): i}), # DropContext( # Slice(5), # lambda i: i + 1, # ), Slice(10), ) results = s() assert [result for result in s()] == list(range(10)) # [ # (1, {'0': 0}), (2, {'1': 1}), (3, {'2': 2}), (4, {'3': 3}), (5, {'4': 4}) # ] # test start it = CountFrom(start=10) assert list(islice(it(), 5)) == list(range(10, 15)) # test step it = CountFrom(step=2) assert list(islice(it(), 5)) == list(range(0, 10, 2)) # test start and step it = CountFrom(start=10, step=2) assert list(islice(it(), 5)) == list(range(10, 20, 2))
def main(): write = Write("output") s = Source( get_filenames, GetCoordinates(), Split([ ( Variable("x", lambda coord: coord[0]), Histogram(lena.math.mesh((-10, 10), 100)), ), ( Variable("y", lambda coord: coord[1], latex_name="y", unit="mm"), Histogram(lena.math.mesh((-10, 10), 100)), ), ]), MakeFilename("{{variable.name}}"), # UpdateContext("output.filename", "x"), ToCSV(), write, RenderLaTeX("histogram_1d.tex"), write, LaTeXToPDF(), PDFToPNG(), ) return s()
def main(): s = Sequence(lambda i: pow(-1, i) * (2 * i + 1), ) results = s.run([0, 1, 2, 3]) for res in results: print(res, end=" ") # 1 -3 5 -7 # # s0 = Source( # lena.flow.CountFrom(0), # s, # lena.flow.Slice(5), # ) # results = s0() # print(list(results)) spi = Source( lena.flow.CountFrom(0), s, lena.flow.Slice(10**6), lambda x: 4. / x, Sum(), ) results = list(spi()) print(results) # [3.1415916535897743] return results
def test_fill_request_seq(): s1 = Source( ones, FillRequestSeq(FillRequest(Sum(start=0), reset=False, buffer_input=True), reset=False, buffer_input=True, bufsize=1), Slice(10)) assert list(s1()) == list(range(1, 11))
def test_cache_2(): # works well in sequence s1 = Source( cnt1, Slice(2), Cache("cache.tmp"), ) res1 = [result for result in s1()] assert len(res1) == 2 assert res1 == [1, 2] # works well after cached s2 = Source( cnt1, Cache("cache.tmp"), ) res2 = [result for result in s2()] assert res1 == res2 # works well with later elements in sequence s3 = Source( cnt1, Cache("cache.tmp"), Slice(1), ) res3 = [result for result in s3()] assert res3 != res2 assert res2 == res3 + [2] # works well for flow with context s4c = Source( cnt1c, Slice(2), Cache("cache_c.tmp"), ) res4 = [result for result in s4c()] assert res4 == [(1, {'1': 1}), (2, {'2': 2})] # clean up os.remove("cache.tmp") os.remove("cache_c.tmp")
def test_source(): it5 = Slice(5) sseq = Source(cnt0, it5) assert list(sseq()) == [0, 1, 2, 3, 4] ## Test special double underscore methods, emulating a container. # can create a list from that seq2 = Source(*list(sseq)) # can take len assert len(seq2) == 2 # can get item assert callable(seq2[0]) # deletion is prohibited with pytest.raises(TypeError): del seq2[0] # setting elements is prohibited with pytest.raises(TypeError): seq2[0] = 0 with pytest.raises(lena.core.LenaTypeError): s = Source() with pytest.raises(lena.core.LenaTypeError): s = Source(1)
def test_sequence(): it5 = Slice(5) sseq = Source(cnt0, it5) assert list(sseq()) == [0, 1, 2, 3, 4] seq = Sequence(RunIf(int, lambda i: i + 1)) print(list(seq.run(sseq()))) # Test special double underscore methods, # emulating a container. # can create a list from that seq2 = Sequence(*list(seq)) # can take len assert len(seq2) == 1 # can get item assert isinstance(seq2[0], RunIf) # deletion is prohibited # I don't know why, but last time both exceptions below were AttributeError # it changed after I based Sequence on LenaSequence # (based on object, which was not before). with pytest.raises(TypeError): del seq2[0] # setting elements is prohibited with pytest.raises(TypeError): seq2[0] = 0
def test_sequence_init(): it5 = Slice(5) sseq = Source(cnt0, it5) with pytest.raises(LenaTypeError): sseq = Source(cnt0, it5, ())
import string from lena.core import Source, Sequence from lena.flow import Cache class ASCIILowercase(object): """abcdefghijklmnopqrstuvwxyz""" def __call__(self): for s in string.ascii_lowercase: yield s class ASCIIUppercase(object): def __call__(self): for s in string.ascii_uppercase: yield s ascii_lowercase = Source(ASCIILowercase()) ascii_uppercase = Source(ASCIIUppercase()) # The Sequence's cache will be in the directory # from which the module is called, not where this file is! lowercase_cached_filename = "lowercase_cached.pkl" lowercase_cached_seq = Sequence(Cache(lowercase_cached_filename)) ## fill the cache - need to convert to list that first. #lowercase_cached_seq.run(list(ascii_lowercase())) id_ = lambda val: val
def test_fill_request(): # todo: modify Source to use iterables (incl. those from itertools) # from itertools import repeat def ones(): while True: yield 1 ## reset works # reset=False works s0 = Source(ones, Slice(10), FillRequest(Sum(), reset=False, bufsize=1, buffer_input=True)) assert list(s0()) == list(range(1, 11)) # buffer_output works the same s0o = Source( ones, Slice(10), FillRequest(Sum(), reset=False, bufsize=1, buffer_output=True)) assert list(s0o()) == list(range(1, 11)) # reset=True works s10 = Source(ones, Slice(10), FillRequest(Sum(), reset=True, bufsize=1, buffer_input=True)) assert list(s10()) == [1 for _ in range(10)] # buffer_output works the same s10o = Source( ones, Slice(10), FillRequest(Sum(), reset=True, bufsize=1, buffer_output=True)) assert list(s10o()) == [1 for _ in range(10)] # Slice can be moved after FR if bufsize=1 s11 = Source(ones, FillRequest(Sum(), reset=True, bufsize=1, buffer_input=True), Slice(10)) assert list(s11()) == [1 for _ in range(10)] # bufsize 10 s1 = Source(CountFrom(), Slice(100), FillRequest(Sum(), reset=False, bufsize=10, buffer_input=True)) results = list(s1()) assert results == [45, 190, 435, 780, 1225, 1770, 2415, 3160, 4005, 4950] # bufsize 1 s2 = Source(CountFrom(), Slice(10), FillRequest(Sum(), reset=False, bufsize=1, buffer_input=True)) results = list(s2()) assert results == [0, 1, 3, 6, 10, 15, 21, 28, 36, 45] # derive from FillCompute s3 = Source(CountFrom(), Slice(10), FillRequest(Sum(), bufsize=1., reset=True, buffer_input=True)) results = list(s3()) assert results == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # run works correctly class MyRun(): def fill(self, val): pass def request(self): pass def reset(self): pass def run(self, flow): for val in flow: yield True fr = FillRequest(MyRun(), buffer_input=True, reset=True) results = list(fr.run(iter([1]))) assert results == [True] results = list(fr.run(iter([]))) assert results == [] ## run for empty flow should yield nothing fr = FillRequest(Sum(), reset=True, buffer_input=True) # nothing if yield_on_remainder is false assert list(fr.run([])) == [] # nothing for yield_on_remainder=True # - because no remainder is not a remainder. fr = FillRequest(Sum(), yield_on_remainder=True, reset=True, buffer_input=True) assert list(fr.run([])) == []
def test_fill_compute_seq(): # wrong initialization # no FillCompute element with pytest.raises(LenaTypeError): FillComputeSeq(lambda x: x) # Run can't be cast to a FillInto element with pytest.raises(LenaTypeError): FillComputeSeq(Run(Mean()), Mean()) s1 = Source(cnt1, mul2, Slice(10), FillCompute(Mean())) assert next(s1()) == 11. s2 = Source(cnt1, Slice(10), FillComputeSeq(Mean(), mul2)) assert list(s2()) == [11.] s3 = Source(cnt1, Slice(10), FillComputeSeq(mul2, FillCompute(Mean()))) assert next(s3()) == 11.0 s4 = Source(cnt1, Slice(10), FillComputeSeq(mul2, FillCompute(Mean()), Print())) # tuple initialization works s41 = Source(cnt1, Slice(10), FillComputeSeq((mul2, FillCompute(Mean()), Print()))) assert next(s4()) == 11.0 assert next(s41()) == 11.0 # 11.0 s5 = FillComputeSeq(mul2, FillCompute(Mean()), Print()) assert is_fill_compute_seq(Mean()) # Source is not fill_compute_seq. It's also non-iterable. assert not is_fill_compute_seq(s4) # Test Source containing FillComputeSeq # test LenaSequence # can take len assert len(s1) == 4 assert len(s2) == 3 assert len(s3) == 3 assert len(s4) == 3 # can get item assert isinstance(s2[1], Slice) # Test FillComputeSeq assert len(s5) == 3 # can get item assert isinstance(s5[1], FillCompute) # deletion is prohibited with pytest.raises(TypeError): del s5[0] # setting elements is prohibited with pytest.raises(TypeError): s5[0] = 0 # wrong subclass class MyFC(FillComputeSeq): def __init__(self): pass myfc = MyFC() with pytest.raises(LenaNotImplementedError): myfc.fill(0)
def test_fill_request_seq_old(): # FillRequest with preprocess works # how not to write tests. Unclear what the result should be. # But I remember that I carefully checked that when written. # yes, really 3 reset=False, because 3 explicit FillRequests. s1 = Source( cnt1, # this FillRequest is optional FillRequest( FillRequestSeq( lambda x: x - 1, FillRequest(Sum(start=0), reset=False, buffer_input=True), reset=False, buffer_input=True, ), buffer_input=True, reset=False, ), Slice(10)) assert list(s1()) == [0, 1, 3, 6, 10, 15, 21, 28, 36, 45] # FR with postprocess works s2 = Source( cnt1, FillRequestSeq(FillRequest(Sum(start=0), reset=False, buffer_input=True), lambda x: x - 1, reset=False, buffer_input=True), Slice(10)) assert list(s2()) == [0, 2, 5, 9, 14, 20, 27, 35, 44, 54] # bufsize initialization works frs = FillRequestSeq(FillRequest(Sum(), reset=True, buffer_input=True), buffer_input=True, reset=False) assert frs._fr.bufsize == 1 frs2 = FillRequestSeq(FillRequest(Sum(), reset=True, buffer_input=True), bufsize=2, buffer_input=True, reset=True) assert frs2._fr.bufsize == 2 # wrong keyword raises # this is a TypeError now, since kwargs are passed to FillRequest with pytest.raises(TypeError): # with pytest.raises(LenaTypeError): FillRequestSeq(FillRequest(Sum(), reset=True, buffer_input=True), unknown=True, buffer_input=True, reset=False) # wrong subclassing raises (need to implement fill) class MyFR(FillRequestSeq): def __init__(self): pass myfr = MyFR() with pytest.raises(LenaNotImplementedError): myfr.fill(0) # fill with preprocess works flow = list(range(0, 4)) s3 = FillRequestSeq(lambda x: x - 1, FillRequest(Sum(), reset=True, buffer_input=True), reset=True, buffer_input=True) for val in flow: s3.fill(val) assert list(s3.request()) == [-1, 0, 1, 2] # the old value was just the final value. # Now we yield results for each buffer. # assert list(s3.request()) == [2] # direct fill works s4 = FillRequestSeq(FillRequest(Sum(), reset=False, buffer_input=True), reset=False, buffer_input=True) for val in flow: s4.fill(val) assert list(s4.request()) == [0, 1, 3, 6]