Ejemplo n.º 1
0
 def enumerate(self):
     test_list = [1, 3, 2, 2, -1, 4]
     res = Qi(test_list).enumerate().to_list()
     assert res == [(0, 1), (1, 3), (2, 2), (3, 2), (4, -1), (5, 4)]
     test_list = list('hello')
     res = Qi(test_list).enumerate().to_list()
     assert res == [(0, 'h'), (1, 'e'), (2, 'l'), (3, 'l'), (4, 'o')]
Ejemplo n.º 2
0
 def test_sort(self):
     test_list = [1, 3, 2, 2, -1, 4]
     res = Qi(test_list).sort().to_list()
     assert res == [-1, 1, 2, 2, 3, 4]
     res = Qi(test_list).sort(key=lambda e: -e).to_list()
     assert res == [4, 3, 2, 2, 1, -1]
     res = Qi(test_list).sort(reverse=True).to_list()
     assert res == [4, 3, 2, 2, 1, -1]
Ejemplo n.º 3
0
 def test_min_max(self):
     test_list = []
     with self.assertRaises(ValueError) as context:
         res = Qi(test_list).min() == 1
     with self.assertRaises(ValueError) as context:
         res = Qi(test_list).max() == 1
     with self.assertRaises(ValueError) as context:
         res = Qi(test_list).max(key=lambda e: -(e - 2)**2)
     with self.assertRaises(ValueError) as context:
         res = Qi(test_list).min(key=lambda e: (e - 2)**2)
Ejemplo n.º 4
0
 def test_to_list(self):
     test_list = []
     res = Qi(test_list) \
                 .to_list()
     assert res == []
Ejemplo n.º 5
0
 def test_count_contains(self):
     test_list = []
     assert Qi(test_list).count() == 0
     assert Qi(test_list).contains(2) == False
Ejemplo n.º 6
0
 def test_first(self):
     test_list = []
     with self.assertRaises(StopIteration) as context:
         res = Qi(test_list).first()
Ejemplo n.º 7
0
 def test_filter(self):
     test_list = []
     res = Qi(test_list) \
             .filter(lambda e: e >= 2) \
             .to_list()
     assert res == []
Ejemplo n.º 8
0
 def test_map(self):
     test_list = []
     res = Qi(test_list) \
             .map(lambda e: e**2) \
             .to_list()
     assert res == []
Ejemplo n.º 9
0
 def test_filter(self):
     test_list = [1, 3, 2, 2, -1, 4]
     res = Qi(test_list) \
             .filter(lambda e: e >= 2) \
             .to_list()
     assert res == [3, 2, 2, 4]
Ejemplo n.º 10
0
 def test_distinct(self):
     test_list = [1, 3, 2, 2, -1, 4]
     res = Qi(test_list).distinct().to_list()
     assert res == [1, 3, 2, -1, 4]  # second 2 is dropped
Ejemplo n.º 11
0
 def test_to_list(self):
     test_list = [1, 3, 2, 2, -1, 4]
     res = Qi(test_list) \
                 .to_list()
     assert res == [1, 3, 2, 2, -1, 4]
Ejemplo n.º 12
0
 def test_min_max(self):
     test_list = [1, 3, 2, 2, -1, 4]
     assert Qi(test_list).min() == -1
     assert Qi(test_list).max() == 4
     assert Qi(test_list).max(key=lambda e: -(e - 2)**2) == 2
     assert Qi(test_list).min(key=lambda e: (e - 2)**2)
Ejemplo n.º 13
0
 def test_count_contains(self):
     test_list = [1, 3, 2, 2, -1, 4]
     assert Qi(test_list).count() == 6
     assert Qi(test_list).contains(2) == True
     assert Qi(test_list).contains(13) == False
Ejemplo n.º 14
0
 def test_first(self):
     test_list = [1, 3, 2, 2, -1, 4]
     assert Qi(test_list).first() == 1
Ejemplo n.º 15
0
 def test_distinct(self):
     test_list = []
     res = Qi(test_list).distinct().to_list()
     assert res == []
Ejemplo n.º 16
0
import unittest
from iter_query import Qi
from collections import defaultdict



def yield_trace(trace):
    for i in 'MyExampleList':
        print('trace {}'.format(i))        
        trace[i]+= 1 
        yield i
        
trace = defaultdict(int)        
Qi(yield_trace(trace)).to_list()

trace = defaultdict(int)        
Qi(yield_trace(trace)).take(5).to_list()

trace = defaultdict(int)        
Qi(yield_trace(trace)).take(5).count()

trace = defaultdict(int)        
Qi(yield_trace(trace)).max()

'a'.upper()

trace = defaultdict(int)        
Qi(yield_trace(trace)) \
        .filter(lambda e: e==e.upper()) \
        .map(lambda e: e.lower()) \
        .to_list()
Ejemplo n.º 17
0
 def test_sort(self):
     test_list = []
     res = Qi(test_list).sort().to_list()
     assert res == []
Ejemplo n.º 18
0
 def test_map(self):
     test_list = [1, 3, 2, 2, -1, 4]
     res = Qi(test_list) \
             .map(lambda e: e**2) \
             .to_list()
     assert res == [1, 9, 4, 4, 1, 16]