Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

iterators

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

iterators

Iterator utility classes and functions

  • 0.2.0
  • PyPI
  • Socket score

Maintainers
1

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

Keywords

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc