def test_remove_ex(self): iterable_object = r'string iterate by symbol' out = r'' for ch in lazy.Iterator(iterable_object).remove(r' ').removeList( [r'i', r'o', r'a', r'e', r'y']): out += ch self.assertEqual(out, r'strngtrtbsmbl')
def speed_chain(): text = r'a{[()][(][)]}b' arr = [1, 2, 3, 4, 5] out = [] for ch in lazy.Iterator(text).chain(arr): out.append(ch)
def test_map_ex(self): iterable_object = r'string iterate by symbol' out = r'' for ch in lazy.Iterator(iterable_object).map( lambda x: r'b' if x == r's' else x): out += ch self.assertEqual(out, r'btring iterate by bymbol')
def test_liner_group(self): text = r'a{[()][(][)]}b' out = [] def states(ch, b, s): if ch == r'(' or ch == r')': return r'c_bracket' if ch == r'{' or ch == r'}': return r's_bracket' if ch == r'[' or ch == r']': return r'k_bracket' return None for ch in lazy.Iterator(text).groupby(states, recursive=False): out.append(ch) self.assertEqual(out, [r'a', [r'{'], [r'['], [r'(', r')'], [r']', r'['], [r'('], [r']', r'['], [r')'], [r']'], [r'}'], r'b'])
def test_recursive_group_swither_include(self): text = r'a{[()][(][)]}{}b' out = [] def states(ch, b, s): if ch == r'(' and s == r'down2': return r'down3' if ch == r')' and s == r'down3': return r'down2' if ch == r'[' and s == r'down1': return r'down2' if ch == r']' and s == r'down2': return r'down1' if ch == r'{' and s is None: return r'down1' if ch == r'}' and s == r'down1': return None return s for ch in lazy.Iterator(text).groupby(states, swither_inc=True): out.append(ch) self.assertEqual(out, [r'a', [r'{', [r'[', [r'(', r')'], r']'], [r'[', [r'(', r']', r'[', r')'], r']'], r'}'], [r'{', r'}'], r'b'])
def test_group_ex(self): iterable_object = r'string iterate by symbol' out = [] for ch in lazy.Iterator(iterable_object).groupby( lambda x, b, s: r'space' if x == r' ' else r'word', False): out.append(r''.join(ch)) self.assertEqual(out, [r'string', r' ', r'iterate', r' ', r'by', r' ', r'symbol'])
def test_group(self): text = r'word1word2word3' out = [] for ch in lazy.Iterator(text).groupby(lambda x, b, s: not x.isdigit()): if isinstance(ch, list): out.append(r''.join(ch)) else: out.append(ch) self.assertEqual(out, [r'word', r'1', r'word', r'2', r'word', r'3'])
def speed_cycle(): arr = [1, 2, 3, 4, 5] out = [] n = 0 for ch in lazy.Iterator(arr).cycle(): out.append(ch) n += 1 if n >= 5: break
def speed_zip(): one = [1, 2, 3, 4] two = [4, 3, 2, 1] out = [] def totuple(*args): return (i for i in args) for o in lazy.Iterator(one).zip(totuple, two): out.append(o)
def speed_groupby_rec(): text = r'a{[()][(][)]}[]b' out = [] def states(ch, b, s): if ch == r'(' or ch == r')': return r'c_bracket' if ch == r'{' or ch == r'}': return r's_bracket' if ch == r'[' or ch == r']': return r'k_bracket' return None for ch in lazy.Iterator(text).groupby(states): out.append(ch)
def test_liner_group_swither_include(self): text = '"string1"\'string2\'' text += '"string3\'quoted""string4\'quoted"' text += '\'string3"quoted\'\'string4"quoted\'' out = [] def states(ch, b, s): if (ch == '\'' or ch == r'"') and s is None: return ch if ch == s: return None return s for ch in lazy.Iterator(text).groupby(states, False, True): out.append(r''.join(ch)) self.assertEqual(out, [r'"string1"', '\'string2\'', '"string3\'quoted"', '"string4\'quoted"', '\'string3"quoted\'', '\'string4"quoted\''])
def speed_repeat(): out = [] for n in lazy.Iterator(xrange(10)).repeat(2): out.append(n)
def test_remove(self): text = r'word1,word2,word3' out = r'' for ch in lazy.Iterator(text).removeList([r'o', r'd']): out += ch self.assertEqual(out, r'wr1,wr2,wr3')
def test_filter_ex(self): iterable_object = r'string iterate by symbol' out = r'' for ch in lazy.Iterator(iterable_object).filter(lambda x: x != r' '): out += ch self.assertEqual(out, r'stringiteratebysymbol')
def test_filter(self): text = r'word1,word2,word3' out = r'' for ch in lazy.Iterator(text).filter(lambda x: x != r'o'): out += ch self.assertEqual(out, r'wrd1,wrd2,wrd3')
def test_map(self): text = r'word1,word2,word3' out = r'' for ch in lazy.Iterator(text).map(lambda x: x + r'n'): out += ch self.assertEqual(out, r'wnonrndn1n,nwnonrndn2n,nwnonrndn3n')
def speed_filter(): arr = [1, 2, 3, 4, 5] out = [] for ch in lazy.Iterator(arr).filter(lambda x: x != 2): out.append(ch)
def speed_map(): arr = [1, 2, 3, 4, 5] out = [] for n in lazy.Iterator(arr).map(lambda x: x**2): out.append(n)
def speed_takewhile_n(): text = r'abcdefg' out = [] for ch in lazy.Iterator(text).takewhile(4): out.append(ch)
def test_clone(self): text = r'word1,word2,word3' it1 = lazy.Iterator(text) next(it1) it2 = it1.copy() self.assertEqual(next(it1), next(it2))
def speed_takewhile_f(): text = r'abcdefg' out = [] for ch in lazy.Iterator(text).takewhile(lambda ch: ch != r'f'): out.append(ch)