functiongroup


Namefunctiongroup JSON
Version 0.1.5 PyPI version JSON
download
home_page
SummaryPython utility to implement protocols with pure functions
upload_time2023-12-03 12:34:52
maintainer
docs_urlNone
authorDennis Rall
requires_python>=3.11,<4.0
licenseMIT
keywords functional protocol python
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # functiongroup
FunctionGroup is a Python utility designed to simplify the implementation of protocols using pure functions. If you've ever found yourself creating classes solely to group related functions when implementing protocols, this package offers an elegant solution.

## Example

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 save_dict(self, dict_: dict[str, str], file_name: str) -> None:
        with open(file_name, "w") as f:
            json.dump(dict_, f)

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

dict_io = JsonDictIO()
```

However, you may wonder why you need a class when you're not modifying its state or using any dunder methods. You're essentially using the class to group related functions, so why not use a `FunctionGroup` instead?

```python
from functiongroup import FunctionGroup


json_dict_io = FunctionGroup()

@json_dict_io.register
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()
```

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

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "functiongroup",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.11,<4.0",
    "maintainer_email": "",
    "keywords": "functional,protocol,python",
    "author": "Dennis Rall",
    "author_email": "dennis.rall@web.de",
    "download_url": "https://files.pythonhosted.org/packages/8c/46/060238bd5434264fed1c20d9f9bae36de256688a1ff23aba342773ca2f5d/functiongroup-0.1.5.tar.gz",
    "platform": null,
    "description": "# functiongroup\nFunctionGroup is a Python utility designed to simplify the implementation of protocols using pure functions. If you've ever found yourself creating classes solely to group related functions when implementing protocols, this package offers an elegant solution.\n\n## Example\n\nLet's consider a scenario where you have a protocol for saving and loading dictionaries, like the `DictIO` example below:\n\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```\n\nTraditionally, you'd implement this protocol by creating a class:\n\n```python\nimport json\n\n\nclass JsonDictIO(DictIO):\n    def save_dict(self, dict_: dict[str, str], file_name: str) -> None:\n        with open(file_name, \"w\") as f:\n            json.dump(dict_, f)\n\n    def load_dict(self, file_name: str) -> dict[str, str]:\n        with open(file_name) as f:\n            return json.load(f)\n\ndict_io = JsonDictIO()\n```\n\nHowever, you may wonder why you need a class when you're not modifying its state 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 functiongroup import FunctionGroup\n\n\njson_dict_io = FunctionGroup()\n\n@json_dict_io.register\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\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()\n```\n\nThis approach encourages a more functional code style and provides all the advantages of pure functions.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Python utility to implement protocols with pure functions",
    "version": "0.1.5",
    "project_urls": {
        "Documentation": "https://github.com/dennisrall/functiongroup",
        "Homepage": "https://github.com/dennisrall/functiongroup",
        "Repository": "https://github.com/dennisrall/functiongroup"
    },
    "split_keywords": [
        "functional",
        "protocol",
        "python"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c747191fc7872df43087ceca8d4201e556d8204c2869881fc2c1e8af994b9b73",
                "md5": "7d89cd7012872228e80ee63954e39ec4",
                "sha256": "4f8a49bde480f57ce8b3675250b4573d21a03bde31aea9ec34b6cb0e09b2eeed"
            },
            "downloads": -1,
            "filename": "functiongroup-0.1.5-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "7d89cd7012872228e80ee63954e39ec4",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.11,<4.0",
            "size": 3120,
            "upload_time": "2023-12-03T12:34:50",
            "upload_time_iso_8601": "2023-12-03T12:34:50.942758Z",
            "url": "https://files.pythonhosted.org/packages/c7/47/191fc7872df43087ceca8d4201e556d8204c2869881fc2c1e8af994b9b73/functiongroup-0.1.5-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8c46060238bd5434264fed1c20d9f9bae36de256688a1ff23aba342773ca2f5d",
                "md5": "93bd50fabbbd8266c46d6f43c5c4bb78",
                "sha256": "cd3cc20fca09a93a707ca05df93613225b096f19a4208a995a78884875a025ce"
            },
            "downloads": -1,
            "filename": "functiongroup-0.1.5.tar.gz",
            "has_sig": false,
            "md5_digest": "93bd50fabbbd8266c46d6f43c5c4bb78",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.11,<4.0",
            "size": 2454,
            "upload_time": "2023-12-03T12:34:52",
            "upload_time_iso_8601": "2023-12-03T12:34:52.536516Z",
            "url": "https://files.pythonhosted.org/packages/8c/46/060238bd5434264fed1c20d9f9bae36de256688a1ff23aba342773ca2f5d/functiongroup-0.1.5.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-12-03 12:34:52",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "dennisrall",
    "github_project": "functiongroup",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "functiongroup"
}
        
Elapsed time: 0.16116s