Pythonic Java optional
Project description
Maypy is python implementation of the well known Java Optional API.
It's designed to help you handle potential None
values, reducing error from NoneType
.
Features :
- Brings functional programming
- Easy to use
- Fully typed and compatible with
mypy
! - Lightweight
installation
pip install maypy
That's all ! Ready to use
Usage
Description
It's not rare to handle return from function that maybe either a value or None.
Like .get
from a dictionary, results from api or ORM etc.
With no more if value is None
with MayPy
, encapsulate your value and do what you want to.
MayPy brings Maybe
a wrapper (container), it is either empty if the value passed was None
or valuated,
in this case it contains the value, and we can perform operation on it.
Examples
Checking value presence:
from maypy import Maybe
assert Maybe.empty().is_empty()
assert Maybe.of(None).is_empty()
assert Maybe.of("value").is_present()
Using chaining operation over wrapped value:
from maypy import Maybe
from typing import List, Optional
def convert_to_celsius(fahrenheit: float) -> float:
return (fahrenheit - 32) * 5 / 9
fahrenheit_temperatures: List[Optional[float]] = [32.45, None, 26.6, 100, 72, None, 10]
for temp in fahrenheit_temperatures:
Maybe.of(temp).map(convert_to_celsius).filter(lambda celsius: celsius > 0)
Defining return from database result:
from dataclasses import dataclass
from enum import StrEnum, unique
from maypy import Maybe
from pydantic import BaseModel
from sqlalchemy.orm import Session
@unique
class Gender(StrEnum):
MALE = "M"
FEMALE = "F"
UNKNOWN = "X"
@dataclass
class User:
id: str
email: str
gender: Gender
class UserEntity(BaseModel):
id: str
email: str
gender: str
class Config:
orm_mode = True
def get_user(db: Session, user_id: str) -> Maybe[User]:
return Maybe.of(db.query(UserEntity).filter(UserEntity.id == user_id).first()).map(to_domain)
def to_domain(user_dto: UserEntity) -> User:
return User(
id=user_dto.id,
email=user_dto.email,
gender=Gender(user_dto.gender)
)
user = get_user(db, "481efz4x1d").or_else_raise(Exception("User id<481efz4x1d> not found"))
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
File details
Details for the file maypy-1.0.0.tar.gz
.
File metadata
- Download URL: maypy-1.0.0.tar.gz
- Upload date:
- Size: 6.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.9.19 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4ebf992ed75f87bf4253323fcb3d3ce1f93a1a73bc65ac0255d6ea4a9204d426 |
|
MD5 | 7c3cb6f028acd9ab1be15691aa7704dc |
|
BLAKE2b-256 | 75837892c13b6a5bdf43e8ab6bcac42fc0ec17d83dd60ae5c01a0079d19b7c9a |
File details
Details for the file maypy-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: maypy-1.0.0-py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.9.19 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e2fbd8cbf55861c4232349f705a7c25efd3e421d12fc258ca5fbf9a1be31ec58 |
|
MD5 | b3ad4c63e846ec911dbb80976f980e51 |
|
BLAKE2b-256 | 8558e0268c7c11909ba3742f78cd7cc2ea39091f7705904e4dbdaa63b614914d |