Skip to main content

Functional programming in Python with generators and other utilities.

Project description

version build coveralls license

Functional programming in Python with generators and other utilities.

Features

  • Functional-style methods that work with and return generators.

  • Shorthand-style iteratees (callbacks) to easily filter and map data.

  • String object-path support for references nested data structures.

  • 100% test coverage.

  • Python 3.6+

Quickstart

Install using pip:

pip3 install fnc

Import the main module:

import fnc

Start working with data:

users = [
    {'id': 1, 'name': 'Jack', 'email': 'jack@example.org', 'active': True},
    {'id': 2, 'name': 'Max', 'email': 'max@example.com', 'active': True},
    {'id': 3, 'name': 'Allison', 'email': 'allison@example.org', 'active': False},
    {'id': 4, 'name': 'David', 'email': 'david@example.net', 'active': False}
]

Filter active users:

# Uses "matches" shorthand iteratee: dictionary
active_users = fnc.filter({'active': True}, users)
# <filter object at 0x7fa85940ec88>

active_uesrs = list(active_users)
# [{'name': 'Jack', 'email': 'jack@example.org', 'active': True},
#  {'name': 'Max', 'email': 'max@example.com', 'active': True}]

Get a list of email addresses:

# Uses "pathgetter" shorthand iteratee: string
emails = fnc.map('email', users)
# <map object at 0x7fa8577d52e8>

emails = list(emails)
# ['jack@example.org', 'max@example.com', 'allison@example.org', 'david@example.net']

Create a dict of users keyed by 'id':

# Uses "pathgetter" shorthand iteratee: string
users_by_id = fnc.keyby('id', users)
# {1: {'id': 1, 'name': 'Jack', 'email': 'jack@example.org', 'active': True},
#  2: {'id': 2, 'name': 'Max', 'email': 'max@example.com', 'active': True},
#  3: {'id': 3, 'name': 'Allison', 'email': 'allison@example.org', 'active': False},
#  4: {'id': 4, 'name': 'David', 'email': 'david@example.net', 'active': False}}

Select only 'id' and 'email' fields and return as dictionaries:

# Uses "pickgetter" shorthand iteratee: set
user_emails = list(fnc.map({'id', 'email'}, users))
# [{'email': 'jack@example.org', 'id': 1},
#  {'email': 'max@example.com', 'id': 2},
#  {'email': 'allison@example.org', 'id': 3},
#  {'email': 'david@example.net', 'id': 4}]

Select only 'id' and 'email' fields and return as tuples:

# Uses "atgetter" shorthand iteratee: tuple
user_emails = list(fnc.map(('id', 'email'), users))
# [(1, 'jack@example.org'),
#  (2, 'max@example.com'),
#  (3, 'allison@example.org'),
#  (4, 'david@example.net')]

Access nested data structures using object-path notation:

fnc.get('a.b.c[1][0].d', {'a': {'b': {'c': [None, [{'d': 100}]]}}})
# 100

# Same result but using a path list instead of a string.
fnc.get(['a', 'b', 'c', 1, 0, 'd'], {'a': {'b': {'c': [None, [{'d': 100}]]}}})
# 100

Compose multiple functions into a generator pipeline:

from functools import partial

filter_active = partial(fnc.filter, {'active': True})
get_emails = partial(fnc.map, 'email')
get_email_domains = partial(fnc.map, lambda email: email.split('@')[1])

get_active_email_domains = fnc.compose(
    filter_active,
    get_emails,
    get_email_domains,
    set,
)

email_domains = get_active_email_domains(users)
# {'example.com', 'example.org'}

Or do the same thing except using a terser “partial” shorthand:

get_active_email_domains = fnc.compose(
    (fnc.filter, {'active': True}),
    (fnc.map, 'email'),
    (fnc.map, lambda email: email.split('@')[1]),
    set,
)

email_domains = get_active_email_domains(users)
# {'example.com', 'example.org'}

For more details and examples, please see the full documentation at https://fnc.readthedocs.io.

Changelog

v0.5.1 (2020-12-14)

  • Fix bug in get/has that caused defaultdict objects to get populated on key access.

v0.5.0 (2020-10-23)

  • Fix bug in intersection/intersectionby and difference/differenceby where incorrect results could be returned when generators passed in as the sequences to compare with.

  • Add support for Python 3.9.

  • Drop support for Python <= 3.5.

v0.4.0 (2019-01-23)

  • Add functions:

    • differenceby

    • duplicatesby

    • intersectionby

    • unionby

v0.3.0 (2018-08-31)

  • compose: Introduce new “partial” shorthand where instead of passing a callable, a tuple can be given which will then be converted to a callable using functools.partial. For example, instead of fnc.compose(partial(fnc.filter, {'active': True}), partial(fnc.map, 'email')), one can do fnc.compose((fnc.filter, {'active': True}), (fnc.map, 'email')).

v0.2.0 (2018-08-24)

  • Add functions:

    • negate

    • over

    • overall

    • overany

  • Rename functions: (breaking change)

    • ismatch -> conforms

    • matches -> conformance

  • Make conforms/conformance (formerly ismatch/matches) accept callable dictionary values that act as predicates against comparison target. (breaking change)

v0.1.1 (2018-08-17)

  • pick: Don’t return None for keys that don’t exist in source object. Instead of fnc.pick(['a'], {}) == {'a': None}, it’s now fnc.pick(['a'], {}) == {}.

v0.1.0 (2018-08-15)

  • First release.

MIT License

Copyright (c) 2020 Derrick Gilland

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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

fnc-0.5.1.tar.gz (39.5 kB view details)

Uploaded Source

Built Distribution

fnc-0.5.1-py3-none-any.whl (22.2 kB view details)

Uploaded Python 3

File details

Details for the file fnc-0.5.1.tar.gz.

File metadata

  • Download URL: fnc-0.5.1.tar.gz
  • Upload date:
  • Size: 39.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.0

File hashes

Hashes for fnc-0.5.1.tar.gz
Algorithm Hash digest
SHA256 bf91e3a7378b919c5c68b435d42cf9f4dddf4942eca25f6665f8df785a9997d0
MD5 09ef0aebaa32dd59af97c73e5e46a9ea
BLAKE2b-256 6fa561a3aa99dc1e84928f808f5a16d949ac364aa378408b5db06dfb0a49a72b

See more details on using hashes here.

File details

Details for the file fnc-0.5.1-py3-none-any.whl.

File metadata

  • Download URL: fnc-0.5.1-py3-none-any.whl
  • Upload date:
  • Size: 22.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.0

File hashes

Hashes for fnc-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 06dfda77a5c98ee7bf16c109c06e2edf0bbe5410128e67fda3418b7b7d920ba1
MD5 2185e4588b14477d59140697a0cab915
BLAKE2b-256 bdb9646533d3896a7e8936b3aee5e5c6d44c12f096c1bbbd3eb20488fdd2bdc3

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