• Stars
    star
    247
  • Rank 164,117 (Top 4 %)
  • Language
    Python
  • License
    MIT License
  • Created over 6 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Functional programming in Python with generators and other utilities.

fnc

version build coveralls license

Functional programming in Python with generators and other utilities.

Links

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': '[email protected]', 'active': True},
    {'id': 2, 'name': 'Max', 'email': '[email protected]', 'active': True},
    {'id': 3, 'name': 'Allison', 'email': '[email protected]', 'active': False},
    {'id': 4, 'name': 'David', 'email': '[email protected]', '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': '[email protected]', 'active': True},
#  {'name': 'Max', 'email': '[email protected]', '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)
# ['[email protected]', '[email protected]', '[email protected]', '[email protected]']

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': '[email protected]', 'active': True},
#  2: {'id': 2, 'name': 'Max', 'email': '[email protected]', 'active': True},
#  3: {'id': 3, 'name': 'Allison', 'email': '[email protected]', 'active': False},
#  4: {'id': 4, 'name': 'David', 'email': '[email protected]', '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': '[email protected]', 'id': 1},
#  {'email': '[email protected]', 'id': 2},
#  {'email': '[email protected]', 'id': 3},
#  {'email': '[email protected]', '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, '[email protected]'),
#  (2, '[email protected]'),
#  (3, '[email protected]'),
#  (4, '[email protected]')]

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.

More Repositories

1

pydash

The kitchen sink of Python utility libraries for doing "stuff" in a functional way. Based on the Lo-Dash Javascript library.
Python
1,291
star
2

cacheout

A caching library for Python
Python
415
star
3

hashfs

A content-addressable file management system for Python.
Python
212
star
4

sqlservice

The missing SQLAlchemy ORM interface.
Python
178
star
5

pushjack

Push notifications for APNS (iOS) and GCM (Android).
Python
129
star
6

omdb.py

Python wrapper around OMDb API (Open Movie Database): http://omdbapi.com
Python
98
star
7

alchy

The declarative companion to SQLAlchemy
Python
75
star
8

shelmet

A shell power-up for working with the file system and running subprocess commands
Python
74
star
9

flask-pushjack

Flask extension for push notifications on APNS (iOS) and GCM (Android)
Python
70
star
10

verify

A painless assertion and validation library for Python.
Python
66
star
11

zulu

A drop-in replacement for native Python datetimes that embraces UTC.
Python
60
star
12

yummly.py

Python library for Yummly API: https://developer.yummly.com
Python
28
star
13

flask-logconfig

Flask extension for configuring Python logging module
Python
23
star
14

flask-alchy

Flask extension for alchy, the SQLAlchemy enhancement library
Python
17
star
15

logconfig

Simple helper moudle for configuring Python logging
Python
10
star
16

flask-hashfs

Flask extension for HashFS, a content-addressable file management system
Python
7
star
17

pixif

Python script for moving/copying photos from a directory and saving them based on EXIF tag data.
Python
6
star
18

ladder

HTTP client wrapper with URL generation via object notation and argument passing
Python
6
star
19

schemable

Schema validation and parsing library
Python
3
star
20

blog

Source code for my blog
Python
3
star
21

carafe

Flask application factory with extensions geared towards JSON APIs
Python
2
star