Skip to main content

A simple piping syntax

Project description

A simple syntax for piping in Python.

Compare:

from bookends import _
from toolz.curried import map

l = _| [3, 2, 1] | map(lambda n: n*2) | sorted |_   # [2, 4, 6]

with:

l = sorted(map(lambda n: n*2, [3, 2, 1]))

l = sorted([n*2 for n in [3, 2, 1]])

l = []
for n in [3, 2, 1]:
    l.append(n*2)
l.sort()

For an extended comparison, see example.py.

To install:

pip install bookends

To use:

from bookends import _

For similar tools, see:

Note: for multiline usage, wrap the expression in parens.

import csv
from StringIO import StringIO

(_| '40,5,10\n20,6,9\n41,10,10\n'
  | StringIO
  | csv.reader
  | sorted
  |_)

# [['20', '6', '9'], ['40', '5', '10'], ['41', '10', '10']]

Wrap lone lambdas in parens as well.

(_| ['addition', 'multiplication']
  | (lambda l: l + ['exponentiation', 'tetration'])
  | ', '.join
  |_)

# 'addition, multiplication, exponentiation, tetration'

You can use partial or curried functions.

from functools import partial
from toolz.curried import drop

(_| ['ca', 'tx', 'ny']
  | partial(map, lambda state: state.upper())
  | drop(1)
  | list
  |_)

# ['TX', 'NY']

And/or use threading syntax, by putting each function and its arguments into a tuple.

from toolz import drop

(_| ['ca', 'tx', 'ny']
  | (map, lambda state: state.upper())
  | (drop, 1)
  | list
  |_)

# ['TX', 'NY']

If you don’t like the underscore, import the bookend as B.

from bookends import B

(B| ['ca', 'tx', 'ny']
  | (map, lambda state: state.upper())
  | (drop, 1)
  | list
  |B)

To stop in the debugger before each function call, put a step into the pipe.

from bookends import step

(_| [3, 2, 1]
  | (map, lambda x: x*2)
  | step               # <==
  | sorted
  | sum
  |_)

To call off the stepping, drop in an endstep.

from bookends import step, endstep

(_| [3, 2, 1]
  | (map, lambda x: x*2)
  | step               # <==
  | sorted
  | endstep            # <==
  | sum
  |_)

To print each function and its output, drop in a verbose.

from bookends import verbose

(_| [3, 2, 1]
  | verbose            # <==
  | (map, lambda x: x*2)
  | sorted
  | sum
  |_)

You can easily add these options while debugging by tacking on their first letter to the initial bookend.

(_.sv| [3, 2, 1]       # <== Turn on step and verbose (_.s, _.v, and _.vs work too).
  | (map, lambda x: x*2)
  | sorted
  | sum
  |_)

Drop in a function that won’t affect the operand by decorating it with passthrough.

from bookends import passthrough

@passthrough
def log(operand):
  log.info('Operand was {}.'.format(operand))

(_| [3, 2, 1]
  | (map, lambda x: x*2)
  | log                # <==
  | sorted
  |_)

Plays nice with Kachayev’s _.

from fn import _ as __

_| [1, 2, 3] | __ + [4, 5] |_

# [1, 2, 3, 4, 5]

Here’s a simplified version of the source:

class Bookend():
  def __or__(self, operand):
    return Pipe(operand)


class Pipe():
  def __init__(self, operand):
    self.operand = operand

  def __or__(self, f):
    if isinstance(f, Bookend):
      return self.operand
    else:
      self.operand = f(self.operand)
      return self


_ = Bookend()

Contact: @bzrry.

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

bookends-0.0.5.tar.gz (4.8 kB view details)

Uploaded Source

File details

Details for the file bookends-0.0.5.tar.gz.

File metadata

  • Download URL: bookends-0.0.5.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for bookends-0.0.5.tar.gz
Algorithm Hash digest
SHA256 bd74153e6a55714a00d007e0872cbc9fb4781de648e4acb0812baa10e23bbcd5
MD5 8ca5bd1b0db6c85cadb2ca947b333189
BLAKE2b-256 e17612863e1aee582ac25704d3f95bcdbb125d70eb7d4504cde0f6fa075ae621

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