Skip to main content

Provides models for convenient and efficient iteration

Project description

Iter Model

Supported Python versions Package version Test

Supported Versions Supported Versions Supported Versions Supported Versions

Description

Iter Model uses a method approach instead of individual functions to work with iterable objects.

Native approach

result = list(map(
    lambda x: x ** 2,
    filter(lambda x: x % 2 == 0, range(10)),
))

Iter Model approach

from iter_model import SyncIter

result = (
    SyncIter(range(10))
        .where(lambda x: x % 2 == 0)
        .map(lambda x: x ** 2)
        .to_list()
)

Generators

You can decorate your generator function and get SyncIter as a result

from iter_model import sync_iter


@sync_iter
def some_generator():
    for item in range(10):
        yield item


result = some_generator().take_while(lambda x: x < 5).to_list()

Async support

Iter Model also support async iterable and async function as condition.

import asyncio

from iter_model import async_iter


@async_iter
async def some_generator():
    for item in range(10):
        yield item

        
async def condition_a(x):
    """Some async condition"""
    return x % 2 == 0 


def condition_b(x):
    """Some sync condition"""
    return x > 5 


async def main():
    result = await (
        some_generator()
            .where(condition_a)
            .take_while(condition_b)
            .to_list()
    )
    print(result)
    


asyncio.run(main())

SyncIter/AsyncIter provide the following methods

  • to_list()
  • to_tuple()
  • to_set()
  • enumerate()
  • take()
  • map()
  • skip()
  • skip_while()
  • count()
  • first_where()
  • where()
  • take_while()
  • first()
  • last()
  • chain()
  • all()
  • any()

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

iter_model-0.2.0.tar.gz (5.1 kB view hashes)

Uploaded Source

Built Distribution

iter_model-0.2.0-py3-none-any.whl (5.8 kB view hashes)

Uploaded Python 3

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