本文整理汇总了Python中streams.Stream类的典型用法代码示例。如果您正苦于以下问题:Python Stream类的具体用法?Python Stream怎么用?Python Stream使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Stream类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_it_should_limit_the_size_of_the_stream
def test_it_should_limit_the_size_of_the_stream(self):
stream = Stream(xrange(10000000000))
limited = stream.limit(10)
self.assertListEqual(list(limited), list(xrange(10)))
stream = Stream(xrange(100))
stream = stream.limit(1000)
self.assertListEqual(list(stream), list(xrange(100)))
示例2: test_it_should_map_a_function_to_the_stream
def test_it_should_map_a_function_to_the_stream(self):
stream = Stream(range(10))
stream = stream.map(lambda item: -item)
self.assertEqual(max(stream), 0)
stream = Stream(dict((v, v) for v in xrange(100)))
stream = stream.values().skip(10).limit(3)
self.assertListEqual(list(stream), [10, 11, 12])
示例3: test_divisibleby
def test_divisibleby(self):
stream = Stream(xrange(2000))
stream = stream.ints().divisible_by(10)
self.assertEqual(stream.count(), 200)
stream = Stream(xrange(2000))
stream = stream.divisible_by(1000)
self.assertEquals(list(stream), [0, 1000])
示例4: test_it_should_concatenate_iterables
def test_it_should_concatenate_iterables(self):
stream = Stream.concat(Stream.range(10), Stream.range(10))
self.assertListEqual(list(stream), list(xrange(10)) + list(xrange(10)))
stream = Stream.concat(xrange(10), xrange(10), xrange(10))
self.assertListEqual(list(stream.distinct()), list(xrange(10)))
stream = Stream.concat(xrange(10), xrange(10), xrange(10))
self.assertEqual(stream.count(), 30)
示例5: test_it_should_filter_odds
def test_it_should_filter_odds(self):
stream = Stream(range(6))
stream = stream.odds()
self.assertListEqual(list(stream), [1, 3, 5])
stream = Stream(xrange(200))
stream = stream.odds()
elements = list(stream)
self.assertEqual(len(elements), 100)
self.assertFalse(any(item % 2 == 0 for item in elements))
示例6: test_it_should_filter_evens
def test_it_should_filter_evens(self):
stream = Stream(range(6))
stream = stream.evens()
self.assertListEqual(list(stream), [0, 2, 4])
stream = Stream(xrange(200))
stream = stream.ints().evens()
elements = list(stream)
self.assertEqual(len(elements), 100)
self.assertTrue(all(item % 2 == 0 for item in elements))
示例7: test_it_should_filter_by_regular_expression
def test_it_should_filter_by_regular_expression(self):
stream = Stream((text_type(x) for x in xrange(100)))
ones = stream.regexp(r'^1')
self.assertListEqual(
list(ones),
['1', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19'])
stream = Stream(str(item) for item in xrange(1000))
stream = stream.regexp(r"^10*$")
stream = stream.ints()
self.assertListEqual(list(stream), [1, 10, 100])
示例8: test_it_should_filter_instances_of_a_class
def test_it_should_filter_instances_of_a_class(self):
items = list(xrange(10)) + ['foo', 'bar', 'baz']
stream = Stream(items)
strings = stream.instances_of(string_types)
self.assertListEqual(list(strings), ['foo', 'bar', 'baz'])
elements = list(xrange(100))
# noinspection PyTypeChecker
elements = elements + [str(item) for item in elements] + [None, None]
strings = list(Stream(elements).instances_of(str))
ints = list(Stream(elements).instances_of(int))
self.assertEqual(len(strings), 100)
self.assertTrue(all(isinstance(item, str) for item in strings))
self.assertEqual(len(ints), 100)
self.assertTrue(all(isinstance(item, int) for item in ints))
示例9: test_no_cache
def test_no_cache(self):
# Make a normal stream.
stream = Stream.range(10)
# Iterate once
self.assertEqual(list(stream), list(range(10)))
# Iterate twice
self.assertEqual(list(stream), [])
示例10: test_it_should_filter_items
def test_it_should_filter_items(self):
stream = Stream(range(10))
stream = stream.filter(lambda item: item % 2)
self.assertEqual(stream.sum(), 25)
stream = Stream(dict((v, v) for v in xrange(100)))
stream = stream.filter(lambda kv: kv[0] % 2)
stream = stream.filter(lambda kv: kv[0] % 10, parallel=6)
stream = stream.limit(5).keys()
stream = list(stream)
self.assertListEqual(list(stream), [1, 3, 5, 7, 9])
示例11: test_limit
def test_limit(self):
stream = Stream(xrange(100))
stream = stream.limit(50)
self.assertEqual(stream.count(), 50)
stream = Stream(xrange(100))
stream = stream.limit(1000)
self.assertEqual(stream.count(), 100)
示例12: test_it_should_count_the_number_of_occurrences_in_the_stream
def test_it_should_count_the_number_of_occurrences_in_the_stream(self):
stream = Stream(xrange(100))
stream = stream.limit(50)
self.assertEqual(stream.count(), 50)
stream = Stream(xrange(100))
stream = stream.limit(1000)
self.assertEqual(stream.count(), 100)
示例13: test_it_should_filter_by_divisibility
def test_it_should_filter_by_divisibility(self):
stream = Stream(range(6))
stream = stream.divisible_by(2)
self.assertListEqual(list(stream), [0, 2, 4])
stream = Stream(xrange(2000))
stream = stream.ints().divisible_by(10)
self.assertEqual(stream.count(), 200)
stream = Stream(xrange(2000))
stream = stream.divisible_by(1000)
self.assertEquals(list(stream), [0, 1000])
示例14: test_cache
def test_cache(self):
# Make a cached stream.
stream = Stream.range(10).cache()
# Iterate once
self.assertEqual(list(stream), list(range(10)))
# Iterate twice, this time from the cache.
self.assertEqual(list(stream), list(range(10)))
# Now, we make a new, smaller, cached stream fromm our cached stream.
stream = stream.cache(5)
# Iterate once
self.assertEqual(list(stream), list(range(10)))
# Iterate twice, this time from the cache. We get the last 5 values.
self.assertEqual(list(stream), list(range(5, 10)))
示例15: test_it_should_iterate_over_an_iterable_multiple_times
def test_it_should_iterate_over_an_iterable_multiple_times(self):
stream = Stream.range(10)
self.assertListEqual(list(stream), list(xrange(10)))
self.assertListEqual(list(stream), [])