Give any iterable object capability to use .one(), .one_or_none(), .many(k), .skip(k), .all() API.
Project description
⏩ 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
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]
Install
iterproxy is released on PyPI, so all you need is:
$ pip install iterproxy
To upgrade to latest version:
$ pip install --upgrade iterproxy
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for iterproxy-0.1.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8d4bfd36c878a65f00a4f18b7f1d26ffb2baaf2f2c005ffeff11c30dc1680180 |
|
MD5 | e6cfdcc34e7e8106f9086a0ad4f4a999 |
|
BLAKE2b-256 | e5843c7229c197ac8fc5118718de431c95136241691840c98ec0918ecbf85332 |