IterProxy: Extended Iterable API

0.3.1 · active · verified Sun Apr 12

iterproxy is a Python library that enhances any iterable object with a convenient API, allowing access patterns commonly found in ORM frameworks. It provides methods like `.one()`, `.one_or_none()`, `.many(k)`, `.skip(k)`, and `.all()`. The current version is 0.3.1, released on November 2, 2023, indicating an active but infrequent release cadence.

Warnings

Install

Imports

Quickstart

This example demonstrates how to wrap a standard Python iterable with `IterProxy` and use its extended API for accessing elements, including `.one()`, `.many()`, `.skip()`, `.all()`, and `.one_or_none()`. It also shows how to chain multiple filter operations or combine them in a single call.

from iterproxy import IterProxy

# Create an IterProxy from any iterable
my_list = list(range(10))
proxy = IterProxy(my_list)

# Access elements using ORM-like API
print(f"First element: {proxy.one()}") # Output: First element: 0
print(f"Next three elements: {proxy.many(3)}") # Output: Next three elements: [1, 2, 3]

# Skip elements and get subsequent ones
# Note: This operates on the *remaining* elements after previous calls
print(f"Skip 2, get next 2: {proxy.skip(2).many(2)}") # Output: Skip 2, get next 2: [6, 7]

# Get all remaining elements
print(f"All remaining elements: {proxy.all()}") # Output: All remaining elements: [8, 9]

# Handle potentially empty results gracefully
empty_proxy = IterProxy([])
print(f"One or none from empty: {empty_proxy.one_or_none()}") # Output: One or none from empty: None

# Chaining filter operations
def is_odd(x): return x % 2 == 1
def gte_5(x): return x >= 5

# Re-create proxy for fresh iteration
proxy_for_filter = IterProxy(range(10))
filtered_items = proxy_for_filter.filter(is_odd).filter(gte_5).all()
print(f"Filtered (odd and >=5): {filtered_items}") # Output: Filtered (odd and >=5): [5, 7, 9]

# Multiple filter functions in one call (implicit AND logic)
proxy_combined_filter = IterProxy(range(10))
combined_filtered_items = proxy_combined_filter.filter(is_odd, gte_5).all()
print(f"Combined filter (odd and >=5): {combined_filtered_items}") # Output: Combined filter (odd and >=5): [5, 7, 9]

view raw JSON →