Пример #1
0
 def test_stream_iadd(self):
     s1 = stream([1, 2])
     s1 += [3, 4]
     s1 += stream(xrange(5, 6))  # use xrange to cover the iterator case
     self.assertEquals(s1.toList(), [1, 2, 3, 4, 5])
     self.assertEquals(s1.toList(), [1, 2, 3, 4, 5])  # second time to exclude one time iterator bug
     self.assertEquals(s1.toList(), [1, 2, 3, 4, 5])
Пример #2
0
 def test_filterFromGeneratorReinstantiatesProperly(self):
     s = stream(ItrFromFunc(lambda: (i for i in xrange(5))))
     s = s.filter(lambda e: e % 2 == 0)
     self.assertEquals(s.toList(), [0, 2, 4])
     self.assertEquals(s.toList(), [0, 2, 4])
     s = stream(xrange(5)).filter(lambda e: e % 2 == 0)
     self.assertEquals(s.toList(), [0, 2, 4])
     self.assertEquals(s.toList(), [0, 2, 4])
Пример #3
0
 def testStream(self):
     s = self.s
     self.assertEquals(list(ifilter(lambda i: i % 2 == 0, s())), [2])
     self.assertEquals(list(s().filter(lambda i: i % 2 == 0)), [2])
     self.assertEquals(s().filter(lambda i: i % 2 == 0).toList(), [2])
     self.assertEquals(s()[1], 2)
     self.assertEquals(s()[1:].toList(), [2, 3])
     self.assertEqual(s().take(2).toList(), [1, 2])
     self.assertAlmostEqual(stream((0, 1, 2, 3)).filter(lambda x: x > 0).entropy(), 1.4591479)
     self.assertEquals(stream([(1, 2), (3, 4)]).zip().toList(), [(1, 3), (2, 4)])
Пример #4
0
 def test_stream_add(self):
     s1 = stream([1, 2])
     s2 = stream([3, 4])
     s3 = s1 + s2
     ll = s3.toList()
     self.assertEquals(s3.toList(), [1, 2, 3, 4])
     self.assertEquals(s3.toList(), [1, 2, 3, 4])  # second time to exclude one time iterator bug
     s1 = s1 + s2
     self.assertEquals(s1.toList(), [1, 2, 3, 4])
     self.assertEquals(s1.toList(), [1, 2, 3, 4])  # second time to exclude one time iterator bug
Пример #5
0
 def test_mkString(self):
     streamToTest = stream(('a', 'b', 'c'))
     mock = MagicMock()
     joiner = ","
     streamToTest.join = mock
     streamToTest.mkString(joiner)
     mock.assert_called_once_with(joiner)
Пример #6
0
    def testSdictToJson(self):
        from pyxtension.Json import Json

        j = stream((("a", 2), (3, 4))).toMap().toJson()
        self.assertIsInstance(j, Json)
        self.assertEqual(j.a, 2)
        self.assertDictEqual(j, {'a': 2, 3: 4})
Пример #7
0
    def test_joinWithFunction(self):
        class F:
            def __init__(self):
                self.counter = 0

            def __call__(self, *args, **kwargs):
                self.counter += 1
                return str(self.counter)

        strings = ('a', 'b', 'c')
        f = F()
        self.assertEqual(stream(iter(strings)).join(f), "a1b2c")
Пример #8
0
    def test_fastmap_time(self):
        def sleepFunc(el):
            time.sleep(0.3)
            return el * el

        s = stream(xrange(100))
        t1 = time.time()
        res = s.fastmap(sleepFunc, poolSize=50).toSet()
        dt = time.time() - t1
        expected = set(i * i for i in xrange(100))
        self.assertSetEqual(res, expected)
        self.assertLessEqual(dt, 1.5)
Пример #9
0
 def testStreamsFromGenerator(self):
     sg = stream(ItrFromFunc(lambda: (i for i in range(4))))
     self.assertEqual(sg.size(), 4)
     self.assertEqual(sg.size(), 4)
     self.assertEqual(sg.filter(lambda x: x > 1).toList(), [2, 3])
     self.assertEqual(sg.filter(lambda x: x > 1).toList(), [2, 3])
     self.assertEqual(sg.map(lambda x: x > 1).toList(), [False, False, True, True])
     self.assertEqual(sg.map(lambda x: x > 1).toList(), [False, False, True, True])
     self.assertEqual(sg.head(), 0)
     self.assertEqual(sg.head(), 0)
     self.assertEqual(sg.map(lambda i: i ** 2).enumerate().toList(), [(0, 0), (1, 1), (2, 4), (3, 9)])
     self.assertEqual(sg.reduce(lambda x, y: x + y, 5), 11)
Пример #10
0
    def search(self, search_rq: SearchRequest) -> [Video]:
        validate_request(search_rq)

        st = search_rq.SearchType
        sf = search_rq.SearchField

        if st is SearchType.isFavourite:
            return self.search_favourite()

        search_words = [word.strip(" ").lower() for word in sf.split(" ") if word.strip(" ") != ""]
        videos = stream([self.search_by_word(st, word) for word in search_words]).flatMap()

        return self.rank_distinct(videos)
Пример #11
0
 def test_tokenize_text(self):
     tokenizer = SpacyTokenizer()
     sentences = tokenizer.tokenizeText(
         "I am happy? All U.K. is happy after Brexit?")
     self.assertEqual(2, len(sentences))
     tagged_text = sentences[0].getTaggedText()
     jsoned = stream(tagged_text).map(lambda _: _.toJson()).toList()
     self.assertListEqual([{
         'word': 'I',
         'tag': 'PRP',
         'stemmed': '-PRON-'
     }, {
         'word': 'am',
         'tag': 'VBP',
         'stemmed': 'be'
     }, {
         'word': 'happy',
         'tag': 'JJ',
         'stemmed': 'happy'
     }, {
         'tag': 'SYM'
     }], jsoned)
Пример #12
0
 def test_reversedException(self):
     s = stream(xrange(1, 2, 3))
     with self.assertRaises(TypeError):
         s.reversed()
Пример #13
0
 def test_unique_empty_stream(self):
     s = stream([])
     self.assertListEqual(s.unique().toList(), [])
Пример #14
0
 def test_pstddev_nominal(self):
     s = stream([1, 2, 3, 4])
     self.assertAlmostEqual(s.pstddev(), 1.118033988749895)
Пример #15
0
 def test_unique_generator_stream(self):
     s = stream(ItrFromFunc(lambda: xrange(4)))
     u = s.unique()
     self.assertListEqual(u.toList(), [0, 1, 2, 3])
     self.assertListEqual(u.toList(), [0, 1, 2, 3])
Пример #16
0
 def test_unique_nominal(self):
     s = stream([1, 2, 3, 1, 2])
     self.assertListEqual(s.unique().toList(), [1, 2, 3])
Пример #17
0
 def test_mean_exception(self):
     with self.assertRaises(ValueError):
         stream([]).mean()
Пример #18
0
 def test_values_nominal(self):
     self.assertListEqual(stream([(1, 'a'), (2, 'bb'), (0, '')]).values().toList(), ['a', 'bb', ''])
Пример #19
0
    def testStreamToJson(self):
        from pyxtension.Json import JsonList

        j = stream((("a", 2), (3, 4))).toJson()
        self.assertIsInstance(j, JsonList)
        self.assertListEqual(j, [["a", 2], [3, 4]])
Пример #20
0
 def test_pstddev_exception(self):
     with self.assertRaises(ValueError):
         stream([]).pstddev()
Пример #21
0
 def test_keys_nominal(self):
     self.assertListEqual(stream([(1, 'a'), (2, 'bb'), (0, '')]).keystream().toList(), [1, 2, 0])
Пример #22
0
 def test_toSumCounter_onStrings(self):
     s = stream([('a', 'b'), ('a', 'c')])
     self.assertDictEqual(s.toSumCounter(), {'a': 'bc'})
Пример #23
0
 def test_toSumCounter_onEmptyStream(self):
     s = stream([])
     self.assertDictEqual(s.toSumCounter(), {})
Пример #24
0
 def test_toSumCounter_nominal(self):
     s = stream([('a', 2), ('a', 4), ('b', 2.1), ('b', 3), ('c', 2)])
     self.assertDictEqual(s.toSumCounter(), {'a': 6, 'b': 5.1, 'c': 2})
Пример #25
0
 def test_streamExists(self):
     s = stream([0, 1])
     self.assertEqual(s.exists(lambda e: e == 0), True)
     self.assertEqual(s.exists(lambda e: e == 2), False)
Пример #26
0
 def test_toMap(self):
     self.assertDictEqual(stream(((1, 2), (3, 4))).toMap(), {1: 2, 3: 4})
Пример #27
0
 def test_stream_repr_doesntChangeStream(self):
     s = stream(iter((1, 2, 3, 4)))
     repr(s)
     self.assertListEqual(s.toList(), [1, 2, 3, 4])
Пример #28
0
 def test_joinWithString(self):
     s = "|"
     strings = ('a', 'b', 'c')
     self.assertEqual(stream(iter(strings)).join(s), s.join(strings))
Пример #29
0
 def test_unique_mapping(self):
     s = stream(['abc', 'def', 'a', 'b', 'ab'])
     self.assertListEqual(s.unique(len).toList(), ['abc', 'a', 'ab'])
Пример #30
0
 def test_joinWithNone(self):
     s = ""
     strings = ('a', 'b', 'c')
     self.assertEqual(stream(iter(strings)).join(), s.join(strings))
Пример #31
0
 def test_keyBy_nominal(self):
     self.assertListEqual(stream(['a', 'bb', '']).keyBy(len).toList(), [(1, 'a'), (2, 'bb'), (0, '')])
Пример #32
0
 def test_mean(self):
     self.assertAlmostEqual(stream([1, 2, 3, 4]).mean(), 2.5)