Пример #1
0
def test_scan():
    assert (it(
        (1, 2,
         3)).scan(1,
                  lambda acc, ele: acc(acc._ * ele)).collect()) == [1, 2, 6]

    class Scanner:
        def __init__(self):
            self.chars = []
            self.state = 'GET'

    def state_machine(state, char):
        if state._.state == 'PRINT':
            print(char)
            state._.state = 'GET'
        elif state._.state == 'GET':
            state._.chars.append(char)
            state._.state = 'PRINT'
        else:
            raise Exception('Unreachable')

    scanner = Scanner()
    it('abcd').scan(scanner, state_machine).go()
    assert scanner.chars == ['a', 'c']

    assert it('abcd').scan(None, lambda _, __: ()).size_hint() == (4, 4)
    assert it('abcd').scan(None, lambda _, __: ()).rev().size_hint() == (4, 4)
Пример #2
0
def test_neq():
    assert it('asdf').neq('asdf1')
    assert it('asdf').skip(1).neq('asdf')

    assert it('asdf').rev().neq('asdf1')
    assert it('asdf').skip(1).rev().neq('asdf')
    assert not it('asdf').rev().neq('fdsa')
Пример #3
0
def test_partition():
    assert it((1, 2, 3)).partition(lambda x: x % 2 == 0) == ([2], [1, 3])
    assert it('aSdF').partition(lambda x: x.upper() == x) == (['S', 'F'],
                                                              ['a', 'd'])

    assert it((1, 2, 3)).rev().partition(lambda x: x % 2 == 0) == ([2], [3, 1])
    assert it('aSdF').rev().partition(lambda x: x.upper() == x) == (['F', 'S'],
                                                                    ['d', 'a'])
Пример #4
0
def test_unzip():
    gold = [*range(9)], [*range(8, -1, -1)]
    assert it(range(9)).zip(range(8, -1, -1)).unzip() == gold
    with pytest.raises(ChemicalException):
        it(([3, 2, 1, 0], ['a', 'b'])).unzip()
        assert False, "Should never get here"

    assert it('abc').zip('123').rev().unzip() == (['c', 'b',
                                                   'a'], ['3', '2', '1'])

    assert it('abc').zip('123').rev().take(2).unzip() == (['c',
                                                           'b'], ['3', '2'])
Пример #5
0
def test_intermittent_usage():
    a = it('asdf')
    assert a.next() == 'a'
    assert a.next() == 's'
    a = a.take(2)
    assert a.next() == 'd'
    assert a.next() == 'f'

    a = it('asdf')
    assert a.next() == 'a'
    assert a.next() == 's'
    a = a.take(2).peekable()
    assert a.peek() == 'd'
    assert a.next() == 'd'
    assert a.peek() == 'f'
    assert a.next() == 'f'
Пример #6
0
def test_eq():
    assert it('asdf').eq('asdf')
    assert not it('asdf').eq('asdfasdfasdfasdf')
    assert it('asdf').skip(1).eq('sdf')
    assert not it('asdf').eq((2, 1, 23))

    assert it('asdf').rev().eq('fdsa')
    assert not it('asdf').rev().eq('fdsafdsafdsa')
    assert it('asdf').skip(1).rev().eq('fds')
    assert not it('asdf').rev().eq((2, 1, 23))
Пример #7
0
def test_le():
    assert it('as').le('asd')
    assert it('asdf').cycle().take(5).le('asdfa')
    assert it('asdf').le('asdf')
    assert it('asdf').cycle().take(5).le('asdfa')

    assert it('as').rev().le('asd')
    assert it('asdf').cycle().take(5).rev().le('asdfa')
    assert it('asdf').rev().le('asdf')
    assert it('asdf').cycle().take(5).rev().le('asdfa')
Пример #8
0
def test_current():
    assert it(range(4)).current().collect(str) == '0123'
    assert (it(range(100)).skip(10).step_by(5).skip(10).current().skip_while(
        lambda x: x < 80).take(4).collect()) == [80, 85, 90, 95]

    assert it(
        range(10)).current().rev().collect() == [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

    with pytest.raises(ChemicalException):
        a = it('asdf').current()
        a.next()
        a.rev()

    assert it('asdf').current().size_hint() == (4, 4)
    assert it('asdf').current().rev().size_hint() == (4, 4)

    c = it('asdf').current()
    assert c.curr() == 'a'
    assert c.peek() == 'a'
    assert c.next() == 'a'
    assert c.curr() == 'a'
    assert c.peek() == 's'
    assert c.next() == 's'
    assert c.curr() == 's'
    assert c.peek() == 'd'
    assert c.next() == 'd'
    assert c.curr() == 'd'
    assert c.peek() == 'f'
    assert c.next() == 'f'
Пример #9
0
 def toc_tree(self):
     toc = self.fitz_doc._ebook.get_toc(simple=False)
     sect_count = len(toc)
     root = Section(
         title=self.metadata.title,
         pager=Pager(first=0, last=sect_count - 1),
         level=1,
     )
     stack = TreeStackBuilder(root)
     all_html_files = self.get_html_file_list()
     for (idx, (level, title, __, data)) in enumerate(toc):
         href = data["name"]
         section = stack.push(
             Section(
                 title=title,
                 pager=Pager(first=idx, last=idx),
                 level=level + 1,
                 data=dict(href=href),
             ))
         # ----------------
         filename, html_id = (href,
                              None) if "#" not in href else href.split("#")
         self._filename_to_section[filename] = section
         # ----------------
         if html_id is not None:
             self._split_section_anchor_ids.setdefault(filename,
                                                       []).append(html_id)
         # ----------------
         section_filename = (href if "#" not in href else
                             href.split("#")[0]).strip()
         if section_filename not in all_html_files:
             continue
         all_html_files[all_html_files.index(section_filename)] = section
         # End loop
     additional_html_files = ((sect, html_files) for (
         sect, *html_files) in more_itertools.split_before(
             all_html_files, pred=lambda item: isinstance(item, Section))
                              if html_files and isinstance(sect, Section))
     for (sect, additional_html_file_list) in additional_html_files:
         sect.data["additional_html_files"] = additional_html_file_list
         for aditional_file in additional_html_file_list:
             self._filename_to_section[aditional_file] = sect
     # ------------
     if sect_count == 0:
         href = (it(self.epub.items).find(
             lambda item: "html" in item.media_type).file_name)
         stack.push(
             Section(
                 title=_("Book contents"),
                 pager=Pager(first=0, last=0),
                 level=2,
                 data=dict(href=href),
             ))
         object.__setattr__(root.pager, "last", 0)
     return root
Пример #10
0
def test_it():
    assert list(it(range(3))) == [0, 1, 2]
    assert list(it('abc')) == ['a', 'b', 'c']
    assert list(it(dict(name='Pebaz'))) == ['name']

    with pytest.raises(ChemicalException):
        a = it('asdf')
        a.next()
        a.rev()

    assert it('asdf').size_hint() == (4, 4)
    assert it(it('asdf')).size_hint() == (4, 4)
    assert it(range(10)).size_hint() == (10, 10)
Пример #11
0
 def get_sorted_languages(cls):
     langs = cls.get_recognition_languages()
     current_lang = None
     with suppress(ChemicalException):
         current_lang = it(langs).find(
             lambda lang: lang.should_be_considered_equal_to(
                 app.current_language))
     if current_lang is not None:
         langs.remove(current_lang)
         langs.insert(0, current_lang)
     return langs
Пример #12
0
def test_cmp_by():
    func = lambda a, b: a.upper() == b.upper()
    assert it('asdf').cmp_by('asdf', func)
    assert not it('bsdf').cmp_by('asdf', func)
    assert (it('abc').cycle().take(10).cmp_by('abcabcabca', func))

    assert it('asdf').rev().cmp_by('fdsa', func)
    assert not it('bsdf').rev().cmp_by('fdsa', func)
    assert (it('abc').cycle().take(10).rev().cmp_by('acbacbacba', func))

    assert it((8, 9)).cmp_by((4, 3), lambda x, y: x > y)
Пример #13
0
def test_find():
    assert it('asdf').find(lambda x: x.upper() == 'S') == 's'
    with pytest.raises(ChemicalException):
        it('asdf').find(lambda x: x == 'say what?')

    assert it('asdf').rev().find(lambda x: x.upper() == 'S') == 's'
    with pytest.raises(ChemicalException):
        it('asdf').rev().find(lambda x: x == 'say what?')
Пример #14
0
def test_position():
    assert it('asdf').position(lambda x: x == 'd') == 2
    with pytest.raises(ChemicalException):
        it('asdf').position(lambda x: x == 'say what?')

    assert it('asdf').rev().position(lambda x: x == 'd') == 1
    with pytest.raises(ChemicalException):
        it('asdf').rev().position(lambda x: x == 'say what?')
Пример #15
0
def test_chain():
    assert it((1, 2, 3)).chain((4, 5, 6)).collect() == [1, 2, 3, 4, 5, 6]
    assert it('as').chain((3.1, 2.4)).collect() == ['a', 's', 3.1, 2.4]

    assert it('as').chain('df').rev().collect(str) == 'fdsa'
    assert it(range(10)).rev().chain('df').collect(str) == '9876543210df'
    assert it(range(10)).rev().chain('df').rev().collect(str) == 'fd0123456789'

    with pytest.raises(ChemicalException):
        a = it('asdf').chain('fdsa')
        a.next()
        a.rev()

    assert it('as').chain('df').size_hint() == (4, 4)
    assert it('as').chain('df').rev().size_hint() == (4, 4)
Пример #16
0
def test_cmp():
    assert it('asdf').cmp((1, 2, 3, 4)) == Ordering.Equal
    assert it('asdf').cmp((1, 2, 3, 4, 5)) == Ordering.Less
    assert it('asdf').cmp((1, 2, 3)) == Ordering.Greater

    assert it('asdf').rev().cmp((1, 2, 3, 4)) == Ordering.Equal
    assert it('asdf').rev().cmp((1, 2, 3, 4, 5)) == Ordering.Less
    assert it('asdf').rev().cmp((1, 2, 3)) == Ordering.Greater
Пример #17
0
def test_count():
    assert it('abc').count() == 3
    assert it('abc').skip(1).count() == 2
    assert it('abc').skip(1).take(2).count() == 2

    assert it('abc').rev().count() == 3
    assert it('abc').skip(1).rev().count() == 2
    assert it('abc').skip(1).take(2).rev().count() == 2
Пример #18
0
def test_sum():
    assert it((1, 2, 3)).sum() == 6
    assert it((1, 2, 3)).skip(1).sum() == 5
    assert it((1, 2, 3)).cycle().take(22).sum() == 43

    assert it((1, 2, 3)).rev().sum() == 6
    assert it((1, 2, 3)).skip(1).rev().sum() == 5
    assert it((1, 2, 3)).cycle().take(22).rev().sum() == 43
Пример #19
0
def test_last():
    assert it('abc').last() == 'c'
    assert it('abc').skip(1).last() == 'c'
    assert it('abc').cycle().take(8).last() == 'b'

    assert it('abc').rev().last() == 'a'
    assert it('abc').skip(1).rev().last() == 'b'
    assert it('abc').cycle().take(8).rev().last() == 'a'
Пример #20
0
def test_min():
    assert it([1]).min() == 1
    assert it((1, 2, 3, 4)).min() == 1
    assert it('asdf').min() == 'a'
    assert it((MyItem(1), MyItem(2), MyItem(3))).min() == MyItem(1)

    assert it(range(3)).rev().min() == 0
    assert it(range(3)).rev().take(2).rev().min() == 1
Пример #21
0
def test_max():
    assert it([1]).max() == 1
    assert it((1, 2, 3, 4)).max() == 4
    assert it('asdf').max() == 's'
    assert it((MyItem(1), MyItem(2), MyItem(3))).max() == MyItem(3)

    assert it(range(3)).rev().max() == 2
    assert it(range(3)).rev().take(2).rev().max() == 2
Пример #22
0
def test_skip():
    assert it(range(3)).skip(1).collect() == [1, 2]
    assert it(range(100)).skip(10).take(10).collect() == [*range(10, 20)]
    assert it(range(10)).skip(1).skip(1).collect() == [*range(2, 10)]

    assert it(range(3)).skip(1).rev().collect() == [2, 1]
    assert it(range(3)).skip(1).rev().skip(1).rev().collect() == [1]

    with pytest.raises(ChemicalException):
        a = it('asdf').skip(1)
        a.next()
        a.rev()

    assert it('asdf').skip(1).size_hint() == (3, 3)
    assert it('asdf').skip(1).rev().size_hint() == (3, 3)
Пример #23
0
def test_flatten():
    assert it([[1, 2, 3], [4, 5, 6]]).flatten().collect() == [1, 2, 3, 4, 5, 6]
    assert it([[1, 2, 3], [4, 5, 6],
               4]).flatten().collect() == [1, 2, 3, 4, 5, 6, 4]

    assert it([[1, 2, 3],
               [4, 5, 6]]).flatten().rev().collect() == [6, 5, 4, 3, 2, 1]
    assert it('abc').zip('123').flatten().rev().collect(str) == '3c2b1a'

    assert it('abc').zip('123').flatten().size_hint() == (6, 6)
    assert it('abc').zip('123').flatten().rev().size_hint() == (6, 6)
Пример #24
0
def test_cycle():
    assert it(range(3)).cycle().take(6).collect() == [0, 1, 2, 0, 1, 2]
    assert it('abc').cycle().take(6).collect() == [
        'a', 'b', 'c', 'a', 'b', 'c'
    ]

    assert it('abc').cycle().rev().take(6).collect(str) == 'cbacba'
    assert it('abc').cycle().rev().take(6).rev().collect(str) == 'abcabc'

    with pytest.raises(ChemicalException):
        a = it('asdf').cycle()
        a.next()
        a.rev()

    assert it('asdf').cycle().size_hint() == (0, None)
    assert it('asdf').cycle().rev().size_hint() == (0, None)
Пример #25
0
def test_inspect():
    seen = []
    it('abc').inspect(lambda x: seen.append(x.upper())).go()
    assert seen == ['A', 'B', 'C']

    seen = []
    it('abc').inspect(lambda x: seen.append(x.upper())).rev().go()
    assert seen == ['C', 'B', 'A']

    with pytest.raises(ChemicalException):
        a = it('asdf').inspect(print)
        a.next()
        a.rev()

    assert it('abc').inspect(lambda x: ()).size_hint() == (3, 3)
    assert it('abc').inspect(lambda x: ()).rev().size_hint() == (3, 3)
Пример #26
0
def test_go():
    seen = []
    it('abc').inspect(lambda x: seen.append(x.upper())).go()
    assert seen == ['A', 'B', 'C']

    seen = []
    it('abc').skip(1).take(1).inspect(lambda x: seen.append(x.upper())).go()
    assert seen == ['B']

    seen = []
    it('abc').inspect(lambda x: seen.append(x.upper())).rev().go()
    assert seen == ['C', 'B', 'A']

    seen = []
    (it('abc').skip(1).take(1).inspect(
        lambda x: seen.append(x.upper())).rev().go())
    assert seen == ['B']
Пример #27
0
def test_par_iter():
    # Mock parallel execution of HTTP requests
    # import requests

    class requests:
        def __init__(self):
            self.status_code = 200
            self.reason = 'OK'
            self.ok = True

        @staticmethod
        def get(url, timeout):
            return requests()

    urls = [
        'https://aws.amazon.com', 'https://www.google.com',
        'https://www.microsoft.com'
    ]

    assert (it(urls).map(lambda u: requests.get(u, timeout=1)).map(
        lambda u: u.status_code if u.ok else u.reason).par_iter().collect()
            ) == [200] * len(urls)

    assert (it(urls).map(lambda u: requests.get(u, timeout=1)).map(
        lambda u: u.status_code
        if u.ok else u.reason).par_iter().rev().collect()) == [200] * len(urls)

    assert (it(urls).map(lambda u: requests.get(u, timeout=1)).map(
        lambda u: u.status_code if u.ok else u.reason).rev().par_iter().rev().
            collect()) == [200] * len(urls)

    assert it('asdf').par_iter().collect(str) == 'asdf'
    assert it('asdf').par_iter().rev().collect(str) == 'fdsa'

    with pytest.raises(ChemicalException):
        a = it('asdf').par_iter()
        a.next()
        a.rev()

    assert it('asdf').par_iter().size_hint() == (4, 4)
    assert it('asdf').par_iter().rev().size_hint() == (4, 4)
Пример #28
0
def test_for_each():
    stuff = []
    it((1, 2, 3)).for_each(lambda x: stuff.append(x)).go()
    assert stuff == [1, 2, 3]

    stuff = []
    it((1, 2, 3)).for_each(lambda x: stuff.append(x)).rev().go()
    assert stuff == [3, 2, 1]

    with pytest.raises(ChemicalException):
        stuff = []
        a = it((1, 2, 3)).for_each(lambda x: stuff.append(x))
        a.next()
        a.rev()

    assert it('asdf').for_each(lambda _: ()).size_hint() == (4, 4)
    assert it('asdf').for_each(lambda _: ()).rev().size_hint() == (4, 4)
Пример #29
0
def test_zip():
    gold = [(0, 7), (1, 6), (2, 5), (3, 4), (4, 3), (5, 2), (6, 1), (7, 0)]
    assert it(range(8)).zip(range(7, -1, -1)).collect() == gold
    assert it(range(8)).zip(range(7, -100000, -1)).collect() == gold

    assert it('abc').zip('123').rev().collect() == [('c', '3'), ('b', '2'),
                                                    ('a', '1')]
    assert it('abc').zip('123456').rev().collect() == [('c', '6'), ('b', '5'),
                                                       ('a', '4')]

    with pytest.raises(ChemicalException):
        a = it('asdf').zip('fdsa')
        a.next()
        a.rev()

    assert it('asdf').zip('fdsa').size_hint() == (8, 8)
    assert it('asdf').zip('fdsa').rev().size_hint() == (8, 8)
Пример #30
0
def test_take_while():
    assert it(range(10)).take_while(lambda x: x < 4).collect() == [0, 1, 2, 3]
    assert it([10, 2, 20]).take_while(lambda x: x < 4).collect() == [2]

    assert it([3, 10, 2,
               20]).take_while(lambda x: x < 4).rev().collect() == [2, 3]

    with pytest.raises(ChemicalException):
        a = it('asdf').take_while(lambda x: x.upper() in 'AS')
        a.next()
        a.rev()

    assert it('asdf').take_while(lambda _: ()).size_hint() == (0, 4)
    assert it('asdf').take_while(lambda _: ()).rev().size_hint() == (0, 4)