Skip to main content

Python implementation of the Scala standard library.

Project description

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}")

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

pycategory-0.1.2.tar.gz (14.7 kB view details)

Uploaded Source

Built Distribution

pycategory-0.1.2-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

File details

Details for the file pycategory-0.1.2.tar.gz.

File metadata

  • Download URL: pycategory-0.1.2.tar.gz
  • Upload date:
  • Size: 14.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.6 Darwin/23.2.0

File hashes

Hashes for pycategory-0.1.2.tar.gz
Algorithm Hash digest
SHA256 18c77e3118aad46fc920788c7944819e28c53612b4a388871e4bc0353de2f556
MD5 7866b85603ae977f6e258476b175c158
BLAKE2b-256 6539d4f72a633e23fb621d7a54125fd2a8773f21abeddcc97a982fadaace5cfa

See more details on using hashes here.

File details

Details for the file pycategory-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: pycategory-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 18.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.6 Darwin/23.2.0

File hashes

Hashes for pycategory-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 cdaa7ebfbba0a46b7779f5d7c791009f4386c4c36904fca339e8f297c71456c3
MD5 d9d479c5485472c7d29a43ac17e48abf
BLAKE2b-256 389a6402be26f4732f1421d4e7f55d6d8e58fc3952da87f16b6aef1b1960220e

See more details on using hashes here.

Supported by

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