Scala like Option type in Python
Project description
scala-option
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.
You can check your python version by running the command:
Windows:
py --versionLinux 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 buildLinux 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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1aea65aaf7f4dbe8ba67d835dbe25a4dbd6c2da8b041efa51e71b0dd63d2a93a
|
|
| MD5 |
2e15e1984f88e3a16038dc1262f9956b
|
|
| BLAKE2b-256 |
01f8e1e9b8ab3c393d781e183685156ba9c00342ef5840d3192abbea090706ae
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cbada0ec51a5516c93a390f13338396135745d6194d0fb6ef6fdf93bc401050b
|
|
| MD5 |
eeb25bda8764a60dbfc23e6b283512b6
|
|
| BLAKE2b-256 |
c8ffb7dd898636d7c151e87872da994a40cbffc51f88d724399a382830a33159
|