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
andIterChain
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
- itertools.starmap
- itertools.zip_longest
- heapq.nlargest
- heapq.nsmallest
- random.choice
- random.choices
- random.sample
- random.shuffle
funcy
The following is a sub-set of the funcy
cheat sheet including only functions
supported as methods on Chain
and IterChain
objects.
Sequences
Slice | drop take rest butlast takewhile dropwhile split_at split_by |
Transform | map mapcat keep pluck pluck_attr invoke |
Filter | filter remove keep distinct where without |
Join | concat flatten mapcat interleave interpose |
Partition | chunks partition partition_by split_at split_by |
Group | split count_by count_reps group_by group_by_keys group_values |
Aggregate | with_prev with_next accumulate reductions sums |
Iterate | pairwise with_prev with_next zip_values zip_dicts |
Collections
Join | join join_with |
Transform | walk walk_keys walk_values |
Filter | select select_keys select_values compact |
Dicts | flip pluck where [omit](https://funcy.readthedocs.io/en/stable/colls.html#omit">project zip_values zip_dicts |
Project details
Release history Release notifications | RSS feed
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 details)
Built Distribution
File details
Details for the file funcy-chain-0.2.0.tar.gz
.
File metadata
- Download URL: funcy-chain-0.2.0.tar.gz
- Upload date:
- Size: 15.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.26.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b64f09fdebaf7b616c991eb181798191c48f14efd167de8daeb5f02d1821ba6f |
|
MD5 | f7f93597356e177bff46d2e5d1610495 |
|
BLAKE2b-256 | acb452ccf21a3f70c568a4751451e3310476c20315bbfc0a83847ac61e8aa820 |
File details
Details for the file funcy_chain-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: funcy_chain-0.2.0-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.26.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3f83f56b1091ee74a924d60740d592346e0d351a2bb2aa06e916b47db3417a01 |
|
MD5 | 49b7d5a7c2b48e0860efd076731664d7 |
|
BLAKE2b-256 | f2b86b89189054d14c6db00a03eef6b0c4733941d8eac2bdc3117fbc5beb6cf0 |