Python's None Wrapper, inspired by java.util.Optional
Project description
py_nullable
Introduction
Python's None Wrapper, inspired by java.util.Optional
Getting Started
Installing
install from PyPI with:
python -m pip install py_nullable
install from source with:
git clone https://github.com/miyamo2theppl/py_nullable.git
cd py_nullable
python -m pip install .
Simple usage
if you want to get value.
from py_nullable import Nullable
nullable: Nullable[str] = Nullable[str]("some string")
if nullable.isPresent():
print(nullable.get()) # Prints some string
if you want to generate a new Nullable from an existing Nullable with the mapping function.
from typing import Callable
from py_nullable import Nullable
nullable: Nullable[str] = Nullable[str]("1234")
callback: Callable[[str], int] = lambda x: int(x) * 2
result: Nullable[int] = nullable.map(callback)
print(result.get()) # Prints 2468
if you want to refactor the return value from Optional[T] to Nullable[T].
from yourpackage import YourClass
from py_nullable import Nullable, nullable_wrap
in_memory_db: dict[str, YourClass] = {"A001": YourClass("foo")}
@nullable_wrap
def find_by_id(id: str) -> Optional[YourClass]:
return in_memory_db.get(id)
nullable: Nullable[YourClass] = find_by_id("B001")
print(nullable.isEmpty()) # Prints True
Contributing
Create a feature branch
git checkout -b feature_{example}
Set up your environment
python -m venv .venv
.venv/bin/activate
pip install -r dev_requirements.txt
pip install -e .
Running Tests
pytest --cov py_nullable --cov-branch --cov-report=html
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
py_nullable-0.3.1.tar.gz
(10.6 kB
view hashes)
Built Distribution
Close
Hashes for py_nullable-0.3.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6875324fd80b28b37cb72258c0132711b3a45a573259eacac4c8b5d867cde3e6 |
|
MD5 | a69752f4fa78aeade3c4629397c1fe44 |
|
BLAKE2b-256 | 5f01606ec93dbb57f64f9ff3bd55ebf7e7d62c7acf60366458da7ca0eac530e3 |