Skip to main content

pycategory

Installation

pip install works for this library.

pip install pycategory

Usage

Here is an example of how to use the pycategory package:

curry

from pycategory import curry

@curry
def function(arg1: int, /, arg2: int, arg3: int = 3, *, arg4: int = 4) -> int:
    return arg1 + arg2 + arg3 + arg4

function2 = function        # (int) -> ((int) -> int)
function1 = function2(1)    # (int) -> int
result = function1(2)       # int

Pipeline

from pycategory import Pipeline, curry

def squared(value: int) -> int:
    return value**2

assert 42**2**2 == ~(Pipeline(42) >> squared >> squared)

@Pipeline
@curry
def cubed(arg1: int, arg2: int, arg3: int) -> int:
    return arg1 * arg2 * arg3

assert 42**3 == ~(cubed << 42 << 42 << 42)

Either

from pycategory import Either, EitherDo, Frame, Left, Right

class Error(Frame):
    ...

@Either.do
def context(value: int) -> EitherDo[Error, int]:
    one = yield from Left[Error, int](Error(unmask=("value",)))
    two = 2
    three = yield from Right[Error, int](3)
    return one + two + three

match context(42).pattern:
    case Left(value):
        print(f"Left case {value}")
    case Right(value):
        print(f"Right case {value}")

Option

from pycategory import VOID, Option, OptionDo, Some, Void

@Option.do
def context() -> OptionDo[int]:
    one = yield from VOID
    two = 2
    three = yield from Some[int](3)
    return one + two + three

match context().pattern:
    case Void():
        print("Void case")
    case Some(value):
        print(f"Some case {value}")

Try

from pycategory import Failure, Success, Try, TryDo

@Try.hold(unmask=("value",))
def hold_context(value: int, /) -> int:
    if not value:
        raise Exception("error")
    return value

@Try.do
def context() -> TryDo[int]:
    one = yield from hold_context(0)
    two = 2
    three = yield from Success[int](3)
    return one + two + three

match context().pattern:
    case Failure() as failure:
        print(f"Failure case {failure.exception}")
    case Success(value):
        print(f"Success case {value}")

Metadata

Release files for pycategory 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pycategory 0.1.2
File Size Uploaded
pycategory-0.1.2.tar.gz 14.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pycategory 0.1.2
File Interpreter ABI Platform
pycategory-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 33.2 kB

Release files / pycategory-0.1.2.tar.gz

Download URL pycategory-0.1.2.tar.gz
Size 14.7 kB
Tags Source
SHA-256 checksum
How to use checksums
18c77e3118aad46fc920788c7944819e28c53612b4a388871e4bc0353de2f556
BLAKE2b-256 checksum
How to use checksums
6539d4f72a633e23fb621d7a54125fd2a8773f21abeddcc97a982fadaace5cfa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.11.6 Darwin/23.2.0

Release files / pycategory-0.1.2-py3-none-any.whl

Download URL pycategory-0.1.2-py3-none-any.whl
Size 18.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cdaa7ebfbba0a46b7779f5d7c791009f4386c4c36904fca339e8f297c71456c3
BLAKE2b-256 checksum
How to use checksums
389a6402be26f4732f1421d4e7f55d6d8e58fc3952da87f16b6aef1b1960220e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.11.6 Darwin/23.2.0

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page