functools-extra


Namefunctools-extra JSON
Version 0.3.0 PyPI version JSON
download
home_page
SummaryAdditional functional tools for python not covered in the functools library
upload_time2024-02-27 16:39:47
maintainer
docs_urlNone
author
requires_python>=3.8
licenseMIT License Copyright (c) 2024 Dennis Rall 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.
keywords functional python tools
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Functools Extra
[![PyPi](https://img.shields.io/pypi/v/functools-extra?color=%2334D058&label=pypi)](https://pypi.org/project/functools-extra/)
[![Supported Python versions](https://img.shields.io/pypi/pyversions/functools-extra.svg?color=%2334D058)](https://pypi.org/project/functools-extra/)
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
[![Rye](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/mitsuhiko/rye/main/artwork/badge.json)](https://rye-up.com/)

Additional functional tools for python not covered in the [functools](https://docs.python.org/3/library/functools.html) library.

## Installation

```bash
pip install functools-extra

```

## How to use
### Pipes
A pipe is a function that takes a value and list of functions and calls them in order.
So `foo(bar(value))` is equivalent to `pipe(value, bar, foo)`.
You can use built-in functions like `list`, special operators from the [operator](https://docs.python.org/3/library/operator.html) module or custom functions.
All type-hints are preserved.
```python
from functools_extra import pipe
from operator import itemgetter

def add_one(x: int) -> int:
     return x + 1

assert pipe(range(3), list, itemgetter(2), add_one) == 3
```

Or you can use `pipe_builder` to create a reusable pipe:
```python
from functools_extra import pipe_builder

def add_one(x: int) -> int:
    return x + 1

def double(x: int) -> int:
    return x * 2

add_one_and_double = pipe_builder(add_one, double)
assert add_one_and_double(1) == 4
assert add_one_and_double(2) == 6
```

### FunctionGroups
A FunctionGroup makes it possible to implement a `Protocol` with pure functions. If you've ever found yourself creating classes solely to group related functions together to implement protocols, just use a `FunctionGroup` instead.


Let's consider a scenario where you have a protocol for saving and loading dictionaries, like the `DictIO` example below:
```python
from typing import Protocol


class DictIO(Protocol):
    def save_dict(self, dict_: dict[str, str], file_name: str) -> None:
        ...

    def load_dict(self, file_name: str) -> dict[str, str]:
        ...
```
Traditionally, you'd implement this protocol by creating a class:

```python
import json


class JsonDictIO(DictIO):
    def __init__(self, file_name: str):
        self.file_name = file_name

    def save_dict(self, dict_: dict[str, str]) -> None:
        with open(self.file_name, "w") as f:
            json.dump(dict_, f)

    def load_dict(self) -> dict[str, str]:
        with open(self.file_name) as f:
            return json.load(f)

dict_io = JsonDictIO("example.json")
```
However, you may wonder why you need a class when you're not modifying its state (you're just using it to store common args) or using any dunder methods. You're essentially using the class to group related functions, so why not use a `FunctionGroup` instead?

```python
from functools_extra import FunctionGroup


json_dict_io = FunctionGroup()

@json_dict_io.register(name="save_dict")
def save_dict(dict_: dict[str, str], file_name: str) -> None:
    with open(file_name, "w") as f:
        json.dump(dict_, f)

@json_dict_io.register
def load_dict(file_name: str) -> dict[str, str]:
    with open(file_name) as f:
        return json.load(f)

dict_io = json_dict_io(file_name="example.json")
```

This approach encourages a more functional code style and provides all the advantages of pure functions.

## Development
This project is using [Rye](https://rye-up.com/).
Check out the project and run
```bash
rye sync
```
to create a virtual environment and install the dependencies. After that you can run
```bash
rye run test
```
to run the tests,
```bash
rye run lint
```
to check the linting,
```bash
rye run fix
```
to format the project with ruff and fix the fixable errors.

## License

This project is licensed under the terms of the MIT license.

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "functools-extra",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "functional,python,tools",
    "author": "",
    "author_email": "Dennis Rall <dennis.rall@web.de>",
    "download_url": "https://files.pythonhosted.org/packages/13/59/cc7e2582992a94b67b86bc7676c4837ba4923665339315af456fb6b036b6/functools_extra-0.3.0.tar.gz",
    "platform": null,
    "description": "# Functools Extra\n[![PyPi](https://img.shields.io/pypi/v/functools-extra?color=%2334D058&label=pypi)](https://pypi.org/project/functools-extra/)\n[![Supported Python versions](https://img.shields.io/pypi/pyversions/functools-extra.svg?color=%2334D058)](https://pypi.org/project/functools-extra/)\n[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)\n[![Rye](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/mitsuhiko/rye/main/artwork/badge.json)](https://rye-up.com/)\n\nAdditional functional tools for python not covered in the [functools](https://docs.python.org/3/library/functools.html) library.\n\n## Installation\n\n```bash\npip install functools-extra\n\n```\n\n## How to use\n### Pipes\nA pipe is a function that takes a value and list of functions and calls them in order.\nSo `foo(bar(value))` is equivalent to `pipe(value, bar, foo)`.\nYou can use built-in functions like `list`, special operators from the [operator](https://docs.python.org/3/library/operator.html) module or custom functions.\nAll type-hints are preserved.\n```python\nfrom functools_extra import pipe\nfrom operator import itemgetter\n\ndef add_one(x: int) -> int:\n     return x + 1\n\nassert pipe(range(3), list, itemgetter(2), add_one) == 3\n```\n\nOr you can use `pipe_builder` to create a reusable pipe:\n```python\nfrom functools_extra import pipe_builder\n\ndef add_one(x: int) -> int:\n    return x + 1\n\ndef double(x: int) -> int:\n    return x * 2\n\nadd_one_and_double = pipe_builder(add_one, double)\nassert add_one_and_double(1) == 4\nassert add_one_and_double(2) == 6\n```\n\n### FunctionGroups\nA FunctionGroup makes it possible to implement a `Protocol` with pure functions. If you've ever found yourself creating classes solely to group related functions together to implement protocols, just use a `FunctionGroup` instead.\n\n\nLet's consider a scenario where you have a protocol for saving and loading dictionaries, like the `DictIO` example below:\n```python\nfrom typing import Protocol\n\n\nclass DictIO(Protocol):\n    def save_dict(self, dict_: dict[str, str], file_name: str) -> None:\n        ...\n\n    def load_dict(self, file_name: str) -> dict[str, str]:\n        ...\n```\nTraditionally, you'd implement this protocol by creating a class:\n\n```python\nimport json\n\n\nclass JsonDictIO(DictIO):\n    def __init__(self, file_name: str):\n        self.file_name = file_name\n\n    def save_dict(self, dict_: dict[str, str]) -> None:\n        with open(self.file_name, \"w\") as f:\n            json.dump(dict_, f)\n\n    def load_dict(self) -> dict[str, str]:\n        with open(self.file_name) as f:\n            return json.load(f)\n\ndict_io = JsonDictIO(\"example.json\")\n```\nHowever, you may wonder why you need a class when you're not modifying its state (you're just using it to store common args) or using any dunder methods. You're essentially using the class to group related functions, so why not use a `FunctionGroup` instead?\n\n```python\nfrom functools_extra import FunctionGroup\n\n\njson_dict_io = FunctionGroup()\n\n@json_dict_io.register(name=\"save_dict\")\ndef save_dict(dict_: dict[str, str], file_name: str) -> None:\n    with open(file_name, \"w\") as f:\n        json.dump(dict_, f)\n\n@json_dict_io.register\ndef load_dict(file_name: str) -> dict[str, str]:\n    with open(file_name) as f:\n        return json.load(f)\n\ndict_io = json_dict_io(file_name=\"example.json\")\n```\n\nThis approach encourages a more functional code style and provides all the advantages of pure functions.\n\n## Development\nThis project is using [Rye](https://rye-up.com/).\nCheck out the project and run\n```bash\nrye sync\n```\nto create a virtual environment and install the dependencies. After that you can run\n```bash\nrye run test\n```\nto run the tests,\n```bash\nrye run lint\n```\nto check the linting,\n```bash\nrye run fix\n```\nto format the project with ruff and fix the fixable errors.\n\n## License\n\nThis project is licensed under the terms of the MIT license.\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2024 Dennis Rall  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.",
    "summary": "Additional functional tools for python not covered in the functools library",
    "version": "0.3.0",
    "project_urls": {
        "Changelog": "https://github.com/dennisrall/functools-extra/releases",
        "Documentation": "https://github.com/dennisrall/functools-extra",
        "Homepage": "https://github.com/dennisrall/functools-extra",
        "Issues": "https://github.com/dennisrall/functools-extra/issues",
        "Repository": "https://github.com/dennisrall/functools-extra"
    },
    "split_keywords": [
        "functional",
        "python",
        "tools"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "495927b9e4d495bcbefba817826a2c6cd7cf97b71b0f6468275637b3f99ddcf9",
                "md5": "de028ac82618c1e1ddf86c49248ff6cb",
                "sha256": "2927c600281e0a53d3bd4aaeeb92ad7f58108b01bef350fec55c99ec6385da8d"
            },
            "downloads": -1,
            "filename": "functools_extra-0.3.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "de028ac82618c1e1ddf86c49248ff6cb",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 6611,
            "upload_time": "2024-02-27T16:39:45",
            "upload_time_iso_8601": "2024-02-27T16:39:45.910373Z",
            "url": "https://files.pythonhosted.org/packages/49/59/27b9e4d495bcbefba817826a2c6cd7cf97b71b0f6468275637b3f99ddcf9/functools_extra-0.3.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "1359cc7e2582992a94b67b86bc7676c4837ba4923665339315af456fb6b036b6",
                "md5": "ca8abfb7924823b7a86f53a5dd872e8e",
                "sha256": "f093c5f94a738e91ff98004cf6ec4245281ec4b5cc9862204353b56e0f98033e"
            },
            "downloads": -1,
            "filename": "functools_extra-0.3.0.tar.gz",
            "has_sig": false,
            "md5_digest": "ca8abfb7924823b7a86f53a5dd872e8e",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 8295,
            "upload_time": "2024-02-27T16:39:47",
            "upload_time_iso_8601": "2024-02-27T16:39:47.508077Z",
            "url": "https://files.pythonhosted.org/packages/13/59/cc7e2582992a94b67b86bc7676c4837ba4923665339315af456fb6b036b6/functools_extra-0.3.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-02-27 16:39:47",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "dennisrall",
    "github_project": "functools-extra",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "functools-extra"
}
        
Elapsed time: 0.18849s