Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/andybrice/pypework

Functional pipeline library for Python
https://github.com/andybrice/pypework

functional functional-programming pipeline pipeline-framework pipelines pipes python syntactic-sugar

Last synced: 18 days ago
JSON representation

Functional pipeline library for Python

Awesome Lists containing this project

README

        

# Pypework #

Pypework is a functional pipeline library for Python.

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

```python
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:

```python
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:

```console
pip install pypework
```

## Usage ##

Import using:

```python
import pypework
```

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

```python
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:

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

Or chain together multiple functions into a pipeline:

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

You can also split a pipeline across multiple lines if you wrap it in parentheses:
```python
(
"Lorem Ipsum"
>> f.lowercase
>> f.replace(" ", "_")
)

# -> "lorem_ipsum"
```

Or by adding trailing backslashes:

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

# -> "lorem_ipsum"
```