Function Toolbox for Python
This is a collection of general functions I've found useful in Python.
Most of the functions here are pure, and so can be used in a functional style of programming (though they're useful in imperative code too).
You may also want to install the 'fn' package from PyPI and to look at the standard library modules 'functools', 'itertools' and maybe 'operator'.
Python 2.6, 2.7 or Python 3.2 and upwards.
It should also work with 3.0 and 3.1, I just haven't got a copy to run the tests on at the moment.
Note most of my development is in 2.7 now, so while I'm striving for Python 3 compatibility I might not be quite so careful about 2.6 and lower.
I haven't run the tests in 3.0 or 3.1 yet.
The 'functional' package from PyPI:
sudo pip install functional
To run the doctests, I recommend you use 'nosetests':
sudo pip install nose nosetests --with-doctest
Reason I Built This
Because I don't want to repeat myself in my programs. Instead, I put any generic functions I write in here, along with tests and examples of usage.
I also hope that others, including yourself, will contribute functions to this library (see the 'Contributions' section below).
I've found one package with similar goals to this one, and it has some overlap in functionality,
It can be found on PyPI: https://pypi.python.org/pypi/jaraco.util
I haven't tested it yet.
Overview of Modules
flogic is a module of functions for composing functions that return boolean with boolean
operations. Currently only fnot() is implemented, but
fair game for inclusion should I need them.
Functions that are specifically for working on and generating dictionaries.
Make a function call once if necessary, and reuse its result many times.
Occasionally I want to check command line arguments without consuming them via a more heavyweight library like argparse or optparse; for example when I'm wrapping another program and so passing most of them on to it. These functions help with that.
Functions that help validating data and data types.
Functions for dealing with lists of records in the form of lists. For example
add_column will add a column of data to an existing table of records.
There's also funbox.mappings that deals with records in the form of dictionaries.
Functions for manipulating iterators.
Functions for manipulating finite, usually indexable, sequences.
Maybe-monad-like chaining of functions that can fail by returning None.
I welcome patches to add new functions, or to fix bugs or improve performance in existing ones.
If you want to enhance the interface of an existing function, please contribute
yours under a different name. For example you might want to curry a function
that I've left as flat, call it
funcname_c. In other words, I've got code
using this library, and I don't want it to break.
Although the functions in here are ripe for using in a functional programming style, it doesn't mean you have to implement them functionally. I do, however, ask that you provide a rigorous doctest or, if it's more complex, a unittest, to check it works as expected, however you implement it.
Copyright and License
The MIT License (MIT)
Copyright (c) 2015 Nicholas Booker
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.