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.5.tar.gz (2.5 kB view details)

Uploaded Source

Built Distribution

functiongroup-0.1.5-py3-none-any.whl (3.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for functiongroup-0.1.5.tar.gz
Algorithm Hash digest
SHA256 cd3cc20fca09a93a707ca05df93613225b096f19a4208a995a78884875a025ce
MD5 93bd50fabbbd8266c46d6f43c5c4bb78
BLAKE2b-256 8c46060238bd5434264fed1c20d9f9bae36de256688a1ff23aba342773ca2f5d

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for functiongroup-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 4f8a49bde480f57ce8b3675250b4573d21a03bde31aea9ec34b6cb0e09b2eeed
MD5 7d89cd7012872228e80ee63954e39ec4
BLAKE2b-256 c747191fc7872df43087ceca8d4201e556d8204c2869881fc2c1e8af994b9b73

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