Iterator utility classes and functions
Project description
Available at PyPi
Provides a wrapper class TimeoutIterator to add timeout feature to normal iterators
Installation:
pip install iterators
See help of TimeoutIterator for all the features. Check tests for examples on how to use TimeoutIterator. See example tests below for basic usage
Example:
-
TimeoutIterator works like normal iterator:
from iterators import TimeoutIterator def iter_simple(): yield 1 yield 2 def test_normal_iteration(self): i = iter_simple() it = TimeoutIterator(i) self.assertEqual(next(it), 1) self.assertEqual(next(it), 2) self.assertRaises(StopIteration, next, it) self.assertRaises(StopIteration, next, it)
-
When timeout is needed, use like this
def iter_with_sleep(): yield 1 time.sleep(0.6) yield 2 time.sleep(0.4) yield 3 def test_fixed_timeout(self): i = iter_with_sleep() it = TimeoutIterator(i, timeout=0.5) self.assertEqual(next(it), 1) self.assertEqual(next(it), it.get_sentinel()) self.assertEqual(next(it), 2) self.assertEqual(next(it), 3) self.assertRaises(StopIteration, next, it)
-
Dynamic timeout adjustment
def iter_with_sleep(): yield 1 time.sleep(0.6) yield 2 time.sleep(0.4) yield 3 def test_timeout_update(self): i = iter_with_sleep() it = TimeoutIterator(i, timeout=0.5) self.assertEqual(next(it), 1) self.assertEqual(next(it), it.get_sentinel()) it.set_timeout(0.3) self.assertEqual(next(it), 2) self.assertEqual(next(it), it.get_sentinel()) self.assertEqual(next(it), 3) self.assertRaises(StopIteration, next, it)
Run unit tests locally:
python -m unittest discover tests
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
iterators-0.1.0.tar.gz
(3.8 kB
view hashes)
Built Distribution
Close
Hashes for iterators-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d4865e4dd7fdf753b70a5be9a4cda2e40d123aebdbfec2a6d888abf4a4e0991 |
|
MD5 | af76f9f8b10ab53a382488521e4e4b2d |
|
BLAKE2b-256 | 3c3f22510b55a2e6c66b36b5785bf802d8862579b3dce60a13cbd6321b323ea3 |