Skip to main content

Iterator pipeline wrapper in the spirit of Martin Fowler's Collection Pipeline pattern

Project description

IterPipe

Introduction

This is a wrapper for the iterator functions in the Python Standard Library to make it easier and more readable to chain them together into a pipeline.

It is a way to implement Martin Fowler's Collection Pipeline pattern (https://martinfowler.com/articles/collection-pipeline/) with Python's standard iterator functions.

To illustrate the concept, we will perform the following sequence of steps in each example:

  1. Start with a range iterator,
  2. Then filter to pass only the values larger or equal to 6
  3. Then square each number from step 2, using the map function
  4. Then sum the squares from step 3

Here is the code, using intermediate variables, without chaining.

def filter_func(x):
    # Simple function used in "filter" examples below
    return x >= 6


def squared(x):
    # Simple function to square it's input.  Used in "map" below.
    return x * x

input = range(10)

intermediate_1 = filter(filter_func, input)

# Apply squared function to each value returned by intermediate_1 iterator
intermediate_2 = map(squared, intermediate_1)

output = sum(intermediate_2)

print("Output without chaining: {output}".format(output=output))

Here is the same code, using regular Python syntax chaining. Note that execution happens inside-out, with the last operation (sum) appearing first.

def filter_func(x):
    return x >= 6


def squared(x):
    # Simple function to square it's input.  Used in "map" below.
    return x * x

input = range(10)

output = sum(map(squared, filter(filter_func, input)))

print("Output with regular Python chaining: {output}".format(output=output))

And here is the same code again, using the IterPipe wrapper. Note that it reads in the same order as execution happens.

from IterPipe import IterPipe

def filter_func(x):
    return x >= 6


def squared(x):
    # Simple function to square it's input.  Used in "map" below.
    return x * x

input = range(10)

output = (IterPipe(input) 
          .filter(filter_func)
          .map(squared)
          .sum()
          )

print("Output with IterPipe chaining: {output}".format(output=output))

The IterPipe wrapper supports the following functions that operate on iterators from builtins, itertools and functools.

  • accumulate
  • all
  • any
  • chain
  • combinations
  • combinations_with_replacement
  • compress
  • cycle
  • dict
  • dropwhile
  • enumerate
  • filter
  • filterfalse
  • frozenset
  • groupby
  • islice
  • iterator
  • list
  • map
  • max
  • min
  • next
  • permutations
  • product
  • reduce
  • set
  • sorted
  • starmap
  • sum
  • takewhile
  • tee
  • tuple
  • zip
  • zip_longest

Installation

Works with Python 3.4 or later.

pip install -U IterPipe

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

IterPipe-0.0.3.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

IterPipe-0.0.3-py3-none-any.whl (17.0 kB view details)

Uploaded Python 3

File details

Details for the file IterPipe-0.0.3.tar.gz.

File metadata

  • Download URL: IterPipe-0.0.3.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.20.1 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.0

File hashes

Hashes for IterPipe-0.0.3.tar.gz
Algorithm Hash digest
SHA256 d92661893381e790dc615461f2e9645e5a10e4c2a092b4a97dbfe0bc06425c11
MD5 a6540da7f2866479f08f178617539731
BLAKE2b-256 691f47519508da0cc1457b15521059bd2918e639d2179563c51572bf9b702315

See more details on using hashes here.

File details

Details for the file IterPipe-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: IterPipe-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 17.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.20.1 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.0

File hashes

Hashes for IterPipe-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 bc25fd0688ce4ca457c5723941920283104ad2b1e9c37dab8869cd67b843c5c0
MD5 e4d1a5aa63bf37411726150e62f7f94f
BLAKE2b-256 f42001237003d55ca34082271cb29e4dd895b6bef1b348cf55b79805ce9589b2

See more details on using hashes here.

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