Skip to main content

A small package for effect type in python

Project description

Pyffect

Get inspiration from https://github.com/MaT1g3R/option and Scala type system

Try to support mypy type annotation.

You can use following types from this library

  • Option
  • Some
  • NONE
  • Either
  • Left
  • Right
  • Unit

Option Type Usage

from pyffect import Option, NONE, Some


def find_distance_from_sun(planet_name: str) -> Option[str]:
    planet_and_distance = {
        "Mercury": "0.39 AU",
        "Venus": "0.72 AU",
        "Earth": "1.00 AU",
        "Mars": "1.52 AU",
        "Jupiter": "5.20 AU",
        "Saturn": "9.54 AU",
        "Uranus": "19.20 AU",
        "Neptune": "30.06 AU",
    }

    if planet_name in planet_and_distance:
        return Some(planet_and_distance[planet_name])
    else:
        return NONE()


distanceFromJupiterOrNone: Option[str] = find_distance_from_sun("Jupiter")
assert distanceFromJupiterOrNone.is_defined
assert distanceFromJupiterOrNone.value == "5.20 AU"

distanceFromUnknownPlanetOrNone: Option[str] = find_distance_from_sun("Unknown Planet")
assert distanceFromUnknownPlanetOrNone.is_empty
assert distanceFromUnknownPlanetOrNone.get_or_else("Unknown Distance") == "Unknown Distance"

Either Type Usage

from pyffect import Either, Right, Left


def divide(numerator: int, denominator: int) -> Either[str, float]:
    try:
        value = numerator / denominator
        return Right(value)
    except:
        return Left('unable to perform the operation.')


firstValue: Either[str, float] = divide(5, 0)
assert firstValue.is_left
assert firstValue.left_value == 'unable to perform the operation.'
secondValue: Either[str, float] = divide(5, 2)
assert secondValue.is_right
assert secondValue.right_value == 2.5

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

pyffect-0.0.5.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

pyffect-0.0.5-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file pyffect-0.0.5.tar.gz.

File metadata

  • Download URL: pyffect-0.0.5.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for pyffect-0.0.5.tar.gz
Algorithm Hash digest
SHA256 958d3a79be13a4b1ea8fd5a8656d83b3c8d7b6f482afafeb8b46afbcce34ba0b
MD5 f65212c52cc36cd759dd1267aba1b57d
BLAKE2b-256 2c86d1e4f3bd77d28a3397b4438bbd0ee440c95cfbf70cec3280ca7fb2474376

See more details on using hashes here.

File details

Details for the file pyffect-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: pyffect-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.7

File hashes

Hashes for pyffect-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 5c5dada899920a66b712db55a43fb413cb10f7fc0d92473ed8f36d142837c730
MD5 06c15edd541c7a385b952c6a407f01b3
BLAKE2b-256 46abf088cb8651d563f2bb24f7895fb237c69a61f613e36d1fdea7b35e5aa6ce

See more details on using hashes here.

Supported by

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