Skip to content

andybrice/pypework

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pypework

Pypework is a functional pipeline library for Python.

It allows you to rewrite messy nested function calls such as this:

title_sanitized =
  replace(replace(replace(lowercase("Lorem Ipsum Dolor 2018/02/18"), " ", "_"), "/", "-"), "@", "at")

title_sanitized # -> "lorem_ipsum_dolor_2018-02-18"

In a far more readable format like this:

title_sanitized = (
  "Lorem Ipsum Dolor 2018/02/18"
    >> f.lowercase
    >> f.replace("/", "-")
    >> f.replace(" ", "_")
    >> f.replace("@", "at")
)

title_sanitized # -> "lorem_ipsum_dolor_2018-02-18"

Installation

Install using PIP by running:

pip install pypework

Usage

Import using:

import pypework

Initialize by instantiating a Function Catcher with the current module's scope:

f = pypework.FunctionCatcher(scope = __name__)

You can now make any function call pipeable by adding f. before it. For example lowercase() becomes f.lowercase. Trailing parentheses are optional if the function has only one argument.

Use the >> operator to pipe into the function like so:

"Lorem Ipsum" >> f.lowercase # -> "lorem ipsum"

Or chain together multiple functions into a pipeline:

"Lorem Ipsum" >> f.lowercase >> f.replace(" ", "_") # -> "lorem_ipsum"

You can also split a pipeline across multiple lines if you wrap it in parentheses:

(
  "Lorem Ipsum"
    >> f.lowercase
    >> f.replace(" ", "_")
)

 # -> "lorem_ipsum"

Or by adding trailing backslashes:

"Lorem Ipsum" \
  >> f.lowercase \
  >> f.replace(" ", "_")

 # -> "lorem_ipsum"