Additional functional tools for python not covered in the functools library
Project description
Functools Extra
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
Built Distribution
Hashes for functools_extra-0.3.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2927c600281e0a53d3bd4aaeeb92ad7f58108b01bef350fec55c99ec6385da8d |
|
MD5 | de028ac82618c1e1ddf86c49248ff6cb |
|
BLAKE2b-256 | 495927b9e4d495bcbefba817826a2c6cd7cf97b71b0f6468275637b3f99ddcf9 |