Skip to main content

Good Kiwi Common Library

Project description

good-common

A small set of common dependencies for Good Kiwi.

Dependency Provider

BaseProvider is a base class for creating fast_depends (so FastAPI and FastStream compatible) dependency providers.

class APIClient:
    def __init__(self, api_key: str):
        self.api_key = api_key

    def get(self, url: str):
        return f"GET {url} with {self.api_key}"

class APIClientProvider(BaseProvider[APIClient], APIClient):
    pass


from fast_depends import inject

@inject
def some_task(
    api_client: APIClient = APIClientProvider(api_key="1234"),
):
    return api_client.get("https://example.com")

Can also be used without fast_depends:

client = APIClientProvider(api_key="1234").get()

Override initializer to customize how the dependency class is initialized.

class APIClientProvider(BaseProvider[APIClient], APIClient):
    def initializer(
        self,
        cls_args: typing.Tuple[typing.Any, ...],  # args passed to the Provider
        cls_kwargs: typing.Dict[str, typing.Any],  # kwargs passed to the Provider
        fn_kwargs: typing.Dict[str, typing.Any],  # kwargs passed to the function at runtime
    ):
        return cls_args, {**cls_kwargs, **fn_kwargs}  # override the api_key with the one passed to the function


@inject
def some_task(
    api_key: str,
    api_client: APIClient = APIClientProvider(),
):
    return api_client.get("https://example.com")


some_task(api_key="5678")

Pipeline

Overview

The Pipeline library provides a flexible and efficient way to create and execute pipelines of components in Python. It supports both synchronous and asynchronous execution, type checking, parallel processing, and error handling.

Features

  • Create pipelines with multiple components that can accept multiple inputs and produce multiple outputs
  • Typed "channels" for passing data between components
  • Support for both synchronous and asynchronous components
  • Type checking for inputs and outputs using Python type annotations
  • Parallel execution of pipeline instances
  • Error handling with Result types
  • Function mapping for flexible component integration

Quick Start

from typing import Annotated
from good_common.pipeline import Pipeline, Attribute

def add(a: int, b: int) -> Annotated[int, Attribute("result")]:
    return a + b

def multiply(result: int, factor: int) -> Annotated[int, Attribute("result")]:
    return result * factor

# Create a pipeline
my_pipeline = Pipeline(add, multiply)

# Execute the pipeline
result = await my_pipeline(a=2, b=3, factor=4)
print(result.result)  # Output: 20

Usage

Creating a Pipeline

Use the Pipeline class to create a new pipeline:

from pipeline import Pipeline

my_pipeline = Pipeline(component1, component2, component3)

Defining Components

Components can be synchronous or asynchronous functions:

from typing import Annotated
from pipeline import Attribute

def sync_component(x: int) -> Annotated[int, Attribute("result")]:
    return x + 1

async def async_component(x: int) -> Annotated[int, Attribute("result")]:
    await asyncio.sleep(0.1)
    return x * 2

Executing a Pipeline

Execute a pipeline asynchronously:

result = await my_pipeline(x=5)
print(result.result)

Parallel Execution

Execute a pipeline with multiple inputs in parallel:

inputs = [{"a": 1, "b": 2, "factor": 2}, {"a": 2, "b": 3, "factor": 3}]
results = [result async for result in my_pipeline.execute(*inputs, max_workers=3)]

for result in results:
    if result.is_ok():
        print(result.unwrap().result)
    else:
        print(f"Error: {result.unwrap_err()}")

Error Handling

The pipeline handles errors gracefully in parallel execution:

def faulty_component(x: int) -> Annotated[int, Attribute("result")]:
    if x == 2:
        raise ValueError("Error on purpose!")
    return x + 1

pipeline = Pipeline(faulty_component)
inputs = [{"x": 1}, {"x": 2}, {"x": 3}]
results = [result async for result in pipeline.execute(*inputs)]

for result in results:
    if result.is_ok():
        print(result.unwrap().result)
    else:
        print(f"Error: {result.unwrap_err()}")

Function Mapping

Use function_mapper to adjust input parameter names:

from pipeline import function_mapper

def multiply_diff(difference: int, factor: int) -> Annotated[int, Attribute("result")]:
    return difference * factor

pipeline = Pipeline(subtract, function_mapper(multiply_diff, diff="difference"))

Advanced Features

  • Mixed synchronous and asynchronous components in a single pipeline
  • Custom output types with Attribute annotations
  • Flexible error handling in both single and parallel executions

Utilities

Various utility functions for common tasks.

Look at /tests/good_common/utilities for usage

Project details


Release history Release notifications | RSS feed

This version

0.0.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

good_common-0.0.0.tar.gz (413.9 kB view details)

Uploaded Source

Built Distributions

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

good_common-0.0.0-cp313-cp313-win_amd64.whl (502.2 kB view details)

Uploaded CPython 3.13Windows x86-64

good_common-0.0.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

good_common-0.0.0-cp313-cp313-macosx_11_0_arm64.whl (516.4 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

good_common-0.0.0-cp313-cp313-macosx_10_13_x86_64.whl (519.3 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

good_common-0.0.0-cp313-cp313-macosx_10_13_universal2.whl (658.3 kB view details)

Uploaded CPython 3.13macOS 10.13+ universal2 (ARM64, x86-64)

File details

Details for the file good_common-0.0.0.tar.gz.

File metadata

  • Download URL: good_common-0.0.0.tar.gz
  • Upload date:
  • Size: 413.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for good_common-0.0.0.tar.gz
Algorithm Hash digest
SHA256 b70446089fea4ebec8231def1317fc3cb622206ed08cbebcb95ce845b0ab945f
MD5 9d13d1034cd3a8e2ac8038311b30f714
BLAKE2b-256 f204a77314d2f4f03f0147486b19fe70a7d295f4dd8662981823a4ddabca322b

See more details on using hashes here.

File details

Details for the file good_common-0.0.0-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for good_common-0.0.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 02bb543455cc5308ebaf6951021e75bd4c7f4f7a911e9f3f654225e5293dc9ae
MD5 082b4dd8cb7ece7f670bf081f93328d4
BLAKE2b-256 cd3bcbc2dec9a4a779f719925935adfcec1e0886280cf13abd4a3231b3ec1819

See more details on using hashes here.

File details

Details for the file good_common-0.0.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for good_common-0.0.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 91a91491dfcb85a8beabc69d5ee81ef9e65f4ad1e36c1636ee5a5d12346503ca
MD5 2d334807dee52bcea04136a22b01b5a2
BLAKE2b-256 dc2823c5e6a0820d8c394a6ce30853b94817ccf9bb5491688329556605419569

See more details on using hashes here.

File details

Details for the file good_common-0.0.0-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for good_common-0.0.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9d623368b5803731e69b360eedcca6d886d0d11243874ade3d0821a39f05e2d7
MD5 5b4393059d3fd75c4cf58d7522895918
BLAKE2b-256 5cae0e42b9759ef1fb5deb8fd346931002d973dd6a048da150e639f3df31dc0b

See more details on using hashes here.

File details

Details for the file good_common-0.0.0-cp313-cp313-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for good_common-0.0.0-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 e2a73f7cc18a731513ad0334657269d4bad5468feceea10b2b33b9bfea805a82
MD5 c5b5a2186052f02b6e68cdb41e50eacc
BLAKE2b-256 3e219551f93782cfe5dfc56f34b07dd5d2ce4cd9a351fb71bd45b05a6e6c9ecc

See more details on using hashes here.

File details

Details for the file good_common-0.0.0-cp313-cp313-macosx_10_13_universal2.whl.

File metadata

File hashes

Hashes for good_common-0.0.0-cp313-cp313-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 4f9558c6a4b8ca7b607186106812150e45e6ea2c63ebbae6599b6f2b67a5971c
MD5 ee479e83cfc128ffe84ce7841de47d57
BLAKE2b-256 f275eed1e509095fe22734d1e512929e27a50300b8f059e07129f9b99d3c3029

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