funcy 1.5

A fancy and practical functional tools

<object data="https://badges.gitter.im/JoinChat.svg" type="image/svg+xml">Join the chat at https://gitter.im/Suor/funcy</object>

A collection of fancy functional tools focused on practicality.

Inspired by clojure, underscore and my own abstractions. Keep reading to get an overview or read the docs.

Works with Python 2.6+, 3.3+ and pypy.

Installation

pip install funcy

Overview

Just import stuff from funcy to make things happen:

from funcy import whatever, you, need

Merge collections of same type (works for dicts, sets, lists, tuples, iterators and even strings):

merge(coll1, coll2, coll3, ...)
join(colls)
merge_with(sum, dict1, dict2, ...)

Walk through collection, creating its transform (like map but preserves type):

walk(str.upper, {'a', 'b'})            # {'A', 'B'}
walk(reversed, {'a': 1, 'b': 2})       # {1: 'a', 2: 'b'}
walk_keys(double, {'a': 1, 'b': 2})    # {'aa': 1, 'bb': 2}
walk_values(inc, {'a': 1, 'b': 2})     # {'a': 2, 'b': 3}

Select a part of collection:

select(even, {1,2,3,10,20})                  # {2,10,20}
select(r'^a', ('a','b','ab','ba'))           # ('a','ab')
select_keys(callable, {str: '', None: None}) # {str: ''}
compact({2, None, 1, 0})                     # {1,2}

Manipulate sequences:

take(4, iterate(double, 1)) # [1, 2, 4, 8]
first(drop(3, count(10)))   # 13

remove(even, [1, 2, 3])     # [1, 3]
concat([1, 2], [5, 6])      # [1, 2, 5, 6]
cat(map(range, range(4)))   # [0, 0, 1, 0, 1, 2]
mapcat(range, range(4))     # same
flatten(nested_structure)   # flat_list
distinct('abacbdd')         # list('abcd')

split(odd, range(5))        # ([1, 3], [0, 2, 4])
split_at(2, range(5))       # ([0, 1], [2, 3, 4])
group_by(mod3, range(5))    # {0: [0, 3], 1: [1, 4], 2: [2]}

partition(2, range(5))      # [[0, 1], [2, 3]]
chunks(2, range(5))         # [[0, 1], [2, 3], [4]]
pairwise(range(5))          # iter: [0, 1], [1, 2], ...

And functions:

partial(add, 1)             # inc
curry(add)(1)(2)            # 3
compose(inc, double)(10)    # 21
complement(even)            # odd
all_fn(isa(int), even)      # is_even_int

one_third = rpartial(operator.div, 3.0)
has_suffix = rcurry(str.endswith)

Create decorators easily:

@decorator
def log(call):
    print call._func.__name__, call._args
    return call()

Abstract control flow:

walk_values(silent(int), {'a': '1', 'b': 'no'})
# => {'a': 1, 'b': None}

@once
def initialize():
    "..."

with suppress(OSError):
    os.remove('some.file')

@ignore(ErrorRateExceeded)
@limit_error_rate(fails=5, timeout=60)
@retry(tries=2, errors=(HttpError, ServiceDown))
def some_unreliable_action(...):
    "..."

class MyUser(AbstractBaseUser):
    @cached_property
    def public_phones(self):
        return self.phones.filter(public=True)

Ease debugging:

squares = {tap(x, 'x'): tap(x * x, 'x^2') for x in [3, 4]}
# x: 3
# x^2: 9
# ...

@print_exits
def some_func(...):
    "..."

@log_calls(log.info, errors=False)
@log_errors(log.exception)
def some_suspicious_function(...):
    "..."

with print_durations('Creating models'):
    Model.objects.create(...)
    # ...
# 10.2 ms in Creating models

And much more.

Running tests

To run the tests using your default python:

pip install -r test_requirements.txt
py.test

To fully run tox you need all the supported pythons to be installed. These are 2.6, 2.7, 3.3, 3.4, PyPy and PyPy3. You can run it for particular environment even in absense of all of the above:

tox -e py27
tox -e py34
tox -e flakes # lint
BSD

Author

Alexander Schepanovski

Pip

funcy==1.5

Classifiers

  • Development Status :: 5 - Production/Stable
  • Intended Audience :: Developers
  • Operating System :: OS Independent
  • Programming Language :: Python
  • Programming Language :: Python :: 2
  • Programming Language :: Python :: 2.6
  • Programming Language :: Python :: 2.7
  • Programming Language :: Python :: 3
  • Programming Language :: Python :: 3.3
  • Programming Language :: Python :: 3.4
  • License :: OSI Approved :: BSD License
  • Topic :: Software Development :: Libraries :: Python Modules
  • Programming Language :: Python :: Implementation :: CPython
  • Programming Language :: Python :: Implementation :: PyPy
File Type Python Version Uploaded On Downloads
funcy-1.5.tar.gz Source April 24, 2015 26,353
Version Release Date
1.5 April 24, 2015
1.4 Feb. 8, 2015
1.3 Oct. 20, 2014
1.2 Aug. 18, 2014
1.1 June 19, 2014
1.0.0 June 4, 2014
0.10.1 April 16, 2014
0.10 March 3, 2014
0.9 Jan. 5, 2014
0.8 Dec. 19, 2013
0.7 Nov. 13, 2013
0.6.0 Oct. 27, 2013
0.5.6 Oct. 11, 2013
0.5.4 Aug. 21, 2013
0.5.3 May 24, 2013
0.5.2 May 22, 2013
0.5.1 May 8, 2013
0.5.0 April 25, 2013
0.4.1 April 16, 2013
0.4.0 April 6, 2013
0.3.4 March 14, 2013
0.3.3 March 6, 2013
0.3.2 Feb. 25, 2013
0.3.1 Feb. 15, 2013
0.3.0 Jan. 18, 2013
0.2.1 Dec. 18, 2012
0.2.0 Nov. 29, 2012
0.1.0 Nov. 18, 2012
Date Package Version Action
Aug. 24, 2015, 11:19 p.m. funcy 0.8 Release Created
Aug. 24, 2015, 11:19 p.m. funcy 0.10 Release Created
Aug. 24, 2015, 11:19 p.m. funcy 0.9 Release Created
Aug. 24, 2015, 11:19 p.m. funcy 0.5.4 Release Created
Aug. 24, 2015, 11:19 p.m. funcy 0.5.3 Release Created
Aug. 24, 2015, 11:19 p.m. funcy 0.5.2 Release Created
Aug. 24, 2015, 11:19 p.m. funcy 0.5.1 Release Created
Aug. 24, 2015, 11:19 p.m. funcy 0.5.0 Release Created
Aug. 24, 2015, 11:19 p.m. funcy 0.5.6 Release Created
Aug. 24, 2015, 11:19 p.m. funcy 0.4.0 Release Created
Aug. 24, 2015, 11:19 p.m. funcy 0.4.1 Release Created
Aug. 24, 2015, 11:19 p.m. funcy 0.3.4 Release Created
Aug. 24, 2015, 11:19 p.m. funcy 0.6.0 Release Created
Aug. 24, 2015, 11:19 p.m. funcy 0.3.0 Release Created
Aug. 24, 2015, 11:19 p.m. funcy 0.3.3 Release Created
Aug. 24, 2015, 11:19 p.m. funcy 0.3.2 Release Created
Aug. 24, 2015, 11:19 p.m. funcy 0.10.1 Release Created
Aug. 24, 2015, 11:19 p.m. funcy 0.1.0 Release Created
Aug. 24, 2015, 11:19 p.m. funcy 0.2.0 Release Created
Aug. 24, 2015, 11:19 p.m. funcy 0.2.1 Release Created
Aug. 24, 2015, 11:19 p.m. funcy 0.7 Release Created
Aug. 24, 2015, 11:19 p.m. funcy 1.3 Release Created
Aug. 24, 2015, 11:19 p.m. funcy 1.2 Release Created
Aug. 24, 2015, 11:19 p.m. funcy 1.1 Release Created
Aug. 24, 2015, 11:19 p.m. funcy 1.0.0 Release Created
Aug. 24, 2015, 11:19 p.m. funcy 0.3.1 Release Created
Aug. 24, 2015, 11:19 p.m. funcy 1.5 Release Created
Aug. 24, 2015, 11:19 p.m. funcy 1.4 Release Created
Aug. 24, 2015, 11:19 p.m. funcy Package Created