A simple composition library
Project description
Compoz
Compoz is a lightweight composition package.
Overview
Compoz provides with two main functions:
- composite (for standard composition)
- pipe (for reversed composition)
'composite'
Basic usage
'composite' will run from last to the first function provided.
from compoz import composite
def multiply_by_3(n: int) -> int:
return n * 3
def subtract_5(n: int) -> int:
return n - 5
composite_func_1 = composite([subtract_5, multiply_by_3])
print(composite_func_1(10))
# Output will be 25
composite_func_2 = composite([multiply_by_3, subtract_5])
print(composite_func_2(10))
# Output will be 15
Multiple arguments
More than one argument can be passed, which can be useful if you have the guaranty that all your functions share the exact same signature and require multiple arguments.
from compoz import composite
def add_event_type(event: dict, data: dict) -> dict:
event["event_type"] = data["event_type"]
return event
def add_actor_id(event: dict, data: dict) -> dict:
event["actor_id"] = data["actor"]["id"]
return event
build_event = composite([add_event_type, add_actor_id])
some_data = {"event_type": "car_locked", "actor": {"id": "123"}}
some_event = build_event({}, data=some_data)
If your functions do not share the exact same signature you should combine compoz with partial functions. (You can always add **kwargs to your functions signatures, but you should avoid it).
from functools import partial
from compoz import composite
def add_header(event: dict) -> dict:
event["header"] = {}
return event
def add_body(event: dict) -> dict:
event["body"] = {}
return event
def add_event_type(event: dict, event_type: str) -> dict:
event["header"]["event_type"] = event_type
return event
build_event = composite([
partial(add_event_type, event_type="car_locked"),
add_header,
add_body,
])
some_event = build_event({})
'pipe'
'pipe' will run from first to the last function provided.
from compoz import pipe
def multiply_by_3(n: int) -> int:
return n * 3
def subtract_5(n: int) -> int:
return n - 5
pipe_func_1 = pipe([subtract_5, multiply_by_3])
print(pipe_func_1(10))
# Output will be 15
pipe_func_2 = pipe([multiply_by_3, subtract_5])
print(pipe_func_2(10))
# Output will be 25
Project details
Release history Release notifications | RSS feed
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
File details
Details for the file compoz-0.1.0b2.tar.gz
.
File metadata
- Download URL: compoz-0.1.0b2.tar.gz
- Upload date:
- Size: 2.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8cd24164d7e55971f2e7f117f8dfe182134527db4cb2520bf047db04bffd8027 |
|
MD5 | 66cf2908e513fca4c28640efb9c7b64d |
|
BLAKE2b-256 | acaa97029d504950d0691d00bd0825a552bc521e1ed691e2f47dd9420ec09d76 |
File details
Details for the file compoz-0.1.0b2-py3-none-any.whl
.
File metadata
- Download URL: compoz-0.1.0b2-py3-none-any.whl
- Upload date:
- Size: 3.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ebbabec4cb5157256a74adb75007fb0ba2471fc9399ec909e1c5d097e38633ba |
|
MD5 | 7a58278ca37217a4c6b540cd3fbe748c |
|
BLAKE2b-256 | eac5ea71807cd1dd27ff06d3c521d8ee52d6440588f1aba2db3ad1981010e42f |