Skip to main content

Iteration utils for "prev", "curr", and "next"-style for-loops.

Project description

prevcurrnext

Iteration utils for "prev", "curr", and "next"-style for-loops.

Install

pip install prevcurrnext

Usage

The prevcurrnext function yields pairs of consecutive elements from an iterable, allowing you to see the "previous", "current", and "next" items for each element. It includes options to yield None at the beginning and/or end of the sequence for convenient boundary handling.

from prevcurrnext import prevcurr, currnext, prevcurrnext

# Basic usage
for prev, curr in prevcurr([1, 2, 3]):
    print(prev, curr)
# Output:
# None 1
# 1 2
# 2 3

# Customizing boundary behavior
# Adding None at the end
for prev, curr in prevcurr([1, 2, 3], end_curr_on_none=True):
    print(prev, curr)
# Output:
# None 1
# 1 2
# 2 3
# 3 None

for curr, _next in currnext([1, 2, 3]):
    print(curr, _next)
# Output:
# 1 2
# 2 3
# 3 None

for prev, curr, _next in prevcurrnext([1, 2, 3]):
    print(prev, curr, _next)
# Output:
# None 1 2
# 1 2 3
# 2 3 None

Available functions

  • currprev
  • prevcurr
  • currnext
  • nextcurr
  • prevcurrnext
  • nextcurrprev

All functions have nearly the exact same parameters:

  • iterable: The iterable to process.
  • start_prev_on_none: Whether to yield None for the "previous" item before the first element. Default is True.
  • end_curr_on_none: Whether to yield None for the "current" item after the last element. Default is False.
  • end_next_on_none: Whether to yield None for the "next" item after the last element. Default is True.

Contributing

Contributions are welcome! If you find issues or have suggestions, please open an issue or submit a pull request on GitHub.

Development Setup

If you use Poetry

poetry install
poetry run pytest

Otherwise...

python -m venv venv
source venv/bin/activate
pip install -r requirements-dev.txt
pip install -e .
pytest

License

This project is licensed under the MIT License.

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

prevcurrnext-0.1.0.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

prevcurrnext-0.1.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file prevcurrnext-0.1.0.tar.gz.

File metadata

  • Download URL: prevcurrnext-0.1.0.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.1 Darwin/23.5.0

File hashes

Hashes for prevcurrnext-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2754a6429c81ae63fae4fbfd60b4e328416e79e74c349a56ef499f08483fc04c
MD5 c743a20645f80a5d65ed380bd95bf466
BLAKE2b-256 c1bd019c3cd5fd60e81bd52a19e1996cae32083d68e5e40cbdbf1d9bcdebedaf

See more details on using hashes here.

File details

Details for the file prevcurrnext-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: prevcurrnext-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.1 Darwin/23.5.0

File hashes

Hashes for prevcurrnext-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8ece59ade96ece3d17f67e156bc447b7912fba2889eb8d3a8ccdcc4e86cfca3e
MD5 22200d36e4514e7f1517067e8231dbf1
BLAKE2b-256 a24ae9f58f326072cbe38df9d41e9a6f83f8f2904b5d8d598f181c8ae2f2f95a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page