Skip to main content

Iterator utility classes and functions

Project description

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

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.2.0.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

iterators-0.2.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file iterators-0.2.0.tar.gz.

File metadata

  • Download URL: iterators-0.2.0.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.1

File hashes

Hashes for iterators-0.2.0.tar.gz
Algorithm Hash digest
SHA256 e9927a1ea1ef081830fd1512f3916857c36bd4b37272819a6cd29d0f44431b97
MD5 7b0bb7e8281202cb5f5e44462c4d37ce
BLAKE2b-256 02c4135b5bdb9f14f728fe1274361b336f77c5f1606af9a5622a765fe75f5fa0

See more details on using hashes here.

File details

Details for the file iterators-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: iterators-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.1

File hashes

Hashes for iterators-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1d7ff03f576c9de0e01bac66209556c066d6b1fc45583a99cfc9f4645be7900e
MD5 3214589300b6790851943174f4eaf292
BLAKE2b-256 bda19c29772ac9f3bdf9837c92ba5c1fc93f75da14c2e0c3fc41e10485f68feb

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page