fnc


Namefnc JSON
Version 0.5.3 PyPI version JSON
download
home_pagehttps://github.com/dgilland/fnc
SummaryFunctional programming in Python with generators and other utilities.
upload_time2021-10-15 01:06:58
maintainer
docs_urlNone
authorDerrick Gilland
requires_python>=3.6
licenseMIT License
keywords fnc functional functional-programming generators utility
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            fnc
***

|version| |build| |coveralls| |license|


Functional programming in Python with generators and other utilities.


Links
=====

- Project: https://github.com/dgilland/fnc
- Documentation: https://fnc.readthedocs.io
- PyPI: https://pypi.python.org/pypi/fnc/
- Github Actions: https://github.com/dgilland/fnc/actions


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:

.. code-block:: python

    import fnc


Start working with data:

.. code-block:: python

    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:

.. code-block:: python

    # 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:

.. code-block:: python

    # 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'``:

.. code-block:: python

    # 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:

.. code-block:: python

    # 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:

.. code-block:: python

    # 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:

.. code-block:: python

    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:

.. code-block:: python

    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:

.. code-block:: python

    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.


.. |version| image:: https://img.shields.io/pypi/v/fnc.svg?style=flat-square
    :target: https://pypi.python.org/pypi/fnc/

.. |build| image:: https://img.shields.io/github/workflow/status/dgilland/fnc/Main/master?style=flat-square
    :target: https://github.com/dgilland/fnc/actions

.. |coveralls| image:: https://img.shields.io/coveralls/dgilland/fnc/master.svg?style=flat-square
    :target: https://coveralls.io/r/dgilland/fnc

.. |license| image:: https://img.shields.io/pypi/l/fnc.svg?style=flat-square
    :target: https://pypi.python.org/pypi/fnc/

Changelog
=========


v0.5.3 (2021-10-14)
-------------------

- Minor performance optimization in ``pick``.


v0.5.2 (2020-12-24)
-------------------

- Fix regression in ``v0.5.1`` that broke ``get/has`` for dictionaries and dot-delimited keys that reference integer dict-keys.


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.



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/dgilland/fnc",
    "name": "fnc",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": "",
    "keywords": "fnc functional functional-programming generators utility",
    "author": "Derrick Gilland",
    "author_email": "dgilland@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/a7/0f/67708b10e3e6ebe1107772b59228a3e0126e14c5382f5f37a7114206d5da/fnc-0.5.3.tar.gz",
    "platform": "",
    "description": "fnc\n***\n\n|version| |build| |coveralls| |license|\n\n\nFunctional programming in Python with generators and other utilities.\n\n\nLinks\n=====\n\n- Project: https://github.com/dgilland/fnc\n- Documentation: https://fnc.readthedocs.io\n- PyPI: https://pypi.python.org/pypi/fnc/\n- Github Actions: https://github.com/dgilland/fnc/actions\n\n\nFeatures\n========\n\n- Functional-style methods that work with and return generators.\n- Shorthand-style iteratees (callbacks) to easily filter and map data.\n- String object-path support for references nested data structures.\n- 100% test coverage.\n- Python 3.6+\n\n\nQuickstart\n==========\n\nInstall using pip:\n\n\n::\n\n    pip3 install fnc\n\n\nImport the main module:\n\n.. code-block:: python\n\n    import fnc\n\n\nStart working with data:\n\n.. code-block:: python\n\n    users = [\n        {'id': 1, 'name': 'Jack', 'email': 'jack@example.org', 'active': True},\n        {'id': 2, 'name': 'Max', 'email': 'max@example.com', 'active': True},\n        {'id': 3, 'name': 'Allison', 'email': 'allison@example.org', 'active': False},\n        {'id': 4, 'name': 'David', 'email': 'david@example.net', 'active': False}\n    ]\n\n\nFilter active users:\n\n.. code-block:: python\n\n    # Uses \"matches\" shorthand iteratee: dictionary\n    active_users = fnc.filter({'active': True}, users)\n    # <filter object at 0x7fa85940ec88>\n\n    active_uesrs = list(active_users)\n    # [{'name': 'Jack', 'email': 'jack@example.org', 'active': True},\n    #  {'name': 'Max', 'email': 'max@example.com', 'active': True}]\n\n\nGet a list of email addresses:\n\n.. code-block:: python\n\n    # Uses \"pathgetter\" shorthand iteratee: string\n    emails = fnc.map('email', users)\n    # <map object at 0x7fa8577d52e8>\n\n    emails = list(emails)\n    # ['jack@example.org', 'max@example.com', 'allison@example.org', 'david@example.net']\n\n\nCreate a ``dict`` of users keyed by ``'id'``:\n\n.. code-block:: python\n\n    # Uses \"pathgetter\" shorthand iteratee: string\n    users_by_id = fnc.keyby('id', users)\n    # {1: {'id': 1, 'name': 'Jack', 'email': 'jack@example.org', 'active': True},\n    #  2: {'id': 2, 'name': 'Max', 'email': 'max@example.com', 'active': True},\n    #  3: {'id': 3, 'name': 'Allison', 'email': 'allison@example.org', 'active': False},\n    #  4: {'id': 4, 'name': 'David', 'email': 'david@example.net', 'active': False}}\n\n\nSelect only ``'id'`` and ``'email'`` fields and return as dictionaries:\n\n.. code-block:: python\n\n    # Uses \"pickgetter\" shorthand iteratee: set\n    user_emails = list(fnc.map({'id', 'email'}, users))\n    # [{'email': 'jack@example.org', 'id': 1},\n    #  {'email': 'max@example.com', 'id': 2},\n    #  {'email': 'allison@example.org', 'id': 3},\n    #  {'email': 'david@example.net', 'id': 4}]\n\n\nSelect only ``'id'`` and ``'email'`` fields and return as tuples:\n\n.. code-block:: python\n\n    # Uses \"atgetter\" shorthand iteratee: tuple\n    user_emails = list(fnc.map(('id', 'email'), users))\n    # [(1, 'jack@example.org'),\n    #  (2, 'max@example.com'),\n    #  (3, 'allison@example.org'),\n    #  (4, 'david@example.net')]\n\n\nAccess nested data structures using object-path notation:\n\n.. code-block:: python\n\n    fnc.get('a.b.c[1][0].d', {'a': {'b': {'c': [None, [{'d': 100}]]}}})\n    # 100\n\n    # Same result but using a path list instead of a string.\n    fnc.get(['a', 'b', 'c', 1, 0, 'd'], {'a': {'b': {'c': [None, [{'d': 100}]]}}})\n    # 100\n\n\nCompose multiple functions into a generator pipeline:\n\n.. code-block:: python\n\n    from functools import partial\n\n    filter_active = partial(fnc.filter, {'active': True})\n    get_emails = partial(fnc.map, 'email')\n    get_email_domains = partial(fnc.map, lambda email: email.split('@')[1])\n\n    get_active_email_domains = fnc.compose(\n        filter_active,\n        get_emails,\n        get_email_domains,\n        set,\n    )\n\n    email_domains = get_active_email_domains(users)\n    # {'example.com', 'example.org'}\n\n\nOr do the same thing except using a terser \"partial\" shorthand:\n\n.. code-block:: python\n\n    get_active_email_domains = fnc.compose(\n        (fnc.filter, {'active': True}),\n        (fnc.map, 'email'),\n        (fnc.map, lambda email: email.split('@')[1]),\n        set,\n    )\n\n    email_domains = get_active_email_domains(users)\n    # {'example.com', 'example.org'}\n\n\nFor more details and examples, please see the full documentation at https://fnc.readthedocs.io.\n\n\n.. |version| image:: https://img.shields.io/pypi/v/fnc.svg?style=flat-square\n    :target: https://pypi.python.org/pypi/fnc/\n\n.. |build| image:: https://img.shields.io/github/workflow/status/dgilland/fnc/Main/master?style=flat-square\n    :target: https://github.com/dgilland/fnc/actions\n\n.. |coveralls| image:: https://img.shields.io/coveralls/dgilland/fnc/master.svg?style=flat-square\n    :target: https://coveralls.io/r/dgilland/fnc\n\n.. |license| image:: https://img.shields.io/pypi/l/fnc.svg?style=flat-square\n    :target: https://pypi.python.org/pypi/fnc/\n\nChangelog\n=========\n\n\nv0.5.3 (2021-10-14)\n-------------------\n\n- Minor performance optimization in ``pick``.\n\n\nv0.5.2 (2020-12-24)\n-------------------\n\n- Fix regression in ``v0.5.1`` that broke ``get/has`` for dictionaries and dot-delimited keys that reference integer dict-keys.\n\n\nv0.5.1 (2020-12-14)\n-------------------\n\n- Fix bug in ``get/has`` that caused ``defaultdict`` objects to get populated on key access.\n\n\nv0.5.0 (2020-10-23)\n-------------------\n\n- Fix bug in ``intersection/intersectionby`` and ``difference/differenceby`` where incorrect results could be returned when generators passed in as the sequences to compare with.\n- Add support for Python 3.9.\n- Drop support for Python <= 3.5.\n\n\nv0.4.0 (2019-01-23)\n-------------------\n\n- Add functions:\n\n  - ``differenceby``\n  - ``duplicatesby``\n  - ``intersectionby``\n  - ``unionby``\n\n\nv0.3.0 (2018-08-31)\n-------------------\n\n- 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'))``.\n\n\nv0.2.0 (2018-08-24)\n-------------------\n\n- Add functions:\n\n  - ``negate``\n  - ``over``\n  - ``overall``\n  - ``overany``\n\n- Rename functions: (**breaking change**)\n\n  - ``ismatch -> conforms``\n  - ``matches -> conformance``\n\n- Make ``conforms/conformance`` (formerly ``ismatch/matches``) accept callable dictionary values that act as predicates against comparison target. (**breaking change**)\n\n\nv0.1.1 (2018-08-17)\n-------------------\n\n- 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'], {}) == {}``.\n\n\nv0.1.0 (2018-08-15)\n-------------------\n\n- First release.\n\nMIT License\n\nCopyright (c) 2020 Derrick Gilland\n\nPermission 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:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE 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.\n\n\n",
    "bugtrack_url": null,
    "license": "MIT License",
    "summary": "Functional programming in Python with generators and other utilities.",
    "version": "0.5.3",
    "project_urls": {
        "Homepage": "https://github.com/dgilland/fnc"
    },
    "split_keywords": [
        "fnc",
        "functional",
        "functional-programming",
        "generators",
        "utility"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "65cdde8c599b0d2869619401ac56c84133edf3056767656bf9fb155acb811468",
                "md5": "d1ada1b58882165efc51ff106be6b1c1",
                "sha256": "ac32552684fe1616b5f146a44d02d46cf218c6f0563a31486fb4bd63fee76eb5"
            },
            "downloads": -1,
            "filename": "fnc-0.5.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "d1ada1b58882165efc51ff106be6b1c1",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 22011,
            "upload_time": "2021-10-15T01:06:55",
            "upload_time_iso_8601": "2021-10-15T01:06:55.717319Z",
            "url": "https://files.pythonhosted.org/packages/65/cd/de8c599b0d2869619401ac56c84133edf3056767656bf9fb155acb811468/fnc-0.5.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a70f67708b10e3e6ebe1107772b59228a3e0126e14c5382f5f37a7114206d5da",
                "md5": "5d6bead107a507af122ef57ba74006f1",
                "sha256": "f2a6429e5669fee1137be3bd69b73b174cca881a08bccaa4ed09a0d86853ac6f"
            },
            "downloads": -1,
            "filename": "fnc-0.5.3.tar.gz",
            "has_sig": false,
            "md5_digest": "5d6bead107a507af122ef57ba74006f1",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 39330,
            "upload_time": "2021-10-15T01:06:58",
            "upload_time_iso_8601": "2021-10-15T01:06:58.060963Z",
            "url": "https://files.pythonhosted.org/packages/a7/0f/67708b10e3e6ebe1107772b59228a3e0126e14c5382f5f37a7114206d5da/fnc-0.5.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2021-10-15 01:06:58",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "dgilland",
    "github_project": "fnc",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "tox": true,
    "lcname": "fnc"
}
        
Elapsed time: 0.46807s