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

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: functiongroup-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 60f57d80ccaf28254aa98814b3d151c2e31a3553d6bbc2907876684964d23bf2
MD5 eb3bdb09a947c4fed803474801b26b39
BLAKE2b-256 20f964d4fc46c205431595dd8afb47e7cedb7d1edd629ee810b09151ad3410fc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: functiongroup-0.1.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2bfd0e6f23890a9c843ed0f475c980532747c39ec6489169959369425e76b68f
MD5 9f4ded6818035bfb846d90d5afdcd78d
BLAKE2b-256 ab37dfde0d4061baa1fdf090cbda6778e8d56d1df94b50e6918a19c073e83937

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