Skip to main content

Additional functional tools for python not covered in the functools library

Project description

Functools Extra

PyPi Supported Python versions Ruff Rye

Additional functional tools for python not covered in the functools library.

Installation

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 module or custom functions. All type-hints are preserved.

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:

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:

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 __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?

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. Check out the project and run

rye sync

to create a virtual environment and install the dependencies. After that you can run

rye run test

to run the tests,

rye run lint

to check the linting,

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.

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

functools_extra-0.3.0.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

functools_extra-0.3.0-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file functools_extra-0.3.0.tar.gz.

File metadata

  • Download URL: functools_extra-0.3.0.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.12.1

File hashes

Hashes for functools_extra-0.3.0.tar.gz
Algorithm Hash digest
SHA256 f093c5f94a738e91ff98004cf6ec4245281ec4b5cc9862204353b56e0f98033e
MD5 ca8abfb7924823b7a86f53a5dd872e8e
BLAKE2b-256 1359cc7e2582992a94b67b86bc7676c4837ba4923665339315af456fb6b036b6

See more details on using hashes here.

File details

Details for the file functools_extra-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for functools_extra-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2927c600281e0a53d3bd4aaeeb92ad7f58108b01bef350fec55c99ec6385da8d
MD5 de028ac82618c1e1ddf86c49248ff6cb
BLAKE2b-256 495927b9e4d495bcbefba817826a2c6cd7cf97b71b0f6468275637b3f99ddcf9

See more details on using hashes here.

Supported by

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