Skip to main content

Scala like Option type in Python

Project description

scala-option

python PyPI - Implementation PyPI - Wheel Test Coverage License

Scala like Option type in Python.

Implements the child classes None and Some, but renamed to none and some, since None is a built-in type in Python.

Table of Contents

Installation

Requirements

This library requires Python 3.12 or newer, because it uses the latest syntax for generics, introduced in Python 3.12.

Download Python

You can check your python version by running the command:

Windows:

py --version

Linux and macOS:

python3 --version

You can download Python from: Download Python

Installing Using PIP

You can install the package by running the command:

pip install scala-option

Building from Source

First, clone the repository or download source code from the latest release.

Next, install the build package:

pip install build

Then, build the project by running:

Windows:

py -m build

Linux and macOS:

python -m build

To install the package locally, run:

pip install -e .

Usage

Quick Start

Once you have installed the package (see Installation), you just need to add the import statement:

Attention: the package name contains an underscore instead of a dash

from scala_option import Option, none, some

Then you can begin using the Option type. Here's a little example:

import random
from scala_option import Option, none, some

def fivety_fivety() -> Option[int]:
  if random.random() <= 0.5:
    return some(1)
  else:
    return none

def print_option(option: Option) -> None:
  if option.is_empty():
    print("You got nothing!")
  else:
    print(f"You got something: {option.get()}")

Implemented Methods

The Option type implements many of the most important methods of the Scala Option (Scala Option documentation).

The methods are documented using Docstrings.

List of methods:

  • get()
  • is_empty()
  • non_empty()
  • get_or_else(default)
  • or_else(alternative)
  • map(f)
  • flat_map(f)
  • fold(if_empty, f)
  • filter(p)
  • exists(p)
  • contains(elem)
  • to_list()

License

MIT License

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

scala_option-1.0.2.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

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

scala_option-1.0.2-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file scala_option-1.0.2.tar.gz.

File metadata

  • Download URL: scala_option-1.0.2.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for scala_option-1.0.2.tar.gz
Algorithm Hash digest
SHA256 1aea65aaf7f4dbe8ba67d835dbe25a4dbd6c2da8b041efa51e71b0dd63d2a93a
MD5 2e15e1984f88e3a16038dc1262f9956b
BLAKE2b-256 01f8e1e9b8ab3c393d781e183685156ba9c00342ef5840d3192abbea090706ae

See more details on using hashes here.

File details

Details for the file scala_option-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: scala_option-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for scala_option-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 cbada0ec51a5516c93a390f13338396135745d6194d0fb6ef6fdf93bc401050b
MD5 eeb25bda8764a60dbfc23e6b283512b6
BLAKE2b-256 c8ffb7dd898636d7c151e87872da994a40cbffc51f88d724399a382830a33159

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