Skip to content

Commit

Permalink
Merge pull request #11 from eriknw/embedsignature
Browse files Browse the repository at this point in the history
Add function signature to docstrings
  • Loading branch information
eriknw committed Apr 17, 2014
2 parents d71571d + 146ea28 commit cafa3e7
Show file tree
Hide file tree
Showing 7 changed files with 276 additions and 49 deletions.
4 changes: 3 additions & 1 deletion cytoolz/curried_exceptions.pyx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#cython: embedsignature=True
from .dicttoolz cimport c_merge_with

__all__ = ['merge_with']


def merge_with(func, *dicts):
""" Merge dictionaries and apply function to combined values
"""
Merge dictionaries and apply function to combined values
A key may occur in more than one dict, and all values mapped from the key
will be passed to the function as a list, such as func([val1, val2, ...]).
Expand Down
22 changes: 15 additions & 7 deletions cytoolz/dicttoolz.pyx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#cython: embedsignature=True
from cpython.dict cimport (PyDict_Check, PyDict_GetItem, PyDict_Merge,
PyDict_New, PyDict_SetItem, PyDict_Update)
from cpython.exc cimport PyErr_Clear, PyErr_GivenExceptionMatches, PyErr_Occurred
Expand All @@ -21,7 +22,8 @@ cdef dict c_merge(object dicts):


def merge(*dicts):
""" Merge a collection of dictionaries
"""
Merge a collection of dictionaries
>>> merge({1: 'one'}, {2: 'two'})
{1: 'one', 2: 'two'}
Expand Down Expand Up @@ -61,7 +63,8 @@ cdef dict c_merge_with(object func, object dicts):


def merge_with(func, *dicts):
""" Merge dictionaries and apply function to combined values
"""
Merge dictionaries and apply function to combined values
A key may occur in more than one dict, and all values mapped from the key
will be passed to the function as a list, such as func([val1, val2, ...]).
Expand All @@ -81,7 +84,8 @@ def merge_with(func, *dicts):


cpdef dict valmap(object func, dict d):
""" Apply function to values of dictionary
"""
Apply function to values of dictionary
>>> bills = {"Alice": [20, 15, 30], "Bob": [10, 35]}
>>> valmap(sum, bills) # doctest: +SKIP
Expand All @@ -98,7 +102,8 @@ cpdef dict valmap(object func, dict d):


cpdef dict keymap(object func, dict d):
""" Apply function to keys of dictionary
"""
Apply function to keys of dictionary
>>> bills = {"Alice": [20, 15, 30], "Bob": [10, 35]}
>>> keymap(str.lower, bills) # doctest: +SKIP
Expand All @@ -115,7 +120,8 @@ cpdef dict keymap(object func, dict d):


cpdef dict valfilter(object predicate, dict d):
""" Filter items in dictionary by value
"""
Filter items in dictionary by value
>>> iseven = lambda x: x % 2 == 0
>>> d = {1: 2, 2: 3, 3: 4, 4: 5}
Expand All @@ -135,7 +141,8 @@ cpdef dict valfilter(object predicate, dict d):


cpdef dict keyfilter(object predicate, dict d):
""" Filter items in dictionary by key
"""
Filter items in dictionary by key
>>> iseven = lambda x: x % 2 == 0
>>> d = {1: 2, 2: 3, 3: 4, 4: 5}
Expand Down Expand Up @@ -172,7 +179,8 @@ cpdef dict assoc(dict d, object key, object value):


cpdef dict update_in(dict d, object keys, object func, object default=None):
""" Update value in a (potentially) nested dictionary
"""
Update value in a (potentially) nested dictionary
inputs:
d - dictionary on which to operate
Expand Down
36 changes: 25 additions & 11 deletions cytoolz/functoolz.pyx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#cython: embedsignature=True
import inspect
from cpython.dict cimport PyDict_Merge, PyDict_New
from cpython.exc cimport PyErr_Clear, PyErr_ExceptionMatches, PyErr_Occurred
Expand Down Expand Up @@ -34,7 +35,8 @@ cdef object c_thread_first(object val, object forms):


def thread_first(val, *forms):
""" Thread value through a sequence of functions/forms
"""
Thread value through a sequence of functions/forms
>>> def double(x): return 2*x
>>> def inc(x): return x + 1
Expand Down Expand Up @@ -75,7 +77,8 @@ cdef object c_thread_last(object val, object forms):


def thread_last(val, *forms):
""" Thread value through a sequence of functions/forms
"""
Thread value through a sequence of functions/forms
>>> def double(x): return 2*x
>>> def inc(x): return x + 1
Expand Down Expand Up @@ -107,7 +110,8 @@ def thread_last(val, *forms):


cpdef object _num_required_args(object func):
""" Number of args for func
"""
Number of args for func
>>> def foo(a, b, c=None):
... return a + b + c
Expand All @@ -133,7 +137,9 @@ cpdef object _num_required_args(object func):


cdef class curry:
""" Curry a callable function
""" curry(self, func, *args, **kwargs)
Curry a callable function
Enables partial application of arguments through calling a function with an
incomplete set of arguments.
Expand All @@ -158,7 +164,7 @@ cdef class curry:
See Also:
cytoolz.curried - namespace of curried functions
http://toolz.readthedocs.org/en/latest/curry.html
http://toolz.readthedocs.org/en/latest/curry.html
"""
property __doc__:
def __get__(self):
Expand Down Expand Up @@ -269,7 +275,8 @@ cdef class c_memoize:


cpdef object memoize(object func=None, object cache=None, object key=None):
""" Cache a function's result for speedy future evaluation
"""
Cache a function's result for speedy future evaluation
Considerations:
Trades memory for speed.
Expand Down Expand Up @@ -311,7 +318,9 @@ cpdef object memoize(object func=None, object cache=None, object key=None):


cdef class Compose:
""" A composition of functions
""" Compose(self, *funcs)
A composition of functions
See Also:
compose
Expand All @@ -338,7 +347,8 @@ cdef object c_compose(object funcs):


def compose(*funcs):
""" Compose functions to operate in series.
"""
Compose functions to operate in series.
Returns a function that applies other functions in sequence.
Expand All @@ -365,7 +375,8 @@ cdef object c_pipe(object data, object funcs):


def pipe(data, *funcs):
""" Pipe a value through a sequence of functions
"""
Pipe a value through a sequence of functions
I.e. ``pipe(data, f, g, h)`` is equivalent to ``h(g(f(data)))``
Expand All @@ -387,7 +398,9 @@ def pipe(data, *funcs):


cdef class complement:
""" Convert a predicate function to its logical complement.
""" complement(func)
Convert a predicate function to its logical complement.
In other words, return a function that, for inputs that normally
yield True, yields False, and vice-versa.
Expand Down Expand Up @@ -445,7 +458,8 @@ def juxt(*funcs):


cpdef object do(object func, object x):
""" Runs ``func`` on ``x``, returns ``x``
"""
Runs ``func`` on ``x``, returns ``x``
Because the results of ``func`` are not returned, only the side
effects of ``func`` are relevant.
Expand Down
Loading

0 comments on commit cafa3e7

Please sign in to comment.