Skip to main content
https://github.com/MacHu-GWU/iterproxy-project/workflows/CI/badge.svg https://codecov.io/gh/MacHu-GWU/iterproxy-project/branch/main/graph/badge.svg https://img.shields.io/pypi/v/iterproxy.svg https://img.shields.io/pypi/l/iterproxy.svg https://img.shields.io/pypi/pyversions/iterproxy.svg https://img.shields.io/badge/Release_History!--None.svg?style=social https://img.shields.io/badge/STAR_Me_on_GitHub!--None.svg?style=social
https://img.shields.io/badge/Link-Install-blue.svg https://img.shields.io/badge/Link-GitHub-blue.svg https://img.shields.io/badge/Link-Submit_Issue-blue.svg https://img.shields.io/badge/Link-Request_Feature-blue.svg https://img.shields.io/badge/Link-Download-blue.svg

⏩ Welcome to iterproxy Documentation

You may seen the following code style in many ORM framework, this pattern provides a user friendly API to access items from the iterator object:

query(...).one()
query(...).one_or_none()
query(...).many(3)
query(...).all(5)
query(...).skip(5).many(3)

iterproxy library can give any iterable object similar capabilities.

Usage Example

Convert any iterable object to a IterProxy object:

from iterproxy import IterProxy

# Suppose you have an iterable object
iterator = range(10)

# Convert it to a IterProxy object
proxy = IterProxy(iterator)

Access items from the IterProxy object:

proxy = IterProxy(range(10))

proxy.one() # it will return 0
proxy.many(3) # it will return [1, 2, 3]
proxy.skip(2).many(2) # it will skip [4, 5] and return [6, 7]
proxy.all() # it will return the rest [8, 9]
proxy.one_or_none() # it will return None

IterProxy.iter_chunks can group items into chunks having K items, the last chunk may have less items than K:

proxy = IterProxy(range(3))
list(proxy.iter_chunks(2)) # it will return [[0, 1], [2]]

Another example:

proxy = IterProxy(range(10))
proxy.all() # it will return [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Of course the IterProxy itself is a iterator:

for i in IterProxy(range(10)): # 0, 1, 2, ...
    ...

You can use custom filter function to filter the result. Other than the nesting style in built-in filter function, it use chain pattern.

def is_odd(x):
    return x % 2 == 1

def gte_5(x):
    return x >= 5

# with IterProxy, you can chain them
# it returns you [5, 7, 9]
for i in IterProxy(range(10)).filter(is_odd).filter(gte_5):
    print(i)

# or put them together, by default, it is logic and
for i in IterProxy(range(10)).filter(is_odd, gte_5):
    print(i)

# with the built-in filter, this is not that intuitive
for i in filter(gte_5, filter(is_odd, range(10))):
    ...

You can also use compound logic and_, or_, not_:

def is_odd(i):
    return i % 2

def is_even(i):
    return not (i % 2)

def lte_3(i):
    return i <= 3

def gte_4(i):
    return i >= 4

def lte_6(i):
    return i <= 6

def gte_7(i):
    return i >= 7

IterProxy(range(10)).filter(and_(gte_4, lte_6)).all() # [4, 5, 6]
IterProxy(range(10)).filter(or_(lte_3, gte_7)).all() # [0, 1, 2, 3, 7, 8, 9]
IterProxy(range(10)).filter(not_(is_odd)).all() # [0, 2, 4, 6, 8]

# of course you can nest and_, or_, not_
IterProxy(range(10)).filter(not_(and_(is_odd, or_(lte_3, gte_7)))).all() # [0, 2, 4, 5, 6, 8]

(Advanced) In order to enable type hint, you can do:

from iterproxy import IterProxy

class Dog:
    def bark(self):
        pass

class DogIterProxy(IterProxy[Dog]): # subclass from IterProxy[${YourTypeHint}]
    pass

many_dogs = [Dog(),]*10

proxy = DogIterProxy(many_dogs)

proxy.one_or_none().bark()
for dog in proxy.many(2):
    dog.bark()
for dog in proxy.skip(1).many(2):
    dog.bark()
for dog in proxy.all():
    dog.bark()

filtered_proxy = DogIterProxy(many_dogs).filter(lambda dog: True)
filtered_proxy.one_or_none().bark()
for dog in filtered_proxy.many(2):
    dog.bark()
for dog in filtered_proxy.skip(1).many(2):
    dog.bark()
for dog in filtered_proxy.all():
    dog.bark()

Install

iterproxy is released on PyPI, so all you need is:

$ pip install iterproxy

To upgrade to latest version:

$ pip install --upgrade iterproxy

Metadata

Release files for iterproxy 0.3.1

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

Source distribution (sdist)

Source distribution for iterproxy 0.3.1
File Size Uploaded
iterproxy-0.3.1.tar.gz 11.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for iterproxy 0.3.1
File Interpreter ABI Platform
iterproxy-0.3.1-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 19.8 kB

Release files / iterproxy-0.3.1.tar.gz

Download URL iterproxy-0.3.1.tar.gz
Size 11.5 kB
Tags Source
SHA-256 checksum
How to use checksums
a832d71822e5f658cb3e9c7624496383b57c43c7f51a3aa4eb1f4b986fdd1317
BLAKE2b-256 checksum
How to use checksums
bec86dddbff88a70d9c8d389b1a0d0670a357c1bf3a151c0b0c7cd24b8fb535a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.13

Release files / iterproxy-0.3.1-py2.py3-none-any.whl

Download URL iterproxy-0.3.1-py2.py3-none-any.whl
Size 8.2 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
bf52ad378298db94866631ad78cc3e0332c897ba5f2cd53a02ba5a900a932f70
BLAKE2b-256 checksum
How to use checksums
c51a0076370bd1c7f16bab017378d5258b7396a70d5552296399e644da2b7421
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.13

Release history Release notifications | RSS feed

This release

0.3.1 This release

2 release files

0.2.1

2 release files

0.1.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