Skip to main content

If Funcy and Pipe had a baby. Decorates all Funcy methods with Pipe superpowers.

Project description

Release Notes Downloads Python Versions GitHub CI Status License: MIT

Funcy with pipeline-based operators

If Funcy and Pipe had a baby. Deal with data transformation in python in a sane way.

I love Ruby. It's a great language and one of the things they got right was pipelined data transformation. Elixir got this even more right with the explicit pipeline operator |>.

However, Python is the way of the future. As I worked more with Python, it was driving me nuts that the data transformation options were not chainable.

This project fixes this pet peeve.

Installation

pip install funcy-pipe

Or, if you are using poetry:

poetry add funcy-pipe

Examples

Extract a couple key values from a sql alchemy model:

import funcy_pipe as fp

entities_from_sql_alchemy
  | fp.lmap(lambda r: r.to_dict())
  | fp.lmap(lambda r: r | fp.omit(["id", "created_at", "updated_at"]))
  | fp.to_list

Or, you can be more fancy and use whatever and pmap:

import funcy_pipe as f
import whatever as _

entities_from_sql_alchemy
  | fp.lmap(_.to_dict)
  | fp.pmap(fp.omit(["id", "created_at", "updated_at"]))
  | fp.to_list

Create a map from an array of objects, ensuring the key is always an int:

section_map = api.get_sections() | fp.group_by(f.compose(int, that.id))

Grab the ID of a specific user:

filter_user_id = (
  collaborator_map().values()
  | fp.where(email=target_user)
  | fp.pluck("id")
  | fp.first()
)

Get distinct values from a list (in this case, github events):

events | fp.pluck("type") | fp.distinct() | fp.to_list()

What if the objects are not dicts?

filter_user_id = (
  collaborator_map().values()
  | fp.where_attr(email=target_user)
  | fp.pluck_attr("id")
  | fp.first()
)

How about creating a dict where each value is sorted:

data
  # each element is a dict of city information, let's group by state
  | fp.group_by(itemgetter("state_name"))
  # now let's sort each value by population, which is stored as a string
  | fp.walk_values(
    f.partial(sorted, reverse=True, key=lambda c: int(c["population"])),
  )

A more complicated example (lifted from this project):

comments = (
    # tasks are pulled from the todoist api
    tasks
    # get all comments for each relevant task
    | fp.lmap(lambda task: api.get_comments(task_id=task.id))
    # each task's comments are returned as an array, let's flatten this
    | fp.flatten()
    # dates are returned as strings, let's convert them to datetime objects
    | fp.lmap(enrich_date)
    # no date filter is applied by default, we don't want all comments
    | fp.lfilter(lambda comment: comment["posted_at_date"] > last_synced_date)
    # comments do not come with who created the comment by default, we need to hit a separate API to add this to the comment
    | fp.lmap(enrich_comment)
    # only select the comments posted by our target user
    | fp.lfilter(lambda comment: comment["posted_by_user_id"] == filter_user_id)
    # there is no `sort` in the funcy library, so we reexport the sort built-in so it's pipe-able
    | fp.sort(key="posted_at_date")
    # create a dictionary of task_id => [comments]
    | fp.group_by(lambda comment: comment["task_id"])
)

Extras

  • to_list
  • log
  • bp. run breakpoint() on the input value
  • sort
  • exactly_one. Throw an error if the input is not exactly one element
  • reduce
  • pmap. Pass each element of a sequence into a pipe'd function

Extensions

There are some functions which are not yet merged upstream into funcy, and may never be. You can patch funcy to add them using:

import funcy_pipe
funcy_pipe.patch()

Coming From Ruby?

  • uniq => distinct
  • detect => where(some="Condition") | first or where_attr(some="Condition") | first

Module Alias

Create a module alias for funcy-pipe to make things clean (import * always irks me):

# fp.py
from funcy_pipe import *

# code py
import fp

Inspiration

TODO

  • tests
  • docs for additional utils
  • fix typing threading

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

funcy_pipe-0.11.0.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

funcy_pipe-0.11.0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file funcy_pipe-0.11.0.tar.gz.

File metadata

  • Download URL: funcy_pipe-0.11.0.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.2 Linux/6.5.0-1016-azure

File hashes

Hashes for funcy_pipe-0.11.0.tar.gz
Algorithm Hash digest
SHA256 8dcedb05b13f4502e88ec75a794da1c4682ee3c6aab59c9a423cf42dc3b94805
MD5 33933001c7f64209adaf1011da398a9c
BLAKE2b-256 73ea25fc6dcbdba0e4077871b182330b95833a9c11fb2c3d94cc614d3c716493

See more details on using hashes here.

File details

Details for the file funcy_pipe-0.11.0-py3-none-any.whl.

File metadata

  • Download URL: funcy_pipe-0.11.0-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.2 Linux/6.5.0-1016-azure

File hashes

Hashes for funcy_pipe-0.11.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3ad64a39ea9299e52edaba929803422bd80acc10e04ce1746f4fe549d54f461e
MD5 26cf917e8c95676a2cdf60e8351b268d
BLAKE2b-256 34b771574cb0b3f8d1f8a386dbc9c240f0a23852390292d8fe6b8d4b741809f3

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