Skip to main content

Build

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:

  1. 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)
    
  2. 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)
    
  3. 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

Metadata

Release files for iterators 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for iterators 0.2.0
File Size Uploaded
iterators-0.2.0.tar.gz 4.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for iterators 0.2.0
File Interpreter ABI Platform
iterators-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 9.3 kB

Release files / iterators-0.2.0.tar.gz

Download URL iterators-0.2.0.tar.gz
Size 4.3 kB
Tags Source
SHA-256 checksum
How to use checksums
e9927a1ea1ef081830fd1512f3916857c36bd4b37272819a6cd29d0f44431b97
BLAKE2b-256 checksum
How to use checksums
02c4135b5bdb9f14f728fe1274361b336f77c5f1606af9a5622a765fe75f5fa0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.1

Release files / iterators-0.2.0-py3-none-any.whl

Download URL iterators-0.2.0-py3-none-any.whl
Size 5.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1d7ff03f576c9de0e01bac66209556c066d6b1fc45583a99cfc9f4645be7900e
BLAKE2b-256 checksum
How to use checksums
bda19c29772ac9f3bdf9837c92ba5c1fc93f75da14c2e0c3fc41e10485f68feb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.1

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page