Skip to main content

Python utility to implement protocols with pure functions

Project description

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:

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:

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?

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

functiongroup-0.1.3.tar.gz (2.4 kB view details)

Uploaded Source

Built Distribution

functiongroup-0.1.3-py3-none-any.whl (3.0 kB view details)

Uploaded Python 3

File details

Details for the file functiongroup-0.1.3.tar.gz.

File metadata

  • Download URL: functiongroup-0.1.3.tar.gz
  • Upload date:
  • Size: 2.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: poetry/1.6.1 CPython/3.11.6 Linux/6.2.0-1015-azure

File hashes

Hashes for functiongroup-0.1.3.tar.gz
Algorithm Hash digest
SHA256 4aba74d6e2a56ebcd1e993bf633b4022eb4e7a018a3c8f14240b01024e21ba0b
MD5 ea3eb15793d9250eb2c022c84a854ce6
BLAKE2b-256 090773b235007ace7d86a4cf3c260f905715afda5a80d9fd65c0000f0d6246b9

See more details on using hashes here.

File details

Details for the file functiongroup-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: functiongroup-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 3.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: poetry/1.6.1 CPython/3.11.6 Linux/6.2.0-1015-azure

File hashes

Hashes for functiongroup-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 1f0135b143d35eea212ecbc0119bf6680f36f56cd8a9b6dc6c76e34ee50a28a1
MD5 4ef0875fcd1a3a7720858a05a1306713
BLAKE2b-256 2bc6506a2e82282233caeab2c999d9916a67ff3c858e33e94b4c4f16ce0e5c3d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page