Skip to main content

Method chaining with funcy.

Project description

funcy-chain

Simple Python data processing using method-chaining style and the funcy library.

>>> users = [
...   { 'first_name': 'barney',  'age': 36 },
...   { 'first_name': 'fred',    'age': 40 },
...   { 'first_name': 'pebbles', 'age': 1 }
... ]
>>> (Chain(users)
...     .sort(key=itemgetter("age"))
...     .map(itemgetter("first_name"))
...     .thru(lambda names: names + ["wilma"])
...     .map(str.capitalize)
... ).value
['Pebbles', 'Barney', 'Fred', 'Wilma']

Why?

  • Method chaining can make some multi-stage data processing easier to read and write.
  • Funcy is great, but doesn't support method chaining.
  • Other method-chaining implementations have too much "magic", are overly complex, and support too many ways of doing one thing. (see: pydash, fluentpy)
  • Being simple and straightforward is a great boon, making it easier to reason about code, debug, etc.

Key Features

  • Useful out of the box, supporting built-in, stdlib and funcy functions (see below).
  • Easy to compose with additional utilities (see Chain.thru()).
  • Both "immediate" (list-based) and "lazy" (iterator-based) computations supported, via parallel Chain and IterChain classes.
>>> from funcy_chain import Chain
>>> (Chain([1, 2, 3, 7, 6, 5, 4])
...     .without(3)
...     .filter(lambda x: x > 2)
...     .remove(lambda x: x > 6)
...     .sort(reverse=True)
... ).value
[6, 5, 4]

Supported Methods

built-in

Note: funcy's map and filter are used rather than the built-ins.

stdlib

funcy

The following is a sub-set of the funcy cheat sheet including only functions supported as methods on Chain and IterChain objects.

Sequences

Slicedrop take rest butlast takewhile dropwhile split_at split_by
Transformmap mapcat keep pluck pluck_attr invoke
Filterfilter remove keep distinct where without
Joinconcat flatten mapcat interleave interpose
Partitionchunks partition partition_by split_at split_by
Groupsplit count_by count_reps group_by group_by_keys group_values
Aggregatewith_prev with_next accumulate reductions sums
Iteratepairwise with_prev with_next zip_values zip_dicts

Collections

Joinjoin join_with
Transformwalk walk_keys walk_values
Filterselect select_keys select_values compact
Dictsflip pluck where [omit](https://funcy.readthedocs.io/en/stable/colls.html#omit">project zip_values zip_dicts

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-chain-0.2.0.tar.gz (15.1 kB view hashes)

Uploaded Source

Built Distribution

funcy_chain-0.2.0-py3-none-any.whl (8.3 kB view hashes)

Uploaded Python 3

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